Browse Source

Test package pattern support for CandidateComponentsIndex and restructure tests

pull/35608/head
Sam Brannen 2 months ago
parent
commit
a5d9bd64be
  1. 216
      spring-context/src/test/java/org/springframework/context/index/CandidateComponentsIndexTests.java

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

@ -18,14 +18,12 @@ package org.springframework.context.index; @@ -18,14 +18,12 @@ package org.springframework.context.index;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.util.ClassUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
@ -36,88 +34,150 @@ import static org.assertj.core.api.Assertions.assertThat; @@ -36,88 +34,150 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
class CandidateComponentsIndexTests {
@Test
void getCandidateTypes() {
CandidateComponentsIndex index = new CandidateComponentsIndex(List.of(createSampleProperties()));
Set<String> actual = index.getCandidateTypes("com.example.service", "service");
assertThat(actual).contains("com.example.service.One",
"com.example.service.sub.Two", "com.example.service.Three");
}
@Test
void getCandidateTypesNoMatch() {
CandidateComponentsIndex index = new CandidateComponentsIndex(List.of(createSampleProperties()));
Set<String> actual = index.getCandidateTypes("com.example.service", "entity");
assertThat(actual).isEmpty();
}
@Test
void getCandidateTypesSubPackage() {
CandidateComponentsIndex index = new CandidateComponentsIndex(List.of(createSampleProperties()));
Set<String> actual = index.getCandidateTypes("com.example.service.sub", "service");
assertThat(actual).contains("com.example.service.sub.Two");
}
@Nested
class ComponentIndexFilesTests {
@Test
void getCandidateTypesSubPackageNoMatch() {
CandidateComponentsIndex index = new CandidateComponentsIndex(List.of(createSampleProperties()));
Set<String> actual = index.getCandidateTypes("com.example.service.none", "service");
assertThat(actual).isEmpty();
}
@Test
void mergeCandidateStereotypes() {
CandidateComponentsIndex index = new CandidateComponentsIndex(List.of(
@Test
void getCandidateTypes() {
var index = new CandidateComponentsIndex(List.of(createSampleProperties()));
var candidateTypes = index.getCandidateTypes("com.example.service", "service");
assertThat(candidateTypes).contains("com.example.service.One",
"com.example.service.sub.Two", "com.example.service.Three");
}
@Test
void getCandidateTypesNoMatch() {
var index = new CandidateComponentsIndex(List.of(createSampleProperties()));
var candidateTypes = index.getCandidateTypes("com.example.service", "entity");
assertThat(candidateTypes).isEmpty();
}
@Test
void getCandidateTypesSubPackage() {
var index = new CandidateComponentsIndex(List.of(createSampleProperties()));
var candidateTypes = index.getCandidateTypes("com.example.service.sub", "service");
assertThat(candidateTypes).contains("com.example.service.sub.Two");
}
@Test
void getCandidateTypesSubPackageNoMatch() {
var index = new CandidateComponentsIndex(List.of(createSampleProperties()));
var candidateTypes = index.getCandidateTypes("com.example.service.none", "service");
assertThat(candidateTypes).isEmpty();
}
@Test
void parsesMultipleCandidateStereotypes() {
var index = new CandidateComponentsIndex(List.of(
createProperties("com.example.Foo", "service", "entity")));
assertThat(index.getCandidateTypes("com.example", "service")).contains("com.example.Foo");
assertThat(index.getCandidateTypes("com.example", "entity")).contains("com.example.Foo");
}
@Test
void mergesCandidateStereotypes() {
var index = new CandidateComponentsIndex(List.of(
createProperties("com.example.Foo", "service"),
createProperties("com.example.Foo", "entity")));
assertThat(index.getCandidateTypes("com.example", "service")).contains("com.example.Foo");
assertThat(index.getCandidateTypes("com.example", "entity")).contains("com.example.Foo");
}
assertThat(index.getCandidateTypes("com.example", "service")).contains("com.example.Foo");
assertThat(index.getCandidateTypes("com.example", "entity")).contains("com.example.Foo");
}
private static Properties createSampleProperties() {
var properties = new Properties();
properties.put("com.example.service.One", "service");
properties.put("com.example.service.sub.Two", "service");
properties.put("com.example.service.Three", "service");
properties.put("com.example.domain.Four", "entity");
return properties;
}
private static Properties createProperties(String key, String... stereotypes) {
var properties = new Properties();
properties.put(key, String.join(",", stereotypes));
return properties;
}
@ParameterizedTest // gh-35601
@ValueSource(strings = {
"com.example.service",
"com.example.service.sub",
"com.example.service.subX",
"com.example.domain",
"com.example.domain.X"
})
void hasScannedPackage(String packageName) {
CandidateComponentsIndex index = new CandidateComponentsIndex();
createSampleProperties().keySet()
.forEach(key -> index.registerScan(ClassUtils.getPackageName((String) key)));
assertThat(index.hasScannedPackage(packageName)).isTrue();
}
@ParameterizedTest // gh-35601
@ValueSource(strings = {
"com.example",
"com.exampleX",
"com.exampleX.service",
"com.example.serviceX",
"com.example.domainX"
})
void hasScannedPackageWithNoMatch(String packageName) {
CandidateComponentsIndex index = new CandidateComponentsIndex();
createSampleProperties().keySet()
.forEach(key -> index.registerScan(ClassUtils.getPackageName((String) key)));
assertThat(index.hasScannedPackage(packageName)).isFalse();
}
private static Properties createProperties(String key, String stereotypes) {
Properties properties = new Properties();
properties.put(key, String.join(",", stereotypes));
return properties;
}
@Nested
class ProgrammaticIndexTests {
@ParameterizedTest // gh-35601
@ValueSource(strings = {
"com.example.service",
"com.example.service.sub",
"com.example.service.subX",
"com.example.domain",
"com.example.domain.X"
})
void hasScannedPackage(String packageName) {
var index = new CandidateComponentsIndex();
index.registerScan("com.example.service", "com.example.service.sub", "com.example.domain");
assertThat(index.hasScannedPackage(packageName)).isTrue();
}
@ParameterizedTest // gh-35601
@ValueSource(strings = {
"com.example.service",
"com.example.domain",
"com.example.web",
})
void hasScannedPackageForStarPattern(String packageName) {
var index = new CandidateComponentsIndex();
index.registerScan("com.example.*");
assertThat(index.hasScannedPackage(packageName)).isTrue();
}
@ParameterizedTest // gh-35601
@ValueSource(strings = {
"com.example",
"com.example.service",
"com.example.service.sub",
})
void hasScannedPackageForStarStarPattern(String packageName) {
var index = new CandidateComponentsIndex();
index.registerScan("com.example.**");
assertThat(index.hasScannedPackage(packageName)).isTrue();
}
@ParameterizedTest // gh-35601
@ValueSource(strings = {
"com.example",
"com.exampleX",
"com.exampleX.service",
"com.example.serviceX",
"com.example.domainX"
})
void hasScannedPackageWithNoMatch(String packageName) {
var index = new CandidateComponentsIndex();
index.registerScan("com.example.service", "com.example.domain");
assertThat(index.hasScannedPackage(packageName)).isFalse();
}
@ParameterizedTest // gh-35601
@ValueSource(strings = {
"com.example",
"com.exampleX",
"com.exampleX.service"
})
void hasScannedPackageForStarPatternWithNoMatch(String packageName) {
var index = new CandidateComponentsIndex();
index.registerScan("com.example.*");
assertThat(index.hasScannedPackage(packageName)).isFalse();
}
@ParameterizedTest // gh-35601
@ValueSource(strings = {
"com.exampleX",
"com.exampleX.service"
})
void hasScannedPackageForStarStarPatternWithNoMatch(String packageName) {
var index = new CandidateComponentsIndex();
index.registerScan("com.example.**");
assertThat(index.hasScannedPackage(packageName)).isFalse();
}
private static Properties createSampleProperties() {
Properties properties = new Properties();
properties.put("com.example.service.One", "service");
properties.put("com.example.service.sub.Two", "service");
properties.put("com.example.service.Three", "service");
properties.put("com.example.domain.Four", "entity");
return properties;
}
}

Loading…
Cancel
Save