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.ptests;
  25 
  26 import static jaxp.library.JAXPTestUtilities.FILE_SEP;
  27 import static jaxp.library.JAXPTestUtilities.failUnexpected;
  28 import static org.testng.Assert.assertFalse;
  29 
  30 import java.io.File;
  31 import java.io.FileInputStream;
  32 import java.io.IOException;
  33 
  34 import javax.xml.parsers.DocumentBuilder;
  35 import javax.xml.parsers.DocumentBuilderFactory;
  36 import javax.xml.parsers.ParserConfigurationException;
  37 
  38 import org.testng.annotations.Test;
  39 import org.w3c.dom.Document;
  40 import org.xml.sax.EntityResolver;
  41 import org.xml.sax.InputSource;
  42 import org.xml.sax.SAXException;
  43 
  44 /**
  45  * This checks for the methods of DocumentBuilder
  46  */
  47 public class DocumentBuilderImpl01 implements EntityResolver {
  48 
  49     /**
  50      * Testcase to test the default functionality of isValidation method. Expect
  51      * to return false because not setting the validation.
  52      */
  53     @Test
  54     public void testCheckDocumentBuilderImpl01() {
  55         try {
  56             DocumentBuilderFactory dbfImpl = DocumentBuilderFactory.newInstance();
  57             DocumentBuilder dbImpl = dbfImpl.newDocumentBuilder();
  58             assertFalse(dbImpl.isValidating());
  59         } catch (javax.xml.parsers.ParserConfigurationException e) {
  60             failUnexpected(e);
  61         }
  62 
  63     }
  64 
  65     /**
  66      * Testcase to test the default functionality of isNamespaceAware method.
  67      */
  68     @Test
  69     public void testCheckDocumentBuilderImpl02() {
  70         try {
  71             DocumentBuilderFactory dbfImpl = DocumentBuilderFactory.newInstance();
  72             DocumentBuilder dbImpl = dbfImpl.newDocumentBuilder();
  73             assertFalse(dbImpl.isNamespaceAware());
  74         } catch (javax.xml.parsers.ParserConfigurationException e) {
  75             failUnexpected(e);
  76         }
  77 
  78     }
  79 
  80     /**
  81      * Testcase to test the parse(InputStream).
  82      */
  83     @Test
  84     public void testCheckDocumentBuilderImpl04() {
  85         try {
  86             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  87             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
  88             Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderImpl01.xml")));
  89         } catch (ParserConfigurationException | SAXException | IOException e) {
  90             failUnexpected(e);
  91         }
  92     }
  93 
  94     /**
  95      * Testcase to test the parse(File).
  96      */
  97     @Test
  98     public void testCheckDocumentBuilderImpl05() {
  99         try {
 100             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 101             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 102             Document doc = docBuilder.parse(new File(TestUtils.XML_DIR, "DocumentBuilderImpl01.xml"));
 103         } catch (ParserConfigurationException | SAXException | IOException e) {
 104             failUnexpected(e);
 105         }
 106     }
 107 
 108     /**
 109      * Testcase to test the parse(InputStream,systemId).
 110      */
 111     @Test
 112     public void testCheckDocumentBuilderImpl06() {
 113         try {
 114             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 115             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 116             Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderImpl02.xml")), "file:///" + TestUtils.XML_DIR
 117                     + FILE_SEP);
 118         } catch (ParserConfigurationException | SAXException | IOException e) {
 119             failUnexpected(e);
 120         }
 121     }
 122 
 123     /**
 124      * Testcase to test the setEntityResolver.
 125      */
 126     @Test
 127     public void testCheckDocumentBuilderImpl07() {
 128         try {
 129             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 130             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 131             docBuilder.setEntityResolver(this);
 132             resolveEntity("publicId", "http://www.myhost.com/today");
 133         } catch (ParserConfigurationException e) {
 134             failUnexpected(e);
 135         }
 136     }
 137 
 138     public InputSource resolveEntity(String publicId, String systemId) {
 139         if (systemId.equals("http://www.myhost.com/today"))
 140             return new InputSource(systemId);
 141         else
 142             return null;
 143     }
 144 }