refactor: 리펙토링 진행중 의존성 gradle은 얇게 유지, 각 모듈에서 처리하고 있는 부분에 대해서 리펙토링
This commit is contained in:
+23
@@ -48,6 +48,12 @@ public class MessagingProviderSelection {
|
||||
"kafka", "dev.caskeleton.messaging.autoconfigure.KafkaMessagingAutoConfiguration",
|
||||
"rabbit", "dev.caskeleton.messaging.autoconfigure.RabbitMessagingAutoConfiguration");
|
||||
|
||||
/** Broker id to the starter a deployment adds to its build to obtain that transport. */
|
||||
static final Map<String, String> PROVIDER_STARTERS =
|
||||
Map.of(
|
||||
"kafka", "messaging-spring-boot-starter-kafka",
|
||||
"rabbit", "messaging-spring-boot-starter-rabbit");
|
||||
|
||||
/**
|
||||
* Broker ids that ship without a {@code MessagingTransport}, and why.
|
||||
*
|
||||
@@ -118,6 +124,23 @@ public class MessagingProviderSelection {
|
||||
+ clientClass
|
||||
+ " on the classpath; the deployment selected a transport it did not ship");
|
||||
}
|
||||
// The client library and the broker starter are two different build decisions, and only the
|
||||
// second one carries the configuration that assembles the transport. A deployment can hold the
|
||||
// Kafka client transitively and still not ship the Kafka starter — this core starter is
|
||||
// broker-neutral on purpose — and until this check existed that combination reached Spring's
|
||||
// import machinery, which reported a missing .class file rather than a missing capability.
|
||||
String providerConfiguration = PROVIDER_CONFIGURATIONS.get(broker);
|
||||
if (!ClassUtils.isPresent(
|
||||
providerConfiguration, MessagingProviderSelection.class.getClassLoader())) {
|
||||
throw new IllegalStateException(
|
||||
MessagingSettings.PREFIX
|
||||
+ ".broker="
|
||||
+ broker
|
||||
+ " needs the "
|
||||
+ PROVIDER_STARTERS.get(broker)
|
||||
+ " module on the classpath; this core starter is broker-neutral and the deployment"
|
||||
+ " did not compose the transport it selected");
|
||||
}
|
||||
String unfinished = BROKERS_WITHOUT_A_TRANSPORT.get(broker);
|
||||
if (unfinished != null) {
|
||||
throw new IllegalStateException(
|
||||
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
package dev.caskeleton.messaging.autoconfigure;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
|
||||
/**
|
||||
* This leaf's auto-configurations are registered, not merely annotated.
|
||||
*
|
||||
* <p>Spring Boot needs two independent things before an auto-configuration runs: the annotation on
|
||||
* the class, and a line naming it in {@code META-INF/spring/...AutoConfiguration.imports}. Tests
|
||||
* that build a context with {@code AutoConfigurations.of(...)} supply the second one themselves, so
|
||||
* they stay green for a capability that is present in no running application — which is exactly how
|
||||
* several capabilities in this repository came to be assembled by nothing.
|
||||
*
|
||||
* <p>So this reads the registration file the jar actually ships and asserts both directions: every
|
||||
* class this leaf means to register is named in it, and every name in it resolves to a class that
|
||||
* carries {@code @AutoConfiguration}. A rename, a move or a deleted line fails here rather than in
|
||||
* a deployment that starts and quietly does less than it says.
|
||||
*/
|
||||
class MessagingPlatformAutoConfigurationRegistrationTest {
|
||||
|
||||
private static final String IMPORTS_RESOURCE =
|
||||
"META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports";
|
||||
|
||||
/** The auto-configurations this leaf publishes. */
|
||||
private static final List<String> PUBLISHED_ROOTS =
|
||||
List.of("dev.caskeleton.messaging.autoconfigure.MessagingPlatformRootAutoConfiguration");
|
||||
|
||||
@Test
|
||||
@DisplayName("the shipped registration file names every root this leaf publishes")
|
||||
void theShippedRegistrationFileNamesEveryRoot() {
|
||||
assertThat(registeredNames())
|
||||
.as("the registration lines this leaf's own jar ships")
|
||||
.containsAll(PUBLISHED_ROOTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("every registered name resolves to a real Boot auto-configuration")
|
||||
void everyRegisteredNameResolvesToAnAutoConfiguration() {
|
||||
for (String name : registeredNames()) {
|
||||
Class<?> registered;
|
||||
try {
|
||||
registered =
|
||||
Class.forName(
|
||||
name,
|
||||
false,
|
||||
MessagingPlatformAutoConfigurationRegistrationTest.class.getClassLoader());
|
||||
} catch (ClassNotFoundException missing) {
|
||||
throw new AssertionError(
|
||||
IMPORTS_RESOURCE + " registers " + name + ", which does not exist", missing);
|
||||
}
|
||||
assertThat(AnnotatedElementUtils.hasAnnotation(registered, AutoConfiguration.class))
|
||||
.as("%s is registered but is not annotated @AutoConfiguration", name)
|
||||
.isTrue();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The registration lines this leaf's own resource declares.
|
||||
*
|
||||
* <p>Read from this leaf's source tree rather than from the classpath. {@code
|
||||
* ClassLoader#getResources} returns every registration file on the test runtime classpath —
|
||||
* Spring Boot's, springdoc's, and every dependency leaf's — so asserting over that set tests
|
||||
* other people's modules and fails on their conditional classes. The file below is the one this
|
||||
* leaf packages, which is the thing this contract is about.
|
||||
*/
|
||||
private static List<String> registeredNames() {
|
||||
Path resource = leafRoot().resolve("src/main/resources/" + IMPORTS_RESOURCE);
|
||||
assertThat(Files.isRegularFile(resource)).as("%s", resource).isTrue();
|
||||
List<String> names;
|
||||
try {
|
||||
names =
|
||||
Files.readAllLines(resource, StandardCharsets.UTF_8).stream()
|
||||
.map(String::strip)
|
||||
.filter(line -> !line.isEmpty() && !line.startsWith("#"))
|
||||
.toList();
|
||||
} catch (IOException failure) {
|
||||
throw new UncheckedIOException("cannot read " + resource, failure);
|
||||
}
|
||||
assertThat(names).as("%s registers nothing", resource).isNotEmpty();
|
||||
return names;
|
||||
}
|
||||
|
||||
/** The nearest directory above the test's working directory that ships a registration file. */
|
||||
private static Path leafRoot() {
|
||||
for (Path path = Path.of("").toAbsolutePath(); path != null; path = path.getParent()) {
|
||||
if (Files.isRegularFile(path.resolve("src/main/resources/" + IMPORTS_RESOURCE))) {
|
||||
return path;
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException(
|
||||
"no leaf shipping " + IMPORTS_RESOURCE + " found above " + Path.of("").toAbsolutePath());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user