jaxp/test/javax/xml/jaxp/functional/javax/xml/parsers/ptests/DBFNamespaceTest.java
changeset 27983 f5f19fe0e83b
child 28445 5a87f52ca380
equal deleted inserted replaced
27982:3cafd24fcc3e 27983:f5f19fe0e83b
       
     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.USER_DIR;
       
    28 import static jaxp.library.JAXPTestUtilities.compareWithGold;
       
    29 import static jaxp.library.JAXPTestUtilities.failUnexpected;
       
    30 import static org.testng.Assert.assertTrue;
       
    31 
       
    32 import java.io.File;
       
    33 import java.io.IOException;
       
    34 
       
    35 import javax.xml.parsers.DocumentBuilderFactory;
       
    36 import javax.xml.parsers.ParserConfigurationException;
       
    37 import javax.xml.transform.Transformer;
       
    38 import javax.xml.transform.TransformerException;
       
    39 import javax.xml.transform.TransformerFactory;
       
    40 import javax.xml.transform.TransformerFactoryConfigurationError;
       
    41 import javax.xml.transform.dom.DOMSource;
       
    42 import javax.xml.transform.sax.SAXResult;
       
    43 
       
    44 import org.testng.annotations.DataProvider;
       
    45 import org.testng.annotations.Test;
       
    46 import org.w3c.dom.Document;
       
    47 import org.xml.sax.SAXException;
       
    48 
       
    49 /**
       
    50  * This tests DocumentBuilderFactory for namespace processing and no-namespace
       
    51  * processing.
       
    52  */
       
    53 public class DBFNamespaceTest {
       
    54 
       
    55     /**
       
    56      * Provide input for the cases that supporting namespace or not.
       
    57      */
       
    58     @DataProvider(name = "input-provider")
       
    59     public Object[][] getInput() {
       
    60         DocumentBuilderFactory dbf1 = DocumentBuilderFactory.newInstance();
       
    61         String outputfile1 = USER_DIR + FILE_SEP + "dbfnstest01.out";
       
    62         String goldfile1 = TestUtils.GOLDEN_DIR + FILE_SEP + "dbfnstest01GF.out";
       
    63 
       
    64         DocumentBuilderFactory dbf2 = DocumentBuilderFactory.newInstance();
       
    65         dbf2.setNamespaceAware(true);
       
    66         String outputfile2 = USER_DIR + FILE_SEP + "dbfnstest02.out";
       
    67         String goldfile2 = TestUtils.GOLDEN_DIR + FILE_SEP + "dbfnstest02GF.out";
       
    68         return new Object[][] { { dbf1, outputfile1, goldfile1 }, { dbf2, outputfile2, goldfile2 } };
       
    69     }
       
    70 
       
    71     /**
       
    72      * Test to parse and transform a document without supporting namespace and
       
    73      * with supporting namespace.
       
    74      */
       
    75     @Test(dataProvider = "input-provider")
       
    76     public void testNamespaceTest(DocumentBuilderFactory dbf, String outputfile, String goldfile) {
       
    77         try {
       
    78             Document doc = dbf.newDocumentBuilder().parse(new File(TestUtils.XML_DIR, "namespace1.xml"));
       
    79             dummyTransform(doc, outputfile);
       
    80             assertTrue(compareWithGold(goldfile, outputfile));
       
    81         } catch (SAXException | IOException | ParserConfigurationException | TransformerFactoryConfigurationError | TransformerException e) {
       
    82             failUnexpected(e);
       
    83         }
       
    84     }
       
    85 
       
    86     /**
       
    87      * This method transforms a Node without any xsl file and uses SAXResult to
       
    88      * invoke the callbacks through a ContentHandler. If namespace processing is
       
    89      * not chosen, namespaceURI in callbacks should be an empty string otherwise
       
    90      * it should be namespaceURI.
       
    91      *
       
    92      * @throws TransformerFactoryConfigurationError
       
    93      * @throws TransformerException
       
    94      * @throws IOException
       
    95      */
       
    96     private void dummyTransform(Document document, String fileName) throws TransformerFactoryConfigurationError, TransformerException, IOException {
       
    97         DOMSource domSource = new DOMSource(document);
       
    98         Transformer transformer = TransformerFactory.newInstance().newTransformer();
       
    99         File file = new File(fileName);
       
   100         System.out.println("The fileName is " + file.getAbsolutePath());
       
   101         transformer.transform(domSource, new SAXResult(MyCHandler.newInstance(file)));
       
   102     }
       
   103 
       
   104 }