Skip to content

Commit 3246099

Browse files
committed
GH-1015 - Polishing.
1 parent fe84013 commit 3246099

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.stream.Stream;
2221

2322
import org.springframework.modulith.Modulith;
@@ -33,26 +32,24 @@
3332
public interface ModulithMetadata {
3433

3534
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!";
35+
static final String WITH_ANNOTATIONS = ANNOTATION_MISSING.formatted("%s", Modulith.class.getSimpleName(),
36+
Modulithic.class.getSimpleName(), SpringTypes.AT_SPRING_BOOT_APPLICATION);
3637

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

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

49-
Supplier<IllegalArgumentException> exception = () -> new IllegalArgumentException(
50-
String.format(ANNOTATION_MISSING, annotated.getSimpleName(), Modulith.class.getSimpleName(),
51-
Modulithic.class.getSimpleName(), SpringTypes.AT_SPRING_BOOT_APPLICATION));
52-
53-
Supplier<ModulithMetadata> withDefaults = () -> SpringBootModulithMetadata.of(annotated).orElseThrow(exception);
54-
55-
return AnnotationModulithMetadata.of(annotated).orElseGet(withDefaults);
50+
return AnnotationModulithMetadata.of(annotated)
51+
.or(() -> SpringBootModulithMetadata.of(annotated))
52+
.orElseThrow(() -> new IllegalArgumentException(WITH_ANNOTATIONS.formatted(annotated.getSimpleName())));
5653
}
5754

5855
/**

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)