1   /**
2    * Copyright 2006 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  package tests.w3c;
17  
18  import junit.framework.Test;
19  import junit.framework.TestSuite;
20  import org.w3c.dom.Document;
21  import org.w3c.dom.Element;
22  import org.w3c.dom.Node;
23  import org.w3c.dom.NodeList;
24  import org.xml.sax.InputSource;
25  import org.xml.sax.SAXException;
26  
27  import javax.xml.parsers.DocumentBuilderFactory;
28  import javax.xml.parsers.ParserConfigurationException;
29  import java.io.File;
30  import java.io.IOException;
31  import java.util.ArrayList;
32  import java.util.List;
33  import java.util.ListIterator;
34  
35  
36  /**
37   * Class to represent a set of schema tests as described by a .testSet file
38   * When executed each of the schemas described in the .testSet file is round-trip
39   * tested.   
40   * cmd line parms: arg0 - location of the .testSet file. Defaults to:
41   *                        ./target/xmlschema2002-01-16/NISTXMLSchema1-0-20020116.testSet
42   *
43   */
44  public class TestW3CSchemaTestSet extends TestSuite {
45  
46      private List schemaTests = null;
47      
48      private File testSetFile = null;
49      
50      // If junit called from cmd line without any args, use the NIST test bucket
51      private static String testSetLocation = "./target/xmlschema2002-01-16/NISTXMLSchema1-0-20020116.testSet";
52  
53      private TestW3CSchemaTestSet(String name, File testSetFile) {
54          super(name);
55          this.testSetFile = testSetFile;
56      }
57      
58      public static void main(String[] args) {
59          try {
60              if (args[0] != null) {
61                  testSetLocation = args[0]; 
62              }
63              junit.textui.TestRunner.run(TestW3CSchemaTestSet.suite(new File(testSetLocation)));
64          } catch (Exception e) {
65              e.printStackTrace();
66          }
67      }
68  
69      public static Test suite() throws Exception {
70          testSetLocation =  System.getProperty("W3CTestLocation", testSetLocation);
71          return suite(new File(testSetLocation));
72      }
73      
74      /**
75       * Returns a suite of TestRoundTripXSD test case instances. One for each of the tests
76       * described in the testSetFile
77       * @param testSetFile the File object of the .testSet file 
78       * @throws Exception
79       */
80      public static Test suite(File testSetFile) throws Exception {
81          TestW3CSchemaTestSet suite = new TestW3CSchemaTestSet("Test for tests", testSetFile);
82          String testSetLocation = suite.testSetFile.getPath();
83          suite.schemaTests = getSchemaTests(testSetLocation);
84          ListIterator li = suite.schemaTests.listIterator();
85          while (li.hasNext()) {
86              SchemaTest st = (SchemaTest) li.next();
87              File f = new File(testSetFile.getParent(), st.schemaDocumentLink);
88  
89              if (st.currentStatus!=null) {
90                  if (!st.currentStatus.equals("accepted")) {
91                      System.out.println("Warning: SchemaTest which isn't accepted: " + st);
92                  } else if (st.isValid()){
93                      // for now only test schemas that are valid
94                      suite.addTest(new TestRoundTripXSD(f, true));
95                  }
96              }
97          }
98          return suite;
99      }
100 
101     /**
102      * Returns a list of SchemaTest objects created from the .testSet xml file passed
103      * in to the testSet parameter
104      * @param testSet the filename of the .testSet file
105      * @return List of SchemaTest objects describing the schema files to test
106      * @throws Exception
107      */
108     private static List getSchemaTests(String testSet) throws Exception {
109         List schemaTests = new ArrayList();
110         Document doc = getDocument(new InputSource(testSet));
111         NodeList testGroups = doc.getElementsByTagName("testGroup");
112         for (int i = 0; i < testGroups.getLength(); i++) {
113             Node testGroup = testGroups.item(i);
114             NodeList testGroupChildren = testGroup.getChildNodes();
115             Element schemaTestElem = null;
116             for (int j = 0; j < testGroupChildren.getLength(); j++) {
117                 Node n = testGroupChildren.item(j);
118                 if (!(n instanceof Element))
119                     continue;
120                 schemaTestElem = (Element) n;
121                 if (schemaTestElem.getNodeName().equals("schemaTest")) {
122                     break;
123                 }
124             }
125             if (schemaTestElem != null) {
126                 try {
127                     
128                 SchemaTest schemaTest = new SchemaTest((Element) schemaTestElem);
129                 if (schemaTest.schemaDocumentLink != null) schemaTests.add(schemaTest);
130                 } catch (Exception e) {
131                     
132                 }
133             }
134         }
135 
136         return schemaTests; 
137     }
138 
139     /**
140      * Returns a DOM Document of the file passed in as the inputsource parameter
141      * @param inputSource input to read in as DOM Document
142      * @return DOM Document of the input source
143      * @throws Exception can be IOException or SAXException
144      */
145     private static Document getDocument(InputSource inputSource)
146             throws ParserConfigurationException, SAXException,  IOException  {
147     	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
148     	dbf.setNamespaceAware(true);
149     	dbf.setValidating(false);
150 
151     	return dbf.newDocumentBuilder().parse(inputSource);
152     }
153 }