1 /* 2 * Copyright (c) 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 package validation; 24 25 /* 26 * @bug 8080907 27 * @summary Test processContents attribute of any element 28 */ 29 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; 30 31 import java.net.URISyntaxException; 32 33 import javax.xml.transform.stream.StreamSource; 34 import javax.xml.validation.SchemaFactory; 35 import javax.xml.validation.Validator; 36 37 import org.testng.annotations.BeforeClass; 38 import org.testng.annotations.Listeners; 39 import org.testng.annotations.Test; 40 import org.xml.sax.SAXException; 41 import org.xml.sax.SAXParseException; 42 43 @Listeners({jaxp.library.FilePolicy.class}) 44 public class AnyElementTest { 45 @BeforeClass 46 public void setup() throws URISyntaxException, SAXException { 47 validator = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI).newSchema(new StreamSource(getUri("ProcessContents.xsd"))).newValidator(); 48 } 49 50 /* 51 * processContents attribute - Specifies how the XML processor should handle 52 * validation against the elements specified by this any element. Can be set 53 * to one of the following: 54 * strict - the XML processor must obtain the schema for the required 55 * namespaces and validate the elements (this is default) 56 * lax - same as strict, but if the schema cannot be obtained, no errors 57 * will occur 58 * skip - The XML processor does not attempt to validate any elements from 59 * the specified namespaces 60 */ 61 @Test 62 public void testProcessContents() throws Exception { 63 validator.validate(new StreamSource(getUri("ProcessContents-ok.xml"))); 64 } 65 66 /* 67 * When processContents="lax", validation will be performed when the element 68 * is declared in the schema. 69 */ 70 @Test(expectedExceptions = SAXParseException.class) 71 public void testProcessContentsLax() throws Exception { 72 validator.validate(new StreamSource(getUri("ProcessContents-lax-error.xml"))); 73 } 74 75 /* 76 * Get the URI of the file, which is in the same path as this class 77 */ 78 private String getUri(String fileName) throws URISyntaxException { 79 return this.getClass().getResource(fileName).toURI().toASCIIString(); 80 } 81 82 private Validator validator; 83 }