Skip to content

Commit

Permalink
GH-1015 - Polishing.
Browse files Browse the repository at this point in the history
  • Loading branch information
odrotbohm committed Jan 16, 2025
1 parent 38b7ac7 commit f941756
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.Set;
import java.util.stream.Stream;

Expand All @@ -38,26 +37,24 @@
public interface ModulithMetadata {

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

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

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

Supplier<IllegalArgumentException> exception = () -> new IllegalArgumentException(
String.format(ANNOTATION_MISSING, annotated.getSimpleName(), Modulith.class.getSimpleName(),
Modulithic.class.getSimpleName(), SpringTypes.AT_SPRING_BOOT_APPLICATION));

Supplier<ModulithMetadata> withDefaults = () -> SpringBootModulithMetadata.of(annotated).orElseThrow(exception);

return AnnotationModulithMetadata.of(annotated).orElseGet(withDefaults);
return AnnotationModulithMetadata.of(annotated)
.or(() -> SpringBootModulithMetadata.of(annotated))
.orElseThrow(() -> new IllegalArgumentException(WITH_ANNOTATIONS.formatted(annotated.getSimpleName())));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,38 @@
class ModulithMetadataUnitTest {

@Test
public void inspectsModulithAnnotation() throws Exception {
void inspectsModulithAnnotation() throws Exception {

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

assertThat(it.getBasePackages()).containsExactly("org.springframework.modulith.core", "com.acme.foo");
assertThat(it.getSharedModuleNames()).containsExactly("shared.module");
assertThat(it.getSharedModuleIdentifiers())
.extracting(ApplicationModuleIdentifier::toString)
.containsExactly("shared.module");
assertThat(it.getSystemName()).hasValue("systemName");
assertThat(it.useFullyQualifiedModuleNames()).isTrue();
});
}

@Test
public void usesDefaultsIfModulithAnnotationsAreMissing() {
void usesDefaultsIfModulithAnnotationsAreMissing() {

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

assertThat(metadata.getBasePackages()).contains("org.springframework.modulith.core");
assertThat(metadata.getSharedModuleNames()).isEmpty();
assertThat(metadata.getSharedModuleIdentifiers()).isEmpty();
assertThat(metadata.getSystemName()).hasValue(SpringBootApplicationAnnotated.class.getSimpleName());
assertThat(metadata.useFullyQualifiedModuleNames()).isFalse();
}

@Test
public void rejectsTypeNotAnnotatedWithEitherModulithAnnotationOrSpringBootApplication() {
void rejectsTypeNotAnnotatedWithEitherModulithAnnotationOrSpringBootApplication() {

assertThatExceptionOfType(IllegalArgumentException.class) //
.isThrownBy(() -> ModulithMetadata.of(Unannotated.class)) //
.withMessageContaining(Unannotated.class.getSimpleName()) //
.withMessageContaining(Modulith.class.getSimpleName()) //
.withMessageContaining(Modulithic.class.getSimpleName()) //
.withMessageContaining(SpringBootApplication.class.getSimpleName());
Expand Down

0 comments on commit f941756

Please sign in to comment.