8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package validation; 25 26 27 import java.io.File; 28 import java.net.URL; 29 30 import javax.xml.XMLConstants; 31 import javax.xml.transform.stream.StreamSource; 32 import javax.xml.validation.Schema; 33 import javax.xml.validation.SchemaFactory; 34 import javax.xml.validation.Validator; 35 import org.testng.annotations.DataProvider; 36 37 import org.testng.annotations.Listeners; 38 import org.testng.annotations.Test; 39 import org.xml.sax.SAXException; 40 import org.xml.sax.SAXParseException; 41 42 /* 43 * @test 44 * @bug 8220818 8176447 45 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 46 * @run testng/othervm validation.ValidationTest 47 * @summary Runs validations with schemas and sources 48 */ 49 @Listeners({jaxp.library.FilePolicy.class}) 50 public class ValidationTest { 51 static final String FILE_PATH = "files/"; 52 /* 53 DataProvider: valid xml 54 */ 55 @DataProvider(name = "valid") 56 Object[][] getValid() { 57 return new Object[][]{ 58 {"JDK8220818a.xsd", "JDK8220818a_Valid.xml"}, 59 {"JDK8220818a.xsd", "JDK8220818a_Valid1.xml"}, 60 {"JDK8220818b.xsd", "JDK8220818b_Valid.xml"}, 89 } 90 91 @Test(dataProvider = "valid") 92 public void testValidateRefType1(String xsd, String xml) throws Exception { 93 validate(xsd, xml); 94 } 95 96 /** 97 * @bug 8176447 98 * Verifies that the uniqueness constraint is checked. 99 * @param xsd the XSD 100 * @param xml the XML 101 * @throws Exception expected when the uniqueness constraint is validated 102 * correctly. 103 */ 104 @Test(dataProvider = "uniqueness", expectedExceptions = SAXException.class) 105 public void testUnique(String xsd, String xml) throws Exception { 106 validate(xsd, xml); 107 } 108 109 private void validate(String xsd, String xml) throws Exception { 110 final SchemaFactory schemaFactory = SchemaFactory.newInstance( 111 XMLConstants.W3C_XML_SCHEMA_NS_URI); 112 final Schema schema = schemaFactory.newSchema( 113 new File(getClass().getResource(FILE_PATH + xsd).getFile())); 114 final Validator validator = schema.newValidator(); 115 validator.validate(new StreamSource( 116 new File(getClass().getResource(FILE_PATH + xml).getFile()))); 117 } 118 119 } | 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package validation; 25 26 27 import java.io.File; 28 import java.io.FileInputStream; 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; 36 import javax.xml.transform.stream.StreamSource; 37 import javax.xml.validation.Schema; 38 import javax.xml.validation.SchemaFactory; 39 import javax.xml.validation.Validator; 40 import org.testng.annotations.DataProvider; 41 import org.testng.annotations.Listeners; 42 import org.testng.annotations.Test; 43 import org.xml.sax.Attributes; 44 import org.xml.sax.InputSource; 45 import org.xml.sax.SAXException; 46 import org.xml.sax.SAXParseException; 47 import org.xml.sax.XMLFilter; 48 import org.xml.sax.helpers.XMLFilterImpl; 49 50 /* 51 * @test 52 * @bug 8220818 8176447 53 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 54 * @run testng/othervm validation.ValidationTest 55 * @summary Runs validations with schemas and sources 56 */ 57 @Listeners({jaxp.library.FilePolicy.class}) 58 public class ValidationTest { 59 static final String FILE_PATH = "files/"; 60 /* 61 DataProvider: valid xml 62 */ 63 @DataProvider(name = "valid") 64 Object[][] getValid() { 65 return new Object[][]{ 66 {"JDK8220818a.xsd", "JDK8220818a_Valid.xml"}, 67 {"JDK8220818a.xsd", "JDK8220818a_Valid1.xml"}, 68 {"JDK8220818b.xsd", "JDK8220818b_Valid.xml"}, 97 } 98 99 @Test(dataProvider = "valid") 100 public void testValidateRefType1(String xsd, String xml) throws Exception { 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; 159 } 160 161 private void validate(String xsd, String xml) throws Exception { 162 final SchemaFactory schemaFactory = SchemaFactory.newInstance( 163 XMLConstants.W3C_XML_SCHEMA_NS_URI); 164 final Schema schema = schemaFactory.newSchema( 165 new File(getClass().getResource(FILE_PATH + xsd).getFile())); 166 final Validator validator = schema.newValidator(); 167 validator.validate(new StreamSource( 168 new File(getClass().getResource(FILE_PATH + xml).getFile()))); 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 179 } |