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.testng.annotations.Test;
  40 import org.xml.sax.InputSource;
  41 import org.xml.sax.SAXException;
  42 import org.xml.sax.SAXParseException;
  43 import org.xml.sax.XMLReader;
  44 import org.xml.sax.helpers.XMLFilterImpl;
  45 import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR;
  46 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
  47 import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
  48 
  49 /**
  50  * ErrorHandler unit test. Set a ErrorHandle to XMLReader. Capture fatal error 
  51  * events in ErrorHandler.
  52  */
  53 public class EHFatalTest {
  54     /**
  55      * Error Handler to capture all error events to output file. Verifies the 
  56      * output file is same as golden file.
  57      */
  58     @Test
  59     public void testEHFatal() {
  60         String outputFile = CLASS_DIR + "EHFatal.out";
  61         String goldFile = GOLDEN_DIR + "EHFatalGF.out";
  62         String xmlFile = XML_DIR + "invalid.xml";
  63 
  64         try(MyErrorHandler eHandler = new MyErrorHandler(outputFile);
  65                 FileInputStream instream = new FileInputStream(xmlFile)) {
  66             SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
  67             XMLReader xmlReader = saxParser.getXMLReader();
  68             xmlReader.setErrorHandler(eHandler);
  69             InputSource is = new InputSource(instream);
  70             xmlReader.parse(is);
  71         } catch (IOException | ParserConfigurationException ex) {
  72             failUnexpected(ex);
  73         } catch (SAXException ex) {
  74             System.out.println("This is expected:" + ex);
  75         }
  76         // Need close the output file before we compare it with golden file.
  77         try {
  78             assertTrue(compareWithGold(goldFile, outputFile));
  79         } catch (IOException ex) {
  80             failUnexpected(ex);
  81         } finally {
  82             try {
  83                 Path outputPath = Paths.get(outputFile);
  84                 if(Files.exists(outputPath))
  85                     Files.delete(outputPath);
  86             } catch (IOException ex) {
  87                 failCleanup(ex, outputFile);
  88             }
  89         }
  90     }
  91 }
  92 
  93 /**
  94  * A fatal error event handler only capture fatal error event and write event to
  95  * output file.
  96  */
  97 class MyErrorHandler extends XMLFilterImpl implements AutoCloseable {
  98     /**
  99      * FileWriter to write string to output file.
 100      */
 101     private final BufferedWriter bWriter;
 102 
 103     /**
 104      * Initiate FileWriter when construct a MyContentHandler.
 105      * @param outputFileName output file name.
 106      * @throws SAXException creation of FileWriter failed.
 107      */
 108     MyErrorHandler(String outputFileName) throws SAXException {
 109         super();
 110         try {
 111             bWriter = new BufferedWriter(new FileWriter(outputFileName));
 112         } catch (IOException ex) {
 113             throw new SAXException(ex);
 114         }
 115     }
 116 
 117     /**
 118      * Write fatalError tag along with exception to the file when meet 
 119      * unrecoverable error event.
 120      * @throws IOException error happen when writing file.
 121      */
 122     @Override
 123     public void fatalError(SAXParseException e) throws SAXException {
 124         String str = "In fatalError..\nSAXParseException: " + e.getMessage();
 125         try {
 126             bWriter.write( str, 0,str.length());
 127             bWriter.newLine();
 128         } catch (IOException ex) {
 129             throw new SAXException(ex);
 130         }
 131     }
 132 
 133     /**
 134      * Flush the content and close the file.
 135      * @throws IOException error happen when writing file or closing file.
 136      */
 137     @Override
 138     public void close() throws IOException {
 139         bWriter.flush();
 140         bWriter.close();
 141     }
 142 }