1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
38
39
40
41
42
43
44 public class TestW3CSchemaTestSet extends TestSuite {
45
46 private List schemaTests = null;
47
48 private File testSetFile = null;
49
50
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
76
77
78
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
94 suite.addTest(new TestRoundTripXSD(f, true));
95 }
96 }
97 }
98 return suite;
99 }
100
101
102
103
104
105
106
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
141
142
143
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 }