Unit testing: confirm that Analyzer can parse some sample XML
[cfb.git] / test / net / jaekl / cfb / analyze / AnalyzerTest.java
1 package net.jaekl.cfb.analyze;
2
3 import static org.junit.Assert.*;
4
5 import java.io.ByteArrayInputStream;
6 import java.io.File;
7 import java.io.IOException;
8 import java.nio.charset.Charset;
9 import java.util.HashMap;
10
11 import net.jaekl.cfb.xml.BugCollection;
12 import net.jaekl.cfb.xml.BugInstance;
13 import net.jaekl.qd.xml.XmlParseException;
14
15 import org.junit.Test;
16 import org.xml.sax.InputSource;
17
18 public class AnalyzerTest {
19         private static final String UTF8 = "utf-8";
20         
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"
27                         + "</Project>\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"
31                         + "</Class>\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"
34                         + "</Method>\n"
35                         + "<Method classname=\"java.io.PrintWriter\" name=\"&lt;init&gt;\" 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"
37                         + "</Method>\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"
39                         + "</BugInstance>\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"
43                         + "</Class>\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"
46                         + "</Method>\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"
50                         + "</Method>\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"
52                         + "</BugInstance>\n"
53                         + "</BugCollection>\n";
54
55         @Test
56         public void testOutputWorkFile() {
57                 final String[][] DATA = {
58                                 // findBugsDir, workDir, FBP,        expectedOutputXml
59                                 { "foo",        "bar",   "baz.fbp",  "bar" + File.separator + "baz.xml" },
60                                 { 
61                                         File.separator + "findbugs-3.01",
62                                         "." + File.separator + "work",
63                                         "project.fbp",
64                                         "." + File.separator + "work" + File.separator + "project.xml"
65                                 }
66                         };
67                 
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]);
73
74                         Analyzer analyzer = new Analyzer(findBugsDir);
75                         File actual = analyzer.outputWorkFile(workDir, fbp);
76                         assertEquals(expected.getAbsolutePath(), actual.getAbsolutePath());
77                 }
78         }
79
80         @Test
81         public void testParseSample1() throws IOException, XmlParseException {
82                 Charset utf8 = Charset.forName(UTF8);
83                 BugInstance inst = null;
84                 
85                 try ( ByteArrayInputStream bais = new ByteArrayInputStream(SAMPLE1_XML.getBytes(utf8)))
86                 {
87                         InputSource inputSource = new InputSource(bais); 
88                         Analyzer analyzer = new Analyzer(new File("."));
89                         Analysis analysis = analyzer.parseFbOutput(inputSource);
90                         
91                         assertNotNull(analysis);
92                         
93                         BugCollection bugColl = analysis.getBugCollection();
94                         
95                         assertNotNull(bugColl);
96                         assertEquals(2, bugColl.size());
97                         
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);
102                         }
103                         
104                         inst = typeMap.get("DM_DEFAULT_ENCODING");
105                         assertNotNull(inst);
106                         
107                         inst = typeMap.get("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE");
108                         assertNotNull(inst);
109                 }
110         }
111 }