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