1 package net.jaekl.cfb.analyze;
3 import static org.junit.Assert.*;
5 import java.io.ByteArrayInputStream;
6 import java.io.IOException;
7 import java.nio.charset.Charset;
8 import java.util.HashMap;
10 import net.jaekl.cfb.xml.BugCollection;
11 import net.jaekl.cfb.xml.BugInstance;
12 import net.jaekl.qd.xml.XmlParseException;
14 import org.junit.Test;
15 import org.xml.sax.InputSource;
16 import org.xml.sax.SAXException;
18 public class AnalysisTest {
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 testParseSample1() throws IOException, XmlParseException, SAXException {
57 Charset utf8 = Charset.forName(UTF8);
58 BugInstance inst = null;
60 try ( ByteArrayInputStream bais = new ByteArrayInputStream(SAMPLE1_XML.getBytes(utf8)))
62 InputSource inputSource = new InputSource(bais);
63 Analysis analysis = new Analysis(null);
64 analysis.parse(inputSource);
66 assertNotNull(analysis);
68 BugCollection bugColl = analysis.getBugCollection();
70 assertNotNull(bugColl);
71 assertEquals(2, bugColl.size());
73 HashMap<String, BugInstance> typeMap = new HashMap<String, BugInstance>();
74 for (int idx = 0; idx < bugColl.size(); ++idx) {
75 inst = bugColl.get(idx);
76 typeMap.put(inst.getType(), inst);
79 inst = typeMap.get("DM_DEFAULT_ENCODING");
82 inst = typeMap.get("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE");