Unit testing: confirm that Analyzer can parse some sample XML
authorChris Jaekl <chris@ringo.jaekl.net>
Sun, 6 Sep 2015 12:07:01 +0000 (21:07 +0900)
committerChris Jaekl <chris@ringo.jaekl.net>
Sun, 6 Sep 2015 12:07:01 +0000 (21:07 +0900)
input, and that the result passes some very basic sanity checks.

prod/net/jaekl/cfb/analyze/Analysis.java
prod/net/jaekl/cfb/analyze/Analyzer.java
prod/net/jaekl/cfb/xml/BugCollection.java
prod/net/jaekl/cfb/xml/BugInstance.java
prod/net/jaekl/cfb/xml/SourceLine.java
test/net/jaekl/cfb/analyze/AnalyzerTest.java

index 4e289e2366c4816a679dd0b39019b118b90fb2b7..ab9fffbe798e90b8d23d52a3347008ec931316e3 100644 (file)
@@ -2,22 +2,19 @@ package net.jaekl.cfb.analyze;
 
 // Copyright (C) 2015 Christian Jaekl
 
-import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.PrintWriter;
 
+import net.jaekl.cfb.xml.BugCollection;
+import net.jaekl.qd.xml.ParseErrorHandler;
+import net.jaekl.qd.xml.ParseHandler;
+
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 import org.xml.sax.XMLReader;
 import org.xml.sax.helpers.XMLReaderFactory;
 
-import net.jaekl.cfb.xml.BugCollection;
-import net.jaekl.qd.util.InputStreamWrapper;
-import net.jaekl.qd.xml.ParseErrorHandler;
-import net.jaekl.qd.xml.ParseHandler;
-
 public class Analysis {
        BugCollection m_bugCollection;
        
@@ -27,19 +24,16 @@ public class Analysis {
        
        public BugCollection getBugCollection() { return m_bugCollection; }
        
-       public void parse(File xml) throws FileNotFoundException, IOException, SAXException 
+       public void parse(InputSource xml) throws FileNotFoundException, IOException, SAXException 
        {
                m_bugCollection = new BugCollection();
                
-               try (InputStreamWrapper isw = new InputStreamWrapper(new FileInputStream(xml))) 
-               {
-                   XMLReader reader = XMLReaderFactory.createXMLReader();
-                   ParseHandler ph = new ParseHandler(m_bugCollection);
-                   ParseErrorHandler peh = new ParseErrorHandler();
-                   reader.setContentHandler(ph);
-                   reader.setErrorHandler(peh);
-                   reader.parse(new InputSource(isw));
-               }
+           XMLReader reader = XMLReaderFactory.createXMLReader();
+           ParseHandler ph = new ParseHandler(m_bugCollection);
+           ParseErrorHandler peh = new ParseErrorHandler();
+           reader.setContentHandler(ph);
+           reader.setErrorHandler(peh);
+           reader.parse(xml);
        }
        
        public void dump(PrintWriter pw)
index 9a8d7a64c4ea6f7a4069dfb3b396e1ddd65f382f..5e72fe0499db0b7d2f50c3b67ec09f13eb8b7da8 100644 (file)
@@ -9,6 +9,7 @@ import java.text.MessageFormat;
 import java.util.Locale;
 import java.util.Locale.Category;
 
+import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 
 import net.jaekl.cfb.CfbBundle;
@@ -44,7 +45,7 @@ public class Analyzer {
                        return null;
                }
                
-               result = parseFbOutput(fbOutput);
+               result = parseFbOutput(new InputSource(fbOutput.getAbsolutePath()));
                result.dump(pw);                        
                return result;
        }
@@ -97,7 +98,7 @@ public class Analyzer {
        
        // Parse the output.xml that resulted from a FindBugs run,
        // and store its findings into an Analysis object.
-       Analysis parseFbOutput(File fbOutput) throws XmlParseException 
+       Analysis parseFbOutput(InputSource fbOutput) throws XmlParseException 
        {
                Analysis result = new Analysis();
                try {
index 8fb78e411ab973b770e30a618d26e82f52ad97ad..d906ff983f9bb5b50c98d8b0c9ea512567b3c8e3 100644 (file)
@@ -19,6 +19,9 @@ public class BugCollection extends ParseResult {
                m_bugs = new ArrayList<BugInstance>();
        }
        
+       public int size() { return m_bugs.size(); }
+       public BugInstance get(int idx) { return m_bugs.get(idx); }
+       
        @Override
        public void endContents(String uri, String localName, String qName,     String chars) 
                throws XmlParseException 
index 900cd1cfd37c1f9fa6fa5643b624dbf635d5fb38..4764c1ca5cef826a83bf0e6bf411b891370478c0 100644 (file)
@@ -35,6 +35,8 @@ public class BugInstance extends ParseResult {
                m_lines = new ArrayList<SourceLine>();
        }
        
+       public String getType() { return m_type; }
+       
        @Override
        public void endContents(String uri, String localName, String qName, String chars) 
                throws XmlParseException 
index c719529e2d9231c1a396253174571c932767ca0b..085c3fbf46765d3d7a713139d18b4403ffcaa91f 100644 (file)
@@ -34,10 +34,10 @@ public class SourceLine extends ParseResult {
                
                m_className = getRequiredAttr(TAG, attr, ATTR_CLASS_NAME);
                
-               scratch = getRequiredAttr(TAG, attr, ATTR_START);
+               scratch = getOptionalAttr(attr, ATTR_START, "-1");
                m_start = Integer.parseInt(scratch);
                
-               scratch = getRequiredAttr(TAG, attr, ATTR_END);
+               scratch = getOptionalAttr(attr, ATTR_END, "-1");
                m_end = Integer.parseInt(scratch);
        }
        
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);
+               }
+       }
 }