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 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 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.CLASS_DIR;
  43 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
  44 import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
  45 
  46 /**
  47  * ErrorHandler unit test. Set a ErrorHandle to XMLReader. Capture fatal error
  48  * events in ErrorHandler.
  49  */
  50 public class EHFatalTest extends JAXPFileBaseTest {
  51     /**
  52      * Error Handler to capture all error events to output file. Verifies the
  53      * output file is same as golden file.
  54      * 
  55      * @throws IOException if the file exists but is a directory rather than
  56      *         a regular file, does not exist but cannot be created, or cannot 
  57      *         be opened for any other reason.
  58      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
  59      *         created which satisfies the configuration requested.
  60      */
  61     @Test
  62     public void testEHFatal() throws ParserConfigurationException, IOException {
  63         String outputFile = CLASS_DIR + "EHFatal.out";
  64         String goldFile = GOLDEN_DIR + "EHFatalGF.out";
  65         String xmlFile = XML_DIR + "invalid.xml";
  66 
  67         try(MyErrorHandler eHandler = new MyErrorHandler(outputFile);
  68                 FileInputStream instream = new FileInputStream(xmlFile)) {
  69             SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
  70             XMLReader xmlReader = saxParser.getXMLReader();
  71             xmlReader.setErrorHandler(eHandler);
  72             InputSource is = new InputSource(instream);
  73             xmlReader.parse(is);
  74             fail("Parse should throw SAXException");
  75         } catch (SAXException expected) {
  76             // This is expected.

  77         }
  78         // Need close the output file before we compare it with golden file.

  79         assertTrue(compareWithGold(goldFile, outputFile));











  80     }
  81 }
  82 
  83 /**
  84  * A fatal error event handler only capture fatal error event and write event to
  85  * output file.
  86  */
  87 class MyErrorHandler extends XMLFilterImpl implements AutoCloseable {
  88     /**
  89      * FileWriter to write string to output file.
  90      */
  91     private final BufferedWriter bWriter;
  92 
  93     /**
  94      * Initiate FileWriter when construct a MyContentHandler.
  95      * @param outputFileName output file name.
  96      * @throws SAXException creation of FileWriter failed.
  97      */
  98     MyErrorHandler(String outputFileName) throws SAXException {
  99         super();
 100         try {
 101             bWriter = new BufferedWriter(new FileWriter(outputFileName));
 102         } catch (IOException ex) {
 103             throw new SAXException(ex);
 104         }
 105     }
 106 
 107     /**
 108      * Write fatalError tag along with exception to the file when meet
 109      * unrecoverable error event.
 110      * @throws IOException error happen when writing file.
 111      */
 112     @Override
 113     public void fatalError(SAXParseException e) throws SAXException {
 114         String str = "In fatalError..\nSAXParseException: " + e.getMessage();
 115         try {
 116             bWriter.write( str, 0,str.length());
 117             bWriter.newLine();
 118         } catch (IOException ex) {
 119             throw new SAXException(ex);
 120         }
 121     }
 122 
 123     /**
 124      * Flush the content and close the file.
 125      * @throws IOException error happen when writing file or closing file.
 126      */
 127     @Override
 128     public void close() throws IOException {
 129         bWriter.flush();
 130         bWriter.close();
 131     }
 132 }
--- EOF ---