|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.test.context.aot; |
| 18 | + |
| 19 | +import java.lang.annotation.Annotation; |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Optional; |
| 23 | +import java.util.Set; |
| 24 | +import java.util.stream.Stream; |
| 25 | + |
| 26 | +import org.apache.commons.logging.Log; |
| 27 | +import org.apache.commons.logging.LogFactory; |
| 28 | +import org.junit.platform.engine.support.descriptor.ClassSource; |
| 29 | +import org.junit.platform.launcher.Launcher; |
| 30 | +import org.junit.platform.launcher.LauncherDiscoveryRequest; |
| 31 | +import org.junit.platform.launcher.TestIdentifier; |
| 32 | +import org.junit.platform.launcher.TestPlan; |
| 33 | +import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder; |
| 34 | +import org.junit.platform.launcher.core.LauncherFactory; |
| 35 | + |
| 36 | +import org.springframework.core.annotation.MergedAnnotation; |
| 37 | +import org.springframework.core.annotation.MergedAnnotations; |
| 38 | +import org.springframework.test.context.BootstrapWith; |
| 39 | +import org.springframework.test.context.ContextConfiguration; |
| 40 | +import org.springframework.test.context.TestContextAnnotationUtils; |
| 41 | +import org.springframework.util.Assert; |
| 42 | + |
| 43 | +import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClasspathRoots; |
| 44 | +import static org.junit.platform.engine.discovery.PackageNameFilter.includePackageNames; |
| 45 | +import static org.springframework.core.annotation.MergedAnnotation.VALUE; |
| 46 | +import static org.springframework.core.annotation.MergedAnnotations.SearchStrategy.INHERITED_ANNOTATIONS; |
| 47 | +import static org.springframework.core.annotation.MergedAnnotations.SearchStrategy.TYPE_HIERARCHY; |
| 48 | + |
| 49 | +/** |
| 50 | + * {@code TestClassScanner} scans provided classpath roots for Spring integration |
| 51 | + * test classes using the JUnit Platform {@link Launcher} API which allows all |
| 52 | + * registered {@link org.junit.platform.engine.TestEngine TestEngines} to discover |
| 53 | + * tests according to their own rules. |
| 54 | + * |
| 55 | + * <p>The scanner currently detects the following categories of Spring integration |
| 56 | + * test classes. |
| 57 | + * |
| 58 | + * <ul> |
| 59 | + * <li>JUnit Jupiter: classes that register the {@code SpringExtension} via |
| 60 | + * {@code @ExtendWith}.</li> |
| 61 | + * <li>JUnit 4: classes that register the {@code SpringJUnit4ClassRunner} or |
| 62 | + * {@code SpringRunner} via {@code @RunWith}.</li> |
| 63 | + * <li>Generic: classes that are annotated with {@code @ContextConfiguration} or |
| 64 | + * {@code @BootstrapWith}.</li> |
| 65 | + * </ul> |
| 66 | + * |
| 67 | + * <p>The scanner has been tested with the following |
| 68 | + * {@link org.junit.platform.engine.TestEngine TestEngines}. |
| 69 | + * |
| 70 | + * <ul> |
| 71 | + * <li>JUnit Jupiter</li> |
| 72 | + * <li>JUnit Vintage</li> |
| 73 | + * <li>JUnit Platform Suite Engine</li> |
| 74 | + * <li>TestNG Engine for the JUnit Platform</li> |
| 75 | + * </ul> |
| 76 | + * |
| 77 | + * @author Sam Brannen |
| 78 | + * @since 6.0 |
| 79 | + */ |
| 80 | +class TestClassScanner { |
| 81 | + |
| 82 | + // JUnit 4 |
| 83 | + private static final String RUN_WITH_ANNOTATION_NAME = "org.junit.runner.RunWith"; |
| 84 | + private static final String SPRING_JUNIT4_CLASS_RUNNER_NAME = "org.springframework.test.context.junit4.SpringJUnit4ClassRunner"; |
| 85 | + private static final String SPRING_RUNNER_NAME = "org.springframework.test.context.junit4.SpringRunner"; |
| 86 | + |
| 87 | + // JUnit Jupiter |
| 88 | + private static final String EXTEND_WITH_ANNOTATION_NAME = "org.junit.jupiter.api.extension.ExtendWith"; |
| 89 | + private static final String SPRING_EXTENSION_NAME = "org.springframework.test.context.junit.jupiter.SpringExtension"; |
| 90 | + |
| 91 | + |
| 92 | + private final Log logger = LogFactory.getLog(TestClassScanner.class); |
| 93 | + |
| 94 | + private final Set<Path> classpathRoots; |
| 95 | + |
| 96 | + |
| 97 | + TestClassScanner(Set<Path> classpathRoots) { |
| 98 | + Assert.notEmpty(classpathRoots, "'classpathRoots' must not be null or empty"); |
| 99 | + Assert.noNullElements(classpathRoots, "'classpathRoots' must not contain null elements"); |
| 100 | + this.classpathRoots = classpathRoots; |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + /** |
| 105 | + * Scan the configured classpath roots for Spring integration test classes. |
| 106 | + */ |
| 107 | + Stream<Class<?>> scan() { |
| 108 | + return scan(new String[0]); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Scan the configured classpath roots for Spring integration test classes |
| 113 | + * in the given packages. |
| 114 | + */ |
| 115 | + Stream<Class<?>> scan(String... packageNames) { |
| 116 | + Assert.notEmpty(packageNames, "'packageNames' must not be null or empty"); |
| 117 | + Assert.noNullElements(packageNames, "'packageNames' must not contain null elements"); |
| 118 | + |
| 119 | + if (logger.isInfoEnabled()) { |
| 120 | + if (packageNames.length > 0) { |
| 121 | + logger.info("Scanning for Spring test classes in packages %s in classpath roots %s" |
| 122 | + .formatted(Arrays.toString(packageNames), this.classpathRoots)); |
| 123 | + } |
| 124 | + else { |
| 125 | + logger.info("Scanning for Spring test classes in all packages in classpath roots %s" |
| 126 | + .formatted(this.classpathRoots)); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + LauncherDiscoveryRequestBuilder builder = LauncherDiscoveryRequestBuilder.request(); |
| 131 | + builder.selectors(selectClasspathRoots(this.classpathRoots)); |
| 132 | + if (packageNames.length > 0) { |
| 133 | + builder.filters(includePackageNames(packageNames)); |
| 134 | + } |
| 135 | + LauncherDiscoveryRequest request = builder.build(); |
| 136 | + Launcher launcher = LauncherFactory.create(); |
| 137 | + TestPlan testPlan = launcher.discover(request); |
| 138 | + |
| 139 | + return testPlan.getRoots().stream() |
| 140 | + .map(testPlan::getDescendants) |
| 141 | + .flatMap(Set::stream) |
| 142 | + .map(TestIdentifier::getSource) |
| 143 | + .flatMap(Optional::stream) |
| 144 | + .filter(ClassSource.class::isInstance) |
| 145 | + .map(ClassSource.class::cast) |
| 146 | + .map(this::getJavaClass) |
| 147 | + .flatMap(Optional::stream) |
| 148 | + .filter(this::isSpringTestClass) |
| 149 | + .distinct(); |
| 150 | + } |
| 151 | + |
| 152 | + private Optional<Class<?>> getJavaClass(ClassSource classSource) { |
| 153 | + try { |
| 154 | + return Optional.of(classSource.getJavaClass()); |
| 155 | + } |
| 156 | + catch (Exception ex) { |
| 157 | + // ignore exception |
| 158 | + return Optional.empty(); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + private boolean isSpringTestClass(Class<?> clazz) { |
| 163 | + boolean isSpringTestClass = (isJupiterSpringTestClass(clazz) || isJUnit4SpringTestClass(clazz) || |
| 164 | + isGenericSpringTestClass(clazz)); |
| 165 | + if (isSpringTestClass && logger.isTraceEnabled()) { |
| 166 | + logger.trace("Found Spring test class: " + clazz.getName()); |
| 167 | + } |
| 168 | + return isSpringTestClass; |
| 169 | + } |
| 170 | + |
| 171 | + private static boolean isJupiterSpringTestClass(Class<?> clazz) { |
| 172 | + return MergedAnnotations.search(TYPE_HIERARCHY) |
| 173 | + .withEnclosingClasses(TestContextAnnotationUtils::searchEnclosingClass) |
| 174 | + .from(clazz) |
| 175 | + .stream(EXTEND_WITH_ANNOTATION_NAME) |
| 176 | + .map(annotation -> annotation.getClassArray(VALUE)) |
| 177 | + .flatMap(Arrays::stream) |
| 178 | + .map(Class::getName) |
| 179 | + .anyMatch(SPRING_EXTENSION_NAME::equals); |
| 180 | + } |
| 181 | + |
| 182 | + private static boolean isJUnit4SpringTestClass(Class<?> clazz) { |
| 183 | + MergedAnnotation<Annotation> mergedAnnotation = |
| 184 | + MergedAnnotations.from(clazz, INHERITED_ANNOTATIONS).get(RUN_WITH_ANNOTATION_NAME); |
| 185 | + if (mergedAnnotation.isPresent()) { |
| 186 | + String name = mergedAnnotation.getClass(VALUE).getName(); |
| 187 | + return (SPRING_JUNIT4_CLASS_RUNNER_NAME.equals(name) || SPRING_RUNNER_NAME.equals(name)); |
| 188 | + } |
| 189 | + return false; |
| 190 | + } |
| 191 | + |
| 192 | + private static boolean isGenericSpringTestClass(Class<?> clazz) { |
| 193 | + MergedAnnotations mergedAnnotations = MergedAnnotations.from(clazz, TYPE_HIERARCHY); |
| 194 | + return (mergedAnnotations.isPresent(ContextConfiguration.class) || |
| 195 | + mergedAnnotations.isPresent(BootstrapWith.class)); |
| 196 | + } |
| 197 | + |
| 198 | +} |
0 commit comments