Skip to content

Commit

Permalink
GH-31 - Support for optimized test execution.
Browse files Browse the repository at this point in the history
Initital prototype to support optimized test execution based on the Spring Modulith application module model. The change introduces a new artifact spring-modulith-junit that extends JUnit's test execution lifecycle. It obtains the ApplicationModules model for the application and potentially skips test classes for execution in case the changes made to the application reside in modules the current test case's module does not depend on.

Co-authored-by: Lukas Dohmen <[email protected]>
Co-authored-by: David Bilge <[email protected]>
  • Loading branch information
3 people authored and odrotbohm committed Sep 11, 2024
1 parent f70f174 commit a3cafeb
Show file tree
Hide file tree
Showing 19 changed files with 630 additions and 13 deletions.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
<module>spring-modulith-runtime</module>
<module>spring-modulith-starters</module>
<module>spring-modulith-test</module>
</modules>
<module>spring-modulith-junit</module>
</modules>

<properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-junit</artifactId>
<version>1.3.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package example.order;

import lombok.RequiredArgsConstructor;

import org.junit.jupiter.api.Test;
import org.springframework.modulith.test.ApplicationModuleTest;
import org.springframework.modulith.test.Scenario;
Expand All @@ -28,16 +27,16 @@
@RequiredArgsConstructor
class OrderIntegrationTests {

private final OrderManagement orders;

@Test
void publishesOrderCompletion(Scenario scenario) {
private final OrderManagement orders;

var reference = new Order();
@Test
void publishesOrderCompletion(Scenario scenario) {
// this is a change
var reference = new Order();

scenario.stimulate(() -> orders.complete(reference))
.andWaitForEventOfType(OrderCompleted.class)
.matchingMappedValue(OrderCompleted::orderId, reference.getId())
.toArrive();
}
scenario.stimulate(() -> orders.complete(reference))
.andWaitForEventOfType(OrderCompleted.class)
.matchingMappedValue(OrderCompleted::orderId, reference.getId())
.toArrive();
}
}
57 changes: 57 additions & 0 deletions spring-modulith-junit/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith</artifactId>
<version>1.3.0-GH-31-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<name>Spring Modulith - Test Junit</name>

<artifactId>spring-modulith-junit</artifactId>

<properties>
<module.name>org.springframework.modulith.junit</module.name>
</properties>

<dependencies>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>6.8.0.202311291450-r</version>
</dependency>
<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.springframework.modulith.junit;

