KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > test > xml > XMLTestSuiteRunnerValidator


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.test.xml;
19
20 import java.io.File JavaDoc;
21
22 import java.net.URL JavaDoc;
23
24 import java.util.HashSet JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
29 import javax.xml.parsers.DocumentBuilder JavaDoc;
30
31 import org.w3c.dom.Document JavaDoc;
32
33 import org.apache.batik.test.DefaultTestReport;
34 import org.apache.batik.test.DefaultTestSuite;
35 import org.apache.batik.test.AbstractTest;
36 import org.apache.batik.test.TestReport;
37 import org.apache.batik.test.TestSuiteReport;
38
39 /**
40  * Validates the operation of the <tt>XMLTestSuireRunner</tt> by checking
41  * that it runs the tests specified by the command line parameter and
42  * only these tests. <br />
43  * The test uses an dummy &lt;testRun&gt; which contains: <br />
44  * - testRun, id="all"
45  * - testSuite, HREF="testSuiteA.xml" <br />
46  * - testSuite, HREF="testSuiteB.xml" <br />
47  * Where: <br />
48  * - testSuite, id="A" <br />
49  * - test, id="A1" <br />
50  * - test, id="A2" <br />
51  * - test, id="duplicateId" <br />
52  * - testGroup, id="AG" <br />
53  * - test, id="AG1" <br />
54  * - test, id="AG2" <br />
55  *
56  * and: <br />
57  * - testSuite, id="B" <br />
58  * - test, id="B1" <br />
59  * - test, id="B2" <br />
60  * - test, id="B3" <br />
61  * - test, id="duplicateId" <br />
62  *
63  * where all the leaf test pass. <br />
64  *
65  * @author <a HREF="mailto:vhardy@apache.org">Vincent Hardy</a>
66  * @version $Id: XMLTestSuiteRunnerValidator.java,v 1.7 2004/08/18 07:17:08 vhardy Exp $
67  */

68 public class XMLTestSuiteRunnerValidator extends DefaultTestSuite {
69     public static final String JavaDoc ERROR_TEST_NOT_RUN
70         = "error.test.not.run";
71
72     public static final String JavaDoc ERROR_EXTRA_TEST_RUN
73         = "error.extra.test.run";
74
75     public static final String JavaDoc ENTRY_KEY_CONFIGURATION
76         = "entry.key.configuration";
77
78     public static final String JavaDoc ENTRY_KEY_EXPECTED_RESULT
79         = "entry.key.expected.result";
80
81     public static final String JavaDoc ENTRY_KEY_ACTUAL_RESULT
82         = "entry.key.actual.result";
83
84     public static final String JavaDoc ENTRY_KEY_TEST_IDS_NOT_RUN
85         = "entry.key.test.ids.not.run";
86
87     public static final String JavaDoc ENTRY_KEY_TEST_ID_NOT_EXPECTED
88         = "entry.key.test.id.not.expected";
89
90     /**
91      * Dummy test-suite used to run the test
92      */

93     static final String JavaDoc dummyTestRun =
94         "test-resources/org/apache/batik/test/xml/dummyTestRun.xml";
95
96     /**
97      * This suite is made of elementary tests which validate that
98      * the XML result for a given input contains a list of
99      * report ids and no more (i.e., that the expected test reports
100      * were generated and no more). <br />
101      * Specificaly, with fully qualified ids: <br />
102      * - no arguments. All tests-reports should be produced. <br />
103      * - 1 target test: "all.B.B3". A single test-report should be produced
104      * for B3. <br />
105      * - 1 target test-suite: "all.A". A test-report with
106      * "A1", "A2", "duplicatedId", "AG", "AG.AG1" and "AG.AG2" should be produced.<br />
107      * - 1 target test-suite and 2 tests: "all.B and
108      * all.A.A1 and all.A.A2. A test-report for "all.B.B1", "all.B.B2",
109      * "all.B.B3", "all.B.duplicatedId", "all.A.A1" and "all.A.A2"
110      * should be produced. <br />
111      * - 1 target testGroup: "AG". A test-report with
112      * "A.AG", "A.AG.AG1" and "A.AG.AG2" should be produced.<br />
113      * <br />
114      * In addition, the following test with non-qualified ids: <br />
115      * - 1 target test id: "duplicatedId" should be produced and
116      * pass for "all.A.duplicatedId" and "all.B.duplicatedId".
117      */

118     public XMLTestSuiteRunnerValidator() {
119         Object JavaDoc config[][] = {
120             {"", new String JavaDoc[]{"all",
121                               "all.A", "all.A.A1", "all.A.A2", "all.A.duplicatedId", "all.A.duplicatedId.1", "all.A.duplicatedId.2",
122                               "all.A.AG", "all.A.AG.AG1", "all.A.AG.AG2",
123                               "all.B", "all.B.B1", "all.B.B2", "all.B.B3", "all.B.duplicatedId"}},
124
125             {"all.B.B3", new String JavaDoc[] {"all", "all.B", "all.B.B3"}},
126
127             {"all.A", new String JavaDoc[] {"all",
128                                     "all.A", "all.A.A1", "all.A.A2", "all.A.duplicatedId", "all.A.duplicatedId.1", "all.A.duplicatedId.2",
129                                     "all.A.AG", "all.A.AG.AG1", "all.A.AG.AG2"}},
130
131             {"all.B all.A.A1 all.A.A2",
132              new String JavaDoc[] {"all",
133                            "all.B", "all.B.B1", "all.B.B2", "all.B.B3", "all.B.duplicatedId",
134                            "all.A", "all.A.A1", "all.A.A2"}},
135
136             {"duplicatedId",
137              new String JavaDoc[] {"all",
138                            "all.A", "all.A.duplicatedId", "all.A.duplicatedId.1", "all.A.duplicatedId.2",
139                            "all.B", "all.B.duplicatedId"}},
140
141             {"AG",
142              new String JavaDoc[] {"all",
143                            "all.A",
144                            "all.A.AG",
145                            "all.A.AG.AG1", "all.A.AG.AG2"}}
146         };
147
148         for(int i=0; i<config.length; i++){
149             addTest(new XMLTestSuiteRunnerTest(config[i]));
150         }
151
152     }
153     
154     static class XMLTestSuiteRunnerTest extends AbstractTest {
155         /**
156          * Argument to feed into the XMLTestSuiteRunner
157          */

158         protected String JavaDoc[] args;
159
160         /**
161          * Expected ids in the report generated by the
162          * XMLTestSuiteRunner
163          */

164         protected HashSet JavaDoc ids;
165         protected String JavaDoc[] idsArray;
166
167         /**
168          * @param config an array of two objects: a String containing
169          * the "config" to pass to the XMLTestSuiteRunner and
170          * an array of Strings containing the expected ids
171          * from the XMLTestSuiteRunner operation.
172          */

173         public XMLTestSuiteRunnerTest(Object JavaDoc config[]){
174             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc((String JavaDoc)config[0], " ");
175             int nArgs = st.countTokens();
176             args = new String JavaDoc[nArgs];
177             for(int i=0; i<nArgs; i++){
178                 args[i] = st.nextToken();
179             }
180
181             ids = new HashSet JavaDoc();
182             for(int i=0; i<(config[1] != null? ((Object JavaDoc[])config[1]).length:0); i++){
183                 ids.add(((Object JavaDoc[])config[1])[i]);
184             }
185             idsArray = (String JavaDoc[])config[1];
186         }
187
188         /**
189          * <tt>AbstractTest</tt>'s template method implementation.
190          */

191         public TestReport runImpl() throws Exception JavaDoc{
192             //
193
// Load the template dummy testRun file.
194
//
195
Document JavaDoc doc = loadDummyTestRun();
196
197             //
198
// Now run the test.
199
//
200
XMLTestSuiteRunner runner
201                 = new XMLTestSuiteRunner();
202
203             TestReport runReport
204                 = runner.run(doc, args);
205
206             //
207
// Analyse TestReport
208
//
209
Set JavaDoc idSet = (Set JavaDoc)(ids.clone());
210             String JavaDoc idNotExpected = checkTestReport(runReport, idSet);
211
212             if(idNotExpected == null){
213                 if(idSet.isEmpty()){
214                     return reportSuccess();
215                 }
216                 else{
217                     DefaultTestReport report = new DefaultTestReport(this);
218                     report.setErrorCode(ERROR_TEST_NOT_RUN);
219                     report.addDescriptionEntry(ENTRY_KEY_CONFIGURATION,
220                                                arrayToString(args));
221                     report.addDescriptionEntry(ENTRY_KEY_EXPECTED_RESULT,
222                                                arrayToString(idsArray));
223                     report.addDescriptionEntry(ENTRY_KEY_ACTUAL_RESULT,
224                                                reportIdsToString(runReport));
225                     report.addDescriptionEntry(ENTRY_KEY_TEST_IDS_NOT_RUN,
226                                                arrayToString(idSet.toArray()));
227                 report.setPassed(false);
228                 return report;
229                 }
230             }
231             else{
232                 DefaultTestReport report = new DefaultTestReport(this);
233                 report.setErrorCode(ERROR_EXTRA_TEST_RUN);
234                 report.addDescriptionEntry(ENTRY_KEY_CONFIGURATION,
235                                            arrayToString(args));
236                 report.addDescriptionEntry(ENTRY_KEY_EXPECTED_RESULT,
237                                            arrayToString(idsArray));
238                 report.addDescriptionEntry(ENTRY_KEY_ACTUAL_RESULT,
239                                           reportIdsToString(runReport));
240                 report.addDescriptionEntry(ENTRY_KEY_TEST_ID_NOT_EXPECTED,
241                                            idNotExpected);
242                 report.setPassed(false);
243                 return report;
244             }
245         }
246
247         protected String JavaDoc arrayToString(Object JavaDoc[] array){
248             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
249             if(array != null){
250                 if(array.length > 0){
251                     sb.append(array[0]);
252                 }
253                 for(int i=1; i<array.length; i++){
254                     sb.append(", ");
255                     sb.append(array[i].toString());
256                 }
257             }
258             return sb.toString();
259         }
260
261         protected String JavaDoc reportIdsToString(TestReport r){
262             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
263             if(r != null){
264                 sb.append(r.getTest().getQualifiedId());
265                 if(r instanceof TestSuiteReport){
266                     TestReport[] c = ((TestSuiteReport)r).getChildrenReports();
267                     if(c != null){
268                         for(int i=0; i<c.length; i++){
269                             appendReportIds(c[i], sb);
270                         }
271                     }
272                 }
273             }
274             else{
275                 sb.append("null");
276             }
277
278             return sb.toString();
279         }
280
281         protected void appendReportIds(TestReport r, StringBuffer JavaDoc sb){
282             if(r != null){
283                 sb.append(", ");
284                 sb.append(r.getTest().getQualifiedId());
285
286                 if(r instanceof TestSuiteReport){
287                     TestReport[] c = ((TestSuiteReport)r).getChildrenReports();
288                     if(c != null){
289                         for(int i=0; i<c.length; i++){
290                             appendReportIds(c[i], sb);
291                         }
292                     }
293                 }
294             }
295         }
296            
297         /**
298          * Loads the dummy testRun description
299          */

300         protected Document JavaDoc loadDummyTestRun() throws Exception JavaDoc{
301             DocumentBuilder JavaDoc docBuilder
302                 = DocumentBuilderFactory.newInstance().newDocumentBuilder();
303
304             URL JavaDoc url = (new File JavaDoc(XMLTestSuiteRunnerValidator.dummyTestRun)).toURL();
305             return docBuilder.parse(url.toString());
306
307         }
308
309         /**
310          * Validates that the input <tt>TestReport</tt>
311          * contains only the expected identifiers.
312          * The following code is by no means optimized,
313          * but it gets the job done.
314          */

315         protected String JavaDoc checkTestReport(TestReport report,
316                                          Set JavaDoc idSet){
317             String JavaDoc id = report.getTest().getQualifiedId();
318             if(!(idSet.contains(id))){
319                 return id;
320             }
321             
322             idSet.remove(id);
323
324             //
325
// Now, process children reports if any.
326
//
327
if(report instanceof TestSuiteReport){
328                 TestReport[] childReports = ((TestSuiteReport)report).getChildrenReports();
329                 if(childReports != null){
330                     for(int i=0; i<childReports.length; i++){
331                         String JavaDoc idNotExpected
332                             = checkTestReport(childReports[i],
333                                               idSet);
334                         if(idNotExpected != null){
335                             return idNotExpected;
336                         }
337                     }
338                 }
339             }
340
341             return null;
342         }
343             
344     }
345
346     
347 }
348
Popular Tags