forked from spring-projects/spring-modulith
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spring-projectsGH-31 Streamline state handling and move it to its own…
… class
- Loading branch information
1 parent
653333b
commit 21cf5fe
Showing
3 changed files
with
85 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
spring-modulith-junit/src/main/java/org/springframework/modulith/StateStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.springframework.modulith; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ExtensionContext.Namespace; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor; | ||
import org.springframework.core.env.StandardEnvironment; | ||
import org.springframework.modulith.Change.JavaClassChange; | ||
import org.springframework.modulith.Change.JavaTestClassChange; | ||
import org.springframework.modulith.Change.OtherFileChange; | ||
import org.springframework.util.ClassUtils; | ||
|
||
class StateStore { | ||
private static final Logger log = LoggerFactory.getLogger(StateStore.class); | ||
|
||
private final ExtensionContext.Store store; | ||
|
||
StateStore(ExtensionContext context) { | ||
store = context.getRoot().getStore(Namespace.create(ModulithExecutionExtension.class)); | ||
} | ||
|
||
Set<Class<?>> getChangedClasses() { | ||
//noinspection unchecked | ||
return (Set<Class<?>>) store.getOrComputeIfAbsent("changed-files", s -> { | ||
var environment = new StandardEnvironment(); | ||
ConfigDataEnvironmentPostProcessor.applyTo(environment); | ||
|
||
var detector = FileModificationDetector.loadFileModificationDetector(environment); | ||
try { | ||
Set<ModifiedFilePath> modifiedFiles = detector.getModifiedFiles(environment); | ||
Set<Change> changes = Changes.toChanges(modifiedFiles); | ||
return toChangedClasses(changes); | ||
} catch (Exception e) { | ||
log.error("ModulithExecutionExtension: Unable to fetch changed files, executing all tests", e); | ||
return Set.of(); | ||
} | ||
}); | ||
} | ||
|
||
private static Set<Class<?>> toChangedClasses(Set<Change> changes) { | ||
Set<Class<?>> changedClasses = new HashSet<>(); | ||
for (Change change : changes) { | ||
if (change instanceof OtherFileChange) { | ||
continue; | ||
} | ||
|
||
String className; | ||
if (change instanceof JavaClassChange jcc) { | ||
className = jcc.fullyQualifiedClassName(); | ||
} else if (change instanceof JavaTestClassChange jtcc) { | ||
className = jtcc.fullyQualifiedClassName(); | ||
} else { | ||
throw new IllegalStateException("Unexpected change type: " + change.getClass()); | ||
} | ||
|
||
try { | ||
Class<?> aClass = ClassUtils.forName(className, null); | ||
changedClasses.add(aClass); | ||
} catch (ClassNotFoundException e) { | ||
log.trace("ModulithExecutionExtension: Unable to find class \"{}\"", className); | ||
} | ||
} | ||
return changedClasses; | ||
} | ||
} |