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