Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.hjug.graphbuilder.visitor;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import org.jgrapht.Graph;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.SimpleDirectedWeightedGraph;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.openrewrite.ExecutionContext;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.java.JavaParser;

public class JavaNewClassVisitorFullTest {

@Test
void visitNewClass() throws IOException {

File srcDirectory = new File("src/test/java/org/hjug/graphbuilder/visitor/testclasses/newClass");

JavaParser javaParser = JavaParser.fromJavaVersion().build();
ExecutionContext ctx = new InMemoryExecutionContext(Throwable::printStackTrace);

Graph<String, DefaultWeightedEdge> classReferencesGraph =
new SimpleDirectedWeightedGraph<>(DefaultWeightedEdge.class);

Graph<String, DefaultWeightedEdge> packageReferencesGraph =
new SimpleDirectedWeightedGraph<>(DefaultWeightedEdge.class);

final JavaVisitor<ExecutionContext> javaVisitor =
new JavaVisitor<>(classReferencesGraph, packageReferencesGraph);
final JavaVariableTypeVisitor<ExecutionContext> javaVariableTypeVisitor =
new JavaVariableTypeVisitor<>(classReferencesGraph, packageReferencesGraph);
final JavaMethodDeclarationVisitor<ExecutionContext> javaMethodDeclarationVisitor =
new JavaMethodDeclarationVisitor<>(classReferencesGraph, packageReferencesGraph);

List<Path> list = Files.walk(Paths.get(srcDirectory.getAbsolutePath())).collect(Collectors.toList());

// Parse sources with all visitors, not only
javaParser.parse(list, Paths.get(srcDirectory.getAbsolutePath()), ctx).forEach(cu -> {
javaVisitor.visit(cu, ctx);
javaVariableTypeVisitor.visit(cu, ctx);
javaMethodDeclarationVisitor.visit(cu, ctx);
});

Graph<String, DefaultWeightedEdge> graph = javaVisitor.getClassReferencesGraph();
Assertions.assertTrue(graph.containsVertex("org.hjug.graphbuilder.visitor.testclasses.newClass.A"));
Assertions.assertTrue(graph.containsVertex("org.hjug.graphbuilder.visitor.testclasses.newClass.B"));
Assertions.assertTrue(graph.containsVertex("org.hjug.graphbuilder.visitor.testclasses.newClass.C"));

// capturing counts of all types
Assertions.assertEquals(
6,
graph.getEdgeWeight(graph.getEdge(
"org.hjug.graphbuilder.visitor.testclasses.newClass.A",
"org.hjug.graphbuilder.visitor.testclasses.newClass.B")));

Assertions.assertEquals(
3,
graph.getEdgeWeight(graph.getEdge(
"org.hjug.graphbuilder.visitor.testclasses.newClass.A",
"org.hjug.graphbuilder.visitor.testclasses.newClass.C")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ void visitNewClass() throws IOException {
Assertions.assertTrue(graph.containsVertex("org.hjug.graphbuilder.visitor.testclasses.newClass.B"));
Assertions.assertTrue(graph.containsVertex("org.hjug.graphbuilder.visitor.testclasses.newClass.C"));

// TODO: Investigate further to confirm correctness
// only looking for what was visited by classDeclarationVisitor and variableTypeVisitor
Assertions.assertEquals(
5,
graph.getEdgeWeight(graph.getEdge(
"org.hjug.graphbuilder.visitor.testclasses.newClass.A",
"org.hjug.graphbuilder.visitor.testclasses.newClass.B")));

Assertions.assertEquals(
3,
graph.getEdgeWeight(graph.getEdge(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package org.hjug.graphbuilder.visitor.testclasses.newClass;

import java.util.ArrayList;
import java.util.List;

public class A {

B newClassMethod() {
new C();
C c = new C();

// var treated like "B", counts as 2
var b = new B(null);
// <> treated like <B>, counts as 2
List<B> listB = new ArrayList<>();

// TODO: add visitor for J.ReturnType
return new B(c);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ public void executeReport(Locale locale) {
testSourceDirectory,
projectName,
projectVersion,
project.getBasedir(),
300)
project.getBasedir())
.toString();

mainSink.rawText(report);
Expand All @@ -95,24 +94,17 @@ private void printHead(Sink mainSink) {
renderJsDeclaration(mainSink, "https://buttons.github.io/buttons.js");
// google chart import
renderJsDeclaration(mainSink, "https://www.gstatic.com/charts/loader.js");
// d3 dot graph imports
renderJsDeclaration(mainSink, "https://d3js.org/d3.v5.min.js");
renderJsDeclaration(mainSink, "https://cdnjs.cloudflare.com/ajax/libs/d3-graphviz/3.0.5/d3-graphviz.min.js");
renderJsDeclaration(mainSink, "https://unpkg.com/@hpcc-js/[email protected]/dist/index.min.js");
// for DOT graph zooming
renderJsDeclaration(mainSink, "https://cdn.jsdelivr.net/npm/[email protected]/dist/svg-pan-zoom.min.js");

// sigma graph imports - sigma, graphology, graphlib, and graphlib-dot
renderJsDeclaration(mainSink, "https://cdnjs.cloudflare.com/ajax/libs/sigma.js/2.4.0/sigma.min.js");
renderJsDeclaration(mainSink, "https://cdnjs.cloudflare.com/ajax/libs/graphology/0.25.4/graphology.umd.min.js");

// may only need graphlib-dot
renderJsDeclaration(mainSink, "https://cdnjs.cloudflare.com/ajax/libs/graphlib/2.1.8/graphlib.min.js");
renderJsDeclaration(mainSink, "https://cdn.jsdelivr.net/npm/[email protected]/dist/graphlib-dot.min.js");
renderJsDeclaration(mainSink, "https://unpkg.com/3d-force-graph");

// renderJsDeclaration(mainSink, HtmlReport.SUGIYAMA_SIGMA_GRAPH);
// renderJsDeclaration(mainSink, HtmlReport.FORCE_3D_GRAPH);
// renderJsDeclaration(mainSink, HtmlReport.POPUP_FUNCTIONS);

// renderStyle(mainSink);
renderJsDeclaration(mainSink, "https://cdn.jsdelivr.net/npm/3d-force-graph");

mainSink.head_();
}
Expand Down
Loading