CR6740048.java revision 968:874082a9b565
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 24package validation; 25 26import java.io.IOException; 27import java.io.InputStream; 28 29import javax.xml.parsers.DocumentBuilder; 30import javax.xml.parsers.DocumentBuilderFactory; 31import javax.xml.parsers.ParserConfigurationException; 32 33import org.testng.annotations.Listeners; 34import org.testng.annotations.Test; 35import org.testng.Assert; 36import org.w3c.dom.Document; 37import org.xml.sax.ErrorHandler; 38import org.xml.sax.SAXException; 39import org.xml.sax.SAXParseException; 40 41/* 42 * @test 43 * @bug 6740048 44 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 45 * @run testng/othervm -DrunSecMngr=true validation.CR6740048 46 * @run testng/othervm validation.CR6740048 47 * @summary Test DocumentBuilder can be reused when the DocumentBuilderFactory sets schema. 48 */ 49@Listeners({jaxp.library.FilePolicy.class}) 50public class CR6740048 { 51 private static final String TAG_INFO = "containerInfo"; 52 private static final String SCHEMA_LANGUAGE_URL = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; 53 private static final String SCHEMA_SOURCE_URL = "http://java.sun.com/xml/jaxp/properties/schemaSource"; 54 private static final String XML_SCHEMA_URL = "http://www.w3.org/2001/XMLSchema"; 55 56 @Test 57 public final void testReusingDocumentBuilder() { 58 59 try { 60 InputStream xsd = this.getClass().getResourceAsStream("CR6740048.xsd"); 61 // create document builder 62 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 63 docBuilderFactory.setNamespaceAware(true); 64 65 if (xsd != null) { 66 docBuilderFactory.setValidating(true); 67 docBuilderFactory.setAttribute(SCHEMA_LANGUAGE_URL, XML_SCHEMA_URL); 68 docBuilderFactory.setAttribute(SCHEMA_SOURCE_URL, xsd); 69 } 70 71 final DocumentBuilder documentBuilder = docBuilderFactory.newDocumentBuilder(); 72 documentBuilder.setErrorHandler(new ErrorHandler() { 73 74 public void error(SAXParseException exception) throws SAXException { 75 throw exception; 76 } 77 78 public void fatalError(SAXParseException exception) throws SAXException { 79 throw exception; 80 } 81 82 public void warning(SAXParseException exception) throws SAXException { 83 throw exception; 84 } 85 }); 86 87 // TODO set the system properties in order to match the ones from 88 // the server environment 89 /** 90 * Properties props = new Properties(); 91 * props.load(loader.getResourceAsStream("sysprops.properties")); 92 * System.setProperties(props); 93 */ 94 95 // now parse the document 96 // InputStream is = loader.getResourceAsStream("CR6740048.xml"); 97 InputStream is = this.getClass().getResourceAsStream("CR6740048.xml"); 98 try { 99 Document doc = documentBuilder.parse(is); 100 } catch (Exception se) { 101 102 se.printStackTrace(); 103 Assert.fail(se.getMessage()); 104 105 } finally { 106 is.close(); 107 } 108 109 // now use the parser object as second time 110 // is = loader.getResourceAsStream("CR6740048.xml"); 111 is = this.getClass().getResourceAsStream("CR6740048.xml"); 112 try { 113 Document doc = documentBuilder.parse(is); 114 } catch (Exception se) { 115 116 se.printStackTrace(); 117 Assert.fail(se.getMessage()); 118 119 } finally { 120 is.close(); 121 } 122 123 System.err.println("Parse successful"); 124 125 is.close(); 126 } catch (ParserConfigurationException pce) { 127 pce.printStackTrace(); 128 } catch (IOException ioe) { 129 ioe.printStackTrace(); 130 } 131 } 132 133} 134 135