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 parsers; 25 26 import java.io.File; 27 import java.io.IOException; 28 29 import javax.xml.parsers.DocumentBuilder; 30 import javax.xml.parsers.DocumentBuilderFactory; 31 32 import jaxp.library.JAXPTestUtilities; 33 34 import org.testng.Assert; 35 import org.testng.annotations.BeforeMethod; 36 import org.testng.annotations.Listeners; 37 import org.testng.annotations.Test; 38 import org.w3c.dom.Document; 39 import org.xml.sax.InputSource; 40 import org.xml.sax.SAXException; 41 import org.xml.sax.SAXParseException; 42 import org.xml.sax.helpers.DefaultHandler; 43 44 /* 45 * @bug 7157608 46 * @summary Test feature standard-uri-conformant works. 47 */ 48 @Listeners({jaxp.library.FilePolicy.class}) 49 public class Bug7157608Test { 50 public static boolean isWindows = false; 51 static { 52 if (System.getProperty("os.name").indexOf("Windows") > -1) { 53 isWindows = true; 54 } 55 }; 56 57 String xml1, xml2; 58 59 @BeforeMethod 60 protected void setUp() throws IOException { 61 File file1 = new File(getClass().getResource("Bug7157608.xml").getFile()); 62 xml1 = file1.getPath().replace("\\", "\\\\"); 63 File file2 = new File(getClass().getResource("Bug7157608_1.xml").getFile()); 64 xml2 = file2.getPath(); 65 } 66 67 // case 1 68 // standard-uri-confomant is false 69 // dtd-validation is false 70 @Test 71 public void test1() { 72 if (isWindows) { 73 try { 74 ParserSettings ps = new ParserSettings(); 75 76 DocumentBuilder db = getDocumentBuilder(ps); 77 InputSource is = new InputSource(); 78 is.setSystemId(xml1); 79 Document doc = db.parse(is); 80 System.out.println("test1() :OK"); 81 } catch (Exception e) { 82 Assert.fail("test1() :NG"); 83 84 } 85 } 86 } 87 88 // case 2 89 // standard-uri-confomant is false 90 // dtd-validation is true 91 @Test 92 public void test2() { 93 if (isWindows) { 94 try { 95 ParserSettings ps = new ParserSettings(); 96 ps.validating = true; 97 98 DocumentBuilder db = getDocumentBuilder(ps); 99 InputSource is = new InputSource(xml2); 100 Document doc = db.parse(is); 101 System.out.println("test2() :OK"); 102 } catch (Exception e) { 103 Assert.fail("test2() :NG"); 104 // logger.info(e.getMessage()); 105 } 106 } 107 } 108 109 // case 3 110 // standard-uri-confomant is true 111 @Test 112 public void test3() { 113 if (isWindows) { 114 try { 115 ParserSettings ps = new ParserSettings(); 116 ps.standardUriConformant = true; 117 118 DocumentBuilder db = getDocumentBuilder(ps); 119 InputSource is = new InputSource(); 120 is.setSystemId(xml1); 121 Document doc = db.parse(is); 122 Assert.fail("test3() :NG"); 123 } catch (IOException e) { 124 String returnedErr = e.getMessage(); 125 String expectedStr = "Opaque part contains invalid character"; 126 127 if (returnedErr.indexOf(expectedStr) >= 0) { 128 System.out.println("test3() :OK"); 129 } else { 130 Assert.fail("test3() :NG"); 131 } 132 } catch (Exception e) { 133 System.out.println("test3() :NG"); 134 } 135 } 136 } 137 138 // case 4 139 // standard-uri-confomant is true 140 // dtd-validation is true 141 @Test 142 public void test4() { 143 if (isWindows) { 144 try { 145 ParserSettings ps = new ParserSettings(); 146 ps.standardUriConformant = true; 147 ps.validating = true; 148 149 DocumentBuilder db = getDocumentBuilder(ps); 150 InputSource is = new InputSource(xml2); 151 Document doc = db.parse(is); 152 Assert.fail("test4() :NG"); 153 } catch (IOException e) { 154 String returnedErr = e.getMessage(); 155 String expectedStr = "Opaque part contains invalid character"; 156 157 if (returnedErr.indexOf(expectedStr) >= 0) { 158 System.out.println("test3() :OK"); 159 } else { 160 Assert.fail("test3() :NG"); 161 } 162 } catch (Exception e) { 163 Assert.fail("test4() :NG"); 164 } 165 } 166 } 167 168 public DocumentBuilder getDocumentBuilder(ParserSettings ps) { 169 DocumentBuilder db = null; 170 try { 171 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 172 if (ps.standardUriConformant) { 173 dbf.setFeature("http://apache.org/xml/features/standard-uri-conformant", true); 174 } 175 dbf.setValidating(ps.validating); 176 db = dbf.newDocumentBuilder(); 177 db.setErrorHandler(new MyHandler()); 178 } catch (Exception e) { 179 Assert.fail("standard-uri-conformant not recognized"); 180 } 181 return db; 182 } 183 184 class MyHandler extends DefaultHandler { 185 @Override 186 public void warning(SAXParseException e) throws SAXException { 187 printDetail("**Warning**", e); 188 } 189 190 @Override 191 public void error(SAXParseException e) throws SAXException { 192 printDetail("**Error**", e); 193 throw new SAXException("Error encountered"); 194 } 195 196 @Override 197 public void fatalError(SAXParseException e) throws SAXException { 198 printDetail("**Fatal Error**", e); 199 throw new SAXException("Fatal Error encountered"); 200 } 201 202 public void printDetail(String msg, SAXParseException e) { 203 System.out.println(msg); 204 System.out.println(e.getMessage()); 205 System.out.println(" Line: " + e.getLineNumber()); 206 System.out.println(" Column: " + e.getColumnNumber()); 207 System.out.println(" URI: " + e.getSystemId()); 208 } 209 210 } 211 212 class ParserSettings { 213 boolean standardUriConformant = false; 214 boolean validating = false; 215 } 216 }