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