Browse Source

Ported change to Migrate to MergedAnnotations API

See b879972d0d (diff-d013390d98c38b0a24a04c53f1f875ce)
pull/572/head
Spencer Gibb 6 years ago
parent
commit
eff06c8c57
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 76
      spring-cloud-test-support/pom.xml
  2. 37
      spring-cloud-test-support/src/main/java/org/springframework/cloud/test/ModifiedClassPathRunner.java
  3. 55
      spring-cloud-test-support/src/test/java/org/springframework/cloud/test/ModifiedClassPathRunnerExclusionsTests.java
  4. 48
      spring-cloud-test-support/src/test/java/org/springframework/cloud/test/ModifiedClassPathRunnerOverridesTests.java

76
spring-cloud-test-support/pom.xml

@ -15,49 +15,41 @@ @@ -15,49 +15,41 @@
<name>spring-cloud-test-support</name>
<description>Spring Cloud Test Support</description>
<properties>
<aether.version>1.0.2.v20150114</aether.version>
<maven.version>3.3.9</maven.version>
<maven.version>3.5.4</maven.version>
<maven-resolver.version>1.1.1</maven-resolver.version>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-api</artifactId>
<version>${aether.version}</version>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-connector-basic</artifactId>
<version>${maven-resolver.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-connector-basic</artifactId>
<version>${aether.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-impl</artifactId>
<version>${aether.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-spi</artifactId>
<version>${aether.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-transport-file</artifactId>
<version>${aether.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-transport-http</artifactId>
<version>${aether.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-util</artifactId>
<version>${aether.version}</version>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-impl</artifactId>
<version>${maven-resolver.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<artifactId>maven-resolver-provider</artifactId>
<version>${maven.version}</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-transport-http</artifactId>
<version>${maven-resolver.version}</version>
<exclusions>
<exclusion>
<artifactId>jcl-over-slf4j</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
@ -72,5 +64,21 @@ @@ -72,5 +64,21 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

37
spring-cloud-test-support/src/main/java/org/springframework/cloud/test/ModifiedClassPathRunner.java

@ -52,14 +52,17 @@ import org.junit.runners.model.FrameworkMethod; @@ -52,14 +52,17 @@ import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.TestClass;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.StringUtils;
/**
* A custom {@link BlockJUnit4ClassRunner} that runs tests using a modified class path.
* Entries are excluded from the class path using {@link ClassPathExclusions} and
* overridden using {@link ClassPathOverrides} on the test class. A class loader is
* Entries are excluded from the class path using
* {@link ClassPathExclusions @ClassPathExclusions} and overridden using
* {@link ClassPathOverrides @ClassPathOverrides} on the test class. A class loader is
* created with the customized class path and is used both to load the test class and as
* the thread context class loader while the test is being run.
*
@ -178,9 +181,14 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner { @@ -178,9 +181,14 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
}
private URL[] processUrls(URL[] urls, Class<?> testClass) throws Exception {
ClassPathEntryFilter filter = new ClassPathEntryFilter(testClass);
MergedAnnotations annotations = MergedAnnotations.from(testClass,
SearchStrategy.EXHAUSTIVE);
ClassPathEntryFilter filter = new ClassPathEntryFilter(
annotations.get(ClassPathExclusions.class));
List<URL> processedUrls = new ArrayList<>();
processedUrls.addAll(getAdditionalUrls(testClass));
List<URL> additionalUrls = getAdditionalUrls(
annotations.get(ClassPathOverrides.class));
processedUrls.addAll(additionalUrls);
for (URL url : urls) {
if (!filter.isExcluded(url)) {
processedUrls.add(url);
@ -189,13 +197,12 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner { @@ -189,13 +197,12 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
return processedUrls.toArray(new URL[0]);
}
private List<URL> getAdditionalUrls(Class<?> testClass) throws Exception {
ClassPathOverrides overrides = AnnotationUtils.findAnnotation(testClass,
ClassPathOverrides.class);
if (overrides == null) {
private List<URL> getAdditionalUrls(MergedAnnotation<ClassPathOverrides> annotation)
throws Exception {
if (!annotation.isPresent()) {
return Collections.emptyList();
}
return resolveCoordinates(overrides.value());
return resolveCoordinates(annotation.getStringArray(MergedAnnotation.VALUE));
}
private List<URL> resolveCoordinates(String[] coordinates) throws Exception {
@ -243,13 +250,13 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner { @@ -243,13 +250,13 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
private final AntPathMatcher matcher = new AntPathMatcher();
private ClassPathEntryFilter(Class<?> testClass) throws Exception {
private ClassPathEntryFilter(MergedAnnotation<ClassPathExclusions> annotation)
throws Exception {
this.exclusions = new ArrayList<>();
this.exclusions.add("log4j-*.jar");
ClassPathExclusions exclusions = AnnotationUtils.findAnnotation(testClass,
ClassPathExclusions.class);
if (exclusions != null) {
this.exclusions.addAll(Arrays.asList(exclusions.value()));
if (annotation.isPresent()) {
this.exclusions.addAll(
Arrays.asList(annotation.getStringArray(MergedAnnotation.VALUE)));
}
}

55
spring-cloud-test-support/src/test/java/org/springframework/cloud/test/ModifiedClassPathRunnerExclusionsTests.java

@ -0,0 +1,55 @@ @@ -0,0 +1,55 @@
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.test;
import org.hamcrest.Matcher;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.isA;
/**
* Tests for {@link ModifiedClassPathRunner} excluding entries from the class path.
*
* @author Andy Wilkinson
*/
@RunWith(ModifiedClassPathRunner.class)
@ClassPathExclusions("hibernate-validator-*.jar")
public class ModifiedClassPathRunnerExclusionsTests {
private static final String EXCLUDED_RESOURCE = "META-INF/services/"
+ "javax.validation.spi.ValidationProvider";
@Test
public void entriesAreFilteredFromTestClassClassLoader() {
assertThat(getClass().getClassLoader().getResource(EXCLUDED_RESOURCE)).isNull();
}
@Test
public void entriesAreFilteredFromThreadContextClassLoader() {
assertThat(Thread.currentThread().getContextClassLoader()
.getResource(EXCLUDED_RESOURCE)).isNull();
}
@Test
public void testsThatUseHamcrestWorkCorrectly() {
Matcher<IllegalStateException> matcher = isA(IllegalStateException.class);
assertThat(matcher.matches(new IllegalStateException())).isTrue();
}
}

48
spring-cloud-test-support/src/test/java/org/springframework/cloud/test/ModifiedClassPathRunnerOverridesTests.java

@ -0,0 +1,48 @@ @@ -0,0 +1,48 @@
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link ModifiedClassPathRunner} overriding entries on the class path.
*
* @author Andy Wilkinson
*/
@RunWith(ModifiedClassPathRunner.class)
@ClassPathOverrides("org.springframework:spring-context:4.1.0.RELEASE")
public class ModifiedClassPathRunnerOverridesTests {
@Test
public void classesAreLoadedFromOverride() {
assertThat(ApplicationContext.class.getProtectionDomain().getCodeSource()
.getLocation().toString()).endsWith("spring-context-4.1.0.RELEASE.jar");
}
@Test
public void classesAreLoadedFromTransitiveDependencyOfOverride() {
assertThat(StringUtils.class.getProtectionDomain().getCodeSource().getLocation()
.toString()).endsWith("spring-core-4.1.0.RELEASE.jar");
}
}
Loading…
Cancel
Save