1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 package net.objectlab.qalab.parser;
40
41 import junit.framework.Test;
42 import junit.framework.TestSuite;
43
44 import net.objectlab.qalab.AbstractTestCase;
45 import net.objectlab.qalab.util.SilentLogger;
46
47 import org.apache.xerces.parsers.DOMParser;
48 import org.w3c.dom.Document;
49 import org.w3c.dom.Element;
50 import org.w3c.dom.NamedNodeMap;
51 import org.w3c.dom.Node;
52 import org.w3c.dom.NodeList;
53 import org.w3c.dom.Text;
54 import org.xml.sax.InputSource;
55 import org.xml.sax.SAXException;
56
57 import javax.xml.parsers.ParserConfigurationException;
58
59 import java.io.File;
60 import java.io.FileReader;
61 import java.io.IOException;
62 import java.io.StringReader;
63 import java.io.StringWriter;
64 import java.io.Writer;
65
66 import java.util.Calendar;
67
68 public class BuildStatMoverHandlerTest extends AbstractTestCase {
69 /**
70 * Create the test case
71 *
72 * @param testName
73 * name of the test case
74 */
75 public BuildStatMoverHandlerTest(String testName) {
76 super(testName);
77 }
78
79 /**
80 * @return the suite of tests being tested
81 */
82 public static Test suite() {
83 return new TestSuite(BuildStatMoverHandlerTest.class);
84 }
85
86 public void testCheckstyleMovers() {
87 File testInput = new File("target/test-classes/net/objectlab/qalab/parser/qastats.xml");
88
89 Writer testOutput = new StringWriter();
90
91 try {
92 final BuildStatMoverHandler handler = new BuildStatMoverHandler(new InputSource(new FileReader(testInput)),
93 "checkstyle", testOutput, false, new SilentLogger(), false);
94
95 Calendar cal = Calendar.getInstance();
96
97 cal.set(2004, 01, 11, 0, 0, 0);
98 handler.setStartTimeWindow(cal.getTime());
99 cal.set(2004, 01, 14, 0, 0, 0);
100 handler.setEndTimeWindow(cal.getTime());
101 handler.process();
102
103 System.out.println(testOutput.toString());
104
105
106 DOMParser dom = new DOMParser();
107
108 dom.parse(new InputSource(new StringReader(testOutput.toString())));
109
110 Document doc = dom.getDocument();
111
112 checkTagValue(doc, "datethreshold", "2004-02-11 00:00:00", 1);
113 checkTagValue(doc, "enddatethreshold", "2004-02-14 00:00:00", 1);
114 checkTagValue(doc, "types", "checkstyle", 1);
115
116
117 NodeList listUp = doc.getElementsByTagName("up");
118
119 assertEquals("size for up", 1, listUp.getLength());
120 assertEquals("number of files in up list", 5, countFileTags(listUp));
121 assertNotNull("List UP presence of net/objectlab/test/MoveUpAndDown.java", checkFilePresent(listUp,
122 toId("net/objectlab/test/MoveUpAndDown.java")));
123 assertNotNull("List UP presence of net/objectlab/test/MoveUpAndUp.java", checkFilePresent(listUp,
124 toId("net/objectlab/test/MoveUpAndUp.java")));
125 assertNotNull("List UP presence of net/objectlab/test/MoveUp.java", checkFilePresent(listUp,
126 toId("net/objectlab/test/MoveUp.java")));
127 assertNotNull("List UP presence of net/objectlab/test/NewUp.java", checkFilePresent(listUp,
128 toId("net/objectlab/test/NewUp.java")));
129 assertNull("List UP presence of net/objectlab/test/MoveDown.java", checkFilePresent(listUp,
130 toId("net/objectlab/test/MoveDown.java")));
131 assertNull("List UP presence of net/objectlab/test/MoveDownAndDown.java", checkFilePresent(listUp,
132 toId("net/objectlab/test/MoveDownAndDown.java")));
133 assertNull("List UP presence of net/objectlab/test/NoMove.java", checkFilePresent(listUp,
134 toId("net/objectlab/test/NoMove.java")));
135 assertNull("List UP presence of BLA.java", checkFilePresent(listUp, "BLA.java"));
136 assertNotNull("List UP presence of net/objectlab/test/MoveUpAndDownToZero.java", checkFilePresent(listUp,
137 toId("net/objectlab/test/MoveUpAndDownToZero.java")));
138
139
140 NodeList listDown = doc.getElementsByTagName("down");
141
142 assertEquals("size for down", 1, listDown.getLength());
143 assertEquals("number of files in down list", 5, countFileTags(listDown));
144 assertNotNull("List Down presence of net/objectlab/test/MoveUpAndDownToZero.java", checkFilePresent(listDown,
145 toId("net/objectlab/test/MoveUpAndDownToZero.java")));
146 checkDiff(listDown, "net/objectlab/test/MoveUpAndDownToZero.java", 1,
147
148
149 new String[] { "2004-02-11 22:00:06" },
150 new String[] { "-" },
151 new String[] { "checkstyle" },
152 new int[] { 2 },
153 new int[] { 0 },
154 new int[] { -2 });
155
156 assertNotNull("List Down presence of net/objectlab/test/MoveUpAndDown.java", checkFilePresent(listDown,
157 toId("net/objectlab/test/MoveUpAndDown.java")));
158 checkDiff(listDown, "net/objectlab/test/MoveUpAndDown.java", 1,
159
160
161 new String[] { "2004-02-12 22:00:06" },
162 new String[] { "2004-02-13 22:00:06" },
163 new String[] { "checkstyle" },
164 new int[] { 3 },
165 new int[] { 1 },
166 new int[] { -2 });
167
168 assertNotNull("List Down presence of net/objectlab/test/MoveDownToZero.java", checkFilePresent(listDown,
169 toId("net/objectlab/test/MoveDownToZero.java")));
170 checkDiff(listDown, "net/objectlab/test/MoveDownToZero.java", 1,
171
172
173 new String[] { "2004-02-11 22:00:06" },
174 new String[] { "-" },
175 new String[] { "checkstyle" },
176 new int[] { 2 },
177 new int[] { 0 },
178 new int[] { -2 });
179
180 assertNotNull("List Down presence of net/objectlab/test/MoveDown.java", checkFilePresent(listDown,
181 toId("net/objectlab/test/MoveDown.java")));
182 checkDiff(listDown, "net/objectlab/test/MoveDown.java", 1,
183
184
185 new String[] { "2004-02-10 22:00:06" },
186
187 new String[] { "2004-02-12 22:00:06" },
188 new String[] { "checkstyle" },
189 new int[] { 2 },
190
191 new int[] { 1 },
192 new int[] { -1 });
193
194 assertNotNull("List Down presence of net/objectlab/test/MoveDownAndDown.java", checkFilePresent(listDown,
195 toId("net/objectlab/test/MoveDownAndDown.java")));
196 checkDiff(listDown, "net/objectlab/test/MoveDownAndDown.java", 2,
197
198
199 new String[] { "2004-02-12 22:00:06", "2004-02-10 22:00:06" },
200 new String[] { "2004-02-13 22:00:06", "2004-02-12 22:00:06" },
201 new String[] { "checkstyle", "checkstyle" },
202 new int[] { 3, 4 },
203 new int[] { 2, 3 },
204 new int[] { -1, -1 });
205
206 assertNull("List Down presence of net/objectlab/test/MoveUp.java", checkFilePresent(listDown,
207 toId("net/objectlab/test/MoveUp.java")));
208 assertNull("List Down presence of net/objectlab/test/NewUp.java", checkFilePresent(listDown,
209 toId("net/objectlab/test/NewUp.java")));
210 assertNull("List Down presence of net/objectlab/test/MoveUpAndUp.java", checkFilePresent(listDown,
211 toId("net/objectlab/test/MoveUpAndUp.java")));
212 assertNull("List Down presence of net/objectlab/test/NoMove.java", checkFilePresent(listDown,
213 toId("net/objectlab/test/NoMove.java")));
214 assertNull("List Down presence of BLA.java", checkFilePresent(listDown, "BLA.java"));
215 } catch (ParserConfigurationException e) {
216 fail(e.toString());
217 } catch (SAXException e) {
218 fail(e.toString());
219 } catch (IOException e) {
220 fail(e.toString());
221 }
222 }
223
224 private String toId(String string) {
225 return string.replace('/', '_');
226 }
227
228 public void testPMDMovers() {
229 File testInput = new File("target/test-classes/net/objectlab/qalab/parser/qastats.xml");
230
231 Writer testOutput = new StringWriter();
232
233 try {
234 final BuildStatMoverHandler handler = new BuildStatMoverHandler(new InputSource(new FileReader(testInput)), "pmd",
235 testOutput, false, new SilentLogger(), false);
236
237 Calendar cal = Calendar.getInstance();
238
239 cal.set(2004, 01, 11, 0, 0, 0);
240 handler.setStartTimeWindow(cal.getTime());
241 cal.set(2004, 01, 14, 0, 0, 0);
242 handler.setEndTimeWindow(cal.getTime());
243 handler.process();
244
245 System.out.println(testOutput.toString());
246
247
248 DOMParser dom = new DOMParser();
249
250 dom.parse(new InputSource(new StringReader(testOutput.toString())));
251
252 Document doc = dom.getDocument();
253
254 checkTagValue(doc, "datethreshold", "2004-02-11 00:00:00", 1);
255 checkTagValue(doc, "enddatethreshold", "2004-02-14 00:00:00", 1);
256 checkTagValue(doc, "types", "pmd", 1);
257
258
259 NodeList listUp = doc.getElementsByTagName("up");
260
261 assertEquals("size for up", 1, listUp.getLength());
262 assertEquals("number of files in up list", 2, countFileTags(listUp));
263 assertNotNull("List UP presence of net/objectlab/test/MoveUp.java", checkFilePresent(listUp,
264 toId("net/objectlab/test/MoveUp.java")));
265 assertNull("List UP presence of net/objectlab/test/NoMovePMD.java", checkFilePresent(listUp,
266 toId("net/objectlab/test/NoMovePMD.java")));
267 checkDiff(listUp, "net/objectlab/test/MoveUp.java", 2,
268
269 new String[] { "2004-02-12 22:00:06", "-" },
270 new String[] { "2004-02-13 22:00:06", "2004-02-12 22:00:06" },
271 new String[] { "pmd", "pmd" },
272 new int[] { 1, 0 },
273 new int[] { 2, 1 },
274 new int[] { 1, 1 });
275
276 assertNotNull("List UP presence of net/objectlab/test/NewUp.java", checkFilePresent(listUp,
277 toId("net/objectlab/test/NewUp.java")));
278 checkDiff(listUp, "net/objectlab/test/NewUp.java", 1,
279
280 new String[] { "-" },
281 new String[] { "2004-02-13 22:00:06" },
282 new String[] { "pmd" },
283 new int[] { 0 },
284 new int[] { 1 },
285 new int[] { 1 });
286
287 assertNull("List UP presence of net/objectlab/test/MoveUpAndDownToZero.java", checkFilePresent(listUp,
288 toId("net/objectlab/test/MoveUpAndDownToZero.java")));
289 assertNull("List UP presence of net/objectlab/test/MoveUpAndDown.java", checkFilePresent(listUp,
290 toId("net/objectlab/test/MoveUpAndDown.java")));
291 assertNull("List UP presence of net/objectlab/test/MoveUpAndUp.java", checkFilePresent(listUp,
292 toId("net/objectlab/test/MoveUpAndUp.java")));
293 assertNull("List UP presence of net/objectlab/test/MoveDown.java", checkFilePresent(listUp,
294 toId("net/objectlab/test/MoveDown.java")));
295 assertNull("List UP presence of net/objectlab/test/MoveDownAndDown.java", checkFilePresent(listUp,
296 toId("net/objectlab/test/MoveDownAndDown.java")));
297 assertNull("List UP presence of net/objectlab/test/NoMove.java", checkFilePresent(listUp,
298 toId("net/objectlab/test/NoMove.java")));
299 assertNull("List UP presence of net/objectlab/test/NoMovePMD.java", checkFilePresent(listUp,
300 toId("net/objectlab/test/NoMovePMD.java")));
301 assertNull("List UP presence of BLA.java", checkFilePresent(listUp, "BLA.java"));
302
303
304 NodeList listDown = doc.getElementsByTagName("down");
305
306 assertEquals("size for down", 1, listDown.getLength());
307 assertEquals("number of files in down list", 1, countFileTags(listDown));
308 assertNotNull("List Down presence of net/objectlab/test/MoveDownPMD.java", checkFilePresent(listDown,
309 toId("net/objectlab/test/MoveDownPMD.java")));
310 checkDiff(listDown, "net/objectlab/test/MoveDownPMD.java", 1,
311
312
313 new String[] { "2004-02-10 22:00:06" },
314 new String[] { "2004-02-12 22:00:06" },
315 new String[] { "pmd" },
316 new int[] { 2 },
317 new int[] { 1 },
318 new int[] { -1 });
319
320 assertNull("List Down presence of net/objectlab/test/MoveUpAndDownToZero.java", checkFilePresent(listDown,
321 "net/objectlab/test/MoveUpAndDownToZero.java"));
322 assertNull("List Down presence of net/objectlab/test/MoveUpAndDown.java", checkFilePresent(listDown,
323 "net/objectlab/test/MoveUpAndDown.java"));
324 assertNull("List Down presence of net/objectlab/test/MoveDownToZero.java", checkFilePresent(listDown,
325 "net/objectlab/test/MoveDownToZero.java"));
326 assertNull("List Down presence of net/objectlab/test/MoveDown.java", checkFilePresent(listDown,
327 "net/objectlab/test/MoveDown.java"));
328 assertNull("List Down presence of net/objectlab/test/MoveDownAndDown.java", checkFilePresent(listDown,
329 "net/objectlab/test/MoveDownAndDown.java"));
330 assertNull("List Down presence of net/objectlab/test/MoveUp.java", checkFilePresent(listDown,
331 "net/objectlab/test/MoveUp.java"));
332 assertNull("List Down presence of net/objectlab/test/NewUp.java", checkFilePresent(listDown,
333 "net/objectlab/test/NewUp.java"));
334 assertNull("List Down presence of net/objectlab/test/MoveUpAndUp.java", checkFilePresent(listDown,
335 "net/objectlab/test/MoveUpAndUp.java"));
336 assertNull("List Down presence of net/objectlab/test/NoMove.java", checkFilePresent(listDown,
337 "net/objectlab/test/NoMove.java"));
338 assertNull("List Down presence of net/objectlab/test/NoMovePMD.java", checkFilePresent(listDown,
339 "net/objectlab/test/NoMovePMD.java"));
340 assertNull("List Down presence of BLA.java", checkFilePresent(listDown, "BLA.java"));
341 } catch (ParserConfigurationException e) {
342 fail(e.toString());
343 } catch (SAXException e) {
344 fail(e.toString());
345 } catch (IOException e) {
346 fail(e.toString());
347 }
348 }
349
350 public void testUnknownTypeMovers() {
351 File testInput = new File("target/test-classes/net/objectlab/qalab/parser/qastats.xml");
352
353 Writer testOutput = new StringWriter();
354
355 try {
356 final BuildStatMoverHandler handler = new BuildStatMoverHandler(new InputSource(new FileReader(testInput)), "bla",
357 testOutput, false, new SilentLogger(), false);
358
359 Calendar cal = Calendar.getInstance();
360
361 cal.set(2004, 01, 11, 0, 0, 0);
362 handler.setStartTimeWindow(cal.getTime());
363 cal.set(2004, 01, 14, 0, 0, 0);
364 handler.setEndTimeWindow(cal.getTime());
365 handler.process();
366
367 System.out.println(testOutput.toString());
368
369
370 DOMParser dom = new DOMParser();
371
372 dom.parse(new InputSource(new StringReader(testOutput.toString())));
373
374 Document doc = dom.getDocument();
375
376 checkTagValue(doc, "datethreshold", "2004-02-11 00:00:00", 1);
377 checkTagValue(doc, "enddatethreshold", "2004-02-14 00:00:00", 1);
378 checkTagValue(doc, "types", "bla", 1);
379
380
381 NodeList listUp = doc.getElementsByTagName("up");
382
383 assertEquals("size for up", 1, listUp.getLength());
384 assertEquals("number of files in up list", 0, countFileTags(listUp));
385 assertNull("List UP presence of net/objectlab/test/MoveUpAndDownToZero.java", checkFilePresent(listUp,
386 "net/objectlab/test/MoveUpAndDownToZero.java"));
387 assertNull("List UP presence of net/objectlab/test/MoveUpAndDown.java", checkFilePresent(listUp,
388 "net/objectlab/test/MoveUpAndDown.java"));
389 assertNull("List UP presence of net/objectlab/test/MoveUpAndUp.java", checkFilePresent(listUp,
390 "net/objectlab/test/MoveUpAndUp.java"));
391 assertNull("List UP presence of net/objectlab/test/MoveUp.java", checkFilePresent(listUp,
392 "net/objectlab/test/MoveUp.java"));
393 assertNull("List UP presence of net/objectlab/test/NewUp.java", checkFilePresent(listUp,
394 "net/objectlab/test/NewUp.java"));
395 assertNull("List UP presence of net/objectlab/test/MoveDown.java", checkFilePresent(listUp,
396 "net/objectlab/test/MoveDown.java"));
397 assertNull("List UP presence of net/objectlab/test/MoveDownAndDown.java", checkFilePresent(listUp,
398 "net/objectlab/test/MoveDownAndDown.java"));
399 assertNull("List UP presence of net/objectlab/test/NoMove.java", checkFilePresent(listUp,
400 "net/objectlab/test/NoMove.java"));
401 assertNull("List UP presence of BLA.java", checkFilePresent(listUp, "BLA.java"));
402
403
404 NodeList listDown = doc.getElementsByTagName("down");
405
406 assertEquals("size for down", 1, listDown.getLength());
407 assertEquals("number of files in down list", 0, countFileTags(listDown));
408 assertNull("List Down presence of net/objectlab/test/MoveUpAndDownToZero.java", checkFilePresent(listDown,
409 "net/objectlab/test/MoveUpAndDownToZero.java"));
410 assertNull("List Down presence of net/objectlab/test/MoveUpAndDown.java", checkFilePresent(listDown,
411 "net/objectlab/test/MoveUpAndDown.java"));
412 assertNull("List Down presence of net/objectlab/test/MoveDownToZero.java", checkFilePresent(listDown,
413 "net/objectlab/test/MoveDownToZero.java"));
414 assertNull("List Down presence of net/objectlab/test/MoveDown.java", checkFilePresent(listDown,
415 "net/objectlab/test/MoveDown.java"));
416 assertNull("List Down presence of net/objectlab/test/MoveDownAndDown.java", checkFilePresent(listDown,
417 "net/objectlab/test/MoveDownAndDown.java"));
418 assertNull("List Down presence of net/objectlab/test/MoveUp.java", checkFilePresent(listDown,
419 "net/objectlab/test/MoveUp.java"));
420 assertNull("List Down presence of net/objectlab/test/NewUp.java", checkFilePresent(listDown,
421 "net/objectlab/test/NewUp.java"));
422 assertNull("List Down presence of net/objectlab/test/MoveUpAndUp.java", checkFilePresent(listDown,
423 "net/objectlab/test/MoveUpAndUp.java"));
424 assertNull("List Down presence of net/objectlab/test/NoMove.java", checkFilePresent(listDown,
425 "net/objectlab/test/NoMove.java"));
426 assertNull("List Down presence of BLA.java", checkFilePresent(listDown, "BLA.java"));
427 } catch (ParserConfigurationException e) {
428 fail(e.toString());
429 } catch (SAXException e) {
430 fail(e.toString());
431 } catch (IOException e) {
432 fail(e.toString());
433 }
434 }
435
436 /**
437 * Movers for both Checkstyle and PMD in the same report.
438 */
439 public void testCheckstyleAndPMDMovers() {
440 File testInput = new File("target/test-classes/net/objectlab/qalab/parser/qastats.xml");
441
442 Writer testOutput = new StringWriter();
443
444 try {
445 final BuildStatMoverHandler handler = new BuildStatMoverHandler(new InputSource(new FileReader(testInput)),
446 "checkstyle,pmd", testOutput, false, new SilentLogger(), false);
447
448 Calendar cal = Calendar.getInstance();
449
450 cal.set(2004, 01, 11, 0, 0, 0);
451 handler.setStartTimeWindow(cal.getTime());
452 cal.set(2004, 01, 14, 0, 0, 0);
453 handler.setEndTimeWindow(cal.getTime());
454 handler.process();
455
456 System.out.println(testOutput.toString());
457
458
459 DOMParser dom = new DOMParser();
460
461 dom.parse(new InputSource(new StringReader(testOutput.toString())));
462
463 Document doc = dom.getDocument();
464
465 checkTagValue(doc, "datethreshold", "2004-02-11 00:00:00", 1);
466 checkTagValue(doc, "enddatethreshold", "2004-02-14 00:00:00", 1);
467 checkTagValue(doc, "types", "checkstyle,pmd", 1);
468
469
470 NodeList listUp = doc.getElementsByTagName("up");
471
472 assertEquals("size for up", 1, listUp.getLength());
473 assertEquals("number of files in up list", 5, countFileTags(listUp));
474 assertNotNull("List UP presence of net/objectlab/test/MoveUpAndDownToZero.java", checkFilePresent(listUp,
475 "net/objectlab/test/MoveUpAndDownToZero.java"));
476 checkDiff(listUp, "net/objectlab/test/MoveUpAndDownToZero.java", 1,
477
478
479 new String[] { "-" },
480 new String[] { "2004-02-11 22:00:06" },
481 new String[] { "checkstyle" },
482 new int[] { 0 },
483 new int[] { 2 },
484 new int[] { 2 });
485
486 assertNotNull("List UP presence of net/objectlab/test/MoveUpAndDown.java", checkFilePresent(listUp,
487 "net/objectlab/test/MoveUpAndDown.java"));
488 checkDiff(listUp, "net/objectlab/test/MoveUpAndDown.java", 2,
489
490
491 new String[] { "2004-02-11 22:00:06", "-" },
492 new String[] { "2004-02-12 22:00:06", "2004-02-11 22:00:06" },
493 new String[] { "checkstyle", "checkstyle" },
494 new int[] { 2, 0 },
495 new int[] { 3, 2 },
496 new int[] { 1, 2 });
497
498 assertNotNull("List UP presence of net/objectlab/test/MoveUpAndUp.java", checkFilePresent(listUp,
499 "net/objectlab/test/MoveUpAndUp.java"));
500 checkDiff(listUp, "net/objectlab/test/MoveUpAndUp.java", 3,
501
502
503 new String[] { "2004-02-12 22:00:06", "2004-02-11 22:00:06", "-" },
504 new String[] { "2004-02-13 22:00:06", "2004-02-12 22:00:06", "2004-02-11 22:00:06" },
505 new String[] { "checkstyle", "checkstyle", "checkstyle" },
506 new int[] { 3, 2, 0 },
507 new int[] { 4, 3, 2 },
508 new int[] { 1, 1, 2 });
509
510 checkDiff(listUp, "net/objectlab/test/MoveUp.java", 4,
511
512 new String[] { "2004-02-12 22:00:06", "-", "2004-02-11 22:00:06", "-" },
513 new String[] { "2004-02-13 22:00:06", "2004-02-12 22:00:06", "2004-02-12 22:00:06", "2004-02-11 22:00:06" },
514 new String[] { "pmd", "pmd", "checkstyle", "checkstyle" },
515 new int[] { 1, 0, 2, 0 },
516 new int[] { 2, 1, 3, 2 },
517 new int[] { 1, 1, 1, 2 });
518
519 assertNotNull("List UP presence of net/objectlab/test/NewUp.java", checkFilePresent(listUp,
520 "net/objectlab/test/NewUp.java"));
521 checkDiff(listUp, "net/objectlab/test/NewUp.java", 2,
522
523 new String[] { "-", "-" },
524 new String[] { "2004-02-13 22:00:06", "2004-02-13 22:00:06" },
525 new String[] { "checkstyle", "pmd" },
526 new int[] { 0, 0 },
527 new int[] { 3, 1 },
528 new int[] { 3, 1 });
529
530 assertNull("List UP presence of net/objectlab/test/MoveDown.java", checkFilePresent(listUp,
531 "net/objectlab/test/MoveDown.java"));
532 assertNull("List UP presence of net/objectlab/test/MoveDownAndDown.java", checkFilePresent(listUp,
533 "net/objectlab/test/MoveDownAndDown.java"));
534 assertNull("List UP presence of net/objectlab/test/NoMove.java", checkFilePresent(listUp,
535 "net/objectlab/test/NoMove.java"));
536 assertNull("List UP presence of BLA.java", checkFilePresent(listUp, "BLA.java"));
537
538
539 NodeList listDown = doc.getElementsByTagName("down");
540
541 assertEquals("size for down", 1, listDown.getLength());
542 assertEquals("number of files in down list", 6, countFileTags(listDown));
543 assertNotNull("List Down presence of net/objectlab/test/MoveDownPMD.java", checkFilePresent(listDown,
544 "net/objectlab/test/MoveDownPMD.java"));
545 checkDiff(listDown, "net/objectlab/test/MoveDownPMD.java", 1,
546
547
548 new String[] { "2004-02-10 22:00:06" },
549 new String[] { "2004-02-12 22:00:06" },
550 new String[] { "pmd" },
551 new int[] { 2 },
552 new int[] { 1 },
553 new int[] { -1 });
554
555 assertNotNull("List Down presence of net/objectlab/test/MoveUpAndDownToZero.java", checkFilePresent(listDown,
556 "net/objectlab/test/MoveUpAndDownToZero.java"));
557 checkDiff(listDown, "net/objectlab/test/MoveUpAndDownToZero.java", 1,
558
559
560 new String[] { "2004-02-11 22:00:06" },
561 new String[] { "-" },
562 new String[] { "checkstyle" },
563 new int[] { 2 },
564 new int[] { 0 },
565 new int[] { -2 });
566
567 assertNotNull("List Down presence of net/objectlab/test/MoveUpAndDown.java", checkFilePresent(listDown,
568 "net/objectlab/test/MoveUpAndDown.java"));
569 checkDiff(listDown, "net/objectlab/test/MoveUpAndDown.java", 1,
570
571
572 new String[] { "2004-02-12 22:00:06" },
573 new String[] { "2004-02-13 22:00:06" },
574 new String[] { "checkstyle" },
575 new int[] { 3 },
576 new int[] { 1 },
577 new int[] { -2 });
578
579 assertNotNull("List Down presence of net/objectlab/test/MoveDownToZero.java", checkFilePresent(listDown,
580 "net/objectlab/test/MoveDownToZero.java"));
581 checkDiff(listDown, "net/objectlab/test/MoveDownToZero.java", 1,
582
583
584 new String[] { "2004-02-11 22:00:06" },
585 new String[] { "-" },
586 new String[] { "checkstyle" },
587 new int[] { 2 },
588 new int[] { 0 },
589 new int[] { -2 });
590
591 assertNotNull("List Down presence of net/objectlab/test/MoveDown.java", checkFilePresent(listDown,
592 "net/objectlab/test/MoveDown.java"));
593 checkDiff(listDown, "net/objectlab/test/MoveDown.java", 1,
594
595
596 new String[] { "2004-02-10 22:00:06" },
597 new String[] { "2004-02-12 22:00:06" },
598 new String[] { "checkstyle" },
599 new int[] { 2 },
600 new int[] { 1 },
601 new int[] { -1 });
602
603 assertNotNull("List Down presence of net/objectlab/test/MoveDownAndDown.java", checkFilePresent(listDown,
604 "net/objectlab/test/MoveDownAndDown.java"));
605 checkDiff(listDown, "net/objectlab/test/MoveDownAndDown.java", 2,
606
607
608 new String[] { "2004-02-12 22:00:06", "2004-02-10 22:00:06" },
609 new String[] { "2004-02-13 22:00:06", "2004-02-12 22:00:06" },
610 new String[] { "checkstyle", "checkstyle" },
611 new int[] { 3, 4 },
612 new int[] { 2, 3 },
613 new int[] { -1, -1 });
614
615 assertNull("List Down presence of net/objectlab/test/MoveUp.java", checkFilePresent(listDown,
616 "net/objectlab/test/MoveUp.java"));
617 assertNull("List Down presence of net/objectlab/test/NewUp.java", checkFilePresent(listDown,
618 "net/objectlab/test/NewUp.java"));
619 assertNull("List Down presence of net/objectlab/test/MoveUpAndUp.java", checkFilePresent(listDown,
620 "net/objectlab/test/MoveUpAndUp.java"));
621 assertNull("List Down presence of net/objectlab/test/NoMove.java", checkFilePresent(listDown,
622 "net/objectlab/test/NoMove.java"));
623 assertNull("List Down presence of BLA.java", checkFilePresent(listDown, "BLA.java"));
624 } catch (ParserConfigurationException e) {
625 fail(e.toString());
626 } catch (SAXException e) {
627 fail(e.toString());
628 } catch (IOException e) {
629 fail(e.toString());
630 }
631 }
632
633 /**
634 * Runa test to check the impact of weekendAdjustment.
635 */
636 public void testWeekendAdjust() {
637 File testInput = new File("target/test-classes/net/objectlab/qalab/parser/qastats.xml");
638
639 try {
640 {
641 Writer testOutput = new StringWriter();
642 final BuildStatMoverHandler handlerNoWeekendAdjustment = new BuildStatMoverHandler(new InputSource(
643 new FileReader(testInput)), "pmd", testOutput, false, new SilentLogger(), false);
644
645 Calendar cal = Calendar.getInstance();
646
647
648
649 cal.set(2005, 05, 25, 0, 0, 0);
650 handlerNoWeekendAdjustment.setStartTimeWindow(cal.getTime());
651 cal.set(2005, 06, 04, 0, 0, 0);
652 handlerNoWeekendAdjustment.setEndTimeWindow(cal.getTime());
653 handlerNoWeekendAdjustment.process();
654
655 System.out.println(testOutput.toString());
656
657
658 DOMParser dom = new DOMParser();
659 dom.parse(new InputSource(new StringReader(testOutput.toString())));
660
661 Document doc = dom.getDocument();
662
663 checkTagValue(doc, "datethreshold", "2005-06-25 00:00:00", 1);
664 checkTagValue(doc, "enddatethreshold", "2005-07-04 00:00:00", 1);
665 checkTagValue(doc, "types", "pmd", 1);
666
667
668 NodeList listUp = doc.getElementsByTagName("up");
669 assertEquals("size for up", 1, listUp.getLength());
670 assertEquals("number of files in up list", 0, countFileTags(listUp));
671 NodeList listDown = doc.getElementsByTagName("down");
672 assertEquals("size for down", 1, listDown.getLength());
673 assertEquals("number of files in down list", 0, countFileTags(listDown));
674 }
675
676
677
678 {
679 Writer testOutput2 = new StringWriter();
680 final BuildStatMoverHandler handlerWeekendAdjustment = new BuildStatMoverHandler(new InputSource(new FileReader(
681 testInput)), "pmd", testOutput2, true, new SilentLogger(), false);
682
683
684
685 Calendar cal = Calendar.getInstance();
686 cal.set(2005, 05, 25, 0, 0, 0);
687 handlerWeekendAdjustment.setStartTimeWindow(cal.getTime());
688 cal.set(2005, 06, 04, 0, 0, 0);
689 handlerWeekendAdjustment.setEndTimeWindow(cal.getTime());
690 handlerWeekendAdjustment.process();
691
692 System.out.println(testOutput2.toString());
693
694
695 DOMParser dom2 = new DOMParser();
696 dom2.parse(new InputSource(new StringReader(testOutput2.toString())));
697
698 Document doc2 = dom2.getDocument();
699
700 checkTagValue(doc2, "datethreshold", "2005-06-23 00:00:00", 1);
701 checkTagValue(doc2, "enddatethreshold", "2005-07-04 00:00:00", 1);
702 checkTagValue(doc2, "types", "pmd", 1);
703
704 NodeList listUp2 = doc2.getElementsByTagName("up");
705 assertEquals("size for up", 1, listUp2.getLength());
706 assertEquals("number of files in up list", 2, countFileTags(listUp2));
707 assertNotNull("List UP presence of net/objectlab/test/WeekendAdjustUpAndDownToZero.java", checkFilePresent(
708 listUp2, "net/objectlab/test/WeekendAdjustUpAndDownToZero.java"));
709 checkDiff(listUp2, "net/objectlab/test/WeekendAdjustUpAndDownToZero.java", 1,
710
711
712 new String[] { "-" },
713 new String[] { "2005-06-24 22:00:06" },
714 new String[] { "pmd" },
715 new int[] { 0 },
716 new int[] { 2 },
717 new int[] { 2 });
718
719 assertNotNull("List UP presence of net/objectlab/test/WeekendAdjustDown.java", checkFilePresent(listUp2,
720 "net/objectlab/test/WeekendAdjustDown.java"));
721 checkDiff(listUp2, "net/objectlab/test/WeekendAdjustDown.java", 1,
722
723
724 new String[] { "-" },
725 new String[] { "2005-06-23 22:00:06" },
726 new String[] { "pmd" },
727 new int[] { 0 },
728 new int[] { 4 },
729 new int[] { 4 });
730
731
732 NodeList listDown2 = doc2.getElementsByTagName("down");
733
734 assertEquals("size for down", 1, listDown2.getLength());
735 assertEquals("number of files in down list", 2, countFileTags(listDown2));
736 assertNotNull("List Down presence of net/objectlab/test/WeekendAdjustDown.java", checkFilePresent(listDown2,
737 "net/objectlab/test/WeekendAdjustDown.java"));
738 checkDiff(listDown2, "net/objectlab/test/WeekendAdjustDown.java", 2,
739
740
741 new String[] { "2005-06-24 22:00:06", "2005-06-23 22:00:06" },
742 new String[] { "-", "2005-06-24 22:00:06" },
743 new String[] { "pmd", "pmd" },
744 new int[] { 2, 4 },
745 new int[] { 0, 2 },
746 new int[] { -2, -2 });
747
748 assertNotNull("List Down presence of net/objectlab/test/WeekendAdjustUpAndDownToZero.java", checkFilePresent(
749 listDown2, "net/objectlab/test/WeekendAdjustUpAndDownToZero.java"));
750 checkDiff(listDown2, "net/objectlab/test/WeekendAdjustUpAndDownToZero.java", 1,
751
752
753 new String[] { "2005-06-24 22:00:06" },
754 new String[] { "-" },
755 new String[] { "pmd" },
756 new int[] { 2 },
757 new int[] { 0 },
758 new int[] { -2 });
759 }
760 } catch (ParserConfigurationException e) {
761 fail(e.toString());
762 } catch (SAXException e) {
763 fail(e.toString());
764 } catch (IOException e) {
765 fail(e.toString());
766 }
767 }
768
769 /**
770 * Test added by Thomas Stoffer. It should be noted that if a value goes up
771 * (or down) during the interval BUT fails to appear within 24h of the end
772 * of the timw window, it should NOT appear at all as it has been deemed to
773 * have fallen to 0...
774 */
775 public void testCheckstyleMoversToZero() {
776 File testInput = new File("target/test-classes/net/objectlab/qalab/parser/qastatszero.xml");
777
778 Writer testOutput = new StringWriter();
779
780 try {
781 final BuildStatMoverHandler handler = new BuildStatMoverHandler(new InputSource(new FileReader(testInput)),
782 "checkstyle", testOutput, false, new SilentLogger(), false);
783 handler.setHoursOffsetForLastRun(0);
784
785 Calendar cal = Calendar.getInstance();
786
787 cal.set(Calendar.MILLISECOND, 0);
788 cal.set(2006, 06, 26, 15, 14, 59);
789 handler.setStartTimeWindow(cal.getTime());
790 cal.set(2006, 06, 31, 16, 59, 53);
791 handler.setEndTimeWindow(cal.getTime());
792 handler.process();
793
794 System.out.println(testOutput.toString());
795
796
797 DOMParser dom = new DOMParser();
798
799 dom.parse(new InputSource(new StringReader(testOutput.toString())));
800
801 Document doc = dom.getDocument();
802
803 checkTagValue(doc, "datethreshold", "2006-07-26 15:14:59", 1);
804 checkTagValue(doc, "enddatethreshold", "2006-07-31 16:59:53", 1);
805 checkTagValue(doc, "types", "checkstyle", 1);
806
807
808 NodeList listUp = doc.getElementsByTagName("up");
809 assertEquals("size for up", 1, listUp.getLength());
810 assertNotNull("List UP presence of net/objectlab/test/ZeroToManyAtStartTime.java", checkFilePresent(listUp,
811 toId("net/objectlab/test/ZeroToManyAtStartTime.java")));
812
813 assertEquals("number of files in up list", 6, countFileTags(listUp));
814 assertNotNull("List UP presence of net/objectlab/test/ZeroToManyAfterStartTime.java", checkFilePresent(listUp,
815 toId("net/objectlab/test/ZeroToManyAfterStartTime.java")));
816 checkDiff(listUp, "net/objectlab/test/ZeroToManyAfterStartTime.java", 1,
817
818
819 new String[] { "-" },
820 new String[] { "2006-07-26 15:37:58" },
821 new String[] { "checkstyle" },
822 new int[] { 0 },
823 new int[] { 16 },
824 new int[] { 16 });
825 assertNotNull("List UP presence of net/objectlab/test/ZeroToManyAndChange.java", checkFilePresent(listUp,
826 toId("net/objectlab/test/ZeroToManyAndChange.java")));
827 checkDiff(listUp, "net/objectlab/test/ZeroToManyAndChange.java", 1,
828
829
830 new String[] { "-" },
831 new String[] { "2006-07-26 15:37:58" },
832 new String[] { "checkstyle" },
833 new int[] { 0 },
834 new int[] { 33 },
835 new int[] { 33 });
836
837 assertNotNull("List UP presence of net/objectlab/test/ManyToZeroToMany.java", checkFilePresent(listUp,
838 toId("net/objectlab/test/ManyToZeroToMany.java")));
839 checkDiff(listUp, "net/objectlab/test/ManyToZeroToMany.java", 1,
840
841
842 new String[] { "-" },
843 new String[] { "2006-07-26 15:14:59" },
844 new String[] { "checkstyle" },
845 new int[] { 0 },
846 new int[] { 33 },
847 new int[] { 33 });
848
849
850 NodeList listDown = doc.getElementsByTagName("down");
851
852 assertEquals("size for down", 1, listDown.getLength());
853 assertEquals("number of files in down list", 4, countFileTags(listDown));
854 assertNotNull("List Down presence of net/objectlab/test/ZeroToManyAtStartTime.java", checkFilePresent(listDown,
855 toId("net/objectlab/test/ZeroToManyAtStartTime.java")));
856 checkDiff(listDown, "net/objectlab/test/ZeroToManyAtStartTime.java", 1,
857
858
859 new String[] { "2006-07-26 15:14:59" },
860 new String[] { "-" },
861 new String[] { "checkstyle" },
862 new int[] { 16 },
863 new int[] { 0 },
864 new int[] { -16 });
865
866 assertNull("List Down presence of net/objectlab/test/ManyToZeroToMany", checkFilePresent(listDown,
867 toId("net/objectlab/test/ManyToZeroToMany")));
868
869
870
871
872
873
874
875
876
877
878
879
880 assertNotNull("List Down presence of net/objectlab/test/ManyToZero2Days.java", checkFilePresent(listDown,
881 toId("net/objectlab/test/ManyToZero2Days.java")));
882 checkDiff(listDown, "net/objectlab/test/ManyToZero2Days.java", 1,
883
884
885 new String[] { "2006-07-28 18:40:47" },
886 new String[] { "-" },
887 new String[] { "checkstyle" },
888 new int[] { 33 },
889 new int[] { 0 },
890 new int[] { -33 });
891
892
893
894
895
896
897
898
899 assertNotNull("List Down presence of net/objectlab/test/ManyToZeroLastRun.java", checkFilePresent(listDown,
900 toId("net/objectlab/test/ManyToZeroLastRun.java")));
901 checkDiff(listDown, "net/objectlab/test/ManyToZeroLastRun.java", 1,
902
903
904 new String[] { "2006-07-31 16:43:03" },
905 new String[] { "-" },
906 new String[] { "checkstyle" },
907 new int[] { 33 },
908 new int[] { 0 },
909 new int[] { -33 });
910
911 } catch (ParserConfigurationException e) {
912 fail(e.toString());
913 } catch (SAXException e) {
914 fail(e.toString());
915 } catch (IOException e) {
916 fail(e.toString());
917 }
918 }
919
920 private void checkDiff(NodeList list, String file, int numberDiff, String[] previousRuns, String[] currentRuns,
921 String[] types, int[] previousErrors, int[] currentErrors, int[] diffs) {
922 NodeList children = checkFilePresent(list, toId(file));
923 int countDiff = 0;
924
925 for (int i = 0; i < children.getLength(); i++) {
926 Node diff = children.item(i);
927
928 if ("diff".equals(diff.getLocalName())) {
929
930
931
932 String previousRun = getAttribute(diff, "previousrun");
933 int difference = Integer.parseInt(getAttribute(diff, "diff"));
934 String currentRun = getAttribute(diff, "currentrun");
935 String type = getAttribute(diff, "type");
936 int previousErrorCount = Integer.parseInt(getAttribute(diff, "previouserrors"));
937 int currentErrorCount = Integer.parseInt(getAttribute(diff, "currenterrors"));
938
939 boolean found = false;
940 for (int u = 0; u < previousErrors.length && !found; u++) {
941 found = previousRuns[u].equals(previousRun) && currentRuns[u].equals(currentRun) && types[u].equals(type)
942 && previousErrors[u] == previousErrorCount && currentErrors[u] == currentErrorCount
943 && diffs[u] == difference;
944 }
945
946 if (!found) {
947 fail("Could not match this set of data in mover report with test data for " + file + "\n" + "previousRun "
948 + previousRun + "\n" + "currentRun " + currentRun + "\n" + "type " + type + "\n" + "previousErrors "
949 + previousErrors + "\n" + "currentErrors " + currentErrors + "\n" + "diff " + diff + "\n");
950 }
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965 countDiff++;
966 }
967 }
968
969 assertEquals("Number of diff tags", numberDiff, countDiff);
970 }
971
972 private NodeList checkFilePresent(NodeList listUp, String shouldBePresent) {
973 String checkFile = toId(shouldBePresent);
974 for (int i = 0; i < listUp.getLength(); i++) {
975 Element element = (Element) listUp.item(i);
976 NodeList list = element.getChildNodes();
977
978 for (int u = 0; u < list.getLength(); u++) {
979 Node file = list.item(u);
980
981 if ("file".equals(file.getLocalName())) {
982 String fileName = getAttribute(file, "name");
983
984 if (checkFile.equals(fileName)) {
985 return file.getChildNodes();
986 }
987 }
988 }
989 }
990
991 return null;
992 }
993
994 private int countFileTags(NodeList listUp) {
995 int count = 0;
996
997 for (int i = 0; i < listUp.getLength(); i++) {
998 Element element = (Element) listUp.item(i);
999 NodeList list = element.getChildNodes();
1000
1001 for (int u = 0; u < list.getLength(); u++) {
1002 Node file = list.item(u);
1003
1004
1005
1006 if ("file".equals(file.getLocalName())) {
1007 count++;
1008 }
1009 }
1010 }
1011
1012 return count;
1013 }
1014
1015 private void checkTagValue(Document doc, String tagName, String value, int count) {
1016
1017 NodeList list = doc.getElementsByTagName(tagName);
1018
1019 assertEquals("size for " + tagName, count, list.getLength());
1020
1021 for (int i = 0; i < list.getLength(); i++) {
1022
1023 Element element = (Element) list.item(i);
1024
1025 assertEquals("Value for " + tagName, value, getElementText(element));
1026 }
1027 }
1028
1029 /**
1030 * Gets the value of the child text node.
1031 *
1032 * @param node
1033 * the XML element.
1034 * @return the child Text node value. If one of the nodes child elements is
1035 * not of type Text then return null;
1036 */
1037 protected final String getElementText(final Node node) {
1038 String text = null;
1039 NodeList childNodes = node.getChildNodes();
1040
1041 for (int i = 0; i < childNodes.getLength(); i++) {
1042 Node childNode = childNodes.item(i);
1043
1044 if (childNode.getNodeType() == Node.TEXT_NODE) {
1045 text = ((Text) childNode).getData();
1046
1047 break;
1048 }
1049 }
1050
1051 return text;
1052 }
1053
1054 /**
1055 * get the attribute value of a node.
1056 *
1057 * @param node
1058 * XML Dom Node
1059 * @param attributeName
1060 * attribute.
1061 * @return string value of the attributeName.
1062 */
1063 private final String getAttribute(final Node node, final String attributeName) {
1064 String text = null;
1065 NamedNodeMap map = node.getAttributes();
1066
1067 if (map != null) {
1068 Node n = map.getNamedItem(attributeName);
1069
1070 if (n != null) {
1071 return getElementText(n);
1072 }
1073 }
1074
1075 return text;
1076 }
1077 }
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090