< prev index next >

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

Print this page




   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 {




   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.ParserConfigurationException;
  30 import javax.xml.parsers.SAXParser;
  31 import javax.xml.parsers.SAXParserFactory;
  32 import jaxp.library.JAXPFileBaseTest;
  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.CLASS_DIR;
  40 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
  41 import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
  42 
  43 /**
  44  * Entity resolver should be invoked in XML parse. This test verifies parsing
  45  * process by checking the output with golden file.
  46  */
  47 public class ResolverTest extends JAXPFileBaseTest {
  48     /**
  49      * Unit test for entityResolver setter. 
  50      * 
  51      * @throws SAXException If there is a problem processing the document.
  52      * @throws IOException if the file exists but is a directory rather than
  53      *         a regular file, does not exist but cannot be created, or cannot 
  54      *         be opened for any other reason.
  55      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
  56      *         created which satisfies the configuration requested.
  57      */
  58     public void testResolver() throws IOException, SAXException, 
  59             ParserConfigurationException {
  60         String outputFile = CLASS_DIR + "EntityResolver.out";
  61         String goldFile = GOLDEN_DIR + "EntityResolverGF.out";
  62         String xmlFile = XML_DIR + "publish.xml";
  63 
  64         try(FileInputStream instream = new FileInputStream(xmlFile);
  65                 MyEntityResolver eResolver = new MyEntityResolver(outputFile)) {
  66             SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
  67             XMLReader xmlReader = saxParser.getXMLReader();
  68             xmlReader.setEntityResolver(eResolver);
  69             InputSource is = new InputSource(instream);
  70             xmlReader.parse(is);


  71         } 


  72         assertTrue(compareWithGold(goldFile, outputFile));











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


< prev index next >