KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > xs > junit > NISTTest


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

17 package org.apache.ws.jaxme.xs.junit;
18
19 import java.net.URL JavaDoc;
20
21 import javax.xml.parsers.DocumentBuilder JavaDoc;
22 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
23 import javax.xml.parsers.ParserConfigurationException JavaDoc;
24
25 import org.apache.ws.jaxme.xs.XSParser;
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.Element JavaDoc;
28 import org.w3c.dom.NodeList JavaDoc;
29 import org.xml.sax.InputSource JavaDoc;
30
31 import junit.framework.TestCase;
32
33 /** <p>A test case running the NIST test suite. To invoke
34  * the test suite, set the system property
35  * <code>NISTXMLSchemaTestSuite.location</code>.</p>
36  * <p>The NIST test suite is delivered as a ZIP file
37  * <a HREF="http://xw2k.sdct.itl.nist.gov/brady/schema/NISTSchemaTests.zip">NISTSchemaTests.zip</a>.
38  * For example, if you have extracted this file into
39  * <code>c:\jwi\Workspace\ws-jaxme\Nist</code>, then you
40  * should set the property to
41  * <code>file:///c:/jwi/Workspace/ws-jaxme/Nist/</code>.
42  * (Note the trailing slash!)</p>
43  *
44  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
45  */

46 public class NISTTest extends TestCase {
47     private int numOk;
48     private int numFailed;
49     private boolean verbose;
50
51     public NISTTest(String JavaDoc pName) {
52         super(pName);
53     }
54
55     public void setUp() {
56         numOk = numFailed = 0;
57         verbose = Boolean.valueOf(System.getProperty("verbose")).booleanValue();
58     }
59
60     protected void log(String JavaDoc pMessage) {
61         if (verbose) {
62             System.out.println(pMessage);
63         }
64     }
65
66     public DocumentBuilder JavaDoc getDocumentBuilder() throws ParserConfigurationException JavaDoc {
67         DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
68         dbf.setValidating(false);
69         dbf.setNamespaceAware(true);
70         return dbf.newDocumentBuilder();
71     }
72
73     protected void runTest(URL JavaDoc pBaseURL, String JavaDoc pName, String JavaDoc pHref) throws Exception JavaDoc {
74         URL JavaDoc url = new URL JavaDoc(pBaseURL, pHref);
75         XSParser parser = new XSParser();
76         parser.setValidating(false);
77         InputSource JavaDoc isource = new InputSource JavaDoc(url.openStream());
78         isource.setSystemId(url.toString());
79         String JavaDoc result;
80         try {
81             parser.parse(isource);
82             ++numOk;
83             result = "Ok";
84         } catch (Exception JavaDoc e) {
85             ++numFailed;
86             result = e.getMessage();
87         }
88         log("Running test " + pName + " with URL " + url+ ": " + result);
89     }
90
91     protected void runTests(URL JavaDoc pBaseURL, String JavaDoc pName, String JavaDoc pHref) throws Exception JavaDoc {
92         URL JavaDoc url = new URL JavaDoc(pBaseURL, pHref);
93         InputSource JavaDoc isource = new InputSource JavaDoc(url.openStream());
94         isource.setSystemId(url.toString());
95         Document JavaDoc document = getDocumentBuilder().parse(isource);
96         NodeList JavaDoc schemas = document.getElementsByTagNameNS(null, "Schema");
97         for (int i = 0; i < schemas.getLength(); i++) {
98             Element JavaDoc schema = (Element JavaDoc) schemas.item(i);
99             runTest(url, schema.getAttribute("name"), schema.getAttribute("href"));
100         }
101     }
102
103     public void testNIST() throws Exception JavaDoc {
104         String JavaDoc p = "NISTXMLSchemaTestSuite.location";
105         String JavaDoc v = System.getProperty(p);
106         if (v == null || v.length() == 0) {
107             System.out.println("System property " + p + " is not set, skipping this test.");
108             return;
109         }
110
111         URL JavaDoc url = new URL JavaDoc(v);
112         url = new URL JavaDoc(url, "NISTXMLSchemaTestSuite.xml");
113         InputSource JavaDoc isource = new InputSource JavaDoc(url.openStream());
114         isource.setSystemId(url.toString());
115         Document JavaDoc document = getDocumentBuilder().parse(isource);
116         NodeList JavaDoc links = document.getElementsByTagNameNS(null, "Link");
117         for (int i = 0; i < links.getLength(); i++) {
118             Element JavaDoc link = (Element JavaDoc) links.item(i);
119             runTests(url, link.getAttribute("name"), link.getAttribute("href"));
120         }
121         System.out.println("Result: Passed = " + numOk + ", Failed = " + numFailed);
122     }
123 }
124
Popular Tags