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 
  24 package javax.xml.transform.ptests;
  25 
  26 import java.io.File;
  27 import javax.xml.transform.ErrorListener;
  28 import javax.xml.transform.TransformerConfigurationException;
  29 import javax.xml.transform.TransformerException;
  30 import javax.xml.transform.TransformerFactory;
  31 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  32 import javax.xml.transform.stream.StreamSource;
  33 import static org.testng.Assert.assertEquals;
  34 import static org.testng.Assert.fail;
  35 import org.testng.annotations.Test;
  36 
  37 /**
  38  * Class containing the test cases for ErrorListener interface
  39  */
  40 public class ErrorListenerTest implements ErrorListener {
  41     /**
  42      * Define ErrorListener's status.
  43      */
  44     private static enum ListenerStatus{NOT_INVOKED, ERROR, WARNING, FATAL};
  45 
  46     /**
  47      * No ErrorListener invoked at the beginning.
  48      */
  49     private volatile ListenerStatus status = ListenerStatus.NOT_INVOKED;
  50 
  51     /**
  52      * Expect a TransformerConfigurationException when transforming a file
  53      * invalid.xsl that has some well-formedness error.
  54      */
  55     @Test
  56     public void errorListener01() {
  57         ErrorListenerTest listener = new ErrorListenerTest();
  58         try {
  59             TransformerFactory tfactory = TransformerFactory.newInstance();
  60             tfactory.setErrorListener (listener);
  61             tfactory.newTransformer(new StreamSource(
  62                                         new File(XML_DIR + "invalid.xsl")));
  63             fail("We expect an Exception here");
  64         } catch (TransformerConfigurationException ex) {
  65             assertEquals(listener.status, ListenerStatus.FATAL);
  66         }
  67     }
  68 
  69     /**
  70      * Set status as ERROR when receiving notification of a recoverable error.
  71      * @param e The error information encapsulated in a transformer exception.
  72      */
  73     @Override
  74     public void error (TransformerException e) {
  75         this.status = ListenerStatus.ERROR;
  76     }
  77 
  78     /**
  79      * Set status as WARNING when receiving notification of a warning.
  80      * @param e The error information encapsulated in a transformer exception.
  81      */
  82     @Override
  83     public void warning (TransformerException e) {
  84         this.status = ListenerStatus.WARNING;
  85     }
  86 
  87     /**
  88      * Set status as FATAL when receiving notification of a non-recoverable error.
  89      * @param e The error information encapsulated in a transformer exception.
  90      */
  91     @Override
  92     public void fatalError (TransformerException e) {
  93         this.status = ListenerStatus.FATAL;
  94     }
  95 }