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