1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
41
42
43
44
45
46
47 public class TestW3CSchemaTestSet extends TestSuite {
48
49 private List schemaTests = null;
50
51 private File testSetFile = null;
52
53
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
79
80
81
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
97 suite.addTest(new TestRoundTripXSD(f, true));
98 }
99 }
100 }
101 return suite;
102 }
103
104
105
106
107
108
109
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
144
145
146
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 }