|
| 1 | +package dev.archunitjava.contract; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 8 | + |
| 9 | +import dev.archunitjava.graph.DependencyEvidence; |
| 10 | +import dev.archunitjava.graph.DependencyGraph; |
| 11 | +import dev.archunitjava.graph.DependencyKind; |
| 12 | +import dev.archunitjava.graph.GraphNode; |
| 13 | +import dev.archunitjava.graph.GraphValidationException; |
| 14 | +import dev.archunitjava.graph.LocationId; |
| 15 | +import dev.archunitjava.graph.MemberId; |
| 16 | +import dev.archunitjava.graph.ModuleId; |
| 17 | +import dev.archunitjava.graph.PackageId; |
| 18 | +import dev.archunitjava.graph.TypeId; |
| 19 | +import java.util.List; |
| 20 | +import java.util.function.Function; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +final class GraphKernelContractTest { |
| 24 | + @Test |
| 25 | + void identifiersHaveTypedStableKeysAndRejectAmbiguousText() { |
| 26 | + PackageId namedPackage = PackageId.named("com.example.api"); |
| 27 | + PackageId unnamedPackage = PackageId.unnamed(); |
| 28 | + TypeId type = TypeId.ofBinaryName("com.example.api.Outer$Inner"); |
| 29 | + MemberId member = MemberId.of(type, "call", "(Ljava/lang/String;)V"); |
| 30 | + ModuleId module = ModuleId.named("com.example.module"); |
| 31 | + LocationId location = LocationId.ofResourcePath("classes/com/example/api/Outer$Inner.class"); |
| 32 | + |
| 33 | + assertEquals("com.example.api", namedPackage.qualifiedName()); |
| 34 | + assertTrue(unnamedPackage.isUnnamed()); |
| 35 | + assertEquals("com.example.api.Outer$Inner", type.binaryName()); |
| 36 | + assertEquals(type, member.owner()); |
| 37 | + assertEquals("call", member.name()); |
| 38 | + assertEquals("(Ljava/lang/String;)V", member.descriptor()); |
| 39 | + assertEquals("com.example.module", module.name()); |
| 40 | + assertEquals("classes/com/example/api/Outer$Inner.class", location.resourcePath()); |
| 41 | + assertNotEquals(PackageId.named("same").stableKey(), TypeId.ofBinaryName("same").stableKey()); |
| 42 | + |
| 43 | + rejects(List.of("", " ", ".com", "com.", "com..example", "com/example"), PackageId::named); |
| 44 | + rejects(List.of("", " ", ".Type", "com..Type", "com/Type", "Lcom.Type;", "[Lcom.Type;"), |
| 45 | + TypeId::ofBinaryName); |
| 46 | + rejects(List.of("", " ", ".module", "module.", "com..module", "com/module"), ModuleId::named); |
| 47 | + rejects(List.of("", " ", "/absolute.class", "C:/absolute.class", "../escape.class", |
| 48 | + "classes/../escape.class", "classes\\Type.class"), LocationId::ofResourcePath); |
| 49 | + |
| 50 | + assertThrows(IllegalArgumentException.class, () -> PackageId.named(null)); |
| 51 | + assertThrows(IllegalArgumentException.class, () -> TypeId.ofBinaryName(null)); |
| 52 | + assertThrows(IllegalArgumentException.class, () -> ModuleId.named(null)); |
| 53 | + assertThrows(IllegalArgumentException.class, () -> LocationId.ofResourcePath(null)); |
| 54 | + assertThrows(IllegalArgumentException.class, () -> MemberId.of(type, "", "I")); |
| 55 | + assertThrows(IllegalArgumentException.class, () -> MemberId.of(type, "bad name", "I")); |
| 56 | + assertThrows(IllegalArgumentException.class, () -> MemberId.of(type, "value", "")); |
| 57 | + assertThrows(IllegalArgumentException.class, () -> MemberId.of(type, "value", "bad descriptor")); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void parallelEdgesMergeDuplicateFreeEvidenceAndIterateDeterministically() { |
| 62 | + TypeId api = TypeId.ofBinaryName("com.example.Api"); |
| 63 | + TypeId isolated = TypeId.ofBinaryName("com.example.Isolated"); |
| 64 | + TypeId service = TypeId.ofBinaryName("com.example.Service"); |
| 65 | + MemberId caller = MemberId.of(api, "invoke", "()V"); |
| 66 | + DependencyEvidence later = DependencyEvidence.at( |
| 67 | + LocationId.ofResourcePath("classes/com/example/Z.class"), caller, 21, 8); |
| 68 | + DependencyEvidence earlier = DependencyEvidence.at( |
| 69 | + LocationId.ofResourcePath("classes/com/example/A.class"), caller, 4, 3); |
| 70 | + |
| 71 | + DependencyGraph graph = DependencyGraph.builder() |
| 72 | + .addNode(service) |
| 73 | + .addNode(api) |
| 74 | + .addNode(isolated) |
| 75 | + .addDependency(api, service, DependencyKind.METHOD_CALL, later) |
| 76 | + .addDependency(api, service, DependencyKind.METHOD_CALL, earlier) |
| 77 | + .addDependency(api, service, DependencyKind.METHOD_CALL, earlier) |
| 78 | + .addDependency(api, service, DependencyKind.FIELD_TYPE, later) |
| 79 | + .build(); |
| 80 | + |
| 81 | + assertEquals(List.of(api, isolated, service), graph.nodes().stream().map(GraphNode::id).toList()); |
| 82 | + assertEquals(2, graph.edges().size()); |
| 83 | + var methodCall = graph.edges().stream() |
| 84 | + .filter(edge -> edge.kind() == DependencyKind.METHOD_CALL) |
| 85 | + .findFirst() |
| 86 | + .orElseThrow(); |
| 87 | + assertEquals(api, methodCall.origin()); |
| 88 | + assertEquals(service, methodCall.target()); |
| 89 | + assertEquals(List.of(earlier, later), methodCall.evidence()); |
| 90 | + |
| 91 | + DependencyGraph reverseInsertion = DependencyGraph.builder() |
| 92 | + .addNode(isolated) |
| 93 | + .addNode(api) |
| 94 | + .addNode(service) |
| 95 | + .addDependency(api, service, DependencyKind.FIELD_TYPE, later) |
| 96 | + .addDependency(api, service, DependencyKind.METHOD_CALL, earlier) |
| 97 | + .addDependency(api, service, DependencyKind.METHOD_CALL, later) |
| 98 | + .build(); |
| 99 | + assertEquals(graph.nodes(), reverseInsertion.nodes()); |
| 100 | + assertEquals(graph.edges(), reverseInsertion.edges()); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void builtGraphsAreImmutableSnapshotsAndPreserveIsolatedNodes() { |
| 105 | + TypeId source = TypeId.ofBinaryName("com.example.Source"); |
| 106 | + TypeId target = TypeId.ofBinaryName("com.example.Target"); |
| 107 | + TypeId isolated = TypeId.ofBinaryName("com.example.Isolated"); |
| 108 | + DependencyEvidence evidence = DependencyEvidence.at( |
| 109 | + LocationId.ofResourcePath("classes/com/example/Source.class")); |
| 110 | + DependencyGraph.Builder builder = DependencyGraph.builder() |
| 111 | + .addNode(source) |
| 112 | + .addNode(target) |
| 113 | + .addNode(isolated) |
| 114 | + .addDependency(source, target, DependencyKind.METHOD_CALL, evidence); |
| 115 | + |
| 116 | + DependencyGraph snapshot = builder.build(); |
| 117 | + builder.addNode(TypeId.ofBinaryName("com.example.AddedLater")); |
| 118 | + |
| 119 | + assertEquals(3, snapshot.nodes().size()); |
| 120 | + assertTrue(snapshot.nodes().stream().map(GraphNode::id).anyMatch(isolated::equals)); |
| 121 | + assertFalse(snapshot.nodes().stream().map(GraphNode::id) |
| 122 | + .anyMatch(TypeId.ofBinaryName("com.example.AddedLater")::equals)); |
| 123 | + assertThrows(UnsupportedOperationException.class, () -> snapshot.nodes().clear()); |
| 124 | + assertThrows(UnsupportedOperationException.class, () -> snapshot.edges().clear()); |
| 125 | + assertThrows(UnsupportedOperationException.class, () -> snapshot.edges().getFirst().evidence().clear()); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + void graphConstructionRejectsUnknownEndpoints() { |
| 130 | + TypeId known = TypeId.ofBinaryName("com.example.Known"); |
| 131 | + TypeId missing = TypeId.ofBinaryName("com.example.Missing"); |
| 132 | + DependencyEvidence evidence = DependencyEvidence.at( |
| 133 | + LocationId.ofResourcePath("classes/com/example/Known.class")); |
| 134 | + |
| 135 | + GraphValidationException error = assertThrows( |
| 136 | + GraphValidationException.class, |
| 137 | + () -> DependencyGraph.builder() |
| 138 | + .addNode(known) |
| 139 | + .addDependency(known, missing, DependencyKind.METHOD_CALL, evidence) |
| 140 | + .build()); |
| 141 | + |
| 142 | + assertTrue(error.getMessage().contains(missing.stableKey())); |
| 143 | + } |
| 144 | + |
| 145 | + private static <T> void rejects(List<String> values, Function<String, T> factory) { |
| 146 | + for (String value : values) { |
| 147 | + assertThrows(IllegalArgumentException.class, () -> factory.apply(value), () -> "accepted " + value); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments