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