diff --git a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/runner/classpath/ClassPathExclusions.java b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/runner/classpath/ClassPathExclusions.java index 8c4f501395..dfb83d63f4 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/runner/classpath/ClassPathExclusions.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/runner/classpath/ClassPathExclusions.java @@ -24,8 +24,8 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** - * Annotation used in combination with {@link ModifiedClassPathRunner} or - * {@link ModifiedClassPathExtension} to exclude entries from the classpath. + * Annotation used in combination with {@link ModifiedClassPathExtension} to exclude + * entries from the classpath. * * @author Andy Wilkinson * @since 1.5.0 diff --git a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/runner/classpath/ClassPathOverrides.java b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/runner/classpath/ClassPathOverrides.java index bc3fe9aaf9..67aee114d8 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/runner/classpath/ClassPathOverrides.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/runner/classpath/ClassPathOverrides.java @@ -23,8 +23,8 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** - * Annotation used in combination with {@link ModifiedClassPathRunner} or - * {@link ModifiedClassPathExtension} to override entries on the classpath. + * Annotation used in combination with {@link ModifiedClassPathExtension} to override + * entries on the classpath. * * @author Andy Wilkinson * @since 1.5.0 diff --git a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/runner/classpath/ModifiedClassPathRunner.java b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/runner/classpath/ModifiedClassPathRunner.java deleted file mode 100644 index e013f604be..0000000000 --- a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/runner/classpath/ModifiedClassPathRunner.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Copyright 2012-2019 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.boot.testsupport.runner.classpath; - -import java.lang.annotation.Annotation; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.List; - -import org.junit.runners.BlockJUnit4ClassRunner; -import org.junit.runners.model.FrameworkMethod; -import org.junit.runners.model.InitializationError; -import org.junit.runners.model.TestClass; - -/** - * A custom {@link BlockJUnit4ClassRunner} that runs tests using a modified class path. - * 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. - * - * @author Andy Wilkinson - * @since 1.5.0 - */ -public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner { - - public ModifiedClassPathRunner(Class testClass) throws InitializationError { - super(testClass); - } - - @Override - protected TestClass createTestClass(Class testClass) { - try { - ClassLoader classLoader = ModifiedClassPathClassLoaderFactory.createTestClassLoader(testClass); - return new ModifiedClassPathTestClass(classLoader, testClass.getName()); - } - catch (Exception ex) { - throw new IllegalStateException(ex); - } - } - - @Override - protected Object createTest() throws Exception { - ModifiedClassPathTestClass testClass = (ModifiedClassPathTestClass) getTestClass(); - return testClass - .doWithModifiedClassPathThreadContextClassLoader(() -> ModifiedClassPathRunner.super.createTest()); - } - - /** - * Custom {@link TestClass} that uses a modified class path. - */ - private static final class ModifiedClassPathTestClass extends TestClass { - - private final ClassLoader classLoader; - - ModifiedClassPathTestClass(ClassLoader classLoader, String testClassName) throws ClassNotFoundException { - super(classLoader.loadClass(testClassName)); - this.classLoader = classLoader; - } - - @Override - public List getAnnotatedMethods(Class annotationClass) { - try { - return getAnnotatedMethods(annotationClass.getName()); - } - catch (ClassNotFoundException ex) { - throw new RuntimeException(ex); - } - } - - @SuppressWarnings("unchecked") - private List getAnnotatedMethods(String annotationClassName) throws ClassNotFoundException { - Class annotationClass = (Class) this.classLoader - .loadClass(annotationClassName); - List methods = super.getAnnotatedMethods(annotationClass); - return wrapFrameworkMethods(methods); - } - - private List wrapFrameworkMethods(List methods) { - List wrapped = new ArrayList<>(methods.size()); - for (FrameworkMethod frameworkMethod : methods) { - wrapped.add(new ModifiedClassPathFrameworkMethod(frameworkMethod.getMethod())); - } - return wrapped; - } - - private T doWithModifiedClassPathThreadContextClassLoader( - ModifiedClassPathTcclAction action) throws E { - ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); - Thread.currentThread().setContextClassLoader(this.classLoader); - try { - return action.perform(); - } - finally { - Thread.currentThread().setContextClassLoader(originalClassLoader); - } - } - - /** - * An action to be performed with the {@link ModifiedClassPathClassLoader} set as - * the thread context class loader. - */ - private interface ModifiedClassPathTcclAction { - - T perform() throws E; - - } - - /** - * Custom {@link FrameworkMethod} that runs methods with - * {@link ModifiedClassPathClassLoader} as the thread context class loader. - */ - private final class ModifiedClassPathFrameworkMethod extends FrameworkMethod { - - private ModifiedClassPathFrameworkMethod(Method method) { - super(method); - } - - @Override - public Object invokeExplosively(Object target, Object... params) throws Throwable { - return doWithModifiedClassPathThreadContextClassLoader( - () -> ModifiedClassPathFrameworkMethod.super.invokeExplosively(target, params)); - } - - } - - } - -} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/test/java/org/springframework/boot/testsupport/runner/classpath/ModifiedClassPathRunnerExclusionsTests.java b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/test/java/org/springframework/boot/testsupport/runner/classpath/ModifiedClassPathRunnerExclusionsTests.java deleted file mode 100644 index dd20b1c9e3..0000000000 --- a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/test/java/org/springframework/boot/testsupport/runner/classpath/ModifiedClassPathRunnerExclusionsTests.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2012-2019 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.boot.testsupport.runner.classpath; - -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.Matchers.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 matcher = isA(IllegalStateException.class); - assertThat(matcher.matches(new IllegalStateException())).isTrue(); - } - -} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/test/java/org/springframework/boot/testsupport/runner/classpath/ModifiedClassPathRunnerOverridesTests.java b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/test/java/org/springframework/boot/testsupport/runner/classpath/ModifiedClassPathRunnerOverridesTests.java deleted file mode 100644 index 557bfd1bfa..0000000000 --- a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/test/java/org/springframework/boot/testsupport/runner/classpath/ModifiedClassPathRunnerOverridesTests.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2012-2019 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.boot.testsupport.runner.classpath; - -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"); - } - -}