Skip to content

Commit cb6d71b

Browse files
committed
GH-319 - Fix ApplicationModule by package name lookup.
Prior to this commit, the lookup for an ApplicationModule would find modules solely depending on the reference string starting with the base package. That means that a module with base package com.acme.foo, a request for com.acme.foobar would've resulted in a positive match, which of course is wrong. We now match against either the module's base package or against the reference starting with the base package followed by a dot.
1 parent db1c2b8 commit cb6d71b

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,24 @@ boolean contains(String candidate) {
414414
return getType(candidate).isPresent();
415415
}
416416

417+
/**
418+
* Returns whether the {@link ApplicationModule} contains the package with the given name, which means the given
419+
* package is either the module's base package or a sub package of it.
420+
*
421+
* @param packageName must not be {@literal null} or empty.
422+
* @return whether the {@link ApplicationModule} contains the package with the given name.
423+
* @since 1.0.2
424+
*/
425+
boolean containsPackage(String packageName) {
426+
427+
Assert.hasText(packageName, "Package name must not be null or empty!");
428+
429+
var basePackageName = basePackage.getName();
430+
431+
return packageName.equals(basePackageName)
432+
|| packageName.startsWith(basePackageName + ".");
433+
}
434+
417435
/*
418436
* (non-Javadoc)
419437
* @see java.lang.Object#equals(java.lang.Object)

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,16 @@ public Optional<ApplicationModule> getModuleByType(Class<?> candidate) {
326326
return getModuleByType(candidate.getName());
327327
}
328328

329+
/**
330+
* Returns the {@link ApplicationModule} containing the given package.
331+
*
332+
* @param name must not be {@literal null} or empty.
333+
* @return will never be {@literal null}.
334+
*/
329335
public Optional<ApplicationModule> getModuleForPackage(String name) {
330336

331337
return modules.values().stream() //
332-
.filter(it -> name.startsWith(it.getBasePackage().getName())) //
338+
.filter(it -> it.containsPackage(name)) //
333339
.findFirst();
334340
}
335341

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
package org.springframework.modulith.core;
1717

1818
import static org.assertj.core.api.Assertions.*;
19+
import static org.junit.jupiter.api.Assertions.*;
1920

2021
import java.util.List;
2122

2223
import javax.sql.DataSource;
2324

25+
import org.junit.jupiter.api.DynamicTest;
2426
import org.junit.jupiter.api.Test;
2527
import org.junit.jupiter.api.TestInstance;
2628
import org.junit.jupiter.api.TestInstance.Lifecycle;
@@ -83,4 +85,13 @@ void detectsAggregates() {
8385
.<Class<?>> extracting(JavaClass::reflect)
8486
.containsExactly(SampleAggregate.class);
8587
}
88+
89+
@Test // GH-319
90+
void containsPackage() {
91+
92+
assertThat(module.containsPackage(packageName)).isTrue();
93+
assertThat(module.containsPackage(packageName + ".foo")).isTrue();
94+
95+
assertThat(module.containsPackage(packageName + "foo")).isFalse();
96+
}
8697
}

0 commit comments

Comments
 (0)