Skip to content

Commit a5d9bd6

Browse files
committed
Test package pattern support for CandidateComponentsIndex and restructure tests
1 parent 0fc7c41 commit a5d9bd6

File tree

1 file changed

+138
-78
lines changed

1 file changed

+138
-78
lines changed

spring-context/src/test/java/org/springframework/context/index/CandidateComponentsIndexTests.java

Lines changed: 138 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@
1818

1919
import java.util.List;
2020
import java.util.Properties;
21-
import java.util.Set;
2221

22+
import org.junit.jupiter.api.Nested;
2323
import org.junit.jupiter.api.Test;
2424
import org.junit.jupiter.params.ParameterizedTest;
2525
import org.junit.jupiter.params.provider.ValueSource;
2626

27-
import org.springframework.util.ClassUtils;
28-
2927
import static org.assertj.core.api.Assertions.assertThat;
3028

3129
/**
@@ -36,88 +34,150 @@
3634
*/
3735
class CandidateComponentsIndexTests {
3836

39-
@Test
40-
void getCandidateTypes() {
41-
CandidateComponentsIndex index = new CandidateComponentsIndex(List.of(createSampleProperties()));
42-
Set<String> actual = index.getCandidateTypes("com.example.service", "service");
43-
assertThat(actual).contains("com.example.service.One",
44-
"com.example.service.sub.Two", "com.example.service.Three");
45-
}
46-
47-
@Test
48-
void getCandidateTypesNoMatch() {
49-
CandidateComponentsIndex index = new CandidateComponentsIndex(List.of(createSampleProperties()));
50-
Set<String> actual = index.getCandidateTypes("com.example.service", "entity");
51-
assertThat(actual).isEmpty();
52-
}
53-
54-
@Test
55-
void getCandidateTypesSubPackage() {
56-
CandidateComponentsIndex index = new CandidateComponentsIndex(List.of(createSampleProperties()));
57-
Set<String> actual = index.getCandidateTypes("com.example.service.sub", "service");
58-
assertThat(actual).contains("com.example.service.sub.Two");
59-
}
37+
@Nested
38+
class ComponentIndexFilesTests {
6039

61-
@Test
62-
void getCandidateTypesSubPackageNoMatch() {
63-
CandidateComponentsIndex index = new CandidateComponentsIndex(List.of(createSampleProperties()));
64-
Set<String> actual = index.getCandidateTypes("com.example.service.none", "service");
65-
assertThat(actual).isEmpty();
66-
}
67-
68-
@Test
69-
void mergeCandidateStereotypes() {
70-
CandidateComponentsIndex index = new CandidateComponentsIndex(List.of(
40+
@Test
41+
void getCandidateTypes() {
42+
var index = new CandidateComponentsIndex(List.of(createSampleProperties()));
43+
var candidateTypes = index.getCandidateTypes("com.example.service", "service");
44+
assertThat(candidateTypes).contains("com.example.service.One",
45+
"com.example.service.sub.Two", "com.example.service.Three");
46+
}
47+
48+
@Test
49+
void getCandidateTypesNoMatch() {
50+
var index = new CandidateComponentsIndex(List.of(createSampleProperties()));
51+
var candidateTypes = index.getCandidateTypes("com.example.service", "entity");
52+
assertThat(candidateTypes).isEmpty();
53+
}
54+
55+
@Test
56+
void getCandidateTypesSubPackage() {
57+
var index = new CandidateComponentsIndex(List.of(createSampleProperties()));
58+
var candidateTypes = index.getCandidateTypes("com.example.service.sub", "service");
59+
assertThat(candidateTypes).contains("com.example.service.sub.Two");
60+
}
61+
62+
@Test
63+
void getCandidateTypesSubPackageNoMatch() {
64+
var index = new CandidateComponentsIndex(List.of(createSampleProperties()));
65+
var candidateTypes = index.getCandidateTypes("com.example.service.none", "service");
66+
assertThat(candidateTypes).isEmpty();
67+
}
68+
69+
@Test
70+
void parsesMultipleCandidateStereotypes() {
71+
var index = new CandidateComponentsIndex(List.of(
72+
createProperties("com.example.Foo", "service", "entity")));
73+
assertThat(index.getCandidateTypes("com.example", "service")).contains("com.example.Foo");
74+
assertThat(index.getCandidateTypes("com.example", "entity")).contains("com.example.Foo");
75+
}
76+
77+
@Test
78+
void mergesCandidateStereotypes() {
79+
var index = new CandidateComponentsIndex(List.of(
7180
createProperties("com.example.Foo", "service"),
7281
createProperties("com.example.Foo", "entity")));
73-
assertThat(index.getCandidateTypes("com.example", "service")).contains("com.example.Foo");
74-
assertThat(index.getCandidateTypes("com.example", "entity")).contains("com.example.Foo");
75-
}
82+
assertThat(index.getCandidateTypes("com.example", "service")).contains("com.example.Foo");
83+
assertThat(index.getCandidateTypes("com.example", "entity")).contains("com.example.Foo");
84+
}
85+
86+
private static Properties createSampleProperties() {
87+
var properties = new Properties();
88+
properties.put("com.example.service.One", "service");
89+
properties.put("com.example.service.sub.Two", "service");
90+
properties.put("com.example.service.Three", "service");
91+
properties.put("com.example.domain.Four", "entity");
92+
return properties;
93+
}
94+
95+
private static Properties createProperties(String key, String... stereotypes) {
96+
var properties = new Properties();
97+
properties.put(key, String.join(",", stereotypes));
98+
return properties;
99+
}
76100

77-
@ParameterizedTest // gh-35601
78-
@ValueSource(strings = {
79-
"com.example.service",
80-
"com.example.service.sub",
81-
"com.example.service.subX",
82-
"com.example.domain",
83-
"com.example.domain.X"
84-
})
85-
void hasScannedPackage(String packageName) {
86-
CandidateComponentsIndex index = new CandidateComponentsIndex();
87-
createSampleProperties().keySet()
88-
.forEach(key -> index.registerScan(ClassUtils.getPackageName((String) key)));
89-
assertThat(index.hasScannedPackage(packageName)).isTrue();
90101
}
91102

92-
@ParameterizedTest // gh-35601
93-
@ValueSource(strings = {
94-
"com.example",
95-
"com.exampleX",
96-
"com.exampleX.service",
97-
"com.example.serviceX",
98-
"com.example.domainX"
99-
})
100-
void hasScannedPackageWithNoMatch(String packageName) {
101-
CandidateComponentsIndex index = new CandidateComponentsIndex();
102-
createSampleProperties().keySet()
103-
.forEach(key -> index.registerScan(ClassUtils.getPackageName((String) key)));
104-
assertThat(index.hasScannedPackage(packageName)).isFalse();
105-
}
106-
107-
108-
private static Properties createProperties(String key, String stereotypes) {
109-
Properties properties = new Properties();
110-
properties.put(key, String.join(",", stereotypes));
111-
return properties;
112-
}
103+
@Nested
104+
class ProgrammaticIndexTests {
105+
106+
@ParameterizedTest // gh-35601
107+
@ValueSource(strings = {
108+
"com.example.service",
109+
"com.example.service.sub",
110+
"com.example.service.subX",
111+
"com.example.domain",
112+
"com.example.domain.X"
113+
})
114+
void hasScannedPackage(String packageName) {
115+
var index = new CandidateComponentsIndex();
116+
index.registerScan("com.example.service", "com.example.service.sub", "com.example.domain");
117+
assertThat(index.hasScannedPackage(packageName)).isTrue();
118+
}
119+
120+
@ParameterizedTest // gh-35601
121+
@ValueSource(strings = {
122+
"com.example.service",
123+
"com.example.domain",
124+
"com.example.web",
125+
})
126+
void hasScannedPackageForStarPattern(String packageName) {
127+
var index = new CandidateComponentsIndex();
128+
index.registerScan("com.example.*");
129+
assertThat(index.hasScannedPackage(packageName)).isTrue();
130+
}
131+
132+
@ParameterizedTest // gh-35601
133+
@ValueSource(strings = {
134+
"com.example",
135+
"com.example.service",
136+
"com.example.service.sub",
137+
})
138+
void hasScannedPackageForStarStarPattern(String packageName) {
139+
var index = new CandidateComponentsIndex();
140+
index.registerScan("com.example.**");
141+
assertThat(index.hasScannedPackage(packageName)).isTrue();
142+
}
143+
144+
@ParameterizedTest // gh-35601
145+
@ValueSource(strings = {
146+
"com.example",
147+
"com.exampleX",
148+
"com.exampleX.service",
149+
"com.example.serviceX",
150+
"com.example.domainX"
151+
})
152+
void hasScannedPackageWithNoMatch(String packageName) {
153+
var index = new CandidateComponentsIndex();
154+
index.registerScan("com.example.service", "com.example.domain");
155+
assertThat(index.hasScannedPackage(packageName)).isFalse();
156+
}
157+
158+
@ParameterizedTest // gh-35601
159+
@ValueSource(strings = {
160+
"com.example",
161+
"com.exampleX",
162+
"com.exampleX.service"
163+
})
164+
void hasScannedPackageForStarPatternWithNoMatch(String packageName) {
165+
var index = new CandidateComponentsIndex();
166+
index.registerScan("com.example.*");
167+
assertThat(index.hasScannedPackage(packageName)).isFalse();
168+
}
169+
170+
@ParameterizedTest // gh-35601
171+
@ValueSource(strings = {
172+
"com.exampleX",
173+
"com.exampleX.service"
174+
})
175+
void hasScannedPackageForStarStarPatternWithNoMatch(String packageName) {
176+
var index = new CandidateComponentsIndex();
177+
index.registerScan("com.example.**");
178+
assertThat(index.hasScannedPackage(packageName)).isFalse();
179+
}
113180

114-
private static Properties createSampleProperties() {
115-
Properties properties = new Properties();
116-
properties.put("com.example.service.One", "service");
117-
properties.put("com.example.service.sub.Two", "service");
118-
properties.put("com.example.service.Three", "service");
119-
properties.put("com.example.domain.Four", "entity");
120-
return properties;
121181
}
122182

123183
}

0 commit comments

Comments
 (0)