Refine ImportsContextCustomizer cache logic
Update `ImportsContextCustomizer` so that whenever possible a more specific cache key is used. Prior to this commit the customizer would generate a key based on *all* annotations on the test class. This has repeatedly caused issues where test classes that should have the same cache key did not due to unrelated annotations. A new `DeterminableImports` interface has been added that can be implemented by `ImportSelector` and `ImportBeanDefinitionRegistrar` implementations that are able to determine their imports early. The existing `ImportAutoConfigurationImportSelector` and `AutoConfigurationPackages` classes have been retrofitted with this interface. Fixes gh-7953pull/8206/head
parent
aaf118c544
commit
fa6a138598
@ -0,0 +1,158 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* http://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.test.autoconfigure.cache;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.notification.RunNotifier;
|
||||
import org.junit.runners.model.InitializationError;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.ExampleEntity;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@code ImportsContextCustomizerFactory} when used with
|
||||
* {@link ImportAutoConfiguration}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class ImportsContextCustomizerFactoryWithAutoConfigurationTests {
|
||||
|
||||
static ApplicationContext contextFromTest;
|
||||
|
||||
@Test
|
||||
public void testClassesThatHaveSameAnnotationsShareAContext()
|
||||
throws InitializationError {
|
||||
RunNotifier notifier = new RunNotifier();
|
||||
new SpringJUnit4ClassRunner(DataJpaTest1.class).run(notifier);
|
||||
ApplicationContext test1Context = contextFromTest;
|
||||
new SpringJUnit4ClassRunner(DataJpaTest3.class).run(notifier);
|
||||
ApplicationContext test2Context = contextFromTest;
|
||||
assertThat(test1Context).isSameAs(test2Context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassesThatOnlyHaveDifferingUnrelatedAnnotationsShareAContext()
|
||||
throws InitializationError {
|
||||
RunNotifier notifier = new RunNotifier();
|
||||
new SpringJUnit4ClassRunner(DataJpaTest1.class).run(notifier);
|
||||
ApplicationContext test1Context = contextFromTest;
|
||||
new SpringJUnit4ClassRunner(DataJpaTest2.class).run(notifier);
|
||||
ApplicationContext test2Context = contextFromTest;
|
||||
assertThat(test1Context).isSameAs(test2Context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassesThatOnlyHaveDifferingPropertyMappedAnnotationAttributesDoNotShareAContext()
|
||||
throws InitializationError {
|
||||
RunNotifier notifier = new RunNotifier();
|
||||
new SpringJUnit4ClassRunner(DataJpaTest1.class).run(notifier);
|
||||
ApplicationContext test1Context = contextFromTest;
|
||||
new SpringJUnit4ClassRunner(DataJpaTest4.class).run(notifier);
|
||||
ApplicationContext test2Context = contextFromTest;
|
||||
assertThat(test1Context).isNotSameAs(test2Context);
|
||||
}
|
||||
|
||||
@DataJpaTest
|
||||
@ContextConfiguration(classes = EmptyConfig.class)
|
||||
@Unrelated1
|
||||
public static class DataJpaTest1 {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
contextFromTest = this.context;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ContextConfiguration(classes = EmptyConfig.class)
|
||||
@DataJpaTest
|
||||
@Unrelated2
|
||||
public static class DataJpaTest2 {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
contextFromTest = this.context;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ContextConfiguration(classes = EmptyConfig.class)
|
||||
@DataJpaTest
|
||||
@Unrelated1
|
||||
public static class DataJpaTest3 {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
contextFromTest = this.context;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ContextConfiguration(classes = EmptyConfig.class)
|
||||
@DataJpaTest(showSql = false)
|
||||
@Unrelated1
|
||||
public static class DataJpaTest4 {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
contextFromTest = this.context;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
static @interface Unrelated1 {
|
||||
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
static @interface Unrelated2 {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EntityScan(basePackageClasses = ExampleEntity.class)
|
||||
static class EmptyConfig {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* http://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.context.annotation;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.factory.Aware;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||
import org.springframework.context.annotation.ImportSelector;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
|
||||
/**
|
||||
* Interface that can be implemented by {@link ImportSelector} and
|
||||
* {@link ImportBeanDefinitionRegistrar} implementations when they can determine imports
|
||||
* early. The {@link ImportSelector} and {@link ImportBeanDefinitionRegistrar} interfaces
|
||||
* are quite flexible which can make it hard to tell exactly what bean definitions they
|
||||
* will add. This interface should be used when an implementation consistently result in
|
||||
* the same imports, given the same source.
|
||||
* <p>
|
||||
* Using {@link DeterminableImports} is particularly useful when working with Spring's
|
||||
* testing support. It allows for better generation of {@link ApplicationContext} cache
|
||||
* keys.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
* @since 1.5.0
|
||||
*/
|
||||
public interface DeterminableImports {
|
||||
|
||||
/**
|
||||
* Return a set of objects that represent the imports. Objects within the returned
|
||||
* {@code Set} must implement a valid {@link Object#hashCode() hashCode} and
|
||||
* {@link Object#equals(Object) equals}.
|
||||
* <p>
|
||||
* Imports from multiple {@link DeterminableImports} instances may be combined by the
|
||||
* caller to create a complete set.
|
||||
* <p>
|
||||
* Unlike {@link ImportSelector} and {@link ImportBeanDefinitionRegistrar} any
|
||||
* {@link Aware} callbacks will not be invoked before this method is called.
|
||||
* @param metadata the source meta-data
|
||||
* @return a key representing the annotations that actually drive the import
|
||||
*/
|
||||
Set<Object> determineImports(AnnotationMetadata metadata);
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* http://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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Classes related to Spring's {@link org.springframework.context.ApplicationContext}
|
||||
* annotations.
|
||||
*/
|
||||
package org.springframework.boot.context.annotation;
|
Loading…
Reference in New Issue