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