23 |
23 |
24 package validation; |
24 package validation; |
25 |
25 |
26 |
26 |
27 import java.io.File; |
27 import java.io.File; |
28 import java.net.URL; |
28 import java.io.FileInputStream; |
29 |
|
30 import javax.xml.XMLConstants; |
29 import javax.xml.XMLConstants; |
|
30 import javax.xml.parsers.SAXParserFactory; |
|
31 import javax.xml.stream.XMLInputFactory; |
|
32 import javax.xml.stream.XMLStreamReader; |
|
33 import javax.xml.stream.events.XMLEvent; |
|
34 import javax.xml.transform.Source; |
|
35 import javax.xml.transform.sax.SAXSource; |
31 import javax.xml.transform.stream.StreamSource; |
36 import javax.xml.transform.stream.StreamSource; |
32 import javax.xml.validation.Schema; |
37 import javax.xml.validation.Schema; |
33 import javax.xml.validation.SchemaFactory; |
38 import javax.xml.validation.SchemaFactory; |
34 import javax.xml.validation.Validator; |
39 import javax.xml.validation.Validator; |
35 import org.testng.annotations.DataProvider; |
40 import org.testng.annotations.DataProvider; |
36 |
|
37 import org.testng.annotations.Listeners; |
41 import org.testng.annotations.Listeners; |
38 import org.testng.annotations.Test; |
42 import org.testng.annotations.Test; |
|
43 import org.xml.sax.Attributes; |
|
44 import org.xml.sax.InputSource; |
|
45 import org.xml.sax.SAXException; |
39 import org.xml.sax.SAXParseException; |
46 import org.xml.sax.SAXParseException; |
|
47 import org.xml.sax.XMLFilter; |
|
48 import org.xml.sax.helpers.XMLFilterImpl; |
40 |
49 |
41 /* |
50 /* |
42 * @test |
51 * @test |
43 * @bug 8220818 |
52 * @bug 8220818 8176447 |
44 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest |
53 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest |
45 * @run testng/othervm validation.ValidationTest |
54 * @run testng/othervm validation.ValidationTest |
46 * @summary Runs validations with schemas and sources |
55 * @summary Runs validations with schemas and sources |
47 */ |
56 */ |
48 @Listeners({jaxp.library.FilePolicy.class}) |
57 @Listeners({jaxp.library.FilePolicy.class}) |
69 {"JDK8220818a.xsd", "JDK8220818a_Invalid.xml"}, |
78 {"JDK8220818a.xsd", "JDK8220818a_Invalid.xml"}, |
70 {"JDK8220818b.xsd", "JDK8220818b_Invalid.xml"}, |
79 {"JDK8220818b.xsd", "JDK8220818b_Invalid.xml"}, |
71 }; |
80 }; |
72 } |
81 } |
73 |
82 |
|
83 /* |
|
84 DataProvider: uniqueness |
|
85 */ |
|
86 @DataProvider(name = "uniqueness") |
|
87 Object[][] getUniqueData() { |
|
88 return new Object[][]{ |
|
89 {"JDK8176447a.xsd", "JDK8176447a.xml"}, |
|
90 {"JDK8176447b.xsd", "JDK8176447b.xml"}, |
|
91 }; |
|
92 } |
|
93 |
74 @Test(dataProvider = "invalid", expectedExceptions = SAXParseException.class) |
94 @Test(dataProvider = "invalid", expectedExceptions = SAXParseException.class) |
75 public void testValidateRefType(String xsd, String xml) throws Exception { |
95 public void testValidateRefType(String xsd, String xml) throws Exception { |
76 validate(xsd, xml); |
96 validate(xsd, xml); |
77 } |
97 } |
78 |
98 |
79 @Test(dataProvider = "valid") |
99 @Test(dataProvider = "valid") |
80 public void testValidateRefType1(String xsd, String xml) throws Exception { |
100 public void testValidateRefType1(String xsd, String xml) throws Exception { |
81 validate(xsd, xml); |
101 validate(xsd, xml); |
|
102 } |
|
103 |
|
104 /** |
|
105 * @bug 8176447 |
|
106 * Verifies that the uniqueness constraint is checked. |
|
107 * @param xsd the XSD |
|
108 * @param xml the XML |
|
109 * @throws Exception expected when the uniqueness constraint is validated |
|
110 * correctly. |
|
111 */ |
|
112 @Test(dataProvider = "uniqueness", expectedExceptions = SAXException.class) |
|
113 public void testUnique(String xsd, String xml) throws Exception { |
|
114 validate(xsd, xml); |
|
115 } |
|
116 |
|
117 /** |
|
118 * @bug 8068376 |
|
119 * Verifies that validation performs normally with externally provided string |
|
120 * parameters. |
|
121 * @throws Exception if the test fails |
|
122 */ |
|
123 @Test |
|
124 public void testJDK8068376() throws Exception { |
|
125 |
|
126 String xsdFile = getClass().getResource(FILE_PATH + "JDK8068376.xsd").getFile(); |
|
127 String xmlFile = getClass().getResource(FILE_PATH + "JDK8068376.xml").getFile(); |
|
128 String targetNamespace = getTargetNamespace(xsdFile); |
|
129 |
|
130 XMLFilter namespaceFilter = new XMLFilterImpl(SAXParserFactory.newDefaultNSInstance().newSAXParser().getXMLReader()) { |
|
131 @Override |
|
132 public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { |
|
133 uri = targetNamespace; // overwriting the uri with our own choice |
|
134 super.startElement(uri, localName, qName, atts); |
|
135 } |
|
136 }; |
|
137 |
|
138 Source xmlSource = new SAXSource(namespaceFilter, new InputSource(xmlFile)); |
|
139 Source schemaSource = new StreamSource(xsdFile); |
|
140 validate(schemaSource, xmlSource); |
|
141 |
|
142 } |
|
143 |
|
144 private static String getTargetNamespace(String xsdFile) throws Exception { |
|
145 XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(xsdFile)); |
|
146 while (reader.hasNext()) { |
|
147 int event = reader.next(); |
|
148 |
|
149 // Get the root element's "targetNamespace" attribute |
|
150 if (event == XMLEvent.START_ELEMENT) { |
|
151 // validation fails before patch |
|
152 String value = reader.getAttributeValue(null, "targetNamespace"); // fails validation |
|
153 // validation passes due to a reference comparison in the original code |
|
154 // String value = "mynamespace"; |
|
155 return value; |
|
156 } |
|
157 } |
|
158 return null; |
82 } |
159 } |
83 |
160 |
84 private void validate(String xsd, String xml) throws Exception { |
161 private void validate(String xsd, String xml) throws Exception { |
85 final SchemaFactory schemaFactory = SchemaFactory.newInstance( |
162 final SchemaFactory schemaFactory = SchemaFactory.newInstance( |
86 XMLConstants.W3C_XML_SCHEMA_NS_URI); |
163 XMLConstants.W3C_XML_SCHEMA_NS_URI); |
88 new File(getClass().getResource(FILE_PATH + xsd).getFile())); |
165 new File(getClass().getResource(FILE_PATH + xsd).getFile())); |
89 final Validator validator = schema.newValidator(); |
166 final Validator validator = schema.newValidator(); |
90 validator.validate(new StreamSource( |
167 validator.validate(new StreamSource( |
91 new File(getClass().getResource(FILE_PATH + xml).getFile()))); |
168 new File(getClass().getResource(FILE_PATH + xml).getFile()))); |
92 } |
169 } |
|
170 |
|
171 private void validate(Source xsd, Source xml) throws Exception { |
|
172 final SchemaFactory schemaFactory = SchemaFactory.newInstance( |
|
173 XMLConstants.W3C_XML_SCHEMA_NS_URI); |
|
174 final Schema schema = schemaFactory.newSchema(xsd); |
|
175 final Validator validator = schema.newValidator(); |
|
176 validator.validate(xml); |
|
177 } |
|
178 |
93 } |
179 } |