more fixes
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
package click.kamil.springstatemachineexporter.analysis.service;
|
||||
|
||||
import click.kamil.springstatemachineexporter.analysis.model.CallChain;
|
||||
import click.kamil.springstatemachineexporter.analysis.model.EntryPoint;
|
||||
import click.kamil.springstatemachineexporter.analysis.model.TriggerPoint;
|
||||
import click.kamil.springstatemachineexporter.analysis.enricher.routing.HeuristicBeanResolutionEngine;
|
||||
import click.kamil.springstatemachineexporter.ast.common.CodebaseContext;
|
||||
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.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class EnterpriseBugsTest {
|
||||
|
||||
@Test
|
||||
void shouldResolveStaticImportAndAvoidMismatchedConstructorEnumArgs(@TempDir Path tempDir) throws IOException {
|
||||
String source = """
|
||||
package com.example;
|
||||
import static com.example.EventUtils.assertMatchingCancelEventType;
|
||||
|
||||
public class OrderController {
|
||||
private OrderService service;
|
||||
public void handleCancel(Cancellation.Sender sender) {
|
||||
service.process(new OrderCancelledEvent(sender).getType());
|
||||
}
|
||||
}
|
||||
|
||||
class OrderService {
|
||||
public void process(CommonOrderEvent event) {}
|
||||
}
|
||||
|
||||
class OrderCancelledEvent {
|
||||
private CommonOrderEvent type;
|
||||
public OrderCancelledEvent(Cancellation.Sender sender) {
|
||||
this.type = assertMatchingCancelEventType(sender);
|
||||
}
|
||||
public CommonOrderEvent getType() { return type; }
|
||||
}
|
||||
|
||||
class EventUtils {
|
||||
public static CommonOrderEvent assertMatchingCancelEventType(Cancellation.Sender sender) {
|
||||
return switch(sender) {
|
||||
case registry_corp -> CommonOrderEvent.CANCELLED_BY_registry_corp;
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class Cancellation {
|
||||
public enum Sender { registry_corp }
|
||||
}
|
||||
|
||||
enum CommonOrderEvent { CANCELLED_BY_registry_corp }
|
||||
""";
|
||||
|
||||
Files.writeString(tempDir.resolve("OrderConfig.java"), source);
|
||||
CodebaseContext context = new CodebaseContext();
|
||||
context.scan(tempDir);
|
||||
|
||||
HeuristicCallGraphEngine engine = new HeuristicCallGraphEngine(context);
|
||||
|
||||
EntryPoint entryPoint = EntryPoint.builder()
|
||||
.className("com.example.OrderController")
|
||||
.methodName("handleCancel")
|
||||
.build();
|
||||
TriggerPoint trigger = TriggerPoint.builder()
|
||||
.className("com.example.OrderService")
|
||||
.methodName("process")
|
||||
.event("event")
|
||||
.build();
|
||||
|
||||
List<CallChain> chains = engine.findChains(List.of(entryPoint), List.of(trigger));
|
||||
|
||||
assertThat(chains).hasSize(1);
|
||||
TriggerPoint resolvedTp = chains.get(0).getTriggerPoint();
|
||||
|
||||
// Assert Bug 3: assertMatchingCancelEventType method is resolved via static import
|
||||
// Assert Bug 2: Cancellation.Sender.registry_corp is NOT extracted as an event argument
|
||||
assertThat(resolvedTp.getPolymorphicEvents())
|
||||
.as("Polymorphic events found: %s", resolvedTp.getPolymorphicEvents())
|
||||
.contains("CommonOrderEvent.CANCELLED_BY_registry_corp")
|
||||
.doesNotContain("registry_corp", "Cancellation.Sender.registry_corp");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAcceptConnectorCallChainUsingTriggerPointDomainMatch() {
|
||||
HeuristicBeanResolutionEngine engine = new HeuristicBeanResolutionEngine();
|
||||
|
||||
CallChain chain = CallChain.builder()
|
||||
.triggerPoint(TriggerPoint.builder()
|
||||
.className("project.shop.OrderService")
|
||||
.build())
|
||||
.methodChain(List.of("project.enterprise.connector.org.OrgOrderControllerImpl.accept"))
|
||||
.build();
|
||||
|
||||
// Assert Bug 4: Connector entry points targeting shop SM trigger point are accepted
|
||||
boolean isMatched = engine.isRoutedToCorrectMachine(chain, "project.shop.OrderStateMachine");
|
||||
assertThat(isMatched).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldResolveClassConstantToSimpleNameWithoutFQNDuplicates(@TempDir Path tempDir) throws IOException {
|
||||
String source = """
|
||||
package com.example;
|
||||
public enum CommonOrderEvent { ACCEPTED_BY_corp }
|
||||
""";
|
||||
Files.writeString(tempDir.resolve("CommonOrderEvent.java"), source);
|
||||
CodebaseContext context = new CodebaseContext();
|
||||
context.scan(tempDir);
|
||||
|
||||
HeuristicCallGraphEngine engine = new HeuristicCallGraphEngine(context);
|
||||
|
||||
List<String> resolved = engine.resolveClassConstantReturns("com.example.CommonOrderEvent.ACCEPTED_BY_corp", context, null);
|
||||
|
||||
// Assert Bug 6: resolves cleanly to simple name (since prefix is a known type)
|
||||
assertThat(resolved).containsExactly("CommonOrderEvent.ACCEPTED_BY_corp");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user