heuristics classpath
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
package click.kamil.springstatemachineexporter.analysis.service;
|
||||
|
||||
import org.junit.jupiter.api.Tag;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@Tag("dynamic_classpath_resolver")
|
||||
class DynamicClasspathResolverTest {
|
||||
|
||||
@Test
|
||||
void shouldResolveMavenClasspathDynamically(@TempDir Path tempDir) throws IOException {
|
||||
// Create dummy pom.xml
|
||||
Files.createFile(tempDir.resolve("pom.xml"));
|
||||
|
||||
// Mock the resolver so it doesn't actually run maven, but writes the expected output file
|
||||
DynamicClasspathResolver resolver = new DynamicClasspathResolver() {
|
||||
@Override
|
||||
protected void runProcess(ProcessBuilder pb) throws IOException {
|
||||
// Pretend maven executed and generated the cp.txt
|
||||
String cp = "/fake/m2/repo/spring-core.jar" + File.pathSeparator + "/fake/m2/repo/spring-context.jar";
|
||||
|
||||
String outputFileArg = pb.command().stream()
|
||||
.filter(arg -> arg.startsWith("-Dmdep.outputFile="))
|
||||
.findFirst()
|
||||
.orElseThrow();
|
||||
Path outputFile = Path.of(outputFileArg.substring("-Dmdep.outputFile=".length()));
|
||||
|
||||
Files.writeString(outputFile, cp);
|
||||
}
|
||||
};
|
||||
|
||||
List<String> classpath = resolver.resolveClasspath(tempDir);
|
||||
|
||||
assertThat(classpath).containsExactly(
|
||||
"/fake/m2/repo/spring-core.jar",
|
||||
"/fake/m2/repo/spring-context.jar"
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldResolveGradleClasspathDynamically(@TempDir Path tempDir) throws IOException {
|
||||
// Create dummy build.gradle
|
||||
Files.createFile(tempDir.resolve("build.gradle"));
|
||||
|
||||
DynamicClasspathResolver resolver = new DynamicClasspathResolver() {
|
||||
@Override
|
||||
protected String runProcessAndGetOutput(ProcessBuilder pb) {
|
||||
// Pretend gradle executed and printed the marker
|
||||
String cp = "/fake/gradle/caches/spring-core.jar" + File.pathSeparator + "/fake/gradle/caches/spring-context.jar";
|
||||
return "Some noisy gradle output\n" +
|
||||
"SME_CLASSPATH_MARKER:" + cp + "\n" +
|
||||
"BUILD SUCCESSFUL\n";
|
||||
}
|
||||
};
|
||||
|
||||
List<String> classpath = resolver.resolveClasspath(tempDir);
|
||||
|
||||
assertThat(classpath).containsExactly(
|
||||
"/fake/gradle/caches/spring-core.jar",
|
||||
"/fake/gradle/caches/spring-context.jar"
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnEmptyListIfNoBuildFileFound(@TempDir Path tempDir) {
|
||||
DynamicClasspathResolver resolver = new DynamicClasspathResolver();
|
||||
List<String> classpath = resolver.resolveClasspath(tempDir);
|
||||
assertThat(classpath).isEmpty();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package click.kamil.springstatemachineexporter.analysis.service;
|
||||
|
||||
import click.kamil.springstatemachineexporter.analysis.model.TriggerPoint;
|
||||
import click.kamil.springstatemachineexporter.analysis.resolver.ConstantResolver;
|
||||
import click.kamil.springstatemachineexporter.ast.common.CodebaseContext;
|
||||
import org.junit.jupiter.api.Tag;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@Tag("control_flow_sibling_avoidance")
|
||||
class GenericEventDetectorControlFlowTest {
|
||||
|
||||
@Test
|
||||
void shouldDetectStateFromDirectIfWrapper(@TempDir Path tempDir) throws IOException {
|
||||
String source = """
|
||||
package com.example;
|
||||
public class OrderService {
|
||||
private StateMachine sm;
|
||||
public void processOrder(OrderState state) {
|
||||
if (state == OrderState.PENDING) {
|
||||
sm.sendEvent(OrderEvent.PAY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class StateMachine {
|
||||
public void sendEvent(OrderEvent e) {}
|
||||
}
|
||||
|
||||
enum OrderState { PENDING }
|
||||
enum OrderEvent { PAY }
|
||||
""";
|
||||
Files.writeString(tempDir.resolve("OrderService.java"), source);
|
||||
|
||||
CodebaseContext context = new CodebaseContext();
|
||||
context.scan(tempDir);
|
||||
|
||||
GenericEventDetector detector = new GenericEventDetector(context, new ConstantResolver(), Collections.emptyList());
|
||||
List<TriggerPoint> triggers = detector.detect((org.eclipse.jdt.core.dom.CompilationUnit) context.getTypeDeclaration("com.example.OrderService").getRoot());
|
||||
|
||||
assertThat(triggers).hasSize(1);
|
||||
TriggerPoint tp = triggers.get(0);
|
||||
assertThat(tp.getEvent()).isEqualTo("OrderEvent.PAY");
|
||||
assertThat(tp.getSourceState()).isEqualTo("PENDING");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDetectStateFromGuardClauseSibling(@TempDir Path tempDir) throws IOException {
|
||||
String source = """
|
||||
package com.example;
|
||||
public class OrderService {
|
||||
private StateMachine sm;
|
||||
public void processOrder(OrderState state) {
|
||||
if (state != OrderState.PENDING) {
|
||||
return;
|
||||
}
|
||||
|
||||
sm.sendEvent(OrderEvent.PAY);
|
||||
}
|
||||
}
|
||||
|
||||
class StateMachine {
|
||||
public void sendEvent(OrderEvent e) {}
|
||||
}
|
||||
|
||||
enum OrderState { PENDING }
|
||||
enum OrderEvent { PAY }
|
||||
""";
|
||||
Files.writeString(tempDir.resolve("OrderService.java"), source);
|
||||
|
||||
CodebaseContext context = new CodebaseContext();
|
||||
context.scan(tempDir);
|
||||
|
||||
GenericEventDetector detector = new GenericEventDetector(context, new ConstantResolver(), Collections.emptyList());
|
||||
List<TriggerPoint> triggers = detector.detect((org.eclipse.jdt.core.dom.CompilationUnit) context.getTypeDeclaration("com.example.OrderService").getRoot());
|
||||
|
||||
assertThat(triggers).hasSize(1);
|
||||
TriggerPoint tp = triggers.get(0);
|
||||
assertThat(tp.getEvent()).isEqualTo("OrderEvent.PAY");
|
||||
assertThat(tp.getSourceState()).isEqualTo("PENDING");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDetectStateFromSwitchCaseSibling(@TempDir Path tempDir) throws IOException {
|
||||
String source = """
|
||||
package com.example;
|
||||
public class OrderService {
|
||||
private StateMachine sm;
|
||||
public void processOrder(OrderState state) {
|
||||
switch (state) {
|
||||
case PENDING:
|
||||
sm.sendEvent(OrderEvent.PAY);
|
||||
break;
|
||||
case PAID:
|
||||
sm.sendEvent(OrderEvent.SHIP);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class StateMachine {
|
||||
public void sendEvent(OrderEvent e) {}
|
||||
}
|
||||
|
||||
enum OrderState { PENDING, PAID }
|
||||
enum OrderEvent { PAY, SHIP }
|
||||
""";
|
||||
Files.writeString(tempDir.resolve("OrderService.java"), source);
|
||||
|
||||
CodebaseContext context = new CodebaseContext();
|
||||
context.scan(tempDir);
|
||||
|
||||
GenericEventDetector detector = new GenericEventDetector(context, new ConstantResolver(), Collections.emptyList());
|
||||
List<TriggerPoint> triggers = detector.detect((org.eclipse.jdt.core.dom.CompilationUnit) context.getTypeDeclaration("com.example.OrderService").getRoot());
|
||||
|
||||
assertThat(triggers).hasSize(2);
|
||||
|
||||
TriggerPoint payTrigger = triggers.stream().filter(t -> t.getEvent().equals("OrderEvent.PAY")).findFirst().get();
|
||||
assertThat(payTrigger.getSourceState()).isEqualTo("PENDING");
|
||||
|
||||
TriggerPoint shipTrigger = triggers.stream().filter(t -> t.getEvent().equals("OrderEvent.SHIP")).findFirst().get();
|
||||
assertThat(shipTrigger.getSourceState()).isEqualTo("PAID");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user