1 /*
2 * Copyright (c) 2014, 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 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.Test;
35 import org.xml.sax.Attributes;
36 import org.xml.sax.ErrorHandler;
37 import org.xml.sax.InputSource;
38 import org.xml.sax.SAXException;
39 import org.xml.sax.helpers.DefaultHandler;
40
41 /*
42 * @bug 6809409
43 * @summary Test SAXException has Cause.
44 */
45 public class IssueTracker56Test {
46
47 @Test
48 public void testException() {
49 try {
50 SAXParserFactory spf = SAXParserFactory.newInstance();
51 SAXParser parser = spf.newSAXParser();
52 String xmlToParse = "<root>Issue 56: SAXException does not do the exception chaining properly</root>";
53 InputSource source = new InputSource(new StringReader(xmlToParse));
54 parser.parse(source, new MyHandler());
55 } catch (SAXException ex) {
56 System.out.println(ex.getCause());
57 if (ex.getCause() == null)
58 Assert.fail("failed chaining exception properly.");
59 // ex.printStackTrace(); //will not print out root cause without the
60 // fix
61 } catch (IOException ex) {
62 // shouldn't happen
63 } catch (ParserConfigurationException ex) {
64 // shouldn't happen
67
68 @Test
69 public void testWorkAround() throws Exception {
70 try {
71 SAXParserFactory spf = SAXParserFactory.newInstance();
72 SAXParser parser = spf.newSAXParser();
73 String xmlToParse = "<root>Issue 56: SAXException does not do the exception chaining properly</root>";
74 InputSource source = new InputSource(new StringReader(xmlToParse));
75 parser.parse(source, new MyHandler1());
76 } catch (SAXException ex) {
77 System.out.println(ex.getCause());
78 // ex.printStackTrace(); //will print out root cause
79 } catch (IOException ex) {
80 // shouldn't happen
81 } catch (ParserConfigurationException ex) {
82 // shouldn't happen
83 }
84
85 }
86
87 public class MyHandler extends DefaultHandler implements ErrorHandler {
88
89 public void startDocument() throws SAXException {
90 }
91
92 public void endDocument() throws SAXException {
93 }
94
95 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
96 try {
97 System.out.println(uri);
98 System.out.println(uri.charAt(56));
99 } catch (Exception e) {
100 throw new SAXException(e);
101 }
102
103 }
104
105 public void endElement(String uri, String localName, String qName) throws SAXException {
106 }
107
108 public void characters(char ch[], int start, int length) throws SAXException {
109 }
110
111 }
112
113 public class MyHandler1 extends DefaultHandler implements ErrorHandler {
114
115 public void startDocument() throws SAXException {
116 }
117
118 public void endDocument() throws SAXException {
119 }
120
121 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXExceptionExt {
122 try {
123 System.out.println(uri);
124 System.out.println(uri.charAt(56));
125 } catch (Exception e) {
126 throw new SAXExceptionExt(e);
127 }
128
129 }
130
131 public void endElement(String uri, String localName, String qName) throws SAXException {
132 }
133
|
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 */
23
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 * @bug 6809409
44 * @summary Test SAXException has Cause.
45 */
46 @Listeners({jaxp.library.BasePolicy.class})
47 public class IssueTracker56Test {
48
49 @Test
50 public void testException() {
51 try {
52 SAXParserFactory spf = SAXParserFactory.newInstance();
53 SAXParser parser = spf.newSAXParser();
54 String xmlToParse = "<root>Issue 56: SAXException does not do the exception chaining properly</root>";
55 InputSource source = new InputSource(new StringReader(xmlToParse));
56 parser.parse(source, new MyHandler());
57 } catch (SAXException ex) {
58 System.out.println(ex.getCause());
59 if (ex.getCause() == null)
60 Assert.fail("failed chaining exception properly.");
61 // ex.printStackTrace(); //will not print out root cause without the
62 // fix
63 } catch (IOException ex) {
64 // shouldn't happen
65 } catch (ParserConfigurationException ex) {
66 // shouldn't happen
69
70 @Test
71 public void testWorkAround() throws Exception {
72 try {
73 SAXParserFactory spf = SAXParserFactory.newInstance();
74 SAXParser parser = spf.newSAXParser();
75 String xmlToParse = "<root>Issue 56: SAXException does not do the exception chaining properly</root>";
76 InputSource source = new InputSource(new StringReader(xmlToParse));
77 parser.parse(source, new MyHandler1());
78 } catch (SAXException ex) {
79 System.out.println(ex.getCause());
80 // ex.printStackTrace(); //will print out root cause
81 } catch (IOException ex) {
82 // shouldn't happen
83 } catch (ParserConfigurationException ex) {
84 // shouldn't happen
85 }
86
87 }
88
89 @Listeners({jaxp.library.BasePolicy.class})
90 public class MyHandler extends DefaultHandler implements ErrorHandler {
91
92 public void startDocument() throws SAXException {
93 }
94
95 public void endDocument() throws SAXException {
96 }
97
98 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
99 try {
100 System.out.println(uri);
101 System.out.println(uri.charAt(56));
102 } catch (Exception e) {
103 throw new SAXException(e);
104 }
105
106 }
107
108 public void endElement(String uri, String localName, String qName) throws SAXException {
109 }
110
111 public void characters(char ch[], int start, int length) throws SAXException {
112 }
113
114 }
115
116 @Listeners({jaxp.library.BasePolicy.class})
117 public class MyHandler1 extends DefaultHandler implements ErrorHandler {
118
119 public void startDocument() throws SAXException {
120 }
121
122 public void endDocument() throws SAXException {
123 }
124
125 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXExceptionExt {
126 try {
127 System.out.println(uri);
128 System.out.println(uri.charAt(56));
129 } catch (Exception e) {
130 throw new SAXExceptionExt(e);
131 }
132
133 }
134
135 public void endElement(String uri, String localName, String qName) throws SAXException {
136 }
137
|