Further unit tests: Util and Delta.
[cfb.git] / 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 (file)
index 0000000..04f1dcd
--- /dev/null
@@ -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 = ""
+                       + "<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";
+       
+       private static final String RCN_REDUNDANT_NULL_CHECK = ""
+                       + "<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";
+
+       /*
+       private static String VO_VOLATILE_INCREMENT = ""
+                       + "<BugInstance type=\"VO_VOLATILE_INCREMENT\" priority=\"2\" abbrev=\"VO\" category=\"MT_CORRECTNESS\">"
+                       + "<Class classname=\"junit.extensions.ActiveTestSuite\">"
+                       + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
+                       + "</Class>"
+                       + "<Method classname=\"junit.extensions.ActiveTestSuite\" name=\"runFinished\" signature=\"()V\" isStatic=\"false\">"
+                       + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" start=\"67\" end=\"69\" startBytecode=\"0\" endBytecode=\"64\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
+                       + "</Method>"
+                       + "<Field classname=\"junit.extensions.ActiveTestSuite\" name=\"fActiveTestDeathCount\" signature=\"I\" isStatic=\"false\">"
+                       + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
+                       + "</Field>"
+                       + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" start=\"67\" end=\"67\" startBytecode=\"7\" endBytecode=\"7\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
+                       + "</BugInstance>";
+       */
+       
+       private static final String DM_NUMBER_CTOR_156 = ""
+                       + "<BugInstance type=\"DM_NUMBER_CTOR\" priority=\"2\" abbrev=\"Bx\" category=\"PERFORMANCE\">"
+                       + "<Class classname=\"junit.framework.Assert\">"
+                       + "<SourceLine classname=\"junit.framework.Assert\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
+                       + "</Class>"
+                       + "<Method classname=\"junit.framework.Assert\" name=\"assertEquals\" signature=\"(Ljava/lang/String;BB)V\" isStatic=\"true\">"
+                       + "<SourceLine classname=\"junit.framework.Assert\" start=\"156\" end=\"157\" startBytecode=\"0\" endBytecode=\"86\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
+                       + "</Method>"
+                       + "<Method classname=\"java.lang.Byte\" name=\"&lt;init&gt;\" signature=\"(B)V\" isStatic=\"false\" role=\"METHOD_CALLED\">"
+                       + "<SourceLine classname=\"java.lang.Byte\" start=\"307\" end=\"309\" startBytecode=\"0\" endBytecode=\"41\" sourcefile=\"Byte.java\" sourcepath=\"java/lang/Byte.java\"/>"
+                       + "</Method>"
+                       + "<Method classname=\"java.lang.Byte\" name=\"valueOf\" signature=\"(B)Ljava/lang/Byte;\" isStatic=\"true\" role=\"SHOULD_CALL\">"
+                       + "<SourceLine classname=\"java.lang.Byte\" start=\"87\" end=\"87\" startBytecode=\"0\" endBytecode=\"33\" sourcefile=\"Byte.java\" sourcepath=\"java/lang/Byte.java\"/>"
+                       + "</Method>"
+                       + "<SourceLine classname=\"junit.framework.Assert\" start=\"156\" end=\"156\" startBytecode=\"6\" endBytecode=\"6\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
+                       + "</BugInstance>";
+
+       private static final String DM_NUMBER_CTOR_169 = ""
+                       + "<BugInstance type=\"DM_NUMBER_CTOR\" priority=\"2\" abbrev=\"Bx\" category=\"PERFORMANCE\">"
+                       + "<Class classname=\"junit.framework.Assert\">"
+                       + "<SourceLine classname=\"junit.framework.Assert\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
+                       + "</Class>"
+                       + "<Method classname=\"junit.framework.Assert\" name=\"assertEquals\" signature=\"(Ljava/lang/String;CC)V\" isStatic=\"true\">"
+                       + "<SourceLine classname=\"junit.framework.Assert\" start=\"169\" end=\"170\" startBytecode=\"0\" endBytecode=\"86\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
+                       + "</Method>"
+                       + "<Method classname=\"java.lang.Character\" name=\"&lt;init&gt;\" signature=\"(C)V\" isStatic=\"false\" role=\"METHOD_CALLED\">"
+                       + "<SourceLine classname=\"java.lang.Character\" start=\"2056\" end=\"2058\" startBytecode=\"0\" endBytecode=\"41\" sourcefile=\"Character.java\" sourcepath=\"java/lang/Character.java\"/>"
+                       + "</Method>"
+                       + "<Method classname=\"java.lang.Character\" name=\"valueOf\" signature=\"(C)Ljava/lang/Character;\" isStatic=\"true\" role=\"SHOULD_CALL\">"
+                       + "<SourceLine classname=\"java.lang.Character\" start=\"2085\" end=\"2088\" startBytecode=\"0\" endBytecode=\"52\" sourcefile=\"Character.java\" sourcepath=\"java/lang/Character.java\"/>"
+                       + "</Method>"
+                       + "<SourceLine classname=\"junit.framework.Assert\" start=\"169\" end=\"169\" startBytecode=\"6\" endBytecode=\"6\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
+                       + "</BugInstance>";
+
+       private static final String PROLOGUE = ""
+                       + "<?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";
+       
+       private static final String EPILOGUE = ""
+                       + "</BugCollection>\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<String> buildSet(String[][] bugSpecs, int from, int to)
+       {
+               HashSet<String> set = new HashSet<String>();
+               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<String> firstSet = buildSet(bugSpecs, w, x);
+               HashSet<String> 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);
+                                       }
+                               }
+                       }
+               }
+       }
+
+}