1   ////////////////////////////////////////////////////////////////////////////////
2   //
3   //                  ObjectLab is sponsoring QALab
4   // 
5   // Based in London, we are world leaders in the design and development 
6   // of bespoke applications for the Securities Financing markets.
7   // 
8   // <a href="http://www.objectlab.co.uk/open">Click here to learn more</a>
9   //           ___  _     _           _   _          _
10  //          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
11  //         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
12  //         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
13  //          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
14  //                   |__/
15  //
16  //                   http://www.ObjectLab.co.uk
17  // ---------------------------------------------------------------------------
18  //
19  //QALab is released under the GNU General Public License.
20  //
21  //QALab: Collects QA Statistics from your build over time.
22  //2005+, ObjectLab Ltd
23  //
24  //This library is free software; you can redistribute it and/or
25  //modify it under the terms of the GNU General Public
26  //License as published by the Free Software Foundation; either
27  //version 2.1 of the License, or (at your option) any later version.
28  //
29  //This library is distributed in the hope that it will be useful,
30  //but WITHOUT ANY WARRANTY; without even the implied warranty of
31  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
32  //General Public License for more details.
33  //
34  //You should have received a copy of the GNU General Public
35  //License along with this library; if not, write to the Free Software
36  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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.exporter.ConsoleExporter;
45  import net.objectlab.qalab.exporter.QALabXMLExporter;
46  import net.objectlab.qalab.util.ConsoleLogger;
47  import net.objectlab.qalab.util.SilentLogger;
48  
49  import org.xml.sax.InputSource;
50  
51  import java.io.File;
52  import java.io.IOException;
53  
54  import java.util.Properties;
55  
56  public class CheckstyleStatMergeTest extends BaseMerge {
57      /**
58       * Create the test case
59       *
60       * @param testName name of the test case
61       */
62      public CheckstyleStatMergeTest(String testName) {
63          super(testName);
64      }
65  
66      /**
67       * @return the suite of tests being tested
68       */
69      public static Test suite() {
70          return new TestSuite(CheckstyleStatMergeTest.class);
71      }
72  
73      /**
74       * test simple merge from empty
75       */
76      public void testMergeWithConsole() {
77          try {
78              StatMerger merger = (StatMerger) Class.forName(
79                      "net.objectlab.qalab.parser.CheckstyleStatMerge")
80                                                    .newInstance();
81  
82              merger.setQuiet(false);
83              merger.setSrcDir("src/main/java");
84              merger.setTaskLogger(new SilentLogger());
85              merger.mergeStats(new InputSource(
86                      CheckstyleStatMergeTest.class.getResourceAsStream(
87                          "good-checkstyle.xml")), new ConsoleExporter());
88          }
89          catch (IllegalAccessException e) {
90              fail(e.toString());
91          }
92          catch (ClassNotFoundException e) {
93              fail(e.toString());
94          }
95          catch (InstantiationException e) {
96              fail(e.toString());
97          }
98      }
99  
100     /**
101      * test simple merge from empty
102      */
103     public void testMerge() {
104         try {
105             Properties properties = new Properties();
106 
107             properties.setProperty("qalab.merge.output.resourcename",
108                 "empty-stats.xml");
109             properties.setProperty("qalab.merge.output.classname",
110             "net.objectlab.qalab.parser.CheckstyleStatMergeTest");
111 
112             QALabXMLExporter exporter = new QALabXMLExporter();
113 
114             exporter.setQuiet(true);
115             exporter.setTaskLogger(new SilentLogger());
116 
117             StatMerger merger = (StatMerger) Class.forName(
118                     "net.objectlab.qalab.parser.CheckstyleStatMerge")
119                                                   .newInstance();
120 
121             merger.setQuiet(false);
122             merger.setSrcDir("src/main/java");
123             merger.setTaskLogger(new SilentLogger());
124 
125             properties.setProperty("qalab.merge.output.timestamp",
126                     merger.getMergerTimeStamp());
127             properties.setProperty("qalab.merge.type",
128                     merger.getType());
129             exporter.configure(properties);
130 
131             merger.mergeStats(new InputSource(
132                     CheckstyleStatMergeTest.class.getResourceAsStream(
133                         "good-checkstyle.xml")), exporter);
134 
135             checkSummary(exporter.getDocument(), 1, 168, 25);
136             checkFileCount(exporter.getDocument(), 6, 168);
137         }
138         catch (IllegalAccessException e) {
139             fail(e.toString());
140         }
141         catch (ClassNotFoundException e) {
142             fail(e.toString());
143         }
144         catch (InstantiationException e) {
145             fail(e.toString());
146         }
147     }
148 
149     /**
150      * test simple merge from empty
151      */
152     public void testMergeNonExistingQALab() {
153         try {
154             final String fileName = "new-stats.xml";
155             File file = new File(fileName);
156 
157             if (file.exists()) {
158                 file.delete();
159             }
160 
161             Properties properties = new Properties();
162 
163             properties.setProperty("qalab.merge.output.file", fileName);
164 
165             QALabXMLExporter exporter = new QALabXMLExporter();
166 
167             exporter.setQuiet(true);
168             exporter.setTaskLogger(new SilentLogger());
169 
170             StatMerger merger = (StatMerger) Class.forName(
171                     "net.objectlab.qalab.parser.CheckstyleStatMerge")
172                                                   .newInstance();
173 
174             merger.setQuiet(false);
175             merger.setSrcDir("src/main/java");
176             merger.setTaskLogger(new SilentLogger());
177 
178             properties.setProperty("qalab.merge.output.timestamp",
179                     merger.getMergerTimeStamp());
180             properties.setProperty("qalab.merge.type",
181                     merger.getType());
182             exporter.configure(properties);
183 
184             merger.mergeStats(new InputSource(
185                     CheckstyleStatMergeTest.class.getResourceAsStream(
186                         "good-checkstyle.xml")), exporter);
187 
188             System.out.println(streamToString(exporter.getDocument()));
189             checkDTDPresent(exporter.getDocument());
190             checkSummary(exporter.getDocument(), 1, 168, 25);
191             checkFileCount(exporter.getDocument(), 6, 168);
192 
193             exporter.save();
194 
195             if (file.exists()) {
196                 file.delete();
197             }
198         }
199         catch (IllegalAccessException e) {
200             fail(e.toString());
201         }
202         catch (ClassNotFoundException e) {
203             fail(e.toString());
204         }
205         catch (InstantiationException e) {
206             fail(e.toString());
207         }
208         catch (IOException e) {
209             fail(e.toString());
210         }
211     }
212 
213     /**
214      * test simple merge with empty checkstyle and empty qalab.
215      */
216     public void testMergeEmptyCheckstyleAndQALab() {
217         try {
218             Properties properties = new Properties();
219 
220             properties.setProperty("qalab.merge.output.resourcename",
221                 "empty-stats.xml");
222             properties.setProperty("qalab.merge.output.classname",
223                 "net.objectlab.qalab.parser.CheckstyleStatMergeTest");
224 
225             QALabXMLExporter exporter = new QALabXMLExporter();
226 
227             exporter.setQuiet(true);
228             exporter.setTaskLogger(new SilentLogger());
229 
230             StatMerger merger = (StatMerger) Class.forName(
231                     "net.objectlab.qalab.parser.CheckstyleStatMerge")
232                                                   .newInstance();
233 
234             merger.setQuiet(false);
235             merger.setSrcDir("src/main/java");
236             merger.setTaskLogger(new SilentLogger());
237 
238             properties.setProperty("qalab.merge.output.timestamp",
239                     merger.getMergerTimeStamp());
240             properties.setProperty("qalab.merge.type",
241                     merger.getType());
242             exporter.configure(properties);
243 
244             merger.mergeStats(new InputSource(
245                     CheckstyleStatMergeTest.class.getResourceAsStream(
246                         "checkstyle-noviolations.xml")), exporter);
247 
248             checkDTDPresent(exporter.getDocument());
249             checkSummary(exporter.getDocument(), 0, 0, 0);
250             checkFileCount(exporter.getDocument(), 0, 0);
251         }
252         catch (IllegalAccessException e) {
253             fail(e.toString());
254         }
255         catch (ClassNotFoundException e) {
256             fail(e.toString());
257         }
258         catch (InstantiationException e) {
259             fail(e.toString());
260         }
261     }
262 
263     /**
264      * test simple merge with one checkstyle violation and empty qalab.
265      */
266     public void testMergeSingleCheckstyleViolationAndEmptyQALab() {
267         try {
268             Properties properties = new Properties();
269 
270             properties.setProperty("qalab.merge.output.resourcename",
271                 "empty-stats.xml");
272             properties.setProperty("qalab.merge.output.classname",
273                 "net.objectlab.qalab.parser.CheckstyleStatMergeTest");
274 
275             QALabXMLExporter exporter = new QALabXMLExporter();
276 
277             exporter.setQuiet(true);
278             exporter.setTaskLogger(new SilentLogger());
279 
280             StatMerger merger = (StatMerger) Class.forName(
281                     "net.objectlab.qalab.parser.CheckstyleStatMerge")
282                                                   .newInstance();
283 
284             merger.setQuiet(false);
285             merger.setSrcDir("src/main/java");
286             merger.setTaskLogger(new SilentLogger());
287 
288             properties.setProperty("qalab.merge.output.timestamp",
289                     merger.getMergerTimeStamp());
290             properties.setProperty("qalab.merge.type",
291                     merger.getType());
292             exporter.configure(properties);
293 
294             merger.mergeStats(new InputSource(
295                     CheckstyleStatMergeTest.class.getResourceAsStream(
296                         "checkstyle-one-violation.xml")), exporter);
297 
298             checkDTDPresent(exporter.getDocument());
299             checkSummary(exporter.getDocument(), 1, 1, 25);
300             checkFileCount(exporter.getDocument(), 1, 1);
301         }
302         catch (IllegalAccessException e) {
303             fail(e.toString());
304         }
305         catch (ClassNotFoundException e) {
306             fail(e.toString());
307         }
308         catch (InstantiationException e) {
309             fail(e.toString());
310         }
311     }
312 
313     /**
314      * test simple merge with empty checkstyle and NON-empty qalab.
315      */
316     public void testMergeEmptyCheckstyleAndNonEmptyQALab() {
317         try {
318             Properties properties = new Properties();
319 
320             properties.setProperty("qalab.merge.output.resourcename",
321                 "one-set-checkstyle-stats.xml");
322             properties.setProperty("qalab.merge.output.classname",
323                 "net.objectlab.qalab.parser.CheckstyleStatMergeTest");
324 
325             QALabXMLExporter exporter = new QALabXMLExporter();
326 
327             exporter.setQuiet(true);
328             exporter.setTaskLogger(new SilentLogger());
329 
330             StatMerger merger = (StatMerger) Class.forName(
331                     "net.objectlab.qalab.parser.CheckstyleStatMerge")
332                                                   .newInstance();
333 
334             merger.setQuiet(false);
335             merger.setSrcDir("src/main/java");
336             merger.setTaskLogger(new SilentLogger());
337 
338             properties.setProperty("qalab.merge.output.timestamp",
339                     merger.getMergerTimeStamp());
340             properties.setProperty("qalab.merge.type",
341                     merger.getType());
342             exporter.configure(properties);
343 
344             merger.mergeStats(new InputSource(
345                     CheckstyleStatMergeTest.class.getResourceAsStream(
346                         "checkstyle-noviolations.xml")), exporter);
347 
348             checkDTDPresent(exporter.getDocument());
349             checkSummary(exporter.getDocument(), 1, new int[] { 168 },
350                 new int[] { 25 },
351                 new String[] { "checkstyle" });
352             checkFileCount(exporter.getDocument(), 6, 168);
353         }
354         catch (IllegalAccessException e) {
355             fail(e.toString());
356         }
357         catch (ClassNotFoundException e) {
358             fail(e.toString());
359         }
360         catch (InstantiationException e) {
361             fail(e.toString());
362         }
363     }
364 
365     /**
366      * test simple merge with 1 set checkstyle
367      */
368     public void testMergeOneSetCheckstyle() {
369         try {
370             Properties properties = new Properties();
371 
372             properties.setProperty("qalab.merge.output.resourcename",
373                 "one-set-checkstyle-stats.xml");
374             properties.setProperty("qalab.merge.output.classname",
375                 "net.objectlab.qalab.parser.CheckstyleStatMergeTest");
376 
377             QALabXMLExporter exporter = new QALabXMLExporter();
378 
379             exporter.setQuiet(false);
380             exporter.setTaskLogger(new ConsoleLogger());
381 
382             StatMerger merger = (StatMerger) Class.forName(
383                     "net.objectlab.qalab.parser.CheckstyleStatMerge")
384                                                   .newInstance();
385 
386             merger.setQuiet(false);
387             merger.setSrcDir("src/main/java");
388             merger.setTaskLogger(new SilentLogger());
389 
390             properties.setProperty("qalab.merge.output.timestamp",
391                     merger.getMergerTimeStamp());
392             properties.setProperty("qalab.merge.type",
393                     merger.getType());
394             exporter.configure(properties);
395 
396             merger.mergeStats(new InputSource(
397                     CheckstyleStatMergeTest.class.getResourceAsStream(
398                         "good-checkstyle.xml")), exporter);
399 
400             System.out.println(streamToString(exporter.getDocument()));
401 
402             checkDTDPresent(exporter.getDocument());
403             checkSummary(exporter.getDocument(), 2, new int[] { 168, 168 },
404                 new int[] { 25, 25 },
405                 new String[] { "checkstyle", "checkstyle" });
406             checkFileCount(exporter.getDocument(), 6, 336);
407         }
408         catch (IllegalAccessException e) {
409             fail(e.toString());
410         }
411         catch (ClassNotFoundException e) {
412             fail(e.toString());
413         }
414         catch (InstantiationException e) {
415             fail(e.toString());
416         }
417         catch (IOException e) {
418             fail(e.toString());
419         }
420     }
421 
422     /**
423      * test simple merge with 1 set checkstyle with replace!
424      */
425     public void testMergeOneSetCheckstyleWithReplace() {
426         try {
427             Properties properties = new Properties();
428 
429             properties.setProperty("qalab.merge.output.resourcename",
430                 "one-set-checkstyle-stats.xml");
431             properties.setProperty("qalab.merge.output.classname",
432                 "net.objectlab.qalab.parser.CheckstyleStatMergeTest");
433             properties.setProperty("qalab.merge.action","replace");
434             properties.setProperty("qalab.merge.timestampdateonly","true");
435 
436             QALabXMLExporter exporter = new QALabXMLExporter();
437 
438             exporter.setQuiet(false);
439             exporter.setTaskLogger(new ConsoleLogger());
440 
441             StatMerger merger = (StatMerger) Class.forName(
442                     "net.objectlab.qalab.parser.CheckstyleStatMerge")
443                                                   .newInstance();
444 
445             merger.setQuiet(false);
446             merger.setSrcDir("src/main/java");
447             merger.setTaskLogger(new SilentLogger());
448             
449             // by chosing this date and having replace, we will replace
450             // the value in the summary! 
451             merger.setMergerTimeStamp("2004-06-09 00:00:00",true);
452 
453             properties.setProperty("qalab.merge.output.timestamp",
454                     merger.getMergerTimeStamp());
455             properties.setProperty("qalab.merge.type",
456                     merger.getType());
457             exporter.configure(properties);
458 
459             merger.mergeStats(new InputSource(
460                     CheckstyleStatMergeTest.class.getResourceAsStream(
461                         "good-checkstyle-forreplace.xml")), exporter);
462 
463             System.out.println(streamToString(exporter.getDocument()));
464 
465             checkDTDPresent(exporter.getDocument());
466             checkSummary(exporter.getDocument(), 1, new int[] { 166 },
467                 new int[] { 24 },
468                 new String[] { "checkstyle" });
469             checkFileCount(exporter.getDocument(), 6, 166);
470         }
471         catch (IllegalAccessException e) {
472             fail(e.toString());
473         }
474         catch (ClassNotFoundException e) {
475             fail(e.toString());
476         }
477         catch (InstantiationException e) {
478             fail(e.toString());
479         }
480         catch (IOException e) {
481             fail(e.toString());
482         }
483     }
484 
485     /**
486      * test simple merge with 1 set checkstyle giving a timestamp.
487      */
488     public void testMergeOneSetCheckstyleWithTimestamp() {
489         try {
490             Properties properties = new Properties();
491 
492             properties.setProperty("qalab.merge.output.resourcename",
493                 "one-set-checkstyle-stats.xml");
494             properties.setProperty("qalab.merge.output.classname",
495                 "net.objectlab.qalab.parser.CheckstyleStatMergeTest");
496 
497             QALabXMLExporter exporter = new QALabXMLExporter();
498 
499             exporter.setQuiet(true);
500             exporter.setTaskLogger(new SilentLogger());
501 
502             StatMerger merger = (StatMerger) Class.forName(
503                     "net.objectlab.qalab.parser.CheckstyleStatMerge")
504                                                   .newInstance();
505 
506             merger.setQuiet(false);
507             merger.setSrcDir("src/main/java");
508             merger.setTaskLogger(new SilentLogger());
509             try {
510                 merger.setMergerTimeStamp("incorrect stuff", true);
511                 fail("Incorrect time stamp was given");
512             } catch(IllegalArgumentException e) {
513                 assertNotNull(e);
514             }
515             
516             merger.setMergerTimeStamp("2005-07-08 15:45:30", false);
517 
518             properties.setProperty("qalab.merge.output.timestamp",
519                     merger.getMergerTimeStamp());
520             properties.setProperty("qalab.merge.type",
521                     merger.getType());
522             exporter.configure(properties);
523 
524             merger.mergeStats(new InputSource(
525                     CheckstyleStatMergeTest.class.getResourceAsStream(
526                         "good-checkstyle.xml")), exporter);
527 
528             checkDTDPresent(exporter.getDocument());
529             checkSummary(exporter.getDocument(), 2, new int[] { 168, 168 },
530                 new int[] { 25, 25 },
531                 new String[] { "checkstyle", "checkstyle" });
532             checkFileCount(exporter.getDocument(), 6, 336);
533         }
534         catch (IllegalAccessException e) {
535             fail(e.toString());
536         }
537         catch (ClassNotFoundException e) {
538             fail(e.toString());
539         }
540         catch (InstantiationException e) {
541             fail(e.toString());
542         }
543     }
544 }
545 /*
546  *                   ObjectLab is sponsoring QALab
547  * 
548  * Based in London, we are world leaders in the design and development 
549  * of bespoke applications for the securities financing markets.
550  * 
551  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
552  *           ___  _     _           _   _          _
553  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
554  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
555  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
556  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
557  *                   |__/
558  *
559  *                     www.ObjectLab.co.uk
560  */