1 package net.jaekl.cfb.analyze;
3 import static org.junit.Assert.*;
5 import java.io.ByteArrayInputStream;
6 import java.io.FileNotFoundException;
7 import java.io.IOException;
8 import java.util.HashSet;
10 import net.jaekl.cfb.util.Command;
11 import net.jaekl.cfb.xml.BugInstance;
13 import org.junit.Test;
14 import org.xml.sax.InputSource;
15 import org.xml.sax.SAXException;
17 public class DeltaTest {
18 private Analysis analysisFromXml(String xml, String projectName, String version)
19 throws FileNotFoundException, IOException, SAXException
21 Analysis analysis = new Analysis(projectName, version);
22 ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes(Command.UTF_8));
23 InputSource inputSource = new InputSource(bais);
24 analysis.parse(inputSource);
29 private HashSet<String> buildSet(String[][] bugSpecs, int from, int to)
31 HashSet<String> set = new HashSet<String>();
32 for (int i = from; i <= to; ++i) {
33 set.add(bugSpecs[i][1]);
39 private String buildXml(String[][] bugSpecs, int from, int to)
41 StringBuilder sb = new StringBuilder(BugReportData.getPrologue());
42 for (int i = from; i <= to; ++i) {
43 sb.append(bugSpecs[i][0]);
45 sb.append(BugReportData.getEpilogue());
50 private boolean contains(BugInstance[] bugs, String bugName)
52 for (BugInstance bug : bugs) {
53 if (bugName.equals(bug.getType())) {
60 private void execDeltaForRanges (String[][] bugSpecs, int w, int x, int y, int z) throws FileNotFoundException, IOException, SAXException
62 final String PROJECT_NAME = "ProjectName";
63 String firstXml = buildXml(bugSpecs, w, x);
64 String secondXml = buildXml(bugSpecs, y, z);
65 HashSet<String> firstSet = buildSet(bugSpecs, w, x);
66 HashSet<String> secondSet = buildSet(bugSpecs, y, z);
68 Analysis first = analysisFromXml(firstXml, PROJECT_NAME, "1.0.1");
69 Analysis second = analysisFromXml(secondXml, PROJECT_NAME, "1.0.2");
72 assertNotNull(second);
74 Delta delta = new Delta(first, second);
76 for (int i = 0; i < bugSpecs.length; ++i) {
77 String bugName = bugSpecs[i][1];
78 if (firstSet.contains(bugName) && secondSet.contains(bugName)) {
79 assertTrue(contains(delta.getCommon(), bugName));
80 assertFalse(contains(delta.getFixed(), bugName));
81 assertFalse(contains(delta.getNew(), bugName));
83 else if (firstSet.contains(bugName) && !secondSet.contains(bugName)) {
84 assertFalse(contains(delta.getCommon(), bugName));
85 assertTrue(contains(delta.getFixed(), bugName));
86 assertFalse(contains(delta.getNew(), bugName));
88 else if (!firstSet.contains(bugName) && secondSet.contains(bugName)) {
89 assertFalse(contains(delta.getCommon(), bugName));
90 assertFalse(contains(delta.getFixed(), bugName));
91 assertTrue(contains(delta.getNew(), bugName));
93 else if (!firstSet.contains(bugName) && !secondSet.contains(bugName)) {
94 assertFalse(contains(delta.getCommon(), bugName));
95 assertFalse(contains(delta.getFixed(), bugName));
96 assertFalse(contains(delta.getNew(), bugName));
102 public void test_computeDeltas() throws FileNotFoundException, IOException, SAXException
104 String[][] bugSpecs = {
105 { BugReportData.getDmDefaultEncoding(), "DM_DEFAULT_ENCODING" },
106 { BugReportData.getRcnRedundantNullCheck(), "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE" },
107 { BugReportData.getDmNumberCtor169(), "DM_NUMBER_CTOR" }
110 for (int w = 0; w < bugSpecs.length; ++w) {
111 for (int x = w + 1; x < bugSpecs.length; ++x) {
112 for (int y = 0; y < bugSpecs.length; ++y) {
113 for (int z = y + 1; z < bugSpecs.length; ++z) {
114 execDeltaForRanges(bugSpecs, w, x, y, z);
122 public void test_deltaWithNoPrior() throws FileNotFoundException, IOException, SAXException
124 String[][] bugSpecs = {
125 { BugReportData.getDmDefaultEncoding(), "DM_DEFAULT_ENCODING" },
126 { BugReportData.getRcnRedundantNullCheck(), "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE" },
127 { BugReportData.getVoVolatileIncrement(), "VO_VOLATILE_INCREMENT" },
128 { BugReportData.getDmNumberCtor156(), "DM_NUMBER_CTOR" },
129 { BugReportData.getDmNumberCtor169(), "DM_NUMBER_CTOR" }
132 final String PROJECT_NAME = "AlphaOne";
133 String secondXml = buildXml(bugSpecs, 0, bugSpecs.length - 1);
135 Analysis second = analysisFromXml(secondXml, PROJECT_NAME, "1.0.1");
137 Delta delta = new Delta(null, second);
139 assertNotNull(delta);
140 for (int i = 0; i < bugSpecs.length; ++i) {
141 assertFalse(contains(delta.getCommon(), bugSpecs[i][1]));
142 assertFalse(contains(delta.getFixed(), bugSpecs[i][1]));
143 assertTrue(contains(delta.getNew(), bugSpecs[i][1]));