Display LocalVariables along with Locations in the html report.
[cfb.git] / test / net / jaekl / cfb / analyze / DeltaTest.java
1 package net.jaekl.cfb.analyze;
2
3 import static org.junit.Assert.*;
4
5 import java.io.ByteArrayInputStream;
6 import java.io.FileNotFoundException;
7 import java.io.IOException;
8 import java.util.HashSet;
9
10 import net.jaekl.cfb.util.Command;
11 import net.jaekl.cfb.xml.BugInstance;
12
13 import org.junit.Test;
14 import org.xml.sax.InputSource;
15 import org.xml.sax.SAXException;
16
17 public class DeltaTest {
18         private Analysis analysisFromXml(String xml, String projectName, String version) 
19                 throws FileNotFoundException, IOException, SAXException 
20         {
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);
25                 
26                 return analysis;
27         }
28         
29         private HashSet<String> buildSet(String[][] bugSpecs, int from, int to)
30         {
31                 HashSet<String> set = new HashSet<String>();
32                 for (int i = from; i <= to; ++i) {
33                         set.add(bugSpecs[i][1]);
34                 }
35                 
36                 return set;
37         }
38         
39         private String buildXml(String[][] bugSpecs, int from, int to)
40         {
41                 StringBuilder sb = new StringBuilder(BugReportData.getPrologue());
42                 for (int i = from; i <= to; ++i) {
43                         sb.append(bugSpecs[i][0]);
44                 }
45                 sb.append(BugReportData.getEpilogue());
46                 
47                 return sb.toString();           
48         }
49         
50         private boolean contains(BugInstance[] bugs, String bugName)
51         {
52                 for (BugInstance bug : bugs) {
53                         if (bugName.equals(bug.getType())) {
54                                 return true;
55                         }
56                 }
57                 return false;
58         }
59         
60         private void execDeltaForRanges (String[][] bugSpecs, int w, int x, int y, int z) throws FileNotFoundException, IOException, SAXException 
61         {
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);
67                 
68                 Analysis first = analysisFromXml(firstXml, PROJECT_NAME, "1.0.1");
69                 Analysis second = analysisFromXml(secondXml, PROJECT_NAME, "1.0.2");
70                 
71                 assertNotNull(first);
72                 assertNotNull(second);
73                 
74                 Delta delta = new Delta(first, second);
75
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));
82                         }
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));
87                         }
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));
92                         }
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));                         
97                         }
98                 }
99         }
100
101         @Test
102         public void test_computeDeltas() throws FileNotFoundException, IOException, SAXException 
103         {
104                 String[][] bugSpecs = {
105                                 { BugReportData.getDmDefaultEncoding(), "DM_DEFAULT_ENCODING" },
106                                 { BugReportData.getRcnRedundantNullCheck(), "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE" },
107                                 { BugReportData.getDmNumberCtor169(), "DM_NUMBER_CTOR" }
108                 };
109
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);
115                                         }
116                                 }
117                         }
118                 }
119         }
120
121         @Test
122         public void test_deltaWithNoPrior() throws FileNotFoundException, IOException, SAXException 
123         {
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" }
130                 };
131                 
132                 final String PROJECT_NAME = "AlphaOne";
133                 String secondXml = buildXml(bugSpecs, 0, bugSpecs.length - 1);
134                 
135                 Analysis second = analysisFromXml(secondXml, PROJECT_NAME, "1.0.1");
136                 
137                 Delta delta = new Delta(null, second);
138                 
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]));
144                 }
145         }
146 }