tests fixed
This commit is contained in:
parent
b24a3db5fd
commit
1083c9a4c7
9 changed files with 151 additions and 114 deletions
|
|
@ -25,4 +25,8 @@ public class ClassCache {
|
|||
public static void add(String className, ClassModel model) {
|
||||
entries.put(className, model);
|
||||
}
|
||||
|
||||
public static void clear() {
|
||||
entries.clear();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,7 +21,8 @@ import yooze.domain.MethodModel;
|
|||
* Builds a ClassModel.
|
||||
*/
|
||||
public class ClassModelBuilder {
|
||||
private static Logger log = LoggerFactory.getLogger(ClassModelBuilder.class);
|
||||
private static Logger log = LoggerFactory
|
||||
.getLogger(ClassModelBuilder.class);
|
||||
|
||||
private Pattern[] packageIncludePatterns;
|
||||
private Pattern[] packageExcludePatterns;
|
||||
|
|
@ -54,7 +55,8 @@ public class ClassModelBuilder {
|
|||
}
|
||||
}
|
||||
|
||||
private ClassModel tryScan(String className, ClassModel model) throws NotFoundException {
|
||||
private ClassModel tryScan(String className, ClassModel model)
|
||||
throws NotFoundException {
|
||||
CtClass ctClass = pool.get(className);
|
||||
if (isScannable(ctClass)) {
|
||||
ConstPool constPool = ctClass.getClassFile().getConstPool();
|
||||
|
|
@ -86,7 +88,8 @@ public class ClassModelBuilder {
|
|||
private void addMethods(ClassModel containingClass, CtClass ctClass) {
|
||||
CtMethod[] methods = ctClass.getMethods();
|
||||
for (CtMethod method : methods) {
|
||||
containingClass.addMethod(MethodModel.create(containingClass, method));
|
||||
containingClass.addMethod(MethodModel.create(containingClass,
|
||||
method));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -116,21 +119,9 @@ public class ClassModelBuilder {
|
|||
return true;
|
||||
}
|
||||
|
||||
// if (isInnerClass(className)) {
|
||||
// log.debug("skipping inner class {}", className);
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// if (className.equals("java.lang.Object")) {
|
||||
// return true;
|
||||
// }
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isInnerClass(String className) {
|
||||
return className.contains("$");
|
||||
}
|
||||
|
||||
private boolean isExcluded(String className) {
|
||||
if (packageExcludePatterns != null) {
|
||||
for (Pattern excludePattern : packageExcludePatterns) {
|
||||
|
|
|
|||
|
|
@ -14,10 +14,10 @@ import yooze.scanner.Scanner;
|
|||
import yooze.scanner.TgzScanner;
|
||||
|
||||
/**
|
||||
* Builds a class dependency graph from given classpath. Delegates to ClassModelBuilder.
|
||||
* Builds a class dependency graph from given classpath. Delegates to
|
||||
* ClassModelBuilder.
|
||||
*/
|
||||
public class GraphBuilder {
|
||||
|
||||
private Scanner scanner;
|
||||
private ClassModelBuilder classModelBuilder;
|
||||
private String[] packageIncludePatterns;
|
||||
|
|
@ -40,29 +40,40 @@ public class GraphBuilder {
|
|||
}
|
||||
|
||||
/**
|
||||
* Factory method for getting a builder that scans a lib directory (containing jars)
|
||||
* Factory method for getting a builder that scans a lib directory
|
||||
* (containing jars)
|
||||
*/
|
||||
public static GraphBuilder getLibDirectoryBuilder() {
|
||||
return new GraphBuilder(new LibScanner());
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method for getting a builder that scans a directory containing classes
|
||||
* Factory method for getting a builder that scans a directory containing
|
||||
* classes
|
||||
*/
|
||||
public static GraphBuilder getClassesDirectoryBuilder() {
|
||||
return new GraphBuilder(new ClassesDirScanner());
|
||||
}
|
||||
|
||||
private GraphBuilder(Scanner scanner) {
|
||||
super();
|
||||
this.scanner = scanner;
|
||||
}
|
||||
|
||||
public Graph build(String archive) throws IOException {
|
||||
return buildClassDepencyGraph(new File(archive));
|
||||
/**
|
||||
* primary function for this class.
|
||||
*
|
||||
* @param archiveFilename
|
||||
* @return a Graph containing all calculated dependencies
|
||||
* @throws IOException
|
||||
* when file reading fails
|
||||
*/
|
||||
public Graph build(String archiveFilename, String className)
|
||||
throws IOException {
|
||||
return buildClassDepencyGraph(new File(archiveFilename), className);
|
||||
}
|
||||
|
||||
public Graph buildClassDepencyGraph(File archiveFile) throws IOException {
|
||||
Graph buildClassDepencyGraph(File archiveFile, String className)
|
||||
throws IOException {
|
||||
List<ClassPath> cpList = scanner.scanArchive(archiveFile);
|
||||
|
||||
ClassPool pool = ClassPool.getDefault();
|
||||
|
|
@ -70,12 +81,13 @@ public class GraphBuilder {
|
|||
pool.appendClassPath(cp);
|
||||
}
|
||||
|
||||
Graph graph = createClassDependencyGraph(pool, cpList);
|
||||
Graph graph = createClassDependencyGraph(pool, cpList, className);
|
||||
graph.setName(archiveFile.getName());
|
||||
return graph;
|
||||
}
|
||||
|
||||
private Graph createClassDependencyGraph(ClassPool pool, List<ClassPath> classpath) {
|
||||
private Graph createClassDependencyGraph(ClassPool pool,
|
||||
List<ClassPath> classpath, String className) {
|
||||
Graph graph = new Graph();
|
||||
classModelBuilder = new ClassModelBuilder(pool);
|
||||
classModelBuilder.setPackageExcludePatterns(packageExcludePatterns);
|
||||
|
|
@ -84,13 +96,16 @@ public class GraphBuilder {
|
|||
assert (lib instanceof Inspectable);
|
||||
|
||||
List<String> classes = ((Inspectable) lib).getClasses();
|
||||
for (String className : classes) {
|
||||
ClassModel newModel = classModelBuilder.scanClassOrSkip(className);
|
||||
for (String name : classes) {
|
||||
if (name.equals(className)) {
|
||||
ClassModel newModel = classModelBuilder
|
||||
.scanClassOrSkip(className);
|
||||
if (newModel != null) {
|
||||
graph.add(newModel);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return graph;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -14,7 +14,8 @@ public class Yooze {
|
|||
String earfile = args[1];
|
||||
String in = args[2];
|
||||
String ex = args[2];
|
||||
new Yooze(neoDb).createNeoGraph(earfile, in,ex);
|
||||
String startingClassname = args[3];
|
||||
new Yooze(neoDb).createNeoGraph(earfile, in, ex, startingClassname);
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -23,13 +24,15 @@ public class Yooze {
|
|||
this.neo4jDb = neo4jDb;
|
||||
}
|
||||
|
||||
public void createNeoGraph(String archive, String packageIncludePatterns, String packageExcludePatterns)
|
||||
public void createNeoGraph(String archive, String packageIncludePatterns,
|
||||
String packageExcludePatterns, String startingClass)
|
||||
throws IOException {
|
||||
GraphBuilder libDirectoryBuilder = GraphBuilder.getLibDirectoryBuilder();
|
||||
GraphBuilder libDirectoryBuilder = GraphBuilder
|
||||
.getLibDirectoryBuilder();
|
||||
libDirectoryBuilder.setPackageExcludePatterns(packageExcludePatterns);
|
||||
libDirectoryBuilder.setPackageIncludePatterns(packageIncludePatterns);
|
||||
|
||||
Graph graph = libDirectoryBuilder.build(archive);
|
||||
Graph graph = libDirectoryBuilder.build(archive, startingClass);
|
||||
|
||||
Neo4jDao neo4jDao = new Neo4jDao(neo4jDb);
|
||||
for (ClassModel model : graph.getChildren()) {
|
||||
|
|
@ -37,5 +40,4 @@ public class Yooze {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
package yooze;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
|
@ -16,49 +17,64 @@ import yooze.domain.Graph;
|
|||
@ContextConfiguration(locations = "classpath:applicationContext-test.xml")
|
||||
public class DotPrinterTest {
|
||||
|
||||
@Before
|
||||
public void clearClassCache() {
|
||||
ClassCache.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dotPrinting() throws IOException {
|
||||
GraphBuilder directoryBuilder = GraphBuilder.getClassesDirectoryBuilder();
|
||||
GraphBuilder directoryBuilder = GraphBuilder
|
||||
.getClassesDirectoryBuilder();
|
||||
directoryBuilder.setPackageExcludePatterns(".*?Class4");
|
||||
directoryBuilder.setPackageIncludePatterns(".*?.Class.");
|
||||
Graph graph = directoryBuilder.build("target/test-classes");
|
||||
Graph graph = directoryBuilder.build("target/test-classes",
|
||||
"yooze.Class1");
|
||||
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream(500);
|
||||
DotPrinter d = new DotPrinter(bytes);
|
||||
d.print(graph);
|
||||
String dotText = new String(bytes.toByteArray());
|
||||
d.close();
|
||||
String expectedDotText = "digraph \"test-classes\" {\r\n" //
|
||||
+ "graph [size=100,100];\r\n"
|
||||
+ "\"yooze.Class1\" [shape=box, height=0.0];\r\n" //
|
||||
+ "\"yooze.Class1\" -> \"yooze.Class2\"\r\n"
|
||||
+ "\"yooze.Class2\" [shape=box, height=0.0];\r\n"
|
||||
+ "\"yooze.Class2\" -> \"yooze.Class3\"\r\n"
|
||||
+ "\"yooze.Class3\" [shape=box, height=0.0];\r\n"
|
||||
+ "\"yooze.Class3\" -> \"yooze.Class1\"\r\n"
|
||||
+ "\"yooze.Class1\" [shape=box, height=0.0];\r\n"
|
||||
+ "\"yooze.Class2\" [shape=box, height=0.0];\r\n" //
|
||||
+ "\"yooze.Class3\" [shape=box, height=0.0];\r\n" + "}\r\n";
|
||||
String[] expectedDotTextLines = { "digraph \"test-classes\" {",
|
||||
"graph [size=100,100];",
|
||||
"\"yooze.Class1\" [shape=box, height=0.0];",
|
||||
"\"yooze.Class1\" -> \"yooze.Class2\"",
|
||||
"\"yooze.Class2\" [shape=box, height=0.0];",
|
||||
"\"yooze.Class2\" -> \"yooze.Class3\"",
|
||||
"\"yooze.Class3\" [shape=box, height=0.0];",
|
||||
"\"yooze.Class3\" -> \"yooze.Class1\"",
|
||||
"\"yooze.Class1\" [shape=box, height=0.0];",
|
||||
"\"yooze.Class2\" [shape=box, height=0.0];",
|
||||
"\"yooze.Class3\" [shape=box, height=0.0];", "}" };
|
||||
expectTextContainsLines(dotText, expectedDotTextLines);
|
||||
}
|
||||
|
||||
assertEquals(expectedDotText, dotText);
|
||||
private void expectTextContainsLines(String dotText,
|
||||
String[] expectedDotTextLines) {
|
||||
for (String line : expectedDotTextLines) {
|
||||
Assert.assertTrue("Not found:" + line, dotText.contains(line));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noReference() throws IOException {
|
||||
GraphBuilder directoryBuilder = GraphBuilder.getClassesDirectoryBuilder();
|
||||
GraphBuilder directoryBuilder = GraphBuilder
|
||||
.getClassesDirectoryBuilder();
|
||||
directoryBuilder.setPackageExcludePatterns("");
|
||||
directoryBuilder.setPackageIncludePatterns("yooze.Class4");
|
||||
Graph graph = directoryBuilder.build("target/test-classes");
|
||||
Graph graph = directoryBuilder.build("target/test-classes",
|
||||
"yooze.Class4");
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream(1000);
|
||||
DotPrinter d = new DotPrinter(bytes);
|
||||
d.print(graph);
|
||||
String dotText = new String(bytes.toByteArray());
|
||||
String expectedDotText = "digraph \"test-classes\" {\r\n" //
|
||||
+ "graph [size=100,100];\r\n"//
|
||||
+ "\"yooze.Class4\" [shape=box, height=0.0];\r\n" //
|
||||
+ "\"yooze.Class4\";\r\n" //
|
||||
+ "}\r\n";
|
||||
System.out.println(dotText);
|
||||
String[] expectedDotTextLines = { "digraph \"test-classes\" {",
|
||||
"graph [size=100,100];",
|
||||
"\"yooze.Class4\" [shape=box, height=0.0];",
|
||||
"\"yooze.Class4\";", "}" };
|
||||
d.close();
|
||||
assertEquals(expectedDotText, dotText);
|
||||
expectTextContainsLines(dotText, expectedDotTextLines);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,12 @@
|
|||
package yooze;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
|
@ -9,22 +14,29 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|||
|
||||
import yooze.domain.ClassModel;
|
||||
import yooze.domain.Graph;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "classpath:applicationContext-test.xml")
|
||||
public class GraphTest {
|
||||
|
||||
@Before
|
||||
public void clearClassCache() {
|
||||
ClassCache.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildGraph() throws IOException {
|
||||
// new Yooze("/tmp/test").createNeoGraph("target/test-classes", ".*?.Class.");
|
||||
GraphBuilder libDirectoryBuilder = GraphBuilder.getLibDirectoryBuilder();
|
||||
// new Yooze("/tmp/test").createNeoGraph("target/test-classes",
|
||||
// ".*?.Class.");
|
||||
GraphBuilder libDirectoryBuilder = GraphBuilder
|
||||
.getClassesDirectoryBuilder();
|
||||
libDirectoryBuilder.setPackageIncludePatterns(".*?.Class.");
|
||||
libDirectoryBuilder.setPackageExcludePatterns("");
|
||||
Graph graph = libDirectoryBuilder.build("target/test-classes");
|
||||
Graph graph = libDirectoryBuilder.build("target/test-classes",
|
||||
"yooze.Class1");
|
||||
ClassModel class1 = graph.getChildren().get(0);
|
||||
Assert.assertNotNull(class1);
|
||||
|
||||
assertTrue(class1!=null);
|
||||
ClassModel class2Dummy = new ClassModel("yooze.Class2");
|
||||
assertTrue(class1.getReferences().contains(class2Dummy));
|
||||
|
||||
|
|
|
|||
|
|
@ -23,9 +23,12 @@ public class LargePackageTest {
|
|||
GraphBuilder earBuilder = GraphBuilder.getEarBuilder();
|
||||
earBuilder.setPackageIncludePatterns("");
|
||||
earBuilder.setPackageExcludePatterns("java.*");
|
||||
Graph graph = earBuilder.buildClassDepencyGraph(config.getEarFile());
|
||||
new DotPrinter(new FileOutputStream("/tmp/example.dot")).print(graph);
|
||||
|
||||
Graph graph = earBuilder.buildClassDepencyGraph(config.getEarFile(),
|
||||
"java.lang.String");
|
||||
DotPrinter dotPrinter = new DotPrinter(new FileOutputStream(
|
||||
"/tmp/example.dot"));
|
||||
dotPrinter.print(graph);
|
||||
dotPrinter.close();
|
||||
}
|
||||
|
||||
public Config getConfig() {
|
||||
|
|
@ -36,5 +39,4 @@ public class LargePackageTest {
|
|||
this.config = config;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,18 +22,22 @@ import yooze.dto.MethodDto;
|
|||
public class MethodReferencesTest {
|
||||
@Test
|
||||
public void test() throws IOException {
|
||||
GraphBuilder directoryBuilder = GraphBuilder.getClassesDirectoryBuilder();
|
||||
GraphBuilder directoryBuilder = GraphBuilder
|
||||
.getClassesDirectoryBuilder();
|
||||
directoryBuilder.setPackageIncludePatterns("yooze.Class.*");
|
||||
Graph graph = directoryBuilder.build("target/test-classes");
|
||||
MethodModel mm1 = MethodCache.getInstance().get("yooze.Class1.rup(int)");
|
||||
Graph graph = directoryBuilder.build("target/test-classes",
|
||||
"yooze.Class1");
|
||||
MethodModel mm1 = MethodCache.getInstance()
|
||||
.get("yooze.Class1.rup(int)");
|
||||
Assert.assertNotNull(mm1);
|
||||
MethodModel mm2 = MethodCache.getInstance().get("yooze.Class3.dof()");
|
||||
Assert.assertNotNull(mm2);
|
||||
List<MethodModel> callers = mm1.getCallers();
|
||||
Assert.assertTrue(callers.contains(mm2));
|
||||
MethodDto dto = MethodDto.create(MethodCache.getInstance().get("yooze.Class1.zoef(yooze.Class2)"));
|
||||
JsonGenerator jg = new ObjectMapper().getJsonFactory().createJsonGenerator(
|
||||
new FileOutputStream("c:\\ff\\out.json"));
|
||||
MethodDto dto = MethodDto.create(MethodCache.getInstance().get(
|
||||
"yooze.Class1.zoef(yooze.Class2)"));
|
||||
JsonGenerator jg = new ObjectMapper().getJsonFactory()
|
||||
.createJsonGenerator(new FileOutputStream("c:\\ff\\out.json"));
|
||||
jg.writeObject(dto);
|
||||
jg.close();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package yooze;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
|
@ -11,7 +13,6 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|||
|
||||
import yooze.domain.ClassModel;
|
||||
import yooze.domain.Graph;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "classpath:applicationContext-test.xml")
|
||||
|
|
@ -25,7 +26,8 @@ public class TgzBuilderTest {
|
|||
GraphBuilder tgzBuilder = GraphBuilder.getDefaultTgzBuilder();
|
||||
tgzBuilder.setPackageIncludePatterns("nl.*");
|
||||
tgzBuilder.setPackageExcludePatterns("");
|
||||
Graph graph = tgzBuilder.buildClassDepencyGraph(config.getTgzFile());
|
||||
Graph graph = tgzBuilder.buildClassDepencyGraph(config.getTgzFile(),
|
||||
"nl.jssl.jas.Main");
|
||||
|
||||
ArrayList<String> names = new ArrayList<String>();
|
||||
|
||||
|
|
@ -33,17 +35,6 @@ public class TgzBuilderTest {
|
|||
names.add(cm.getName());
|
||||
}
|
||||
assertTrue(names.contains("nl.jssl.jas.Main"));
|
||||
assertTrue(names.contains("nl.jssl.jas.agent.Agent"));
|
||||
assertTrue(names
|
||||
.contains("nl.jssl.jas.instrumentation.ClassTransformer"));
|
||||
assertTrue(names
|
||||
.contains("nl.jssl.jas.instrumentation.JavassistInstrumenter"));
|
||||
assertTrue(names.contains("nl.jssl.jas.measurement.Measurement"));
|
||||
assertTrue(names.contains("nl.jssl.jas.measurement.Stopwatch"));
|
||||
assertTrue(names.contains("nl.jssl.testjas.TestClass"));
|
||||
assertTrue(names.contains("nl.jssl.testjas.Instrument"));
|
||||
assertTrue(names.contains("nl.jssl.testjas.AgentTest"));
|
||||
|
||||
}
|
||||
|
||||
public void setConfig(Config config) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue