Merge pull request #22710 from dreis2211

* gh-22710:
  Introduce @ForkedClassPath for testing unmodified class path

Closes gh-22710
pull/22919/head
Andy Wilkinson 4 years ago
commit b4c75ad7dc

@ -0,0 +1,41 @@
/*
* Copyright 2012-2020 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.classpath;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.extension.ExtendWith;
/**
* Annotation used to fork the classpath. This can be helpful were neither
* {@link ClassPathExclusions} or {@link ClassPathOverrides} are needed, but just a copy
* of the classpath.
*
* @author Christoph Dreis
* @since 2.4.0
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@ExtendWith(ModifiedClassPathExtension.class)
public @interface ForkedClassPath {
}

@ -89,7 +89,14 @@ final class ModifiedClassPathClassLoader extends URLClassLoader {
private static ModifiedClassPathClassLoader compute(Class<?> testClass) { private static ModifiedClassPathClassLoader compute(Class<?> testClass) {
ClassLoader classLoader = testClass.getClassLoader(); ClassLoader classLoader = testClass.getClassLoader();
return new ModifiedClassPathClassLoader(processUrls(extractUrls(classLoader), testClass), MergedAnnotations annotations = MergedAnnotations.from(testClass,
MergedAnnotations.SearchStrategy.TYPE_HIERARCHY);
if (annotations.isPresent(ForkedClassPath.class) && (annotations.isPresent(ClassPathOverrides.class)
|| annotations.isPresent(ClassPathExclusions.class))) {
throw new IllegalStateException("@ForkedClassPath is redundant in combination with either "
+ "@ClassPathOverrides or @ClassPathExclusions");
}
return new ModifiedClassPathClassLoader(processUrls(extractUrls(classLoader), annotations),
classLoader.getParent(), classLoader); classLoader.getParent(), classLoader);
} }
@ -170,9 +177,7 @@ final class ModifiedClassPathClassLoader extends URLClassLoader {
} }
} }
private static URL[] processUrls(URL[] urls, Class<?> testClass) { private static URL[] processUrls(URL[] urls, MergedAnnotations annotations) {
MergedAnnotations annotations = MergedAnnotations.from(testClass,
MergedAnnotations.SearchStrategy.TYPE_HIERARCHY);
ClassPathEntryFilter filter = new ClassPathEntryFilter(annotations.get(ClassPathExclusions.class)); ClassPathEntryFilter filter = new ClassPathEntryFilter(annotations.get(ClassPathExclusions.class));
List<URL> additionalUrls = getAdditionalUrls(annotations.get(ClassPathOverrides.class)); List<URL> additionalUrls = getAdditionalUrls(annotations.get(ClassPathOverrides.class));
List<URL> processedUrls = new ArrayList<>(additionalUrls); List<URL> processedUrls = new ArrayList<>(additionalUrls);

@ -35,14 +35,14 @@ import org.springframework.util.ReflectionUtils;
/** /**
* A custom {@link Extension} that runs tests using a modified class path. Entries are * A custom {@link Extension} that runs tests using a modified class path. Entries are
* excluded from the class path using {@link ClassPathExclusions @ClassPathExclusions} and * excluded from the class path using {@link ClassPathExclusions @ClassPathExclusions} and
* overridden using {@link ClassPathOverrides @ClassPathOverrides} on the test class. A * overridden using {@link ClassPathOverrides @ClassPathOverrides} on the test class. For
* class loader is created with the customized class path and is used both to load the * an unchanged copy of the class path {@link ForkedClassPath @ForkedClassPath} can be
* test class and as the thread context class loader while the test is being run. * used. 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 Christoph Dreis * @author Christoph Dreis
* @since 2.4.0
*/ */
public class ModifiedClassPathExtension implements InvocationInterceptor { class ModifiedClassPathExtension implements InvocationInterceptor {
@Override @Override
public void interceptBeforeAllMethod(Invocation<Void> invocation, public void interceptBeforeAllMethod(Invocation<Void> invocation,

@ -0,0 +1,37 @@
/*
* Copyright 2012-2020 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.classpath;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link ForkedClassPath @ForkedClassPath}.
*
* @author Christoph Dreis
*/
@ForkedClassPath
class ModifiedClassPathExtensionForkTests {
@Test
void modifiedClassLoaderIsUsed() {
ClassLoader classLoader = getClass().getClassLoader();
assertThat(classLoader.getClass().getName()).isEqualTo(ModifiedClassPathClassLoader.class.getName());
}
}

@ -19,15 +19,14 @@ package org.springframework.boot;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.boot.testsupport.classpath.ModifiedClassPathExtension; import org.springframework.boot.testsupport.classpath.ForkedClassPath;
import org.springframework.context.support.StaticApplicationContext; import org.springframework.context.support.StaticApplicationContext;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@ExtendWith(ModifiedClassPathExtension.class) @ForkedClassPath
class IgnoringXmlBeanDefinitionLoaderTests { class IgnoringXmlBeanDefinitionLoaderTests {
@BeforeAll @BeforeAll

Loading…
Cancel
Save