1 package net.jaekl.cfb.analyze;
3 import static org.junit.Assert.*;
5 import java.io.ByteArrayInputStream;
7 import java.io.IOException;
8 import java.nio.charset.Charset;
9 import java.util.HashMap;
11 import net.jaekl.cfb.xml.BugCollection;
12 import net.jaekl.cfb.xml.BugInstance;
13 import net.jaekl.qd.xml.XmlParseException;
15 import org.junit.Test;
16 import org.xml.sax.InputSource;
18 public class AnalyzerTest {
19 private static final String UTF8 = "utf-8";
21 private static final String SAMPLE1_XML = ""
22 + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
23 + "<BugCollection version=\"3.0.1\" sequence=\"0\" timestamp=\"1440757060000\" analysisTimestamp=\"1440761315847\" release=\"\">\n"
24 + "<Project projectName=\"CFB\">\n"
25 + "<Jar>/home/chris/prog/././cfb/bin</Jar>\n"
26 + "<SrcDir>/home/chris/prog/././cfb/src</SrcDir>\n"
28 + "<BugInstance type=\"DM_DEFAULT_ENCODING\" priority=\"1\" rank=\"19\" abbrev=\"Dm\" category=\"I18N\">\n"
29 + "<Class classname=\"net.jaekl.cfb.CFB\">\n"
30 + "<SourceLine classname=\"net.jaekl.cfb.CFB\" start=\"32\" end=\"119\" sourcefile=\"CFB.java\" sourcepath=\"net/jaekl/cfb/CFB.java\"/>\n"
32 + "<Method classname=\"net.jaekl.cfb.CFB\" name=\"main\" signature=\"([Ljava/lang/String;)V\" isStatic=\"true\">\n"
33 + "<SourceLine classname=\"net.jaekl.cfb.CFB\" start=\"112\" end=\"119\" startBytecode=\"0\" endBytecode=\"266\" sourcefile=\"CFB.java\" sourcepath=\"net/jaekl/cfb/CFB.java\"/>\n"
35 + "<Method classname=\"java.io.PrintWriter\" name=\"<init>\" signature=\"(Ljava/io/OutputStream;)V\" isStatic=\"false\" role=\"METHOD_CALLED\">\n"
36 + "<SourceLine classname=\"java.io.PrintWriter\" start=\"131\" end=\"132\" startBytecode=\"0\" endBytecode=\"62\" sourcefile=\"PrintWriter.java\" sourcepath=\"java/io/PrintWriter.java\"/>\n"
38 + "<SourceLine classname=\"net.jaekl.cfb.CFB\" start=\"114\" end=\"114\" startBytecode=\"22\" endBytecode=\"22\" sourcefile=\"CFB.java\" sourcepath=\"net/jaekl/cfb/CFB.java\"/>\n"
40 + "<BugInstance type=\"RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE\" priority=\"2\" rank=\"18\" abbrev=\"RCN\" category=\"STYLE\">\n"
41 + "<Class classname=\"net.jaekl.cfb.CFB\">\n"
42 + "<SourceLine classname=\"net.jaekl.cfb.CFB\" start=\"32\" end=\"119\" sourcefile=\"CFB.java\" sourcepath=\"net/jaekl/cfb/CFB.java\"/>\n"
44 + "<Method classname=\"net.jaekl.cfb.CFB\" name=\"doMain\" signature=\"(Ljava/io/PrintWriter;[Ljava/lang/String;)V\" isStatic=\"false\">\n"
45 + "<SourceLine classname=\"net.jaekl.cfb.CFB\" start=\"96\" end=\"109\" startBytecode=\"0\" endBytecode=\"407\" sourcefile=\"CFB.java\" sourcepath=\"net/jaekl/cfb/CFB.java\"/>\n"
47 + "<LocalVariable name=\"con\" register=\"5\" pc=\"44\" role=\"LOCAL_VARIABLE_VALUE_OF\"/>\n"
48 + "<Method classname=\"net.jaekl.cfb.db.driver.DbDriver\" name=\"connect\" signature=\"(Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/sql/Connection;\" isStatic=\"false\" role=\"METHOD_RETURN_VALUE_OF\">\n"
49 + "<SourceLine classname=\"net.jaekl.cfb.db.driver.DbDriver\" sourcefile=\"DbDriver.java\" sourcepath=\"net/jaekl/cfb/db/driver/DbDriver.java\"/>\n"
51 + "<SourceLine classname=\"net.jaekl.cfb.CFB\" start=\"101\" end=\"101\" startBytecode=\"46\" endBytecode=\"46\" sourcefile=\"CFB.java\" sourcepath=\"net/jaekl/cfb/CFB.java\" role=\"SOURCE_REDUNDANT_NULL_CHECK\"/>\n"
53 + "</BugCollection>\n";
56 public void testOutputWorkFile() {
57 final String[][] DATA = {
58 // findBugsDir, workDir, FBP, expectedOutputXml
59 { "foo", "bar", "baz.fbp", "bar" + File.separator + "baz.xml" },
61 File.separator + "findbugs-3.01",
62 "." + File.separator + "work",
64 "." + File.separator + "work" + File.separator + "project.xml"
68 for (String[] datum : DATA) {
69 File findBugsDir = new File(datum[0]);
70 File workDir = new File(datum[1]);
71 File fbp = new File(datum[2]);
72 File expected = new File(datum[3]);
74 Analyzer analyzer = new Analyzer(findBugsDir);
75 File actual = analyzer.outputWorkFile(workDir, fbp);
76 assertEquals(expected.getAbsolutePath(), actual.getAbsolutePath());
81 public void testParseSample1() throws IOException, XmlParseException {
82 Charset utf8 = Charset.forName(UTF8);
83 BugInstance inst = null;
85 try ( ByteArrayInputStream bais = new ByteArrayInputStream(SAMPLE1_XML.getBytes(utf8)))
87 InputSource inputSource = new InputSource(bais);
88 Analyzer analyzer = new Analyzer(new File("."));
89 Analysis analysis = analyzer.parseFbOutput(inputSource);
91 assertNotNull(analysis);
93 BugCollection bugColl = analysis.getBugCollection();
95 assertNotNull(bugColl);
96 assertEquals(2, bugColl.size());
98 HashMap<String, BugInstance> typeMap = new HashMap<String, BugInstance>();
99 for (int idx = 0; idx < bugColl.size(); ++idx) {
100 inst = bugColl.get(idx);
101 typeMap.put(inst.getType(), inst);
104 inst = typeMap.get("DM_DEFAULT_ENCODING");
107 inst = typeMap.get("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE");