KickJava   Java API By Example, From Geeks To Geeks.

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


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.StringWriter JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22
23
24 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
25 import javax.xml.parsers.DocumentBuilder JavaDoc;
26
27 import org.apache.batik.test.DefaultTestSuite;
28 import org.apache.batik.test.TestSuite;
29 import org.apache.batik.test.Test;
30 import org.apache.batik.test.TestException;
31
32 import org.w3c.dom.Element JavaDoc;
33 import org.w3c.dom.Document JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35 import org.w3c.dom.NodeList JavaDoc;
36
37 /**
38  * This class loads an XML document describing a test suite
39  * into a <tt>TestSuite</tt> object.
40  *
41  * @author <a HREF="mailto:vhardy@apache.org">Vincent Hardy</a>
42  * @version $Id: XMLTestSuiteLoader.java,v 1.9 2004/08/18 07:17:08 vhardy Exp $
43  */

44 public class XMLTestSuiteLoader implements XTSConstants {
45
46     /**
47      * An error happened while loading a test suite document.
48      * {0} : the &lt;testSuite&gt; href value.
49      * {1} : the exception's class name
50      * {2} : exception's message
51      * {3} : exception's stack trace
52      */

53     public static final String JavaDoc TEST_SUITE_LOADING_EXCEPTION
54         = "xml.XMLTestSuiteLoader.error.test.suite.loading.exception";
55
56     /**
57      * An error happened while processing a <tt>Test</tt>
58      * description.
59      * {0} : the <test> "className" attribute value
60      * {1} : exception's class name
61      * {2} : exception's message
62      * {3} : exception's stack trace
63      */

64     public static final String JavaDoc CANNOT_CREATE_TEST
65         = "xml.XMLTestSuiteLoader.error.cannot.create.test";
66
67     /**
68      * Load the test suite defined by the input URI
69      */

70     public static TestSuite loadTestSuite(String JavaDoc testSuiteURI,
71                                           TestSuite parent)
72         throws TestException{
73         // System.out.println("loading test suite: " + testSuiteURI);
74
Document JavaDoc testSuiteDocument = loadTestSuiteDocument(testSuiteURI);
75         return buildTestSuite(testSuiteDocument.getDocumentElement(), parent);
76     }
77
78     /**
79      * Loads the URI as a <tt>Document</tt>
80      */

81     protected static Document JavaDoc loadTestSuiteDocument(String JavaDoc testSuiteURI)
82         throws TestException{
83
84         Document JavaDoc doc = null;
85
86         try{
87             DocumentBuilder JavaDoc docBuilder
88                 = DocumentBuilderFactory.newInstance().newDocumentBuilder();
89
90             doc = docBuilder.parse(testSuiteURI);
91         }catch(Exception JavaDoc e){
92             StringWriter JavaDoc sw = new StringWriter JavaDoc();
93             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
94             e.printStackTrace(pw);
95             throw new TestException(TEST_SUITE_LOADING_EXCEPTION,
96                                     new Object JavaDoc[] { testSuiteURI,
97                                                    e.getClass().getName(),
98                                                    e.getMessage(),
99                                                    sw.toString() },
100                                     e);
101
102         }
103
104         return doc;
105     }
106
107     /**
108      * Builds a <tt>TestSuite</tt> from an input element.
109      * This method assumes that element is a &lt;testSuite&gt;
110      * instance, as the input document should have been
111      * validated when loaded.
112      */

113     protected static TestSuite buildTestSuite(Element JavaDoc element,
114                                               TestSuite parent)
115         throws TestException {
116         DefaultTestSuite testSuite
117             = new DefaultTestSuite();
118
119         /* FIXX: Not used -- should we call testSuite.setName(suiteName)??? */
120         // String suiteName
121
// = element.getAttribute(XTS_NAME_ATTRIBUTE);
122

123         String JavaDoc suiteId
124             = element.getAttribute(XTS_ID_ATTRIBUTE);
125
126         testSuite.setId(suiteId);
127
128         NodeList JavaDoc children = element.getChildNodes();
129         if(children != null && children.getLength() > 0){
130             int n = children.getLength();
131             for(int i=0; i<n; i++){
132                 Node JavaDoc child = children.item(i);
133                 if(child.getNodeType() == Node.ELEMENT_NODE){
134                     Element JavaDoc childElement = (Element JavaDoc)child;
135                     String JavaDoc tagName = childElement.getTagName().intern();
136                     // System.out.println("Processing child : " + tagName);
137
if(tagName == XTS_TEST_TAG){
138                         Test t = buildTest(childElement);
139                         testSuite.addTest(t);
140                     }
141                     else if(tagName == XTS_TEST_GROUP_TAG){
142                         Test t = buildTestSuite(childElement, testSuite);
143                         testSuite.addTest(t);
144                     }
145                 }
146             }
147         }
148
149         return testSuite;
150     }
151
152     protected static Test buildTest(Element JavaDoc element) throws TestException {
153         try{
154             Test t = (Test)XMLReflect.buildObject(element);
155
156             String JavaDoc id
157                 = element.getAttribute(XTS_ID_ATTRIBUTE);
158             t.setId(id);
159             return t;
160         }catch (Exception JavaDoc e) {
161             StringWriter JavaDoc sw = new StringWriter JavaDoc();
162             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
163             e.printStackTrace(pw);
164             throw new TestException(CANNOT_CREATE_TEST,
165                                     new Object JavaDoc[] { element.getAttribute(XR_CLASS_ATTRIBUTE),
166                                                    e.getClass().getName(),
167                                                    e.getMessage(),
168                                                    sw.toString() },
169                                     e);
170         }
171     }
172
173
174 }
175
Popular Tags