Skip to content

Commit 64d3219

Browse files
committed
Ignore Kotlin annotations when creating test context cache key
Every classes that's compiled by Kotlin is annotated with kotlin.Metadata. The attributes of this annotation always differ so if they are used in the cache key, context caching will effectively be disabled. This commit updates the key used by ImportsContextCustomizer to ignore the kotlin.Metadata annotation. Additionally, to align with with Java where annotations in java.lang.annotation are ignored, annotations in kotlin.annotation are also ignored. Closes gh-7101
1 parent 639b0f5 commit 64d3219

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

spring-boot-test/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@
112112
<optional>true</optional>
113113
</dependency>
114114
<!-- Test -->
115+
<dependency>
116+
<groupId>org.jetbrains.kotlin</groupId>
117+
<artifactId>kotlin-runtime</artifactId>
118+
<version>1.0.4</version>
119+
<scope>test</scope>
120+
</dependency>
115121
<dependency>
116122
<groupId>org.apache.tomcat.embed</groupId>
117123
<artifactId>tomcat-embed-core</artifactId>

spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizer.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,10 @@ public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)
207207

208208
/**
209209
* The key used to ensure correct application context caching. Keys are generated
210-
* based on <em>all</em> the annotations used with the test. We must use something
211-
* broader than just {@link Import @Import} annotations since an {@code @Import} may
212-
* use an {@link ImportSelector} which could make decisions based on anything
213-
* available from {@link AnnotationMetadata}.
210+
* based on <em>all</em> the annotations used with the test that aren't core Java or
211+
* Kotlin annotations. We must use something broader than just {@link Import @Import}
212+
* annotations since an {@code @Import} may use an {@link ImportSelector} which could
213+
* make decisions based on anything available from {@link AnnotationMetadata}.
214214
*/
215215
static class ContextCustomizerKey {
216216

@@ -239,14 +239,24 @@ private void collectClassAnnotations(Class<?> classType,
239239
private void collectElementAnnotations(AnnotatedElement element,
240240
Set<Annotation> annotations, Set<Class<?>> seen) {
241241
for (Annotation annotation : element.getDeclaredAnnotations()) {
242-
if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotation)) {
242+
if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotation)
243+
&& !isIgnoredKotlinAnnotation(annotation)) {
243244
annotations.add(annotation);
244245
collectClassAnnotations(annotation.annotationType(), annotations,
245246
seen);
246247
}
247248
}
248249
}
249250

251+
private boolean isIgnoredKotlinAnnotation(Annotation annotation) {
252+
return "kotlin.Metadata".equals(annotation.annotationType().getName())
253+
|| isInKotlinAnnotationPackage(annotation);
254+
}
255+
256+
private boolean isInKotlinAnnotationPackage(Annotation annotation) {
257+
return annotation.annotationType().getName().startsWith("kotlin.annotation.");
258+
}
259+
250260
@Override
251261
public int hashCode() {
252262
return this.annotations.hashCode();
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2012-2016 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+
* http://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.boot.test.context;
18+
19+
import kotlin.Metadata;
20+
import org.junit.Test;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
24+
/**
25+
* Tests for {@link ImportsContextCustomizer}.
26+
*
27+
* @author Andy Wilkinson
28+
*/
29+
public class ImportsContextCustomizerTests {
30+
31+
@Test
32+
public void customizersForTestClassesWithDifferentKotlinMetadataAreEqual() {
33+
assertThat(new ImportsContextCustomizer(FirstKotlinAnnotatedTestClass.class))
34+
.isEqualTo(new ImportsContextCustomizer(
35+
SecondKotlinAnnotatedTestClass.class));
36+
}
37+
38+
@Metadata(d2 = "foo")
39+
static class FirstKotlinAnnotatedTestClass {
40+
41+
}
42+
43+
@Metadata(d2 = "bar")
44+
static class SecondKotlinAnnotatedTestClass {
45+
46+
}
47+
48+
}

0 commit comments

Comments
 (0)