< prev index next >

test/javax/xml/jaxp/functional/javax/xml/transform/ptests/SAXTFactoryTest002.java

Print this page




   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 package javax.xml.transform.ptests;
  24 
  25 import java.io.FileInputStream;
  26 import java.io.FileOutputStream;
  27 import java.io.IOException;
  28 import java.nio.file.Files;
  29 import java.nio.file.Path;
  30 import java.nio.file.Paths;
  31 import javax.xml.transform.Result;
  32 import javax.xml.transform.TransformerConfigurationException;
  33 import javax.xml.transform.TransformerFactory;
  34 import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
  35 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
  36 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  37 import javax.xml.transform.sax.SAXSource;
  38 import javax.xml.transform.sax.SAXTransformerFactory;
  39 import javax.xml.transform.sax.TransformerHandler;
  40 import javax.xml.transform.stream.StreamResult;

  41 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  42 import static jaxp.library.JAXPTestUtilities.failCleanup;
  43 import static jaxp.library.JAXPTestUtilities.failUnexpected;
  44 import static org.testng.Assert.assertTrue;
  45 import org.testng.annotations.Test;
  46 import org.xml.sax.InputSource;
  47 import org.xml.sax.SAXException;
  48 import org.xml.sax.XMLReader;
  49 import org.xml.sax.helpers.XMLReaderFactory;
  50 
  51 /**
  52  * Test newTransformerhandler() method which takes SAXSource as argument can
  53  * be set to XMLReader.
  54  */
  55 public class SAXTFactoryTest002 {
  56     /**
  57      * SAXTFactory.newTransformerhandler() method which takes SAXSource as
  58      * argument can be set to XMLReader. SAXSource has input XML file as its
  59      * input source. XMLReader has a content handler which write out the result
  60      * to output file. Test verifies output file is same as golden file.







  61      */
  62     @Test
  63     public void testcase01() {

  64         String outputFile = CLASS_DIR + "saxtf002.out";
  65         String goldFile = GOLDEN_DIR + "saxtf002GF.out";
  66         String xsltFile = XML_DIR + "cities.xsl";
  67         String xmlFile = XML_DIR + "cities.xml";
  68 
  69         try (FileOutputStream fos = new FileOutputStream(outputFile);
  70                 FileInputStream fis = new FileInputStream(xsltFile)) {
  71             XMLReader reader = XMLReaderFactory.createXMLReader();
  72             SAXTransformerFactory saxTFactory
  73                     = (SAXTransformerFactory) TransformerFactory.newInstance();
  74             SAXSource ss = new SAXSource();
  75             ss.setInputSource(new InputSource(fis));
  76 
  77             TransformerHandler handler = saxTFactory.newTransformerHandler(ss);
  78             Result result = new StreamResult(fos);
  79             handler.setResult(result);
  80             reader.setContentHandler(handler);
  81             reader.parse(xmlFile);
  82             assertTrue(compareWithGold(goldFile, outputFile));
  83         } catch (SAXException | IOException | TransformerConfigurationException ex) {
  84             failUnexpected(ex);
  85         } finally {
  86             try {
  87                 Path outputPath = Paths.get(outputFile);
  88                 if(Files.exists(outputPath))
  89                     Files.delete(outputPath);
  90             } catch (IOException ex) {
  91                 failCleanup(ex, outputFile);
  92             }
  93         }

  94     }
  95 }


   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 package javax.xml.transform.ptests;
  24 
  25 import java.io.FileInputStream;
  26 import java.io.FileOutputStream;
  27 import java.io.IOException;



  28 import javax.xml.transform.Result;
  29 import javax.xml.transform.TransformerConfigurationException;
  30 import javax.xml.transform.TransformerFactory;
  31 import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
  32 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
  33 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  34 import javax.xml.transform.sax.SAXSource;
  35 import javax.xml.transform.sax.SAXTransformerFactory;
  36 import javax.xml.transform.sax.TransformerHandler;
  37 import javax.xml.transform.stream.StreamResult;
  38 import jaxp.library.JAXPFileBaseTest;
  39 import static jaxp.library.JAXPTestUtilities.compareWithGold;


  40 import static org.testng.Assert.assertTrue;
  41 import org.testng.annotations.Test;
  42 import org.xml.sax.InputSource;
  43 import org.xml.sax.SAXException;
  44 import org.xml.sax.XMLReader;
  45 import org.xml.sax.helpers.XMLReaderFactory;
  46 
  47 /**
  48  * Test newTransformerhandler() method which takes SAXSource as argument can
  49  * be set to XMLReader.
  50  */
  51 public class SAXTFactoryTest002 extends JAXPFileBaseTest {
  52     /**
  53      * SAXTFactory.newTransformerhandler() method which takes SAXSource as
  54      * argument can be set to XMLReader. SAXSource has input XML file as its
  55      * input source. XMLReader has a content handler which write out the result
  56      * to output file. Test verifies output file is same as golden file.
  57      * 
  58      * @throws SAXException If any parse errors occur.
  59      * @throws IOException if the file exists but is a directory rather than
  60      *         a regular file, does not exist but cannot be created, or cannot 
  61      *         be opened for any other reason.
  62      * @throws TransformerConfigurationException If for some reason the
  63      *         TransformerHandler can not be created.
  64      */
  65     @Test
  66     public void testcase01() throws IOException, SAXException, 
  67             TransformerConfigurationException {
  68         String outputFile = CLASS_DIR + "saxtf002.out";
  69         String goldFile = GOLDEN_DIR + "saxtf002GF.out";
  70         String xsltFile = XML_DIR + "cities.xsl";
  71         String xmlFile = XML_DIR + "cities.xml";
  72 
  73         try (FileOutputStream fos = new FileOutputStream(outputFile);
  74                 FileInputStream fis = new FileInputStream(xsltFile)) {
  75             XMLReader reader = XMLReaderFactory.createXMLReader();
  76             SAXTransformerFactory saxTFactory
  77                     = (SAXTransformerFactory) TransformerFactory.newInstance();
  78             SAXSource ss = new SAXSource();
  79             ss.setInputSource(new InputSource(fis));
  80 
  81             TransformerHandler handler = saxTFactory.newTransformerHandler(ss);
  82             Result result = new StreamResult(fos);
  83             handler.setResult(result);
  84             reader.setContentHandler(handler);
  85             reader.parse(xmlFile);











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