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.SilentLogger;
47  
48  import org.xml.sax.InputSource;
49  
50  import java.io.File;
51  import java.io.IOException;
52  
53  import java.util.Properties;
54  
55  public class SimianStatMergeTest extends BaseMerge {
56      /**
57       * Create the test case
58       *
59       * @param testName name of the test case
60       */
61      public SimianStatMergeTest(String testName) {
62          super(testName);
63      }
64  
65      /**
66       * @return the suite of tests being tested
67       */
68      public static Test suite() {
69          return new TestSuite(SimianStatMergeTest.class);
70      }
71  
72      /**
73       * test simple merge from empty
74       */
75      public void testMergeWithConsole() {
76          try {
77              StatMerger merger = (StatMerger) Class.forName(
78                      "net.objectlab.qalab.parser.SimianStatMerge").newInstance();
79  
80              merger.setQuiet(false);
81              merger.setSrcDir("src/main/java");
82              merger.setTaskLogger(new SilentLogger());
83              merger.mergeStats(new InputSource(
84                      SimianStatMergeTest.class.getResourceAsStream(
85                          "good-simian.xml")), new ConsoleExporter());
86          }
87          catch (IllegalAccessException e) {
88              fail(e.toString());
89          }
90          catch (ClassNotFoundException e) {
91              fail(e.toString());
92          }
93          catch (InstantiationException e) {
94              fail(e.toString());
95          }
96      }
97  
98      /**
99       * test simple merge from empty
100      */
101     public void testMerge() {
102         try {
103             Properties properties = new Properties();
104 
105             properties.setProperty("qalab.merge.output.resourcename",
106                 "empty-stats.xml");
107             properties.setProperty("qalab.merge.output.classname",
108                 "net.objectlab.qalab.parser.SimianStatMergeTest");
109 
110             QALabXMLExporter exporter = new QALabXMLExporter();
111 
112             exporter.setQuiet(true);
113             exporter.setTaskLogger(new SilentLogger());
114 
115             StatMerger merger = (StatMerger) Class.forName(
116                     "net.objectlab.qalab.parser.SimianStatMerge").newInstance();
117 
118             merger.setQuiet(false);
119             merger.setSrcDir("src/main/java");
120             merger.setTaskLogger(new SilentLogger());
121 
122             properties.setProperty("qalab.merge.output.timestamp",
123                     merger.getMergerTimeStamp());
124             properties.setProperty("qalab.merge.type",
125                     merger.getType());
126             exporter.configure(properties);
127 
128             merger.mergeStats(new InputSource(
129                     SimianStatMergeTest.class.getResourceAsStream(
130                         "good-simian.xml")), exporter);
131 
132             checkSummary(exporter.getDocument(), 1, 421, 3);
133             checkFileCount(exporter.getDocument(), 0, 0); // no file for Simian
134         }
135         catch (IllegalAccessException e) {
136             fail(e.toString());
137         }
138         catch (ClassNotFoundException e) {
139             fail(e.toString());
140         }
141         catch (InstantiationException e) {
142             fail(e.toString());
143         }
144     }
145 
146     /**
147      * test simple merge from empty
148      */
149     public void testMergeNonExistingQALab() {
150         try {
151             final String fileName = "new-stats-simian.xml";
152             File file = new File(fileName);
153 
154             if (file.exists()) {
155                 assertTrue("Delete File: " + file.getAbsolutePath(),
156                     file.delete());
157             }
158 
159             Properties properties = new Properties();
160 
161             properties.setProperty("qalab.merge.output.file", fileName);
162 
163             QALabXMLExporter exporter = new QALabXMLExporter();
164 
165             exporter.setQuiet(true);
166             exporter.setTaskLogger(new SilentLogger());
167 
168             StatMerger merger = (StatMerger) Class.forName(
169                     "net.objectlab.qalab.parser.SimianStatMerge").newInstance();
170 
171             merger.setQuiet(false);
172             merger.setSrcDir("src/main/java");
173             merger.setTaskLogger(new SilentLogger());
174 
175             properties.setProperty("qalab.merge.output.timestamp",
176                     merger.getMergerTimeStamp());
177             properties.setProperty("qalab.merge.type",
178                     merger.getType());
179             exporter.configure(properties);
180 
181             merger.mergeStats(new InputSource(
182                     SimianStatMergeTest.class.getResourceAsStream(
183                         "good-simian.xml")), exporter);
184 
185             System.out.println(streamToString(exporter.getDocument()));
186             checkDTDPresent(exporter.getDocument());
187             checkSummary(exporter.getDocument(), 1, 421, 3);
188             checkFileCount(exporter.getDocument(), 0, 0);
189 
190             exporter.save();
191 
192             if (file.exists()) {
193                 assertTrue("Delete File at the end: " + file.getAbsolutePath(),
194                     file.delete());
195             }
196         }
197         catch (IllegalAccessException e) {
198             fail(e.toString());
199         }
200         catch (ClassNotFoundException e) {
201             fail(e.toString());
202         }
203         catch (InstantiationException e) {
204             fail(e.toString());
205         }
206         catch (IOException e) {
207             fail(e.toString());
208         }
209     }
210 
211     /**
212      * test simple merge with empty Simian and empty qalab.
213      */
214     public void testMergeEmptySimianAndQALab() {
215         try {
216             Properties properties = new Properties();
217 
218             properties.setProperty("qalab.merge.output.resourcename",
219                 "empty-stats.xml");
220             properties.setProperty("qalab.merge.output.classname",
221                 "net.objectlab.qalab.parser.SimianStatMergeTest");
222 
223             QALabXMLExporter exporter = new QALabXMLExporter();
224 
225             exporter.setQuiet(true);
226             exporter.setTaskLogger(new SilentLogger());
227 
228             StatMerger merger = (StatMerger) Class.forName(
229                     "net.objectlab.qalab.parser.SimianStatMerge").newInstance();
230 
231             merger.setQuiet(false);
232             merger.setSrcDir("src/main/java");
233             merger.setTaskLogger(new SilentLogger());
234 
235             properties.setProperty("qalab.merge.output.timestamp",
236                     merger.getMergerTimeStamp());
237             properties.setProperty("qalab.merge.type",
238                     merger.getType());
239             exporter.configure(properties);
240 
241             merger.mergeStats(new InputSource(
242                     SimianStatMergeTest.class.getResourceAsStream(
243                         "simian-noviolations.xml")), exporter);
244 
245             checkDTDPresent(exporter.getDocument());
246             checkSummary(exporter.getDocument(), 0, 0, 0);
247             checkFileCount(exporter.getDocument(), 0, 0);
248         }
249         catch (IllegalAccessException e) {
250             fail(e.toString());
251         }
252         catch (ClassNotFoundException e) {
253             fail(e.toString());
254         }
255         catch (InstantiationException e) {
256             fail(e.toString());
257         }
258     }
259 
260     /**
261      * test simple merge with empty Simian and NON-empty qalab.
262      */
263     public void testMergeEmptySimianAndNonEmptyQALab() {
264         try {
265             Properties properties = new Properties();
266 
267             properties.setProperty("qalab.merge.output.resourcename",
268                 "one-set-simian-stats.xml");
269             properties.setProperty("qalab.merge.output.classname",
270                 "net.objectlab.qalab.parser.SimianStatMergeTest");
271 
272             QALabXMLExporter exporter = new QALabXMLExporter();
273 
274             exporter.setQuiet(true);
275             exporter.setTaskLogger(new SilentLogger());
276 
277             StatMerger merger = (StatMerger) Class.forName(
278                     "net.objectlab.qalab.parser.SimianStatMerge").newInstance();
279 
280             merger.setQuiet(false);
281             merger.setSrcDir("src/main/java");
282             merger.setTaskLogger(new SilentLogger());
283 
284             properties.setProperty("qalab.merge.output.timestamp",
285                     merger.getMergerTimeStamp());
286             properties.setProperty("qalab.merge.type",
287                     merger.getType());
288             exporter.configure(properties);
289 
290             merger.mergeStats(new InputSource(
291                     SimianStatMergeTest.class.getResourceAsStream(
292                         "simian-noviolations.xml")), exporter);
293 
294             checkDTDPresent(exporter.getDocument());
295             checkSummary(exporter.getDocument(), 1, new int[] { 421 },
296                 new int[] { 3 }, new String[] { "simian" });
297             checkFileCount(exporter.getDocument(), 0, 0);
298         }
299         catch (IllegalAccessException e) {
300             fail(e.toString());
301         }
302         catch (ClassNotFoundException e) {
303             fail(e.toString());
304         }
305         catch (InstantiationException e) {
306             fail(e.toString());
307         }
308     }
309 
310     /**
311      * test simple merge with 1 set Simian
312      */
313     public void testMergeOneSetSimian() {
314         try {
315             Properties properties = new Properties();
316 
317             properties.setProperty("qalab.merge.output.resourcename",
318                 "one-set-simian-stats.xml");
319             properties.setProperty("qalab.merge.output.classname",
320                 "net.objectlab.qalab.parser.SimianStatMergeTest");
321 
322             QALabXMLExporter exporter = new QALabXMLExporter();
323 
324             exporter.setQuiet(true);
325             exporter.setTaskLogger(new SilentLogger());
326 
327             StatMerger merger = (StatMerger) Class.forName(
328                     "net.objectlab.qalab.parser.SimianStatMerge").newInstance();
329 
330             merger.setQuiet(false);
331             merger.setSrcDir("src/main/java");
332             merger.setTaskLogger(new SilentLogger());
333 
334             properties.setProperty("qalab.merge.output.timestamp",
335                     merger.getMergerTimeStamp());
336             properties.setProperty("qalab.merge.type",
337                     merger.getType());
338             exporter.configure(properties);
339 
340             merger.mergeStats(new InputSource(
341                     SimianStatMergeTest.class.getResourceAsStream(
342                         "good-simian.xml")), exporter);
343 
344             System.out.println(streamToString(exporter.getDocument()));
345 
346             checkDTDPresent(exporter.getDocument());
347             checkSummary(exporter.getDocument(), 2, new int[] { 421, 421 },
348                 new int[] { 3, 3 }, new String[] { "simian", "simian" });
349             checkFileCount(exporter.getDocument(), 0, 0);
350         }
351         catch (IllegalAccessException e) {
352             fail(e.toString());
353         }
354         catch (ClassNotFoundException e) {
355             fail(e.toString());
356         }
357         catch (InstantiationException e) {
358             fail(e.toString());
359         }
360         catch (IOException e) {
361             fail(e.toString());
362         }
363     }
364 }
365 /*
366  *                   ObjectLab is sponsoring QALab
367  * 
368  * Based in London, we are world leaders in the design and development 
369  * of bespoke applications for the securities financing markets.
370  * 
371  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
372  *           ___  _     _           _   _          _
373  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
374  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
375  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
376  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
377  *                   |__/
378  *
379  *                     www.ObjectLab.co.uk
380  */