sealed interface Change {
record JavaClassChange(String fullyQualifiedClassName) implements Change {}

record JavaTestClassChange(String fullyQualifiedClassName) implements Change {}

record OtherFileChange(String path) implements Change {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.springframework.modulith.junit;

import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.modulith.junit.Change.JavaClassChange;
import org.springframework.modulith.junit.Change.JavaTestClassChange;
import org.springframework.modulith.junit.Change.OtherFileChange;
import org.springframework.modulith.junit.diff.ModifiedFilePath;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

final class Changes {
private static final String STANDARD_SOURCE_DIRECTORY = "src/main/java";
private static final String STANDARD_TEST_SOURCE_DIRECTORY = "src/test/java";

private Changes() {}

static Set<Change> toChanges(Set<ModifiedFilePath> modifiedFilePaths) {
return modifiedFilePaths.stream().map(Changes::toChange).collect(Collectors.toSet());
}

static Change toChange(ModifiedFilePath modifiedFilePath) {
if ("java".equalsIgnoreCase(StringUtils.getFilenameExtension(modifiedFilePath.path()))) {
String withoutExtension = StringUtils.stripFilenameExtension(modifiedFilePath.path());

int startOfMainDir = withoutExtension.indexOf(STANDARD_SOURCE_DIRECTORY);
int startOfTestDir = withoutExtension.indexOf(STANDARD_TEST_SOURCE_DIRECTORY);

if (startOfTestDir > 0 && (startOfMainDir < 0 || startOfTestDir < startOfMainDir)) {
String fullyQualifiedClassName = ClassUtils.convertResourcePathToClassName(
withoutExtension.substring(startOfTestDir + STANDARD_TEST_SOURCE_DIRECTORY.length() + 1));

return new JavaTestClassChange(fullyQualifiedClassName);
} else if (startOfMainDir > 0 && (startOfTestDir < 0 || startOfMainDir < startOfTestDir)) {
String fullyQualifiedClassName = ClassUtils.convertResourcePathToClassName(
withoutExtension.substring(startOfMainDir + STANDARD_SOURCE_DIRECTORY.length() + 1));

return new JavaClassChange(fullyQualifiedClassName);
} else {
// This is unusual, fall back to just assume that the full path is the package -> TODO At least log this
String fullyQualifiedClassName = ClassUtils.convertResourcePathToClassName(withoutExtension);

return new JavaClassChange(fullyQualifiedClassName);
}
} else {
// TODO Do these need to be relative to the module root (i.e. where src/main/java etc. reside)?
return new OtherFileChange(modifiedFilePath.path());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package org.springframework.modulith.junit;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.AnnotatedClassFinder;
import org.springframework.modulith.core.ApplicationModule;
import org.springframework.modulith.core.ApplicationModuleDependency;
import org.springframework.modulith.core.ApplicationModules;
import org.springframework.util.ClassUtils;

import com.tngtech.archunit.core.domain.JavaClass;

// add logging to explain what happens (and why)

/**
* Junit Extension to skip test execution if no changes happened in the module that the test belongs to.
*
* @author Lukas Dohmen, David Bilge
*/
public class ModulithExecutionExtension implements ExecutionCondition {
public static final String CONFIG_PROPERTY_PREFIX = "spring.modulith.test";
final AnnotatedClassFinder spaClassFinder = new AnnotatedClassFinder(SpringBootApplication.class);
private static final Logger log = LoggerFactory.getLogger(ModulithExecutionExtension.class);

@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
if (context.getTestMethod().isPresent()) {
// Is there something similar liken @TestInstance(TestInstance.Lifecycle.PER_CLASS) for Extensions?
return ConditionEvaluationResult.enabled("Enabled, only evaluating per class");
}

StateStore stateStore = new StateStore(context);
Set<Class<?>> changedClasses = stateStore.getChangedClasses();
if (changedClasses.isEmpty()) {
log.trace("No class changes found, running tests");
return ConditionEvaluationResult.enabled("ModulithExecutionExtension: No changes detected");
}

log.trace("Found following changed classes {}", changedClasses);

Optional<Class<?>> testClass = context.getTestClass();
if (testClass.isPresent()) {
if (changedClasses.contains(testClass.get())) {
return ConditionEvaluationResult.enabled("ModulithExecutionExtension: Change in test class detected");
}
Class<?> mainClass = this.spaClassFinder.findFromClass(testClass.get());

if (mainClass == null) {// TODO:: Try with @ApplicationModuleTest -> main class
return ConditionEvaluationResult.enabled(
"ModulithExecutionExtension: Unable to locate SpringBootApplication Class");
}
ApplicationModules applicationModules = ApplicationModules.of(mainClass);

String packageName = ClassUtils.getPackageName(testClass.get());

// always run test if one of whitelisted files is modified (ant matching)
Optional<ApplicationModule> optionalApplicationModule = applicationModules.getModuleForPackage(packageName);
if (optionalApplicationModule.isPresent()) {

Set<JavaClass> dependentClasses = getAllDependentClasses(optionalApplicationModule.get(),
applicationModules);

for (Class<?> changedClass : changedClasses) {

if (optionalApplicationModule.get().contains(changedClass)) {
return ConditionEvaluationResult.enabled(
"ModulithExecutionExtension: Changes in module detected, Executing tests");
}

if (dependentClasses.stream()
.anyMatch(applicationModule -> applicationModule.isEquivalentTo(changedClass))) {
return ConditionEvaluationResult.enabled(
"ModulithExecutionExtension: Changes in dependent module detected, Executing tests");
}
}
}
}

return ConditionEvaluationResult.disabled(
"ModulithExtension: No Changes detected in current module, executing tests");
}

private Set<JavaClass> getAllDependentClasses(ApplicationModule applicationModule,
ApplicationModules applicationModules) {

Set<ApplicationModule> dependentModules = new HashSet<>();
dependentModules.add(applicationModule);
this.getDependentModules(applicationModule, applicationModules, dependentModules);

return dependentModules.stream()
.map(appModule -> appModule.getDependencies(applicationModules))
.flatMap(applicationModuleDependencies -> applicationModuleDependencies.stream()
.map(ApplicationModuleDependency::getTargetType))
.collect(Collectors.toSet());
}

private void getDependentModules(ApplicationModule applicationModule, ApplicationModules applicationModules,
Set<ApplicationModule> modules) {

Set<ApplicationModule> applicationModuleDependencies = applicationModule.getDependencies(applicationModules)
.stream()
.map(ApplicationModuleDependency::getTargetModule)
.collect(Collectors.toSet());

modules.addAll(applicationModuleDependencies);
if (!applicationModuleDependencies.isEmpty()) {
for (ApplicationModule applicationModuleDependency : applicationModuleDependencies) {
this.getDependentModules(applicationModuleDependency, applicationModules, modules);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.springframework.modulith.junit;

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.junit.Change.JavaClassChange;
import org.springframework.modulith.junit.Change.JavaTestClassChange;
import org.springframework.modulith.junit.Change.OtherFileChange;
import org.springframework.modulith.junit.diff.FileModificationDetector;
import org.springframework.modulith.junit.diff.ModifiedFilePath;
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;
}
}
Loading

0 comments on commit a3cafeb

Please sign in to comment.