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