Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,46 +161,60 @@ sealed interface Change permits SourceChange, OtherFileChange {
boolean hasOrigin(String nameOrPath);

/**
* creates a new
*
* @param file
* @return
* Creates a change from a modified file
*/
static Change of(ModifiedFile file) {
boolean isJavaSource = file.isJavaSource();

if (!file.isJavaSource()) {
if (!isJavaSource && !file.isKotlinSource()) {
return new OtherFileChange(file.path());
}

var languageConfig = isJavaSource ? LanguageConfig.JAVA : LanguageConfig.KOTLIN;
return toSourceChange(file, languageConfig);
}

private static Change toSourceChange(ModifiedFile file, LanguageConfig config) {
var withoutExtension = StringUtils.stripFilenameExtension(file.path());
var startOfMainDir = withoutExtension.indexOf(JavaSourceChange.STANDARD_SOURCE_DIRECTORY);
var startOfTestDir = withoutExtension.indexOf(JavaSourceChange.STANDARD_TEST_SOURCE_DIRECTORY);
var startOfMainDir = withoutExtension.indexOf(config.mainDir());
var startOfTestDir = withoutExtension.indexOf(config.testDir());

if (startOfTestDir > -1 && (startOfMainDir < 0 || startOfTestDir < startOfMainDir)) {

var fullyQualifiedClassName = ClassUtils.convertResourcePathToClassName(
withoutExtension.substring(startOfTestDir + JavaSourceChange.STANDARD_TEST_SOURCE_DIRECTORY.length() + 1));
var fullyQualifiedClassName = ClassUtils
.convertResourcePathToClassName(withoutExtension.substring(startOfTestDir + config.testDir().length() + 1));

return new JavaTestSourceChange(fullyQualifiedClassName);
return config.testFactory().apply(fullyQualifiedClassName);
}

if (startOfMainDir > -1 && (startOfTestDir < 0 || startOfMainDir < startOfTestDir)) {

var fullyQualifiedClassName = ClassUtils.convertResourcePathToClassName(
withoutExtension.substring(startOfMainDir + JavaSourceChange.STANDARD_SOURCE_DIRECTORY.length() + 1));
var fullyQualifiedClassName = ClassUtils
.convertResourcePathToClassName(withoutExtension.substring(startOfMainDir + config.mainDir().length() + 1));

return new JavaSourceChange(fullyQualifiedClassName);
return config.mainFactory().apply(fullyQualifiedClassName);
}

return new JavaSourceChange(ClassUtils.convertResourcePathToClassName(withoutExtension));
return config.mainFactory().apply(ClassUtils.convertResourcePathToClassName(withoutExtension));
}

record LanguageConfig(String mainDir, String testDir, Function<String, ? extends SourceChange> mainFactory,
Function<String, ? extends SourceChange> testFactory) {

static final LanguageConfig JAVA = new LanguageConfig(JavaSourceChange.STANDARD_SOURCE_DIRECTORY,
JavaSourceChange.STANDARD_TEST_SOURCE_DIRECTORY, JavaSourceChange::new, JavaTestSourceChange::new);

static final LanguageConfig KOTLIN = new LanguageConfig(KotlinSourceChange.STANDARD_SOURCE_DIRECTORY,
KotlinSourceChange.STANDARD_TEST_SOURCE_DIRECTORY, KotlinSourceChange::new, KotlinTestSourceChange::new);
}

/**
* A change to a source file.
*
* @author Oliver Drotbohm
*/
sealed interface SourceChange extends Change permits JavaSourceChange, JavaTestSourceChange {
sealed interface SourceChange extends Change
permits KotlinSourceChange, KotlinTestSourceChange, JavaSourceChange, JavaTestSourceChange {

String fullyQualifiedClassName();

Expand Down Expand Up @@ -243,7 +257,22 @@ public final String toString() {
* @author David Bilge
* @author Oliver Drotbohm
*/
record JavaTestSourceChange(String fullyQualifiedClassName) implements SourceChange {}
record JavaTestSourceChange(String fullyQualifiedClassName) implements SourceChange {
}

/**
* A change in a Kotlin source file.
*/
record KotlinSourceChange(String fullyQualifiedClassName) implements SourceChange {
private static final String STANDARD_SOURCE_DIRECTORY = "src/main/kotlin";
private static final String STANDARD_TEST_SOURCE_DIRECTORY = "src/test/kotlin";
}

/**
* A change in a Kotlin test source file.
*/
record KotlinTestSourceChange(String fullyQualifiedClassName) implements SourceChange {
}

/**
* Some arbitrary file change.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public boolean isJavaSource() {
return "java".equalsIgnoreCase(StringUtils.getFilenameExtension(path));
}

/**
* Returns whether the modified file is Kotlin source file.
*/
public boolean isKotlinSource() {
return "kt".equalsIgnoreCase(StringUtils.getFilenameExtension(path));
}

public static Stream<ModifiedFile> of(String... paths) {
return Arrays.stream(paths).map(ModifiedFile::new);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.junit.jupiter.api.TestFactory;
import org.springframework.modulith.junit.Changes.Change.JavaSourceChange;
import org.springframework.modulith.junit.Changes.Change.JavaTestSourceChange;
import org.springframework.modulith.junit.Changes.Change.KotlinSourceChange;
import org.springframework.modulith.junit.Changes.Change.KotlinTestSourceChange;
import org.springframework.modulith.junit.Changes.Change.OtherFileChange;
import org.springframework.modulith.junit.diff.ModifiedFile;

Expand Down Expand Up @@ -73,6 +75,8 @@ void shouldInterpredModifiedFilePathsCorrectly() {
var modifiedFilePaths = Stream.of(
"src/main/java/org/springframework/modulith/junit/Changes.java",
"src/test/java/org/springframework/modulith/ChangesTest.java",
"src/test/kotlin/org/springframework/modulith/KotlinServiceTest.kt",
"src/main/kotlin/org/springframework/modulith/KotlinService.kt",
"src/main/resources/META-INF/additional-spring-configuration-metadata.json")
.map(ModifiedFile::new);

Expand All @@ -84,6 +88,8 @@ void shouldInterpredModifiedFilePathsCorrectly() {
assertThat(result).containsExactlyInAnyOrder(
new JavaSourceChange("org.springframework.modulith.junit.Changes"),
new JavaTestSourceChange("org.springframework.modulith.ChangesTest"),
new KotlinTestSourceChange("org.springframework.modulith.KotlinServiceTest"),
new KotlinSourceChange("org.springframework.modulith.KotlinService"),
new OtherFileChange("src/main/resources/META-INF/additional-spring-configuration-metadata.json"));
}
}
Loading