Debian Codegraph @

junit-platform-engine/src/main/java/org/junit/platform/engine/TestEngine.java

raw
1 /*
2 * Copyright 2015-2024 the original author or authors.
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v2.0 which
6 * accompanies this distribution and is available at
7 *
8 * https://www.eclipse.org/legal/epl-v20.html
9 */
10
11 package org.junit.platform.engine;
12
13 import static org.apiguardian.api.API.Status.STABLE;
14
15 import java.util.Optional;
16
17 import org.apiguardian.api.API;
18 import org.junit.platform.commons.util.ModuleUtils;
19 import org.junit.platform.commons.util.PackageUtils;
20
21 /**
22 * A {@code TestEngine} facilitates <em>discovery</em> and <em>execution</em> of
23 * tests for a particular programming model.
24 *
25 * <p>For example, JUnit provides a {@code TestEngine} that discovers and
26 * executes tests written using the JUnit Jupiter programming model.
27 *
28 * <p>Every {@code TestEngine} must {@linkplain #getId provide its own unique ID},
29 * {@linkplain #discover discover tests} from
30 * {@link EngineDiscoveryRequest EngineDiscoveryRequests},
31 * and {@linkplain #execute execute those tests} according to
32 * {@link ExecutionRequest ExecutionRequests}.
33 *
34 * <p>In order to facilitate test discovery within IDEs and tools prior
35 * to launching the JUnit Platform, {@code TestEngine} implementations are
36 * encouraged to make use of the
37 * {@link org.junit.platform.commons.annotation.Testable @Testable} annotation.
38 * For example, the {@code @Test} and {@code @TestFactory} annotations in JUnit
39 * Jupiter are meta-annotated with {@code @Testable}. Consult the Javadoc for
40 * {@code @Testable} for further details.
41 *
42 * @since 1.0
43 * @see org.junit.platform.engine.EngineDiscoveryRequest
44 * @see org.junit.platform.engine.ExecutionRequest
45 * @see org.junit.platform.commons.annotation.Testable
46 */
47 @API(status = STABLE, since = "1.0")
48 public interface TestEngine {
49
50 /**
51 * Get the ID that uniquely identifies this test engine.
52 *
53 * <p>Each test engine must provide a unique ID. For example, JUnit Vintage
54 * and JUnit Jupiter use {@code "junit-vintage"} and {@code "junit-jupiter"},
55 * respectively. When in doubt, you may use the fully qualified name of your
56 * custom {@code TestEngine} implementation class.
57 */
58 String getId();
59
60 /**
61 * Discover tests according to the supplied {@link EngineDiscoveryRequest}.
62 *
63 * <p>The supplied {@link UniqueId} must be used as the unique ID of the
64 * returned root {@link TestDescriptor}. In addition, the {@code UniqueId}
65 * must be used to create unique IDs for children of the root's descriptor
66 * by calling {@link UniqueId#append}.
67 *
68 * @param discoveryRequest the discovery request; never {@code null}
69 * @param uniqueId the unique ID to be used for this test engine's
70 * {@code TestDescriptor}; never {@code null}
71 * @return the root {@code TestDescriptor} of this engine, typically an
72 * instance of {@code EngineDescriptor}
73 * @see org.junit.platform.engine.support.descriptor.EngineDescriptor
74 */
75 TestDescriptor discover(EngineDiscoveryRequest discoveryRequest, UniqueId uniqueId);
76
77 /**
78 * Execute tests according to the supplied {@link ExecutionRequest}.
79 *
80 * <p>The {@code request} passed to this method contains the root
81 * {@link TestDescriptor} that was previously returned by {@link #discover},
82 * the {@link EngineExecutionListener} to be notified of test execution
83 * events, and {@link ConfigurationParameters} that may influence test execution.
84 *
85 * @param request the request to execute tests for; never {@code null}
86 */
87 void execute(ExecutionRequest request);
88
89 /**
90 * Get the <em>Group ID</em> of the JAR in which this test engine is packaged.
91 *
92 * <p>This information is used solely for debugging and reporting purposes.
93 *
94 * <p>The default implementation returns an empty {@link Optional},
95 * signaling that the group ID is unknown.
96 *
97 * <p>Concrete test engine implementations may override this method in
98 * order to provide a known group ID.
99 *
100 * @return an {@code Optional} containing the group ID; never {@code null}
101 * but potentially empty if the group ID is unknown
102 * @see #getArtifactId()
103 * @see #getVersion()
104 */
105 default Optional<String> getGroupId() {
106 return Optional.empty();
107 }
108
109 /**
110 * Get the <em>Artifact ID</em> of the JAR in which this test engine is packaged.
111 *
112 * <p>This information is used solely for debugging and reporting purposes.
113 *
114 * <p>The default implementation assumes the implementation title is equivalent
115 * to the artifact ID and therefore attempts to query the
116 * {@linkplain Package#getImplementationTitle() implementation title}
117 * from the package attributes for the {@link Package} in which the engine
118 * resides. Note that a package only has attributes if the information is
119 * defined in the {@link java.util.jar.Manifest Manifest} of the JAR
120 * containing that package, and if the class loader created the
121 * {@link Package} instance with the attributes from the manifest.
122 *
123 * <p>If the implementation title cannot be queried from the package
124 * attributes, the default implementation returns an empty
125 * {@link Optional}.
126 *
127 * <p>Concrete test engine implementations may override this method in
128 * order to determine the artifact ID by some other means.
129 *
130 * @implNote Since JUnit Platform version 1.1 this default implementation
131 * returns the "module name" stored in the module (modular jar on the
132 * module-path) of this test engine.
133 *
134 * @return an {@code Optional} containing the artifact ID; never
135 * {@code null} but potentially empty if the artifact ID is unknown
136 * @see Class#getPackage()
137 * @see Package#getImplementationTitle()
138 * @see #getGroupId()
139 * @see #getVersion()
140 */
141 default Optional<String> getArtifactId() {
142 Optional<String> moduleName = ModuleUtils.getModuleName(getClass());
143 if (moduleName.isPresent()) {
144 return moduleName;
145 }
146 return PackageUtils.getAttribute(getClass(), Package::getImplementationTitle);
147 }
148
149 /**
150 * Get the version of this test engine.
151 *
152 * <p>This information is used solely for debugging and reporting purposes.
153 *
154 * <p>Initially, the default implementation tries to retrieve the engine
155 * version from the manifest attribute named: {@code "Engine-Version-" + getId()}
156 *
157 * <p>Then the default implementation attempts to query the
158 * {@linkplain Package#getImplementationVersion() implementation version}
159 * from the package attributes for the {@link Package} in which the engine
160 * resides. Note that a package only has attributes if the information is
161 * defined in the {@link java.util.jar.Manifest Manifest} of the JAR
162 * containing that package, and if the class loader created the
163 * {@link Package} instance with the attributes from the manifest.
164 *
165 * <p>If the implementation version cannot be queried from the package
166 * attributes, the default implementation returns {@code "DEVELOPMENT"}.
167 *
168 * <p>Concrete test engine implementations may override this method to
169 * determine the version by some other means.
170 *
171 * <p>implNote: Since JUnit Platform version 1.1 this default implementation
172 * honors the "raw version" information stored in the module (modular jar
173 * on the module-path) of this test engine.
174 *
175 * @return an {@code Optional} containing the version; never {@code null}
176 * but potentially empty if the version is unknown
177 * @see Class#getPackage()
178 * @see Package#getImplementationVersion()
179 * @see #getGroupId()
180 * @see #getArtifactId()
181 */
182 default Optional<String> getVersion() {
183 Optional<String> standalone = PackageUtils.getAttribute(getClass(), "Engine-Version-" + getId());
184 if (standalone.isPresent()) {
185 return standalone;
186 }
187 String fallback = "DEVELOPMENT";
188 Optional<String> moduleVersion = ModuleUtils.getModuleVersion(getClass());
189 if (moduleVersion.isPresent()) {
190 return moduleVersion;
191 }
192 return Optional.of(PackageUtils.getAttribute(getClass(), Package::getImplementationVersion).orElse(fallback));
193 }
194
195 }