1 // Copyright (c) 2008 Fabien DUMINY (fduminy@jnode.org) |
|
2 |
|
3 // This file is part of Mauve. |
|
4 |
|
5 // Mauve is free software; you can redistribute it and/or modify |
|
6 // it under the terms of the GNU General Public License as published by |
|
7 // the Free Software Foundation; either version 2, or (at your option) |
|
8 // any later version. |
|
9 |
|
10 // Mauve is distributed in the hope that it will be useful, |
|
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 // GNU General Public License for more details. |
|
14 |
|
15 // You should have received a copy of the GNU General Public License |
|
16 // along with Mauve; see the file COPYING. If not, write to |
|
17 // the Free Software Foundation, 59 Temple Place - Suite 330, |
|
18 // Boston, MA 02111-1307, USA. */ |
|
19 |
|
20 package gnu.testlet.runner.compare; |
|
21 |
|
22 import gnu.testlet.runner.CheckResult; |
|
23 import gnu.testlet.runner.ClassResult; |
|
24 import gnu.testlet.runner.PackageResult; |
|
25 import gnu.testlet.runner.Result; |
|
26 import gnu.testlet.runner.RunResult; |
|
27 import gnu.testlet.runner.TestResult; |
|
28 import gnu.testlet.runner.XMLReportParser; |
|
29 |
|
30 import java.io.File; |
|
31 import java.io.IOException; |
|
32 import java.util.ArrayList; |
|
33 import java.util.Arrays; |
|
34 import java.util.Iterator; |
|
35 import java.util.List; |
|
36 |
|
37 import net.sourceforge.nanoxml.XMLParseException; |
|
38 |
|
39 /** |
|
40 * Comparator of 2 {@link RunResult}s |
|
41 * |
|
42 * @author fabien |
|
43 * |
|
44 */ |
|
45 public class ReportComparator { |
|
46 |
|
47 private final RunResult result1; |
|
48 private final RunResult result2; |
|
49 |
|
50 public static void main(String[] args) throws XMLParseException, IOException { |
|
51 if (args.length < 3) { |
|
52 System.out.println("usage : java " + ReportComparator.class.getName() + |
|
53 " <report1.xml> <report2.xml> [html|text]"); |
|
54 } else { |
|
55 File report1 = new File(args[0]); |
|
56 File report2 = new File(args[1]); |
|
57 String format = args[2]; |
|
58 compare(report1, report2, format); |
|
59 } |
|
60 } |
|
61 |
|
62 public static File compare(File report1, File report2, String format) throws XMLParseException, IOException { |
|
63 XMLReportParser parser = new XMLReportParser(); |
|
64 RunResult result1 = parser.parse(report1); |
|
65 RunResult result2 = parser.parse(report2); |
|
66 |
|
67 ReportComparator comparator = new ReportComparator(result1, result2); |
|
68 RunComparison comparison = comparator.compare(); |
|
69 |
|
70 final ComparisonWriter writer; |
|
71 final String extension; |
|
72 if ("text".equals(format)) { |
|
73 writer = new TextComparisonWriter(); |
|
74 extension = "txt"; |
|
75 } else if ("html".equals(format)) { |
|
76 writer = new HTMLComparisonWriter(); |
|
77 extension = "html"; |
|
78 } else { |
|
79 extension = "txt"; |
|
80 writer = new TextComparisonWriter(); |
|
81 } |
|
82 |
|
83 int i = 0; |
|
84 File output = new File(report2.getParent(), "comp_" + i + "." + extension); |
|
85 while (output.exists()) { |
|
86 i++; |
|
87 output = new File(report2.getParent(), "comp_" + i + "." + extension); |
|
88 } |
|
89 writer.write(comparison, output); |
|
90 System.out.println("Comparison wrote to " + output.getAbsolutePath()); |
|
91 |
|
92 return output; |
|
93 } |
|
94 |
|
95 public ReportComparator(RunResult result1, RunResult result2) { |
|
96 this.result1 = result1; |
|
97 this.result2 = result2; |
|
98 } |
|
99 |
|
100 /** |
|
101 * TODO handle case of added/removed package/class/test/check results ? |
|
102 * |
|
103 * @return |
|
104 */ |
|
105 public RunComparison compare() { |
|
106 RunComparison cr = new RunComparison(result1, result2); |
|
107 |
|
108 addSystemProperties(cr); |
|
109 |
|
110 for (Iterator itPackage1 = result1.getPackageIterator(); itPackage1.hasNext(); ) { |
|
111 PackageResult pkg1 = (PackageResult) itPackage1.next(); |
|
112 PackageResult pkg2 = (PackageResult) getResult(pkg1, result2.getPackageIterator()); |
|
113 |
|
114 if (pkg2 == null) { |
|
115 continue; |
|
116 } |
|
117 |
|
118 for (Iterator itClass1 = pkg1.getClassIterator(); itClass1.hasNext(); ) { |
|
119 ClassResult cls1 = (ClassResult) itClass1.next(); |
|
120 ClassResult cls2 = (ClassResult) getResult(cls1, pkg2.getClassIterator()); |
|
121 |
|
122 if (cls2 == null) { |
|
123 continue; |
|
124 } |
|
125 |
|
126 for (Iterator itTest1 = cls1.getTestIterator(); itTest1.hasNext(); ) { |
|
127 TestResult test1 = (TestResult) itTest1.next(); |
|
128 TestResult test2 = (TestResult) getResult(test1, cls2.getTestIterator()); |
|
129 |
|
130 compare(test1, pkg2, cls2, test2, cr); |
|
131 } |
|
132 } |
|
133 } |
|
134 |
|
135 return cr; |
|
136 } |
|
137 |
|
138 private void addSystemProperties(RunComparison runComparison) { |
|
139 List names1 = Arrays.asList(result1.getSystemPropertyNames()); |
|
140 for (int i = 0; i < names1.size(); i++) { |
|
141 String name = (String) names1.get(i); |
|
142 runComparison.addSystemProperty(name, result1.getSystemProperty(name), |
|
143 result2.getSystemProperty(name)); |
|
144 } |
|
145 |
|
146 String[] names2 = result2.getSystemPropertyNames(); |
|
147 for (int i = 0; i < names2.length; i++) { |
|
148 String name = names2[i]; |
|
149 if (!names1.contains(name)) { |
|
150 runComparison.addSystemProperty(name, result1.getSystemProperty(name), |
|
151 result2.getSystemProperty(name)); |
|
152 } |
|
153 } |
|
154 } |
|
155 |
|
156 private void compare(TestResult test1, PackageResult pkg2, ClassResult cls2, TestResult test2, |
|
157 RunComparison cr) { |
|
158 if ((test2 == null) || (test1.getCheckCount() != test2.getCheckCount())) { |
|
159 return; |
|
160 } |
|
161 |
|
162 List reachedCheckResults1 = getReachedCheckResults(test1); |
|
163 List reachedCheckResults2 = getReachedCheckResults(test2); |
|
164 |
|
165 final int size1 = reachedCheckResults1.size(); |
|
166 final int size2 = reachedCheckResults2.size(); |
|
167 |
|
168 CheckResult check2 = null; |
|
169 if (!reachedCheckResults2.isEmpty()) { |
|
170 check2 = (CheckResult) reachedCheckResults2.get(reachedCheckResults2.size() - 1); |
|
171 } |
|
172 |
|
173 cr.setProgression(pkg2, cls2, test2, check2, size2 - size1); |
|
174 } |
|
175 |
|
176 private List getReachedCheckResults(TestResult test) { |
|
177 List checkResults = new ArrayList(); |
|
178 |
|
179 for (Iterator itCheck = test.getCheckIterator(); itCheck.hasNext(); ) { |
|
180 CheckResult check = (CheckResult) itCheck.next(); |
|
181 if (!check.getPassed()) { |
|
182 break; |
|
183 } |
|
184 |
|
185 checkResults.add(check); |
|
186 } |
|
187 |
|
188 return checkResults; |
|
189 } |
|
190 |
|
191 private Result getResult(Result result1, Iterator results2) { |
|
192 final String name1 = result1.getName(); |
|
193 Result result2 = null; |
|
194 |
|
195 while (results2.hasNext()) { |
|
196 Result res2 = (Result) results2.next(); |
|
197 if (name1.equals(res2.getName())) { |
|
198 result2 = res2; |
|
199 break; |
|
200 } |
|
201 } |
|
202 |
|
203 return result2; |
|
204 } |
|
205 } |
|