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