001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.test.xml;
020:
021: import java.io.StringWriter;
022: import java.io.PrintWriter;
023:
024: import javax.xml.parsers.DocumentBuilderFactory;
025: import javax.xml.parsers.DocumentBuilder;
026:
027: import org.apache.batik.test.DefaultTestSuite;
028: import org.apache.batik.test.TestSuite;
029: import org.apache.batik.test.Test;
030: import org.apache.batik.test.TestException;
031:
032: import org.w3c.dom.Element;
033: import org.w3c.dom.Document;
034: import org.w3c.dom.Node;
035: import org.w3c.dom.NodeList;
036:
037: /**
038: * This class loads an XML document describing a test suite
039: * into a <tt>TestSuite</tt> object.
040: *
041: * @author <a href="mailto:vhardy@apache.org">Vincent Hardy</a>
042: * @version $Id: XMLTestSuiteLoader.java 475477 2006-11-15 22:44:28Z cam $
043: */
044: public class XMLTestSuiteLoader implements XTSConstants {
045:
046: /**
047: * An error happened while loading a test suite document.
048: * {0} : the <testSuite> href value.
049: * {1} : the exception's class name
050: * {2} : exception's message
051: * {3} : exception's stack trace
052: */
053: public static final String TEST_SUITE_LOADING_EXCEPTION = "xml.XMLTestSuiteLoader.error.test.suite.loading.exception";
054:
055: /**
056: * An error happened while processing a <tt>Test</tt>
057: * description.
058: * {0} : the <test> "className" attribute value
059: * {1} : exception's class name
060: * {2} : exception's message
061: * {3} : exception's stack trace
062: */
063: public static final String CANNOT_CREATE_TEST = "xml.XMLTestSuiteLoader.error.cannot.create.test";
064:
065: /**
066: * Load the test suite defined by the input URI
067: */
068: public static TestSuite loadTestSuite(String testSuiteURI,
069: TestSuite parent) throws TestException {
070: // System.out.println("loading test suite: " + testSuiteURI);
071: Document testSuiteDocument = loadTestSuiteDocument(testSuiteURI);
072: return buildTestSuite(testSuiteDocument.getDocumentElement(),
073: parent);
074: }
075:
076: /**
077: * Loads the URI as a <tt>Document</tt>
078: */
079: protected static Document loadTestSuiteDocument(String testSuiteURI)
080: throws TestException {
081:
082: Document doc = null;
083:
084: try {
085: DocumentBuilder docBuilder = DocumentBuilderFactory
086: .newInstance().newDocumentBuilder();
087:
088: doc = docBuilder.parse(testSuiteURI);
089: } catch (Exception e) {
090: StringWriter sw = new StringWriter();
091: PrintWriter pw = new PrintWriter(sw);
092: e.printStackTrace(pw);
093: throw new TestException(TEST_SUITE_LOADING_EXCEPTION,
094: new Object[] { testSuiteURI,
095: e.getClass().getName(), e.getMessage(),
096: sw.toString() }, e);
097:
098: }
099:
100: return doc;
101: }
102:
103: /**
104: * Builds a <tt>TestSuite</tt> from an input element.
105: * This method assumes that element is a <testSuite>
106: * instance, as the input document should have been
107: * validated when loaded.
108: */
109: protected static TestSuite buildTestSuite(Element element,
110: TestSuite parent) throws TestException {
111: DefaultTestSuite testSuite = new DefaultTestSuite();
112:
113: /* FIXX: Not used -- should we call testSuite.setName(suiteName)??? */
114: // String suiteName
115: // = element.getAttribute(XTS_NAME_ATTRIBUTE);
116: String suiteId = element.getAttribute(XTS_ID_ATTRIBUTE);
117:
118: testSuite.setId(suiteId);
119:
120: NodeList children = element.getChildNodes();
121: if (children != null && children.getLength() > 0) {
122: int n = children.getLength();
123: for (int i = 0; i < n; i++) {
124: Node child = children.item(i);
125: if (child.getNodeType() == Node.ELEMENT_NODE) {
126: Element childElement = (Element) child;
127: String tagName = childElement.getTagName().intern();
128: // System.out.println("Processing child : " + tagName);
129: if (tagName == XTS_TEST_TAG) {
130: Test t = buildTest(childElement);
131: testSuite.addTest(t);
132: } else if (tagName == XTS_TEST_GROUP_TAG) {
133: Test t = buildTestSuite(childElement, testSuite);
134: testSuite.addTest(t);
135: }
136: }
137: }
138: }
139:
140: return testSuite;
141: }
142:
143: protected static Test buildTest(Element element)
144: throws TestException {
145: try {
146: Test t = (Test) XMLReflect.buildObject(element);
147:
148: String id = element.getAttribute(XTS_ID_ATTRIBUTE);
149: t.setId(id);
150: return t;
151: } catch (Exception e) {
152: StringWriter sw = new StringWriter();
153: PrintWriter pw = new PrintWriter(sw);
154: e.printStackTrace(pw);
155: throw new TestException(CANNOT_CREATE_TEST, new Object[] {
156: element.getAttribute(XR_CLASS_ATTRIBUTE),
157: e.getClass().getName(), e.getMessage(),
158: sw.toString() }, e);
159: }
160: }
161:
162: }
|