1
/* Copyright 2005 Elliotte Rusty Harold
3
This library is free software; you can redistribute it and/or modify
4
it under the terms of version 2.1 of the GNU Lesser General Public
5
License as published by the Free Software Foundation.
7
This library is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU Lesser General Public License for more details.
12
You should have received a copy of the GNU Lesser General Public
13
License along with this library; if not, write to the
14
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15
Boston, MA 02111-1307 USA
17
You can contact Elliotte Rusty Harold by sending e-mail to
18
elharo@metalab.unc.edu. Please include the word "XOM" in the
19
subject line. The XOM home page is located at http://www.xom.nu/
24
import java.io.ByteArrayOutputStream;
25
import java.io.IOException;
26
import java.io.ObjectOutputStream;
28
import nu.xom.Element;
29
import nu.xom.XPathException;
30
import nu.xom.XPathTypeException;
34
* Unit tests for the <code>XPathException</code> class.
37
* @author Elliotte Rusty Harold
41
public class XPathExceptionTest extends XOMTestCase {
44
private XPathException ex;
45
private Exception cause;
48
public XPathExceptionTest(String name) {
53
protected void setUp() {
54
ex = new XPathException("message");
55
cause = new Exception();
59
public void testConstructor() {
60
String message = "testing 1-2-3";
61
XPathException ex = new XPathException(message, cause);
62
assertEquals(message, ex.getMessage());
63
assertEquals(cause, ex.getCause());
67
public void testInitCause() {
69
assertNull(ex.getCause());
71
assertEquals(cause, ex.getCause());
75
fail("Reinitialized cause over null");
77
catch (IllegalStateException success) {
78
assertNotNull(success.getMessage());
82
ex.initCause(new Exception());
83
fail("Reinitialized cause over null");
85
catch (IllegalStateException success) {
86
assertNotNull(success.getMessage());
92
public void testNullInitCause() {
94
XPathException ex = new XPathException(null, null);
95
assertNull(ex.getCause());
98
ex.initCause(new Exception());
99
fail("Reinitialized cause over null");
101
catch (IllegalStateException success) {
102
assertNotNull(success.getMessage());
107
fail("Reinitialized cause over null");
109
catch (IllegalStateException success) {
110
assertNotNull(success.getMessage());
116
public void testSelfCause() {
120
fail("Allowed self-causation");
122
catch (IllegalArgumentException success) {
123
assertNotNull(success.getMessage());
129
public void testGetMessage() {
130
Exception ex = new XPathException("testing");
131
assertEquals("testing", ex.getMessage());
134
public void testGetXPathExpression() {
136
Element parent = new Element("Test");
139
parent.query("This is not an XPath expression");
140
fail("Allowed malformed query");
142
catch (XPathException success) {
144
"This is not an XPath expression", success.getXPath());
149
public void testSerializeXPathTypeException() throws IOException {
151
Element parent = new Element("Test");
152
Element child = new Element("child");
153
parent.appendChild(child);
156
parent.query("count(*)");
157
fail("Allowed query to return number");
159
catch (XPathTypeException success) {
160
ByteArrayOutputStream out = new ByteArrayOutputStream();
161
ObjectOutputStream oout = new ObjectOutputStream(out);
162
oout.writeObject(success);