< prev index next >

test/javax/xml/jaxp/functional/org/xml/sax/ptests/ResolverTest.java

Print this page


   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 package org.xml.sax.ptests;
  24 
  25 import java.io.BufferedWriter;
  26 import java.io.FileInputStream;
  27 import java.io.FileWriter;
  28 import java.io.IOException;
  29 import java.nio.file.Files;
  30 import java.nio.file.Path;
  31 import java.nio.file.Paths;
  32 import javax.xml.parsers.ParserConfigurationException;
  33 import javax.xml.parsers.SAXParser;
  34 import javax.xml.parsers.SAXParserFactory;


  35 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  36 import static jaxp.library.JAXPTestUtilities.failCleanup;
  37 import static jaxp.library.JAXPTestUtilities.failUnexpected;
  38 import static org.testng.Assert.assertTrue;
  39 import org.xml.sax.InputSource;
  40 import org.xml.sax.SAXException;
  41 import org.xml.sax.XMLReader;
  42 import org.xml.sax.helpers.XMLFilterImpl;
  43 import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR;
  44 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
  45 import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
  46 
  47 /**
  48  * Entity resolver should be invoked in XML parse. This test verifies parsing
  49  * process by checking the output with golden file.
  50  */
  51 public class ResolverTest {
  52     /**
  53      * Unit test for entityResolver setter.


  54      */
  55     public void testResolver() {
  56         String outputFile = CLASS_DIR + "EntityResolver.out";
  57         String goldFile = GOLDEN_DIR + "EntityResolverGF.out";
  58         String xmlFile = XML_DIR + "publish.xml";
  59 
  60         try(FileInputStream instream = new FileInputStream(xmlFile);
  61                 MyEntityResolver eResolver = new MyEntityResolver(outputFile)) {
  62             SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
  63             XMLReader xmlReader = saxParser.getXMLReader();
  64             xmlReader.setEntityResolver(eResolver);
  65             InputSource is = new InputSource(instream);
  66             xmlReader.parse(is);
  67         } catch(IOException | SAXException | ParserConfigurationException ex ) {
  68             failUnexpected(ex);
  69         }
  70         // Need close the output file before we compare it with golden file.
  71         try {
  72             assertTrue(compareWithGold(goldFile, outputFile));
  73         } catch (IOException ex) {
  74             failUnexpected(ex);
  75         } finally {
  76             try {
  77                 Path outputPath = Paths.get(outputFile);
  78                 if(Files.exists(outputPath))
  79                     Files.delete(outputPath);
  80             } catch (IOException ex) {
  81                 failCleanup(ex, outputFile);
  82             }
  83         }
  84     }
  85 }
  86 
  87 /**
  88  * Simple entity resolver to write every entity to an output file.
  89  */
  90 class MyEntityResolver extends XMLFilterImpl implements AutoCloseable {
  91     /**
  92      * FileWriter to write string to output file.
  93      */
  94     private final BufferedWriter bWriter;
  95 
  96     /**
  97      * Initiate FileWriter when construct a MyContentHandler.
  98      * @param outputFileName output file name.
  99      * @throws SAXException creation of FileWriter failed.
 100      */
 101     MyEntityResolver(String outputFileName) throws SAXException {
 102         super();
 103         try {


   1 /*
   2  * Copyright (c) 2003, 2015, 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 package org.xml.sax.ptests;
  24 
  25 import java.io.BufferedWriter;
  26 import java.io.FileInputStream;
  27 import java.io.FileWriter;
  28 import java.io.IOException;




  29 import javax.xml.parsers.SAXParser;
  30 import javax.xml.parsers.SAXParserFactory;
  31 import jaxp.library.JAXPFileBaseTest;
  32 import static jaxp.library.JAXPTestUtilities.USER_DIR;
  33 import static jaxp.library.JAXPTestUtilities.compareWithGold;


  34 import static org.testng.Assert.assertTrue;
  35 import org.xml.sax.InputSource;
  36 import org.xml.sax.SAXException;
  37 import org.xml.sax.XMLReader;
  38 import org.xml.sax.helpers.XMLFilterImpl;

  39 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
  40 import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
  41 
  42 /**
  43  * Entity resolver should be invoked in XML parse. This test verifies parsing
  44  * process by checking the output with golden file.
  45  */
  46 public class ResolverTest extends JAXPFileBaseTest {
  47     /**
  48      * Unit test for entityResolver setter. 
  49      * 
  50      * @throws Exception If any errors occur.
  51      */
  52     public void testResolver() throws Exception {
  53         String outputFile = USER_DIR + "EntityResolver.out";
  54         String goldFile = GOLDEN_DIR + "EntityResolverGF.out";
  55         String xmlFile = XML_DIR + "publish.xml";
  56 
  57         try(FileInputStream instream = new FileInputStream(xmlFile);
  58                 MyEntityResolver eResolver = new MyEntityResolver(outputFile)) {
  59             SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
  60             XMLReader xmlReader = saxParser.getXMLReader();
  61             xmlReader.setEntityResolver(eResolver);
  62             InputSource is = new InputSource(instream);
  63             xmlReader.parse(is);


  64         } 


  65         assertTrue(compareWithGold(goldFile, outputFile));











  66     }
  67 }
  68 
  69 /**
  70  * Simple entity resolver to write every entity to an output file.
  71  */
  72 class MyEntityResolver extends XMLFilterImpl implements AutoCloseable {
  73     /**
  74      * FileWriter to write string to output file.
  75      */
  76     private final BufferedWriter bWriter;
  77 
  78     /**
  79      * Initiate FileWriter when construct a MyContentHandler.
  80      * @param outputFileName output file name.
  81      * @throws SAXException creation of FileWriter failed.
  82      */
  83     MyEntityResolver(String outputFileName) throws SAXException {
  84         super();
  85         try {


< prev index next >