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;
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;
17 import org.xml.sax.SAXException;
19 public class AnalysisTest {
20 private static final String UTF8 = "utf-8";
22 private static final String SAMPLE1_XML = ""
23 + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
24 + "<BugCollection version=\"3.0.1\" sequence=\"0\" timestamp=\"1440757060000\" analysisTimestamp=\"1440761315847\" release=\"\">\n"
25 + "<Project projectName=\"CFB\">\n"
26 + "<Jar>/home/chris/prog/././cfb/bin</Jar>\n"
27 + "<SrcDir>/home/chris/prog/././cfb/src</SrcDir>\n"
29 + "<BugInstance type=\"DM_DEFAULT_ENCODING\" priority=\"1\" rank=\"19\" abbrev=\"Dm\" category=\"I18N\">\n"
30 + "<Class classname=\"net.jaekl.cfb.CFB\">\n"
31 + "<SourceLine classname=\"net.jaekl.cfb.CFB\" start=\"32\" end=\"119\" sourcefile=\"CFB.java\" sourcepath=\"net/jaekl/cfb/CFB.java\"/>\n"
33 + "<Method classname=\"net.jaekl.cfb.CFB\" name=\"main\" signature=\"([Ljava/lang/String;)V\" isStatic=\"true\">\n"
34 + "<SourceLine classname=\"net.jaekl.cfb.CFB\" start=\"112\" end=\"119\" startBytecode=\"0\" endBytecode=\"266\" sourcefile=\"CFB.java\" sourcepath=\"net/jaekl/cfb/CFB.java\"/>\n"
36 + "<Method classname=\"java.io.PrintWriter\" name=\"<init>\" signature=\"(Ljava/io/OutputStream;)V\" isStatic=\"false\" role=\"METHOD_CALLED\">\n"
37 + "<SourceLine classname=\"java.io.PrintWriter\" start=\"131\" end=\"132\" startBytecode=\"0\" endBytecode=\"62\" sourcefile=\"PrintWriter.java\" sourcepath=\"java/io/PrintWriter.java\"/>\n"
39 + "<SourceLine classname=\"net.jaekl.cfb.CFB\" start=\"114\" end=\"114\" startBytecode=\"22\" endBytecode=\"22\" sourcefile=\"CFB.java\" sourcepath=\"net/jaekl/cfb/CFB.java\"/>\n"
41 + "<BugInstance type=\"RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE\" priority=\"2\" rank=\"18\" abbrev=\"RCN\" category=\"STYLE\">\n"
42 + "<Class classname=\"net.jaekl.cfb.CFB\">\n"
43 + "<SourceLine classname=\"net.jaekl.cfb.CFB\" start=\"32\" end=\"119\" sourcefile=\"CFB.java\" sourcepath=\"net/jaekl/cfb/CFB.java\"/>\n"
45 + "<Method classname=\"net.jaekl.cfb.CFB\" name=\"doMain\" signature=\"(Ljava/io/PrintWriter;[Ljava/lang/String;)V\" isStatic=\"false\">\n"
46 + "<SourceLine classname=\"net.jaekl.cfb.CFB\" start=\"96\" end=\"109\" startBytecode=\"0\" endBytecode=\"407\" sourcefile=\"CFB.java\" sourcepath=\"net/jaekl/cfb/CFB.java\"/>\n"
48 + "<LocalVariable name=\"con\" register=\"5\" pc=\"44\" role=\"LOCAL_VARIABLE_VALUE_OF\"/>\n"
49 + "<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"
50 + "<SourceLine classname=\"net.jaekl.cfb.db.driver.DbDriver\" sourcefile=\"DbDriver.java\" sourcepath=\"net/jaekl/cfb/db/driver/DbDriver.java\"/>\n"
52 + "<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"
54 + "</BugCollection>\n";
57 public void testParseSample1() throws IOException, XmlParseException, SAXException {
58 Charset utf8 = Charset.forName(UTF8);
59 BugInstance inst = null;
61 try ( ByteArrayInputStream bais = new ByteArrayInputStream(SAMPLE1_XML.getBytes(utf8)))
63 InputSource inputSource = new InputSource(bais);
64 Analysis analysis = new Analysis(null, null);
65 analysis.parse(inputSource);
67 assertNotNull(analysis);
69 BugCollection bugColl = analysis.getBugCollection();
70 List<BugInstance> bugs = bugColl.getBugs();
72 assertNotNull(bugColl);
73 assertEquals(2, bugs.size());
75 HashMap<String, BugInstance> typeMap = new HashMap<String, BugInstance>();
76 for (BugInstance bug : bugs) {
77 typeMap.put(bug.getType(), bug);
80 inst = typeMap.get("DM_DEFAULT_ENCODING");
83 inst = typeMap.get("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE");