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 
  24 package validation;
  25 
  26 import static jaxp.library.JAXPTestUtilities.USER_DIR;
  27 
  28 import java.io.File;
  29 import java.io.FileOutputStream;
  30 import java.io.IOException;
  31 import java.io.OutputStream;
  32 import java.io.OutputStreamWriter;
  33 import java.io.StringReader;
  34 import java.nio.file.Paths;
  35 
  36 import javax.xml.XMLConstants;
  37 import javax.xml.transform.stream.StreamSource;
  38 import javax.xml.validation.Schema;
  39 import javax.xml.validation.SchemaFactory;
  40 import javax.xml.validation.Validator;
  41 
  42 import org.testng.annotations.Listeners;
  43 import org.testng.annotations.Test;
  44 
  45 /*
  46  * @test
  47  * @bug 6457662
  48  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  49  * @run testng/othervm -DrunSecMngr=true validation.Bug6457662
  50  * @run testng/othervm validation.Bug6457662
  51  * @summary Test a Validator checks sequence maxOccurs correctly when it validates document repeatedly.
  52  */
  53 @Listeners({jaxp.library.FilePolicy.class})
  54 public class Bug6457662 {
  55 
  56     public static final String xml = "<ACL xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" + "<Tokens access=\"full\">" + "<Token>CheetahTech</Token>"
  57             + "<Token>CheetahView</Token>" + "</Tokens>" + "</ACL>";
  58     /** Schema */
  59     public static final String schema = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  60             + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" elementFormDefault=\"qualified\" attributeFormDefault=\"unqualified\">"
  61             + "<xs:element name=\"ACL\">" + "<xs:complexType mixed=\"false\">" + "<xs:sequence><xs:element ref=\"Tokens\" maxOccurs=\"3\"/></xs:sequence>"
  62             + "<xs:attribute name=\"ACL\" type=\"xs:string\" use=\"optional\"/>" + "</xs:complexType>" + "</xs:element><xs:element name=\"Tokens\">"
  63             + "<xs:complexType mixed=\"false\">" + "<xs:sequence><xs:element ref=\"Token\" maxOccurs=\"unbounded\"/></xs:sequence>"
  64             + "<xs:attribute name=\"access\" type=\"xs:string\" use=\"required\"/>" + "</xs:complexType></xs:element><xs:element name=\"Token\"/>"
  65             + "</xs:schema>";
  66     /** Schema factory */
  67     private static final SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  68 
  69     @Test
  70     public void test() throws Exception {
  71         final Schema sc = factory.newSchema(writeSchema());
  72         final Validator validator = sc.newValidator();
  73         validator.validate(new StreamSource(new StringReader(xml)));
  74         validator.validate(new StreamSource(new StringReader(xml)));
  75         validator.validate(new StreamSource(new StringReader(xml)));
  76         validator.validate(new StreamSource(new StringReader(xml)));
  77     }
  78 
  79     private File writeSchema() throws IOException {
  80         final File rtn = File.createTempFile("scheam", "xsd", Paths.get(USER_DIR).toFile());
  81         final OutputStream out = new FileOutputStream(rtn);
  82         final OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
  83         writer.write(schema);
  84         writer.close();
  85         out.close();
  86         return rtn;
  87     }
  88 }
  89