1 /*
2 * Copyright (c) 2014, 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 */
24 package sax;
25
26 import java.io.IOException;
27 import java.io.StringReader;
28
29 import javax.xml.parsers.ParserConfigurationException;
30 import javax.xml.parsers.SAXParser;
31 import javax.xml.parsers.SAXParserFactory;
32
33 import org.testng.Assert;
34 import org.testng.annotations.Listeners;
35 import org.testng.annotations.Test;
36 import org.xml.sax.Attributes;
37 import org.xml.sax.ErrorHandler;
38 import org.xml.sax.InputSource;
39 import org.xml.sax.SAXException;
40 import org.xml.sax.helpers.DefaultHandler;
41
42 /*
43 * @test
44 * @bug 6809409
45 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
46 * @run testng/othervm -DrunSecMngr=true sax.IssueTracker56Test
47 * @run testng/othervm sax.IssueTracker56Test
48 * @summary Test SAXException has Cause.
49 */
50 @Listeners({jaxp.library.BasePolicy.class})
51 public class IssueTracker56Test {
52
53 @Test
54 public void testException() {
55 try {
56 SAXParserFactory spf = SAXParserFactory.newInstance();
57 SAXParser parser = spf.newSAXParser();
58 String xmlToParse = "<root>Issue 56: SAXException does not do the exception chaining properly</root>";
59 InputSource source = new InputSource(new StringReader(xmlToParse));
60 parser.parse(source, new MyHandler());
61 } catch (SAXException ex) {
62 System.out.println(ex.getCause());
63 if (ex.getCause() == null)
64 Assert.fail("failed chaining exception properly.");
65 // ex.printStackTrace(); //will not print out root cause without the
66 // fix
67 } catch (IOException ex) {
68 // shouldn't happen
71 }
72 }
73
74 @Test
75 public void testWorkAround() throws Exception {
76 try {
77 SAXParserFactory spf = SAXParserFactory.newInstance();
78 SAXParser parser = spf.newSAXParser();
79 String xmlToParse = "<root>Issue 56: SAXException does not do the exception chaining properly</root>";
80 InputSource source = new InputSource(new StringReader(xmlToParse));
81 parser.parse(source, new MyHandler1());
82 } catch (SAXException ex) {
83 System.out.println(ex.getCause());
84 // ex.printStackTrace(); //will print out root cause
85 } catch (IOException ex) {
86 // shouldn't happen
87 } catch (ParserConfigurationException ex) {
88 // shouldn't happen
89 }
90
91 }
92
93 public class MyHandler extends DefaultHandler implements ErrorHandler {
94
95 public void startDocument() throws SAXException {
96 }
97
98 public void endDocument() throws SAXException {
99 }
100
101 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
102 try {
103 System.out.println(uri);
104 System.out.println(uri.charAt(56));
105 } catch (Exception e) {
106 throw new SAXException(e);
107 }
108
109 }
110
|
1 /*
2 * Copyright (c) 2014, 2017, 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 */
24 package sax;
25
26 import java.io.IOException;
27 import java.io.StringReader;
28
29 import javax.xml.parsers.ParserConfigurationException;
30 import javax.xml.parsers.SAXParser;
31 import javax.xml.parsers.SAXParserFactory;
32
33 import org.testng.Assert;
34 import org.testng.annotations.Listeners;
35 import org.testng.annotations.Test;
36 import org.xml.sax.Attributes;
37 import org.xml.sax.ErrorHandler;
38 import org.xml.sax.InputSource;
39 import org.xml.sax.SAXException;
40 import org.xml.sax.helpers.DefaultHandler;
41
42 /*
43 * @test
44 * @bug 6809409 6857903
45 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
46 * @run testng/othervm -DrunSecMngr=true sax.IssueTracker56Test
47 * @run testng/othervm sax.IssueTracker56Test
48 * @summary Test SAXException has Cause and that Cause can be properly
49 * initialized with initCause.
50 */
51 @Listeners({jaxp.library.BasePolicy.class})
52 public class IssueTracker56Test {
53
54 @Test
55 public void testException() {
56 try {
57 SAXParserFactory spf = SAXParserFactory.newInstance();
58 SAXParser parser = spf.newSAXParser();
59 String xmlToParse = "<root>Issue 56: SAXException does not do the exception chaining properly</root>";
60 InputSource source = new InputSource(new StringReader(xmlToParse));
61 parser.parse(source, new MyHandler());
62 } catch (SAXException ex) {
63 System.out.println(ex.getCause());
64 if (ex.getCause() == null)
65 Assert.fail("failed chaining exception properly.");
66 // ex.printStackTrace(); //will not print out root cause without the
67 // fix
68 } catch (IOException ex) {
69 // shouldn't happen
72 }
73 }
74
75 @Test
76 public void testWorkAround() throws Exception {
77 try {
78 SAXParserFactory spf = SAXParserFactory.newInstance();
79 SAXParser parser = spf.newSAXParser();
80 String xmlToParse = "<root>Issue 56: SAXException does not do the exception chaining properly</root>";
81 InputSource source = new InputSource(new StringReader(xmlToParse));
82 parser.parse(source, new MyHandler1());
83 } catch (SAXException ex) {
84 System.out.println(ex.getCause());
85 // ex.printStackTrace(); //will print out root cause
86 } catch (IOException ex) {
87 // shouldn't happen
88 } catch (ParserConfigurationException ex) {
89 // shouldn't happen
90 }
91
92 }
93
94 /*
95 * Test that SAXException::initCause call correctly initializes
96 * cause and it can be acquired with SAXException::getCause call
97 */
98 @Test
99 public void testInitCause() {
100 Exception cause = new Exception();
101 SAXException e = new SAXException("SAX exception");
102 e.initCause(cause);
103 Assert.assertSame(e.getCause(),cause,"Cause was not set by initCause:");
104 }
105
106 public class MyHandler extends DefaultHandler implements ErrorHandler {
107
108 public void startDocument() throws SAXException {
109 }
110
111 public void endDocument() throws SAXException {
112 }
113
114 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
115 try {
116 System.out.println(uri);
117 System.out.println(uri.charAt(56));
118 } catch (Exception e) {
119 throw new SAXException(e);
120 }
121
122 }
123
|