Skip to content

Commit f941756

Browse files
committed
GH-1015 - Polishing.
1 parent 38b7ac7 commit f941756

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

spring-modulith-core/src/main/java/org/springframework/modulith/core/ModulithMetadata.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.util.List;
1919
import java.util.Optional;
20-
import java.util.function.Supplier;
2120
import java.util.Set;
2221
import java.util.stream.Stream;
2322

@@ -38,26 +37,24 @@
3837
public interface ModulithMetadata {
3938

4039
static final String ANNOTATION_MISSING = "Modules can only be retrieved from a root type, but %s is not annotated with either @%s, @%s or @%s!";
40+
static final String WITH_ANNOTATIONS = ANNOTATION_MISSING.formatted("%s", Modulith.class.getSimpleName(),
41+
Modulithic.class.getSimpleName(), SpringTypes.AT_SPRING_BOOT_APPLICATION);
4142

4243
/**
4344
* Creates a new {@link ModulithMetadata} for the given annotated type. Expects the type either be annotated with
4445
* {@link Modulith}, {@link Modulithic} or {@link org.springframework.boot.autoconfigure.SpringBootApplication}.
4546
*
4647
* @param annotated must not be {@literal null}.
47-
* @return
48+
* @return will never be {@literal null}.
4849
* @throws IllegalArgumentException in case none of the above mentioned annotations is present on the given type.
4950
*/
5051
public static ModulithMetadata of(Class<?> annotated) {
5152

5253
Assert.notNull(annotated, "Annotated type must not be null!");
5354

54-
Supplier<IllegalArgumentException> exception = () -> new IllegalArgumentException(
55-
String.format(ANNOTATION_MISSING, annotated.getSimpleName(), Modulith.class.getSimpleName(),
56-
Modulithic.class.getSimpleName(), SpringTypes.AT_SPRING_BOOT_APPLICATION));
57-
58-
Supplier<ModulithMetadata> withDefaults = () -> SpringBootModulithMetadata.of(annotated).orElseThrow(exception);
59-
60-
return AnnotationModulithMetadata.of(annotated).orElseGet(withDefaults);
55+
return AnnotationModulithMetadata.of(annotated)
56+
.or(() -> SpringBootModulithMetadata.of(annotated))
57+
.orElseThrow(() -> new IllegalArgumentException(WITH_ANNOTATIONS.formatted(annotated.getSimpleName())));
6158
}
6259

6360
/**

spring-modulith-core/src/test/java/org/springframework/modulith/core/ModulithMetadataUnitTest.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,38 @@
3232
class ModulithMetadataUnitTest {
3333

3434
@Test
35-
public void inspectsModulithAnnotation() throws Exception {
35+
void inspectsModulithAnnotation() throws Exception {
3636

3737
Stream.of(ModulithAnnotated.class, ModuliticAnnotated.class) //
3838
.map(ModulithMetadata::of) //
3939
.forEach(it -> {
4040

4141
assertThat(it.getBasePackages()).containsExactly("org.springframework.modulith.core", "com.acme.foo");
42-
assertThat(it.getSharedModuleNames()).containsExactly("shared.module");
42+
assertThat(it.getSharedModuleIdentifiers())
43+
.extracting(ApplicationModuleIdentifier::toString)
44+
.containsExactly("shared.module");
4345
assertThat(it.getSystemName()).hasValue("systemName");
4446
assertThat(it.useFullyQualifiedModuleNames()).isTrue();
4547
});
4648
}
4749

4850
@Test
49-
public void usesDefaultsIfModulithAnnotationsAreMissing() {
51+
void usesDefaultsIfModulithAnnotationsAreMissing() {
5052

5153
ModulithMetadata metadata = ModulithMetadata.of(SpringBootApplicationAnnotated.class);
5254

5355
assertThat(metadata.getBasePackages()).contains("org.springframework.modulith.core");
54-
assertThat(metadata.getSharedModuleNames()).isEmpty();
56+
assertThat(metadata.getSharedModuleIdentifiers()).isEmpty();
5557
assertThat(metadata.getSystemName()).hasValue(SpringBootApplicationAnnotated.class.getSimpleName());
5658
assertThat(metadata.useFullyQualifiedModuleNames()).isFalse();
5759
}
5860

5961
@Test
60-
public void rejectsTypeNotAnnotatedWithEitherModulithAnnotationOrSpringBootApplication() {
62+
void rejectsTypeNotAnnotatedWithEitherModulithAnnotationOrSpringBootApplication() {
6163

6264
assertThatExceptionOfType(IllegalArgumentException.class) //
6365
.isThrownBy(() -> ModulithMetadata.of(Unannotated.class)) //
66+
.withMessageContaining(Unannotated.class.getSimpleName()) //
6467
.withMessageContaining(Modulith.class.getSimpleName()) //
6568
.withMessageContaining(Modulithic.class.getSimpleName()) //
6669
.withMessageContaining(SpringBootApplication.class.getSimpleName());

0 commit comments

Comments
 (0)