From e39cfda923b6edce0fb80211cc927a7534abba10 Mon Sep 17 00:00:00 2001 From: Chris Jaekl Date: Thu, 31 Dec 2015 16:11:53 +0900 Subject: [PATCH] Further unit tests: Util and Delta. --- test/net/jaekl/cfb/analyze/DeltaTest.java | 211 ++++++++++++++++++++++ test/net/jaekl/cfb/util/UtilTest.java | 55 ++++++ 2 files changed, 266 insertions(+) create mode 100644 test/net/jaekl/cfb/analyze/DeltaTest.java diff --git a/test/net/jaekl/cfb/analyze/DeltaTest.java b/test/net/jaekl/cfb/analyze/DeltaTest.java new file mode 100644 index 0000000..04f1dcd --- /dev/null +++ b/test/net/jaekl/cfb/analyze/DeltaTest.java @@ -0,0 +1,211 @@ +package net.jaekl.cfb.analyze; + +import static org.junit.Assert.*; + +import java.io.ByteArrayInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.HashSet; + +import net.jaekl.cfb.util.Command; +import net.jaekl.cfb.xml.BugInstance; + +import org.junit.Test; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +public class DeltaTest { + private static final String DM_DEFAULT_ENCODING = "" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n"; + + private static final String RCN_REDUNDANT_NULL_CHECK = "" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n"; + + /* + private static String VO_VOLATILE_INCREMENT = "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + ""; + */ + + private static final String DM_NUMBER_CTOR_156 = "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + ""; + + private static final String DM_NUMBER_CTOR_169 = "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + ""; + + private static final String PROLOGUE = "" + + "\n" + + "\n" + + "\n" + + "/home/chris/prog/././cfb/bin\n" + + "/home/chris/prog/././cfb/src\n" + + "\n"; + + private static final String EPILOGUE = "" + + "\n"; + + private Analysis analysisFromXml(String xml, String projectName, String version) + throws FileNotFoundException, IOException, SAXException + { + Analysis analysis = new Analysis(projectName, version); + ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes(Command.UTF_8)); + InputSource inputSource = new InputSource(bais); + analysis.parse(inputSource); + + return analysis; + } + + private HashSet buildSet(String[][] bugSpecs, int from, int to) + { + HashSet set = new HashSet(); + for (int i = from; i <= to; ++i) { + set.add(bugSpecs[i][1]); + } + + return set; + } + + private String buildXml(String[][] bugSpecs, int from, int to) + { + StringBuilder sb = new StringBuilder(PROLOGUE); + for (int i = from; i <= to; ++i) { + sb.append(bugSpecs[i][0]); + } + sb.append(EPILOGUE); + + return sb.toString(); + } + + private boolean contains(BugInstance[] bugs, String bugName) + { + for (BugInstance bug : bugs) { + if (bugName.equals(bug.getType())) { + return true; + } + } + return false; + } + + private void execDeltaForRanges (String[][] bugSpecs, int w, int x, int y, int z) throws FileNotFoundException, IOException, SAXException + { + final String PROJECT_NAME = "ProjectName"; + String firstXml = buildXml(bugSpecs, w, x); + String secondXml = buildXml(bugSpecs, y, z); + HashSet firstSet = buildSet(bugSpecs, w, x); + HashSet secondSet = buildSet(bugSpecs, y, z); + + Analysis first = analysisFromXml(firstXml, PROJECT_NAME, "1.0.1"); + Analysis second = analysisFromXml(secondXml, PROJECT_NAME, "1.0.2"); + + assertNotNull(first); + assertNotNull(second); + + Delta delta = new Delta(first, second); + + for (int i = 0; i < bugSpecs.length; ++i) { + String bugName = bugSpecs[i][1]; + if (firstSet.contains(bugName) && secondSet.contains(bugName)) { + assertTrue(contains(delta.getCommon(), bugName)); + assertFalse(contains(delta.getFixed(), bugName)); + assertFalse(contains(delta.getNew(), bugName)); + } + else if (firstSet.contains(bugName) && !secondSet.contains(bugName)) { + assertFalse(contains(delta.getCommon(), bugName)); + assertTrue(contains(delta.getFixed(), bugName)); + assertFalse(contains(delta.getNew(), bugName)); + } + else if (!firstSet.contains(bugName) && secondSet.contains(bugName)) { + assertFalse(contains(delta.getCommon(), bugName)); + assertFalse(contains(delta.getFixed(), bugName)); + assertTrue(contains(delta.getNew(), bugName)); + } + else if (!firstSet.contains(bugName) && !secondSet.contains(bugName)) { + assertFalse(contains(delta.getCommon(), bugName)); + assertFalse(contains(delta.getFixed(), bugName)); + assertFalse(contains(delta.getNew(), bugName)); + } + } + } + + @Test + public void test_computeDeltas() throws FileNotFoundException, IOException, SAXException { + String[][] bugSpecs = { + { DM_DEFAULT_ENCODING, "DM_DEFAULT_ENCODING" }, + { RCN_REDUNDANT_NULL_CHECK, "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE" }, + { DM_NUMBER_CTOR_156, "DM_NUMBER_CTOR" }, + { DM_NUMBER_CTOR_169, "DM_NUMBER_CTOR" } + }; + + for (int w = 0; w < bugSpecs.length; ++w) { + for (int x = w + 1; x < bugSpecs.length; ++x) { + for (int y = 0; y < bugSpecs.length; ++y) { + for (int z = y + 1; z < bugSpecs.length; ++z) { + execDeltaForRanges(bugSpecs, w, x, y, z); + } + } + } + } + } + +} diff --git a/test/net/jaekl/cfb/util/UtilTest.java b/test/net/jaekl/cfb/util/UtilTest.java index f9a609a..0f2e299 100644 --- a/test/net/jaekl/cfb/util/UtilTest.java +++ b/test/net/jaekl/cfb/util/UtilTest.java @@ -2,6 +2,7 @@ package net.jaekl.cfb.util; import static org.junit.Assert.*; +import java.sql.SQLException; import java.util.Arrays; import java.util.List; @@ -16,6 +17,10 @@ public class UtilTest { null, null }, + { + {}, + {}, + }, { { "one", "two", "three" }, { "one", "two", "three" } @@ -34,6 +39,10 @@ public class UtilTest { null, { "one", "two", "three" } }, + { + { Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3) }, + null + }, { { "1", "1", "2" }, { "1", "2", "1" } @@ -49,6 +58,18 @@ public class UtilTest { { { "1", "2", "3" }, { Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3) } + }, + { + {}, + { "1", "2", "3"} + }, + { + { "1", "2", "3"}, + {} + }, + { + { "alpha", "beta" }, + { "a", "b" } } }; @@ -66,4 +87,38 @@ public class UtilTest { assertFalse(result); } } + + @Test + public void testObjHashCode() + { + Object[] data = { + Integer.valueOf(12), + "arma virumque cano, Troiae qui primus ab oris", + new String[] { null, null, null }, + new Util() + }; + + int code = 0; + + code = Util.objHashCode(null); + assertEquals(1, code); + + for (Object datum : data) { + code = Util.objHashCode(datum); + assertEquals(datum.hashCode(), code); + } + } + + @Test + public void testStringify() + { + final String msg = "Foo bar baz bat bam"; + SQLException exc = new SQLException(msg); + String actual = Util.stringify(exc); + + assertNotNull(actual); + assertTrue(actual.contains(msg)); + assertTrue(actual.contains("SQLException")); + assertTrue(actual.contains("testStringify")); + } } -- 2.30.2