|
| 1 | +package org.hjug.graphbuilder.visitor; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.nio.file.Files; |
| 6 | +import java.nio.file.Path; |
| 7 | +import java.nio.file.Paths; |
| 8 | +import java.util.List; |
| 9 | +import java.util.stream.Collectors; |
| 10 | +import org.jgrapht.Graph; |
| 11 | +import org.jgrapht.graph.DefaultWeightedEdge; |
| 12 | +import org.jgrapht.graph.SimpleDirectedWeightedGraph; |
| 13 | +import org.junit.jupiter.api.Assertions; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.openrewrite.ExecutionContext; |
| 16 | +import org.openrewrite.InMemoryExecutionContext; |
| 17 | +import org.openrewrite.java.JavaParser; |
| 18 | + |
| 19 | +public class JavaNewClassVisitorFullTest { |
| 20 | + |
| 21 | + @Test |
| 22 | + void visitNewClass() throws IOException { |
| 23 | + |
| 24 | + File srcDirectory = new File("src/test/java/org/hjug/graphbuilder/visitor/testclasses/newClass"); |
| 25 | + |
| 26 | + JavaParser javaParser = JavaParser.fromJavaVersion().build(); |
| 27 | + ExecutionContext ctx = new InMemoryExecutionContext(Throwable::printStackTrace); |
| 28 | + |
| 29 | + Graph<String, DefaultWeightedEdge> classReferencesGraph = |
| 30 | + new SimpleDirectedWeightedGraph<>(DefaultWeightedEdge.class); |
| 31 | + |
| 32 | + Graph<String, DefaultWeightedEdge> packageReferencesGraph = |
| 33 | + new SimpleDirectedWeightedGraph<>(DefaultWeightedEdge.class); |
| 34 | + |
| 35 | + final JavaVisitor<ExecutionContext> javaVisitor = |
| 36 | + new JavaVisitor<>(classReferencesGraph, packageReferencesGraph); |
| 37 | + final JavaVariableTypeVisitor<ExecutionContext> javaVariableTypeVisitor = |
| 38 | + new JavaVariableTypeVisitor<>(classReferencesGraph, packageReferencesGraph); |
| 39 | + final JavaMethodDeclarationVisitor<ExecutionContext> javaMethodDeclarationVisitor = |
| 40 | + new JavaMethodDeclarationVisitor<>(classReferencesGraph, packageReferencesGraph); |
| 41 | + |
| 42 | + List<Path> list = Files.walk(Paths.get(srcDirectory.getAbsolutePath())).collect(Collectors.toList()); |
| 43 | + |
| 44 | + // Parse sources with all visitors, not only |
| 45 | + javaParser.parse(list, Paths.get(srcDirectory.getAbsolutePath()), ctx).forEach(cu -> { |
| 46 | + javaVisitor.visit(cu, ctx); |
| 47 | + javaVariableTypeVisitor.visit(cu, ctx); |
| 48 | + javaMethodDeclarationVisitor.visit(cu, ctx); |
| 49 | + }); |
| 50 | + |
| 51 | + Graph<String, DefaultWeightedEdge> graph = javaVisitor.getClassReferencesGraph(); |
| 52 | + Assertions.assertTrue(graph.containsVertex("org.hjug.graphbuilder.visitor.testclasses.newClass.A")); |
| 53 | + Assertions.assertTrue(graph.containsVertex("org.hjug.graphbuilder.visitor.testclasses.newClass.B")); |
| 54 | + Assertions.assertTrue(graph.containsVertex("org.hjug.graphbuilder.visitor.testclasses.newClass.C")); |
| 55 | + |
| 56 | + // capturing counts of all types |
| 57 | + Assertions.assertEquals( |
| 58 | + 6, |
| 59 | + graph.getEdgeWeight(graph.getEdge( |
| 60 | + "org.hjug.graphbuilder.visitor.testclasses.newClass.A", |
| 61 | + "org.hjug.graphbuilder.visitor.testclasses.newClass.B"))); |
| 62 | + |
| 63 | + Assertions.assertEquals( |
| 64 | + 3, |
| 65 | + graph.getEdgeWeight(graph.getEdge( |
| 66 | + "org.hjug.graphbuilder.visitor.testclasses.newClass.A", |
| 67 | + "org.hjug.graphbuilder.visitor.testclasses.newClass.C"))); |
| 68 | + } |
| 69 | +} |
0 commit comments