From 88c635ae6a039a873cba2d794c55d726dcdc616d Mon Sep 17 00:00:00 2001 From: Chris Jaekl Date: Sun, 6 Sep 2015 21:07:01 +0900 Subject: [PATCH] Unit testing: confirm that Analyzer can parse some sample XML input, and that the result passes some very basic sanity checks. --- prod/net/jaekl/cfb/analyze/Analysis.java | 28 +++----- prod/net/jaekl/cfb/analyze/Analyzer.java | 5 +- prod/net/jaekl/cfb/xml/BugCollection.java | 3 + prod/net/jaekl/cfb/xml/BugInstance.java | 2 + prod/net/jaekl/cfb/xml/SourceLine.java | 4 +- test/net/jaekl/cfb/analyze/AnalyzerTest.java | 75 ++++++++++++++++++++ 6 files changed, 96 insertions(+), 21 deletions(-) diff --git a/prod/net/jaekl/cfb/analyze/Analysis.java b/prod/net/jaekl/cfb/analyze/Analysis.java index 4e289e2..ab9fffb 100644 --- a/prod/net/jaekl/cfb/analyze/Analysis.java +++ b/prod/net/jaekl/cfb/analyze/Analysis.java @@ -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) diff --git a/prod/net/jaekl/cfb/analyze/Analyzer.java b/prod/net/jaekl/cfb/analyze/Analyzer.java index 9a8d7a6..5e72fe0 100644 --- a/prod/net/jaekl/cfb/analyze/Analyzer.java +++ b/prod/net/jaekl/cfb/analyze/Analyzer.java @@ -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 { diff --git a/prod/net/jaekl/cfb/xml/BugCollection.java b/prod/net/jaekl/cfb/xml/BugCollection.java index 8fb78e4..d906ff9 100644 --- a/prod/net/jaekl/cfb/xml/BugCollection.java +++ b/prod/net/jaekl/cfb/xml/BugCollection.java @@ -19,6 +19,9 @@ public class BugCollection extends ParseResult { m_bugs = new ArrayList(); } + 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 diff --git a/prod/net/jaekl/cfb/xml/BugInstance.java b/prod/net/jaekl/cfb/xml/BugInstance.java index 900cd1c..4764c1c 100644 --- a/prod/net/jaekl/cfb/xml/BugInstance.java +++ b/prod/net/jaekl/cfb/xml/BugInstance.java @@ -35,6 +35,8 @@ public class BugInstance extends ParseResult { m_lines = new ArrayList(); } + public String getType() { return m_type; } + @Override public void endContents(String uri, String localName, String qName, String chars) throws XmlParseException diff --git a/prod/net/jaekl/cfb/xml/SourceLine.java b/prod/net/jaekl/cfb/xml/SourceLine.java index c719529..085c3fb 100644 --- a/prod/net/jaekl/cfb/xml/SourceLine.java +++ b/prod/net/jaekl/cfb/xml/SourceLine.java @@ -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); } diff --git a/test/net/jaekl/cfb/analyze/AnalyzerTest.java b/test/net/jaekl/cfb/analyze/AnalyzerTest.java index d7e8395..9691efa 100644 --- a/test/net/jaekl/cfb/analyze/AnalyzerTest.java +++ b/test/net/jaekl/cfb/analyze/AnalyzerTest.java @@ -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 = "" + + "\n" + + "\n" + + "\n" + + "/home/chris/prog/././cfb/bin\n" + + "/home/chris/prog/././cfb/src\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\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 typeMap = new HashMap(); + 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); + } + } } -- 2.39.2