test/jaxp/javax/xml/jaxp/unittest/transform/ErrorListenerTest.java
changeset 55742 6e1161923897
child 58022 12885822f0c5
equal deleted inserted replaced
55741:880266b6e5b3 55742:6e1161923897
       
     1 /*
       
     2  * Copyright (c) 2019, 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 transform;
       
    25 
       
    26 import java.io.ByteArrayInputStream;
       
    27 import java.io.ByteArrayOutputStream;
       
    28 import java.io.InputStream;
       
    29 import java.io.PrintStream;
       
    30 
       
    31 import javax.xml.transform.ErrorListener;
       
    32 import javax.xml.transform.TransformerConfigurationException;
       
    33 import javax.xml.transform.TransformerException;
       
    34 import javax.xml.transform.TransformerFactory;
       
    35 import javax.xml.transform.sax.SAXSource;
       
    36 import org.testng.Assert;
       
    37 import org.testng.annotations.AfterClass;
       
    38 import org.testng.annotations.BeforeClass;
       
    39 import org.testng.annotations.Test;
       
    40 import org.xml.sax.InputSource;
       
    41 
       
    42 /*
       
    43  * @test
       
    44  * @bug 8157830
       
    45  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
       
    46  * @run testng/othervm transform.ErrorListenerTest
       
    47  * @summary Verifies that ErrorListeners are handled properly
       
    48  */
       
    49 public class ErrorListenerTest {
       
    50 
       
    51     static final private String INVALID_STYLESHEET = "xxx";
       
    52     static final private String SYSTEM_ID = "http://openjdk_java_net/xsl/dummy.xsl";
       
    53 
       
    54     PrintStream original;
       
    55 
       
    56     @BeforeClass
       
    57     public void setUpClass() throws Exception {
       
    58         // save the PrintStream
       
    59         original = System.err;
       
    60     }
       
    61 
       
    62     @AfterClass
       
    63     protected void tearDown() throws Exception {
       
    64         // set back to the original
       
    65         System.setErr(original);
       
    66     }
       
    67 
       
    68     /**
       
    69      * Verifies that when an ErrorListener is registered, parser errors are passed
       
    70      * onto the listener without other output.
       
    71      *
       
    72      * @throws Exception
       
    73      */
       
    74     @Test
       
    75     public void test() throws Exception {
       
    76         InputStream is = new ByteArrayInputStream(INVALID_STYLESHEET.getBytes());
       
    77         InputSource source = new InputSource(is);
       
    78         source.setSystemId(SYSTEM_ID);
       
    79 
       
    80         ByteArrayOutputStream baos = new ByteArrayOutputStream();
       
    81         PrintStream ps = new PrintStream(baos);
       
    82         System.setErr(ps);
       
    83 
       
    84         TransformerFactory factory = TransformerFactory.newInstance();
       
    85         factory.setErrorListener(new ErrListener());
       
    86 
       
    87         try {
       
    88             factory.newTransformer(new SAXSource(source));
       
    89         } catch (TransformerConfigurationException e) {
       
    90             // nothing
       
    91         }
       
    92 
       
    93         // all errors are handled by the ErrorListener, no other output
       
    94         Assert.assertEquals(baos.toString(), "");
       
    95 
       
    96     }
       
    97 
       
    98     class ErrListener implements ErrorListener {
       
    99 
       
   100         @Override
       
   101         public void error(TransformerException exception)
       
   102                 throws TransformerException {
       
   103             System.out.println("Correctly handled error: " + exception.getMessage());
       
   104         }
       
   105 
       
   106         @Override
       
   107         public void fatalError(TransformerException exception)
       
   108                 throws TransformerException {
       
   109             System.out.println("Correctly handled fatal: " + exception.getMessage());
       
   110         }
       
   111 
       
   112         @Override
       
   113         public void warning(TransformerException exception)
       
   114                 throws TransformerException {
       
   115             System.out.println("Correctly handled warning: " + exception.getMessage());
       
   116         }
       
   117     }
       
   118 }