View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package tests.w3c;
20  
21  import junit.framework.Test;
22  import junit.framework.TestSuite;
23  import org.w3c.dom.Document;
24  import org.w3c.dom.Element;
25  import org.w3c.dom.Node;
26  import org.w3c.dom.NodeList;
27  import org.xml.sax.InputSource;
28  import org.xml.sax.SAXException;
29  
30  import javax.xml.parsers.DocumentBuilderFactory;
31  import javax.xml.parsers.ParserConfigurationException;
32  import java.io.File;
33  import java.io.IOException;
34  import java.util.ArrayList;
35  import java.util.List;
36  import java.util.ListIterator;
37  
38  
39  /**
40   * Class to represent a set of schema tests as described by a .testSet file
41   * When executed each of the schemas described in the .testSet file is round-trip
42   * tested.   
43   * cmd line parms: arg0 - location of the .testSet file. Defaults to:
44   *                        ./target/xmlschema2002-01-16/NISTXMLSchema1-0-20020116.testSet
45   *
46   */
47  public class TestW3CSchemaTestSet extends TestSuite {
48  
49      private List schemaTests = null;
50      
51      private File testSetFile = null;
52      
53      // If junit called from cmd line without any args, use the NIST test bucket
54      private static String testSetLocation = "./target/xmlschema2002-01-16/NISTXMLSchema1-0-20020116.testSet";
55  
56      private TestW3CSchemaTestSet(String name, File testSetFile) {
57          super(name);
58          this.testSetFile = testSetFile;
59      }
60      
61      public static void main(String[] args) {
62          try {
63              if (args[0] != null) {
64                  testSetLocation = args[0]; 
65              }
66              junit.textui.TestRunner.run(TestW3CSchemaTestSet.suite(new File(testSetLocation)));
67          } catch (Exception e) {
68              e.printStackTrace();
69          }
70      }
71  
72      public static Test suite() throws Exception {
73          testSetLocation =  System.getProperty("W3CTestLocation", testSetLocation);
74          return suite(new File(testSetLocation));
75      }
76      
77      /**
78       * Returns a suite of TestRoundTripXSD test case instances. One for each of the tests
79       * described in the testSetFile
80       * @param testSetFile the File object of the .testSet file 
81       * @throws Exception
82       */
83      public static Test suite(File testSetFile) throws Exception {
84          TestW3CSchemaTestSet suite = new TestW3CSchemaTestSet("Test for tests", testSetFile);
85          String testSetLocation = suite.testSetFile.getPath();
86          suite.schemaTests = getSchemaTests(testSetLocation);
87          ListIterator li = suite.schemaTests.listIterator();
88          while (li.hasNext()) {
89              SchemaTest st = (SchemaTest) li.next();
90              File f = new File(testSetFile.getParent(), st.schemaDocumentLink);
91  
92              if (st.currentStatus!=null) {
93                  if (!st.currentStatus.equals("accepted")) {
94                      System.out.println("Warning: SchemaTest which isn't accepted: " + st);
95                  } else if (st.isValid()){
96                      // for now only test schemas that are valid
97                      suite.addTest(new TestRoundTripXSD(f, true));
98                  }
99              }
100         }
101         return suite;
102     }
103 
104     /**
105      * Returns a list of SchemaTest objects created from the .testSet xml file passed
106      * in to the testSet parameter
107      * @param testSet the filename of the .testSet file
108      * @return List of SchemaTest objects describing the schema files to test
109      * @throws Exception
110      */
111     private static List getSchemaTests(String testSet) throws Exception {
112         List schemaTests = new ArrayList();
113         Document doc = getDocument(new InputSource(testSet));
114         NodeList testGroups = doc.getElementsByTagName("testGroup");
115         for (int i = 0; i < testGroups.getLength(); i++) {
116             Node testGroup = testGroups.item(i);
117             NodeList testGroupChildren = testGroup.getChildNodes();
118             Element schemaTestElem = null;
119             for (int j = 0; j < testGroupChildren.getLength(); j++) {
120                 Node n = testGroupChildren.item(j);
121                 if (!(n instanceof Element))
122                     continue;
123                 schemaTestElem = (Element) n;
124                 if (schemaTestElem.getNodeName().equals("schemaTest")) {
125                     break;
126                 }
127             }
128             if (schemaTestElem != null) {
129                 try {
130                     
131                 SchemaTest schemaTest = new SchemaTest((Element) schemaTestElem);
132                 if (schemaTest.schemaDocumentLink != null) schemaTests.add(schemaTest);
133                 } catch (Exception e) {
134                     
135                 }
136             }
137         }
138 
139         return schemaTests; 
140     }
141 
142     /**
143      * Returns a DOM Document of the file passed in as the inputsource parameter
144      * @param inputSource input to read in as DOM Document
145      * @return DOM Document of the input source
146      * @throws Exception can be IOException or SAXException
147      */
148     private static Document getDocument(InputSource inputSource)
149             throws ParserConfigurationException, SAXException,  IOException  {
150     	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
151     	dbf.setNamespaceAware(true);
152     	dbf.setValidating(false);
153 
154     	return dbf.newDocumentBuilder().parse(inputSource);
155     }
156 }