1/* 2 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 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 24package validation; 25 26import static jaxp.library.JAXPTestUtilities.USER_DIR; 27import static jaxp.library.JAXPTestUtilities.runWithTmpPermission; 28 29import java.io.File; 30import java.io.FileInputStream; 31import java.io.FileWriter; 32import java.util.PropertyPermission; 33 34import javax.xml.XMLConstants; 35import javax.xml.stream.XMLEventReader; 36import javax.xml.stream.XMLInputFactory; 37import javax.xml.stream.XMLOutputFactory; 38import javax.xml.transform.Result; 39import javax.xml.transform.Source; 40import javax.xml.transform.stax.StAXResult; 41import javax.xml.validation.Schema; 42import javax.xml.validation.SchemaFactory; 43import javax.xml.validation.Validator; 44 45import org.testng.Assert; 46import org.testng.annotations.Listeners; 47import org.testng.annotations.Test; 48import org.xml.sax.ErrorHandler; 49 50/* 51 * @test 52 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 53 * @run testng/othervm -DrunSecMngr=true validation.ValidatorTest 54 * @run testng/othervm validation.ValidatorTest 55 * @summary Test Validator.validate(Source, Result). 56 */ 57@Listeners({jaxp.library.FilePolicy.class}) 58public class ValidatorTest { 59 60 @Test 61 public void testValidateStAX() { 62 63 File resultFile = null; 64 try { 65 resultFile = new File(USER_DIR + "stax.result"); 66 if (resultFile.exists()) { 67 resultFile.delete(); 68 } 69 70 Result xmlResult = new javax.xml.transform.stax.StAXResult(XMLOutputFactory.newInstance().createXMLStreamWriter(new FileWriter(resultFile))); 71 Source xmlSource = new javax.xml.transform.stax.StAXSource(getXMLEventReader("toys.xml")); 72 validate("toys.xsd", xmlSource, xmlResult); 73 74 ((StAXResult) xmlResult).getXMLStreamWriter().close(); 75 Assert.assertTrue(resultFile.exists(), "result file is not created"); 76 77 } catch (Exception ex) { 78 ex.printStackTrace(); 79 Assert.fail("Exception : " + ex.getMessage()); 80 } finally { 81 if (resultFile != null && resultFile.exists()) { 82 resultFile.delete(); 83 } 84 } 85 } 86 87 @Test 88 public void testValidateStream() { 89 90 File resultFile = null; 91 try { 92 resultFile = new File(USER_DIR + "stax.result"); 93 if (resultFile.exists()) { 94 resultFile.delete(); 95 } 96 // Validate this instance document against the 97 // Instance document supplied 98 File resultAlias = resultFile; 99 Result xmlResult = runWithTmpPermission(() -> new javax.xml.transform.stream.StreamResult( 100 resultAlias), new PropertyPermission("user.dir", "read")); 101 Source xmlSource = new javax.xml.transform.stream.StreamSource(new File(ValidatorTest.class.getResource("toys.xml").toURI())); 102 103 validate("toys.xsd", xmlSource, xmlResult); 104 Assert.assertTrue(resultFile.exists(), "result file is not created"); 105 } catch (Exception ex) { 106 ex.printStackTrace(); 107 Assert.fail("Exception : " + ex.getMessage()); 108 } finally { 109 if (resultFile != null && resultFile.exists()) { 110 resultFile.delete(); 111 } 112 } 113 } 114 115 @Test 116 public void testValidateGMonth() { 117 118 // test valid gMonths 119 File resultFile = null; 120 try { 121 resultFile = new File(USER_DIR + "gMonths.result.xml"); 122 if (resultFile.exists()) { 123 resultFile.delete(); 124 } 125 126 // Validate this instance document against the 127 // Instance document supplied 128 File resultAlias = resultFile; 129 Result xmlResult = runWithTmpPermission(() -> new javax.xml.transform.stream.StreamResult( 130 resultAlias), new PropertyPermission("user.dir", "read")); 131 Source xmlSource = new javax.xml.transform.stream.StreamSource(new File(ValidatorTest.class.getResource("gMonths.xml").toURI())); 132 133 validate("gMonths.xsd", xmlSource, xmlResult); 134 135 Assert.assertTrue(resultFile.exists(), "result file is not created"); 136 } catch (Exception ex) { 137 ex.printStackTrace(); 138 Assert.fail("Exception : " + ex.getMessage()); 139 } finally { 140 if (resultFile != null && resultFile.exists()) { 141 resultFile.delete(); 142 } 143 } 144 145 // test invalid gMonths 146 File invalidResultFile = null; 147 try { 148 invalidResultFile = new File(USER_DIR + "gMonths-invalid.result.xml"); 149 if (invalidResultFile.exists()) { 150 invalidResultFile.delete(); 151 } 152 153 // Validate this instance document against the 154 // Instance document supplied 155 Result xmlResult = new javax.xml.transform.stream.StreamResult(resultFile); 156 Source xmlSource = new javax.xml.transform.stream.StreamSource(new File(ValidatorTest.class.getResource("gMonths-invalid.xml").toURI())); 157 158 validate("gMonths.xsd", xmlSource, xmlResult); 159 160 // should have failed with an Exception due to invalid gMonths 161 Assert.fail("invalid gMonths were accepted as valid in " + ValidatorTest.class.getResource("gMonths-invalid.xml").toURI()); 162 } catch (Exception ex) { 163 // expected failure 164 System.out.println("Expected failure: " + ex.toString()); 165 } finally { 166 if (invalidResultFile != null && invalidResultFile.exists()) { 167 invalidResultFile.delete(); 168 } 169 } 170 } 171 172 private void validate(final String xsdFile, final Source src, final Result result) throws Exception { 173 try { 174 SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 175 Schema schema = sf.newSchema(new File(ValidatorTest.class.getResource(xsdFile).toURI())); 176 177 // Get a Validator which can be used to validate instance document 178 // against this grammar. 179 Validator validator = schema.newValidator(); 180 ErrorHandler eh = new ErrorHandlerImpl(); 181 validator.setErrorHandler(eh); 182 183 // Validate this instance document against the 184 // Instance document supplied 185 validator.validate(src, result); 186 } catch (Exception ex) { 187 throw ex; 188 } 189 } 190 191 private XMLEventReader getXMLEventReader(final String filename) { 192 193 XMLInputFactory xmlif = null; 194 XMLEventReader xmlr = null; 195 try { 196 xmlif = XMLInputFactory.newInstance(); 197 xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE); 198 xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE); 199 xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE); 200 xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE); 201 202 // FileInputStream fis = new FileInputStream(filename); 203 FileInputStream fis = new FileInputStream(new File(ValidatorTest.class.getResource(filename).toURI())); 204 xmlr = xmlif.createXMLEventReader(filename, fis); 205 } catch (Exception ex) { 206 ex.printStackTrace(); 207 Assert.fail("Exception : " + ex.getMessage()); 208 } 209 return xmlr; 210 } 211} 212