Unit testing: confirm that Analyzer can parse some sample XML
[cfb.git] / test / net / jaekl / cfb / analyze / AnalyzerTest.java
index d7e8395cd173f0907030e76ca2671e91aa540243..9691efa9b2196c0df43c418ccd58a0772ae0abd3 100644 (file)
@@ -2,11 +2,55 @@ package net.jaekl.cfb.analyze;
 
 import static org.junit.Assert.*;
 
+import java.io.ByteArrayInputStream;
 import java.io.File;
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.util.HashMap;
+
+import net.jaekl.cfb.xml.BugCollection;
+import net.jaekl.cfb.xml.BugInstance;
+import net.jaekl.qd.xml.XmlParseException;
 
 import org.junit.Test;
+import org.xml.sax.InputSource;
 
 public class AnalyzerTest {
+       private static final String UTF8 = "utf-8";
+       
+       private static final String SAMPLE1_XML = ""
+                       + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+                       + "<BugCollection version=\"3.0.1\" sequence=\"0\" timestamp=\"1440757060000\" analysisTimestamp=\"1440761315847\" release=\"\">\n"
+                       + "<Project projectName=\"CFB\">\n"
+                       + "<Jar>/home/chris/prog/././cfb/bin</Jar>\n"
+                       + "<SrcDir>/home/chris/prog/././cfb/src</SrcDir>\n"
+                       + "</Project>\n"
+                       + "<BugInstance type=\"DM_DEFAULT_ENCODING\" priority=\"1\" rank=\"19\" abbrev=\"Dm\" category=\"I18N\">\n"
+                       + "<Class classname=\"net.jaekl.cfb.CFB\">\n"
+                       + "<SourceLine classname=\"net.jaekl.cfb.CFB\" start=\"32\" end=\"119\" sourcefile=\"CFB.java\" sourcepath=\"net/jaekl/cfb/CFB.java\"/>\n"
+                       + "</Class>\n"
+                       + "<Method classname=\"net.jaekl.cfb.CFB\" name=\"main\" signature=\"([Ljava/lang/String;)V\" isStatic=\"true\">\n"
+                       + "<SourceLine classname=\"net.jaekl.cfb.CFB\" start=\"112\" end=\"119\" startBytecode=\"0\" endBytecode=\"266\" sourcefile=\"CFB.java\" sourcepath=\"net/jaekl/cfb/CFB.java\"/>\n"
+                       + "</Method>\n"
+                       + "<Method classname=\"java.io.PrintWriter\" name=\"&lt;init&gt;\" signature=\"(Ljava/io/OutputStream;)V\" isStatic=\"false\" role=\"METHOD_CALLED\">\n"
+                       + "<SourceLine classname=\"java.io.PrintWriter\" start=\"131\" end=\"132\" startBytecode=\"0\" endBytecode=\"62\" sourcefile=\"PrintWriter.java\" sourcepath=\"java/io/PrintWriter.java\"/>\n"
+                       + "</Method>\n"
+                       + "<SourceLine classname=\"net.jaekl.cfb.CFB\" start=\"114\" end=\"114\" startBytecode=\"22\" endBytecode=\"22\" sourcefile=\"CFB.java\" sourcepath=\"net/jaekl/cfb/CFB.java\"/>\n"
+                       + "</BugInstance>\n"
+                       + "<BugInstance type=\"RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE\" priority=\"2\" rank=\"18\" abbrev=\"RCN\" category=\"STYLE\">\n"
+                       + "<Class classname=\"net.jaekl.cfb.CFB\">\n"
+                       + "<SourceLine classname=\"net.jaekl.cfb.CFB\" start=\"32\" end=\"119\" sourcefile=\"CFB.java\" sourcepath=\"net/jaekl/cfb/CFB.java\"/>\n"
+                       + "</Class>\n"
+                       + "<Method classname=\"net.jaekl.cfb.CFB\" name=\"doMain\" signature=\"(Ljava/io/PrintWriter;[Ljava/lang/String;)V\" isStatic=\"false\">\n"
+                       + "<SourceLine classname=\"net.jaekl.cfb.CFB\" start=\"96\" end=\"109\" startBytecode=\"0\" endBytecode=\"407\" sourcefile=\"CFB.java\" sourcepath=\"net/jaekl/cfb/CFB.java\"/>\n"
+                       + "</Method>\n"
+                       + "<LocalVariable name=\"con\" register=\"5\" pc=\"44\" role=\"LOCAL_VARIABLE_VALUE_OF\"/>\n"
+                       + "<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"
+                       + "<SourceLine classname=\"net.jaekl.cfb.db.driver.DbDriver\" sourcefile=\"DbDriver.java\" sourcepath=\"net/jaekl/cfb/db/driver/DbDriver.java\"/>\n"
+                       + "</Method>\n"
+                       + "<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"
+                       + "</BugInstance>\n"
+                       + "</BugCollection>\n";
 
        @Test
        public void testOutputWorkFile() {
@@ -33,4 +77,35 @@ public class AnalyzerTest {
                }
        }
 
+       @Test
+       public void testParseSample1() throws IOException, XmlParseException {
+               Charset utf8 = Charset.forName(UTF8);
+               BugInstance inst = null;
+               
+               try ( ByteArrayInputStream bais = new ByteArrayInputStream(SAMPLE1_XML.getBytes(utf8)))
+               {
+                       InputSource inputSource = new InputSource(bais); 
+                       Analyzer analyzer = new Analyzer(new File("."));
+                       Analysis analysis = analyzer.parseFbOutput(inputSource);
+                       
+                       assertNotNull(analysis);
+                       
+                       BugCollection bugColl = analysis.getBugCollection();
+                       
+                       assertNotNull(bugColl);
+                       assertEquals(2, bugColl.size());
+                       
+                       HashMap<String, BugInstance> typeMap = new HashMap<String, BugInstance>();
+                       for (int idx = 0; idx < bugColl.size(); ++idx) {
+                               inst = bugColl.get(idx);
+                               typeMap.put(inst.getType(), inst);
+                       }
+                       
+                       inst = typeMap.get("DM_DEFAULT_ENCODING");
+                       assertNotNull(inst);
+                       
+                       inst = typeMap.get("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE");
+                       assertNotNull(inst);
+               }
+       }
 }