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.TEMP_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 = TEMP_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 }