< prev index next >

test/javax/xml/jaxp/functional/javax/xml/parsers/ptests/DocumentBuilderFactory02.java

Print this page




   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.USER_DIR;
  28 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  29 import static jaxp.library.JAXPTestUtilities.failUnexpected;
  30 import static org.testng.Assert.assertFalse;
  31 import static org.testng.Assert.assertTrue;
  32 
  33 import java.io.File;
  34 import java.io.IOException;
  35 
  36 import javax.xml.parsers.DocumentBuilder;
  37 import javax.xml.parsers.DocumentBuilderFactory;
  38 import javax.xml.parsers.ParserConfigurationException;


  39 import javax.xml.transform.Transformer;
  40 import javax.xml.transform.TransformerException;
  41 import javax.xml.transform.TransformerFactory;
  42 import javax.xml.transform.dom.DOMSource;
  43 import javax.xml.transform.sax.SAXResult;
  44 
  45 import org.testng.annotations.Test;
  46 import org.w3c.dom.Document;
  47 import org.xml.sax.SAXException;
  48 
  49 /**
  50  * This tests the setIgnoringElementWhitespace and setIgnoringComments of
  51  * DocumentBuilderFactory
  52  */
  53 public class DocumentBuilderFactory02 {
  54 
  55     /**
  56      * This testcase tests for the isIgnoringElementContentWhitespace and the
  57      * setIgnoringElementContentWhitespace. The xml file has all kinds of
  58      * whitespace,tab and newline characters, it uses the MyNSContentHandler
  59      * which does not invoke the characters callback when this
  60      * setIgnoringElementContentWhitespace is set to true.








  61      */
  62     @Test
  63     public void testCheckElementContentWhitespace() {
  64         try {
  65             String goldFile = TestUtils.GOLDEN_DIR + FILE_SEP + "dbfactory02GF.out";
  66             String outputFile = USER_DIR + FILE_SEP + "dbfactory02.out";
  67             MyErrorHandler eh = MyErrorHandler.newInstance();
  68             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  69             dbf.setValidating(true);
  70             assertFalse(dbf.isIgnoringElementContentWhitespace());
  71             dbf.setIgnoringElementContentWhitespace(true);
  72             DocumentBuilder db = dbf.newDocumentBuilder();
  73             db.setErrorHandler(eh);
  74             Document doc = db.parse(new File(TestUtils.XML_DIR, "DocumentBuilderFactory06.xml"));
  75             assertFalse(eh.errorOccured);
  76             DOMSource domSource = new DOMSource(doc);
  77             TransformerFactory tfactory = TransformerFactory.newInstance();
  78             Transformer transformer = tfactory.newTransformer();
  79             SAXResult saxResult = new SAXResult();
  80             saxResult.setHandler(MyCHandler.newInstance(new File(outputFile)));

  81             transformer.transform(domSource, saxResult);
  82             assertTrue(compareWithGold(goldFile, outputFile));
  83         } catch (ParserConfigurationException | SAXException | IOException | TransformerException e) {
  84             failUnexpected(e);
  85         }

  86     }
  87 }


   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.CLASS_DIR;

  27 import static jaxp.library.JAXPTestUtilities.compareWithGold;

  28 import static org.testng.Assert.assertFalse;
  29 import static org.testng.Assert.assertTrue;

  30 import java.io.File;
  31 import java.io.IOException;

  32 import javax.xml.parsers.DocumentBuilder;
  33 import javax.xml.parsers.DocumentBuilderFactory;
  34 import javax.xml.parsers.ParserConfigurationException;
  35 import static javax.xml.parsers.ptests.TestUtils.GOLDEN_DIR;
  36 import static javax.xml.parsers.ptests.TestUtils.XML_DIR;
  37 import javax.xml.transform.Transformer;
  38 import javax.xml.transform.TransformerException;
  39 import javax.xml.transform.TransformerFactory;
  40 import javax.xml.transform.dom.DOMSource;
  41 import javax.xml.transform.sax.SAXResult;
  42 import jaxp.library.JAXPFileBaseTest;
  43 import org.testng.annotations.Test;
  44 import org.w3c.dom.Document;
  45 import org.xml.sax.SAXException;
  46 
  47 /**
  48  * This tests the setIgnoringElementWhitespace and setIgnoringComments of
  49  * DocumentBuilderFactory
  50  */
  51 public class DocumentBuilderFactory02 extends JAXPFileBaseTest {

  52     /**
  53      * Test for the isIgnoringElementContentWhitespace and the
  54      * setIgnoringElementContentWhitespace. The xml file has all kinds of
  55      * whitespace,tab and newline characters, it uses the MyNSContentHandler
  56      * which does not invoke the characters callback when this
  57      * setIgnoringElementContentWhitespace is set to true.
  58      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
  59      *         created which satisfies the configuration requested.
  60      * @throws SAXException If any parse errors occur.
  61      * @throws IOException if the file exists but is a directory rather than
  62      *         a regular file, does not exist but cannot be created, or cannot 
  63      *         be opened for any other reason.
  64      * @throws TransformerException If an unrecoverable error occurs during
  65      *         the course of the transformation.
  66      */
  67     @Test
  68     public void testCheckElementContentWhitespace() throws 
  69             ParserConfigurationException, SAXException, IOException, TransformerException {
  70         String goldFile = GOLDEN_DIR + "dbfactory02GF.out";
  71         String outputFile = CLASS_DIR + "dbfactory02.out";
  72         MyErrorHandler eh = MyErrorHandler.newInstance();
  73         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  74         dbf.setValidating(true);
  75         assertFalse(dbf.isIgnoringElementContentWhitespace());
  76         dbf.setIgnoringElementContentWhitespace(true);
  77         DocumentBuilder db = dbf.newDocumentBuilder();
  78         db.setErrorHandler(eh);
  79         Document doc = db.parse(new File(XML_DIR, "DocumentBuilderFactory06.xml"));
  80         assertFalse(eh.isErrorOccured());
  81         DOMSource domSource = new DOMSource(doc);
  82         TransformerFactory tfactory = TransformerFactory.newInstance();
  83         Transformer transformer = tfactory.newTransformer();
  84         SAXResult saxResult = new SAXResult();
  85         try(MyCHandler handler = MyCHandler.newInstance(new File(outputFile))) {
  86             saxResult.setHandler(handler);
  87             transformer.transform(domSource, saxResult);



  88         }
  89         assertTrue(compareWithGold(goldFile, outputFile));
  90     }
  91 }
< prev index next >