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