Debian Codegraph @

junit-jupiter-api/src/main/java/org/junit/jupiter/api/MethodOrderer.java

raw symbols SCIP
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.jupiter.api;
12
13 import static java.util.Comparator.comparingInt;
14 import static org.apiguardian.api.API.Status.DEPRECATED;
15 import static org.apiguardian.api.API.Status.STABLE;
16
17 import java.lang.reflect.Method;
18 import java.util.Collections;
19 import java.util.Comparator;
20 import java.util.Optional;
21
22 import org.apiguardian.api.API;
23 import org.junit.jupiter.api.parallel.ExecutionMode;
24 import org.junit.platform.commons.logging.Logger;
25 import org.junit.platform.commons.logging.LoggerFactory;
26 import org.junit.platform.commons.util.ClassUtils;
27
28 /**
29 * {@code MethodOrderer} defines the API for ordering the <em>test methods</em>
30 * in a given test class.
31 *
32 * <p>In this context, the term "test method" refers to any method annotated with
33 * {@code @Test}, {@code @RepeatedTest}, {@code @ParameterizedTest},
34 * {@code @TestFactory}, or {@code @TestTemplate}.
35 *
36 * <p>A {@link MethodOrderer} can be configured <em>globally</em> for the entire
37 * test suite via the {@value #DEFAULT_ORDER_PROPERTY_NAME} configuration
38 * parameter (see the User Guide for details) or <em>locally</em> for a test
39 * class via the {@link TestMethodOrder @TestMethodOrder} annotation.
40 *
41 * <h2>Built-in Implementations</h2>
42 *
43 * <p>JUnit Jupiter provides the following built-in {@code MethodOrderer}
44 * implementations.
45 *
46 * <ul>
47 * <li>{@link MethodName}</li>
48 * <li>{@link OrderAnnotation}</li>
49 * <li>{@link Random}</li>
50 * </ul>
51 *
52 * @since 5.4
53 * @see TestMethodOrder
54 * @see MethodOrdererContext
55 * @see #orderMethods(MethodOrdererContext)
56 * @see ClassOrderer
57 */
58 @API(status = STABLE, since = "5.7")
59 public interface MethodOrderer {
60
61 /**
62 * Property name used to set the default method orderer class name: {@value}
63 *
64 * <h4>Supported Values</h4>
65 *
66 * <p>Supported values include fully qualified class names for types that
67 * implement {@link org.junit.jupiter.api.MethodOrderer}.
68 *
69 * <p>If not specified, test methods will be ordered using an algorithm that
70 * is deterministic but intentionally non-obvious.
71 *
72 * @since 5.7
73 */
74 @API(status = STABLE, since = "5.9")
75 String DEFAULT_ORDER_PROPERTY_NAME = "junit.jupiter.testmethod.order.default";
76
77 /**
78 * Order the methods encapsulated in the supplied {@link MethodOrdererContext}.
79 *
80 * <p>The methods to order or sort are made indirectly available via
81 * {@link MethodOrdererContext#getMethodDescriptors()}. Since this method
82 * has a {@code void} return type, the list of method descriptors must be
83 * modified directly.
84 *
85 * <p>For example, a simplified implementation of the {@link Random}
86 * {@code MethodOrderer} might look like the following.
87 *
88 * <pre class="code">
89 * public void orderMethods(MethodOrdererContext context) {
90 * Collections.shuffle(context.getMethodDescriptors());
91 * }
92 * </pre>
93 *
94 * @param context the {@code MethodOrdererContext} containing the
95 * {@linkplain MethodDescriptor method descriptors} to order; never {@code null}
96 * @see #getDefaultExecutionMode()
97 */
98 void orderMethods(MethodOrdererContext context);
99
100 /**
101 * Get the <em>default</em> {@link ExecutionMode} for the test class
102 * configured with this {@link MethodOrderer}.
103 *
104 * <p>This method is guaranteed to be invoked after
105 * {@link #orderMethods(MethodOrdererContext)} which allows implementations
106 * of this method to determine the appropriate return value programmatically,
107 * potentially based on actions that were taken in {@code orderMethods()}.
108 *
109 * <p>Defaults to {@link ExecutionMode#SAME_THREAD SAME_THREAD}, since
110 * ordered methods are typically sorted in a fashion that would conflict
111 * with concurrent execution.
112 *
113 * <p>In case the ordering does not conflict with concurrent execution,
114 * implementations should return an empty {@link Optional} to signal that
115 * the engine should decide which execution mode to use.
116 *
117 * <p>Can be overridden via an explicit
118 * {@link org.junit.jupiter.api.parallel.Execution @Execution} declaration
119 * on the test class or in concrete implementations of the
120 * {@code MethodOrderer} API.
121 *
122 * @return the default {@code ExecutionMode}; never {@code null} but
123 * potentially empty
124 * @see #orderMethods(MethodOrdererContext)
125 */
126 default Optional<ExecutionMode> getDefaultExecutionMode() {
127 return Optional.of(ExecutionMode.SAME_THREAD);
128 }
129
130 /**
131 * {@code MethodOrderer} that sorts methods alphanumerically based on their
132 * names using {@link String#compareTo(String)}.
133 *
134 * <p>If two methods have the same name, {@code String} representations of
135 * their formal parameter lists will be used as a fallback for comparing the
136 * methods.
137 *
138 * @since 5.4
139 * @deprecated as of JUnit Jupiter 5.7 in favor of {@link MethodOrderer.MethodName};
140 * to be removed in 6.0
141 */
142 @API(status = DEPRECATED, since = "5.7")
143 @Deprecated
144 class Alphanumeric extends MethodName {
145
146 public Alphanumeric() {
147 }
148 }
149
150 /**
151 * {@code MethodOrderer} that sorts methods alphanumerically based on their
152 * names using {@link String#compareTo(String)}.
153 *
154 * <p>If two methods have the same name, {@code String} representations of
155 * their formal parameter lists will be used as a fallback for comparing the
156 * methods.
157 *
158 * @since 5.7
159 */
160 @API(status = STABLE, since = "5.10")
161 class MethodName implements MethodOrderer {
162
163 public MethodName() {
164 }
165
166 /**
167 * Sort the methods encapsulated in the supplied
168 * {@link MethodOrdererContext} alphanumerically based on their names
169 * and formal parameter lists.
170 */
171 @Override
172 public void orderMethods(MethodOrdererContext context) {
173 context.getMethodDescriptors().sort(comparator);
174 }
175
176 private static final Comparator<MethodDescriptor> comparator = Comparator.<MethodDescriptor, String> //
177 comparing(descriptor -> descriptor.getMethod().getName())//
178 .thenComparing(descriptor -> parameterList(descriptor.getMethod()));
179
180 private static String parameterList(Method method) {
181 return ClassUtils.nullSafeToString(method.getParameterTypes());
182 }
183 }
184
185 /**
186 * {@code MethodOrderer} that sorts methods alphanumerically based on their
187 * display names using {@link String#compareTo(String)}
188 *
189 * @since 5.7
190 */
191 @API(status = STABLE, since = "5.10")
192 class DisplayName implements MethodOrderer {
193
194 public DisplayName() {
195 }
196
197 /**
198 * Sort the methods encapsulated in the supplied
199 * {@link MethodOrdererContext} alphanumerically based on their display
200 * names.
201 */
202 @Override
203 public void orderMethods(MethodOrdererContext context) {
204 context.getMethodDescriptors().sort(comparator);
205 }
206
207 private static final Comparator<MethodDescriptor> comparator = Comparator.comparing(
208 MethodDescriptor::getDisplayName);
209 }
210
211 /**
212 * {@code MethodOrderer} that sorts methods based on the {@link Order @Order}
213 * annotation.
214 *
215 * <p>Any methods that are assigned the same order value will be sorted
216 * arbitrarily adjacent to each other.
217 *
218 * <p>Any methods not annotated with {@code @Order} will be assigned the
219 * {@linkplain Order#DEFAULT default order} value which will effectively cause them
220 * to appear at the end of the sorted list, unless certain methods are assigned
221 * an explicit order value greater than the default order value. Any methods
222 * assigned an explicit order value greater than the default order value will
223 * appear after non-annotated methods in the sorted list.
224 */
225 class OrderAnnotation implements MethodOrderer {
226
227 public OrderAnnotation() {
228 }
229
230 /**
231 * Sort the methods encapsulated in the supplied
232 * {@link MethodOrdererContext} based on the {@link Order @Order}
233 * annotation.
234 */
235 @Override
236 public void orderMethods(MethodOrdererContext context) {
237 context.getMethodDescriptors().sort(comparingInt(OrderAnnotation::getOrder));
238 }
239
240 private static int getOrder(MethodDescriptor descriptor) {
241 return descriptor.findAnnotation(Order.class).map(Order::value).orElse(Order.DEFAULT);
242 }
243 }
244
245 /**
246 * {@code MethodOrderer} that orders methods pseudo-randomly.
247 *
248 * <h2>Custom Seed</h2>
249 *
250 * <p>By default, the random <em>seed</em> used for ordering methods is the
251 * value returned by {@link System#nanoTime()} during static class
252 * initialization. In order to support repeatable builds, the value of the
253 * default random seed is logged at {@code CONFIG} level. In addition, a
254 * custom seed (potentially the default seed from the previous test plan
255 * execution) may be specified via the {@value Random#RANDOM_SEED_PROPERTY_NAME}
256 * <em>configuration parameter</em> which can be supplied via the {@code Launcher}
257 * API, build tools (e.g., Gradle and Maven), a JVM system property, or the JUnit
258 * Platform configuration file (i.e., a file named {@code junit-platform.properties}
259 * in the root of the class path). Consult the User Guide for further information.
260 *
261 * @see Random#RANDOM_SEED_PROPERTY_NAME
262 * @see java.util.Random
263 */
264 class Random implements MethodOrderer {
265
266 private static final Logger logger = LoggerFactory.getLogger(Random.class);
267
268 static {
269 logger.config(() -> "MethodOrderer.Random default seed: " + RandomOrdererUtils.DEFAULT_SEED);
270 }
271
272 /**
273 * Property name used to set the random seed used by this
274 * {@code MethodOrderer}: {@value}
275 *
276 * <p>The same property is used by {@link ClassOrderer.Random} for
277 * consistency between the two random orderers.
278 *
279 * <h4>Supported Values</h4>
280 *
281 * <p>Supported values include any string that can be converted to a
282 * {@link Long} via {@link Long#valueOf(String)}.
283 *
284 * <p>If not specified or if the specified value cannot be converted to
285 * a {@link Long}, the default random seed will be used (see the
286 * {@linkplain Random class-level Javadoc} for details).
287 *
288 * @see ClassOrderer.Random
289 */
290 public static final String RANDOM_SEED_PROPERTY_NAME = RandomOrdererUtils.RANDOM_SEED_PROPERTY_NAME;
291
292 public Random() {
293 }
294
295 /**
296 * Order the methods encapsulated in the supplied
297 * {@link MethodOrdererContext} pseudo-randomly.
298 */
299 @Override
300 public void orderMethods(MethodOrdererContext context) {
301 Collections.shuffle(context.getMethodDescriptors(),
302 new java.util.Random(RandomOrdererUtils.getSeed(context::getConfigurationParameter, logger)));
303 }
304
305 }
306
307 }