| 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.STABLE; |
| 15 | |
| 16 | import java.util.Collections; |
| 17 | import java.util.Comparator; |
| 18 | |
| 19 | import org.apiguardian.api.API; |
| 20 | import org.junit.platform.commons.logging.Logger; |
| 21 | import org.junit.platform.commons.logging.LoggerFactory; |
| 22 | |
| 23 | /** |
| 24 | * {@code ClassOrderer} defines the API for ordering top-level test classes and |
| 25 | * {@link Nested @Nested} test classes. |
| 26 | * |
| 27 | * <p>In this context, the term "test class" refers to any class containing methods |
| 28 | * annotated with {@code @Test}, {@code @RepeatedTest}, {@code @ParameterizedTest}, |
| 29 | * {@code @TestFactory}, or {@code @TestTemplate}. |
| 30 | * |
| 31 | * <p>Top-level test classes will be ordered relative to each other; whereas, |
| 32 | * {@code @Nested} test classes will be ordered relative to other {@code @Nested} |
| 33 | * test classes sharing the same {@linkplain Class#getEnclosingClass() enclosing |
| 34 | * class}. |
| 35 | * |
| 36 | * <p>A {@link ClassOrderer} 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 |
| 39 | * {@link Nested @Nested} test classes via the {@link TestClassOrder @TestClassOrder} |
| 40 | * annotation. |
| 41 | * |
| 42 | * <h2>Built-in Implementations</h2> |
| 43 | * |
| 44 | * <p>JUnit Jupiter provides the following built-in {@code ClassOrderer} |
| 45 | * implementations. |
| 46 | * |
| 47 | * <ul> |
| 48 | * <li>{@link ClassOrderer.ClassName}</li> |
| 49 | * <li>{@link ClassOrderer.DisplayName}</li> |
| 50 | * <li>{@link ClassOrderer.OrderAnnotation}</li> |
| 51 | * <li>{@link ClassOrderer.Random}</li> |
| 52 | * </ul> |
| 53 | * |
| 54 | * @since 5.8 |
| 55 | * @see TestClassOrder |
| 56 | * @see ClassOrdererContext |
| 57 | * @see #orderClasses(ClassOrdererContext) |
| 58 | * @see MethodOrderer |
| 59 | */ |
| 60 | @API(status = STABLE, since = "5.10") |
| 61 | public interface ClassOrderer { |
| 62 | |
| 63 | /** |
| 64 | * Property name used to set the default class orderer class name: {@value} |
| 65 | * |
| 66 | * <h4>Supported Values</h4> |
| 67 | * |
| 68 | * <p>Supported values include fully qualified class names for types that |
| 69 | * implement {@link org.junit.jupiter.api.ClassOrderer}. |
| 70 | * |
| 71 | * <p>If not specified, test classes are not ordered unless test classes are |
| 72 | * annotated with {@link TestClassOrder @TestClassOrder}. |
| 73 | * |
| 74 | * @since 5.8 |
| 75 | */ |
| 76 | @API(status = STABLE, since = "5.9") |
| 77 | String DEFAULT_ORDER_PROPERTY_NAME = "junit.jupiter.testclass.order.default"; |
| 78 | |
| 79 | /** |
| 80 | * Order the classes encapsulated in the supplied {@link ClassOrdererContext}. |
| 81 | * |
| 82 | * <p>The classes to order or sort are made indirectly available via |
| 83 | * {@link ClassOrdererContext#getClassDescriptors()}. Since this method |
| 84 | * has a {@code void} return type, the list of class descriptors must be |
| 85 | * modified directly. |
| 86 | * |
| 87 | * <p>For example, a simplified implementation of the {@link ClassOrderer.Random} |
| 88 | * {@code ClassOrderer} might look like the following. |
| 89 | * |
| 90 | * <pre class="code"> |
| 91 | * public void orderClasses(ClassOrdererContext context) { |
| 92 | * Collections.shuffle(context.getClassDescriptors()); |
| 93 | * } |
| 94 | * </pre> |
| 95 | * |
| 96 | * @param context the {@code ClassOrdererContext} containing the |
| 97 | * {@linkplain ClassDescriptor class descriptors} to order; never {@code null} |
| 98 | */ |
| 99 | void orderClasses(ClassOrdererContext context); |
| 100 | |
| 101 | /** |
| 102 | * {@code ClassOrderer} that sorts classes alphanumerically based on their |
| 103 | * fully qualified names using {@link String#compareTo(String)}. |
| 104 | */ |
| 105 | class ClassName implements ClassOrderer { |
| 106 | |
| 107 | public ClassName() { |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Sort the classes encapsulated in the supplied |
| 112 | * {@link ClassOrdererContext} alphanumerically based on their fully |
| 113 | * qualified names. |
| 114 | */ |
| 115 | @Override |
| 116 | public void orderClasses(ClassOrdererContext context) { |
| 117 | context.getClassDescriptors().sort(comparator); |
| 118 | } |
| 119 | |
| 120 | private static final Comparator<ClassDescriptor> comparator = Comparator.comparing( |
| 121 | descriptor -> descriptor.getTestClass().getName()); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * {@code ClassOrderer} that sorts classes alphanumerically based on their |
| 126 | * display names using {@link String#compareTo(String)} |
| 127 | */ |
| 128 | class DisplayName implements ClassOrderer { |
| 129 | |
| 130 | public DisplayName() { |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Sort the classes encapsulated in the supplied |
| 135 | * {@link ClassOrdererContext} alphanumerically based on their display |
| 136 | * names. |
| 137 | */ |
| 138 | @Override |
| 139 | public void orderClasses(ClassOrdererContext context) { |
| 140 | context.getClassDescriptors().sort(comparator); |
| 141 | } |
| 142 | |
| 143 | private static final Comparator<ClassDescriptor> comparator = Comparator.comparing( |
| 144 | ClassDescriptor::getDisplayName); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * {@code ClassOrderer} that sorts classes based on the {@link Order @Order} |
| 149 | * annotation. |
| 150 | * |
| 151 | * <p>Any classes that are assigned the same order value will be sorted |
| 152 | * arbitrarily adjacent to each other. |
| 153 | * |
| 154 | * <p>Any classes not annotated with {@code @Order} will be assigned the |
| 155 | * {@linkplain Order#DEFAULT default order} value which will effectively cause them |
| 156 | * to appear at the end of the sorted list, unless certain classes are assigned |
| 157 | * an explicit order value greater than the default order value. Any classes |
| 158 | * assigned an explicit order value greater than the default order value will |
| 159 | * appear after non-annotated classes in the sorted list. |
| 160 | */ |
| 161 | class OrderAnnotation implements ClassOrderer { |
| 162 | |
| 163 | public OrderAnnotation() { |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Sort the classes encapsulated in the supplied |
| 168 | * {@link ClassOrdererContext} based on the {@link Order @Order} |
| 169 | * annotation. |
| 170 | */ |
| 171 | @Override |
| 172 | public void orderClasses(ClassOrdererContext context) { |
| 173 | context.getClassDescriptors().sort(comparingInt(OrderAnnotation::getOrder)); |
| 174 | } |
| 175 | |
| 176 | private static int getOrder(ClassDescriptor descriptor) { |
| 177 | return descriptor.findAnnotation(Order.class).map(Order::value).orElse(Order.DEFAULT); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * {@code ClassOrderer} that orders classes pseudo-randomly. |
| 183 | * |
| 184 | * <h2>Custom Seed</h2> |
| 185 | * |
| 186 | * <p>By default, the random <em>seed</em> used for ordering classes is the |
| 187 | * value returned by {@link System#nanoTime()} during static class |
| 188 | * initialization. In order to support repeatable builds, the value of the |
| 189 | * default random seed is logged at {@code CONFIG} level. In addition, a |
| 190 | * custom seed (potentially the default seed from the previous test plan |
| 191 | * execution) may be specified via the {@value Random#RANDOM_SEED_PROPERTY_NAME} |
| 192 | * <em>configuration parameter</em> which can be supplied via the {@code Launcher} |
| 193 | * API, build tools (e.g., Gradle and Maven), a JVM system property, or the JUnit |
| 194 | * Platform configuration file (i.e., a file named {@code junit-platform.properties} |
| 195 | * in the root of the class path). Consult the User Guide for further information. |
| 196 | * |
| 197 | * @see Random#RANDOM_SEED_PROPERTY_NAME |
| 198 | * @see java.util.Random |
| 199 | */ |
| 200 | class Random implements ClassOrderer { |
| 201 | |
| 202 | private static final Logger logger = LoggerFactory.getLogger(Random.class); |
| 203 | |
| 204 | static { |
| 205 | logger.config(() -> "ClassOrderer.Random default seed: " + RandomOrdererUtils.DEFAULT_SEED); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Property name used to set the random seed used by this |
| 210 | * {@code ClassOrderer}: {@value} |
| 211 | * |
| 212 | * <p>The same property is used by {@link MethodOrderer.Random} for |
| 213 | * consistency between the two random orderers. |
| 214 | * |
| 215 | * <h4>Supported Values</h4> |
| 216 | * |
| 217 | * <p>Supported values include any string that can be converted to a |
| 218 | * {@link Long} via {@link Long#valueOf(String)}. |
| 219 | * |
| 220 | * <p>If not specified or if the specified value cannot be converted to |
| 221 | * a {@link Long}, the default random seed will be used (see the |
| 222 | * {@linkplain Random class-level Javadoc} for details). |
| 223 | * |
| 224 | * @see MethodOrderer.Random |
| 225 | */ |
| 226 | public static final String RANDOM_SEED_PROPERTY_NAME = RandomOrdererUtils.RANDOM_SEED_PROPERTY_NAME; |
| 227 | |
| 228 | public Random() { |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Order the classes encapsulated in the supplied |
| 233 | * {@link ClassOrdererContext} pseudo-randomly. |
| 234 | */ |
| 235 | @Override |
| 236 | public void orderClasses(ClassOrdererContext context) { |
| 237 | Collections.shuffle(context.getClassDescriptors(), |
| 238 | new java.util.Random(RandomOrdererUtils.getSeed(context::getConfigurationParameter, logger))); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | } |