|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| package nu.xom.tests; |
|
23 |
| |
|
24 |
| import java.io.ByteArrayOutputStream; |
|
25 |
| import java.io.IOException; |
|
26 |
| import java.io.ObjectOutputStream; |
|
27 |
| |
|
28 |
| import nu.xom.Element; |
|
29 |
| import nu.xom.XPathException; |
|
30 |
| import nu.xom.XPathTypeException; |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| public class XPathExceptionTest extends XOMTestCase { |
|
42 |
| |
|
43 |
| |
|
44 |
| private XPathException ex; |
|
45 |
| private Exception cause; |
|
46 |
| |
|
47 |
| |
|
48 |
7
| public XPathExceptionTest(String name) {
|
|
49 |
7
| super(name);
|
|
50 |
| } |
|
51 |
| |
|
52 |
| |
|
53 |
7
| protected void setUp() {
|
|
54 |
7
| ex = new XPathException("message");
|
|
55 |
7
| cause = new Exception();
|
|
56 |
| } |
|
57 |
| |
|
58 |
| |
|
59 |
1
| public void testConstructor() {
|
|
60 |
1
| String message = "testing 1-2-3";
|
|
61 |
1
| XPathException ex = new XPathException(message, cause);
|
|
62 |
1
| assertEquals(message, ex.getMessage());
|
|
63 |
1
| assertEquals(cause, ex.getCause());
|
|
64 |
| } |
|
65 |
| |
|
66 |
| |
|
67 |
1
| public void testInitCause() {
|
|
68 |
| |
|
69 |
1
| assertNull(ex.getCause());
|
|
70 |
1
| ex.initCause(cause);
|
|
71 |
1
| assertEquals(cause, ex.getCause());
|
|
72 |
| |
|
73 |
1
| try {
|
|
74 |
1
| ex.initCause(null);
|
|
75 |
0
| fail("Reinitialized cause over null");
|
|
76 |
| } |
|
77 |
| catch (IllegalStateException success) { |
|
78 |
1
| assertNotNull(success.getMessage());
|
|
79 |
| } |
|
80 |
| |
|
81 |
1
| try {
|
|
82 |
1
| ex.initCause(new Exception());
|
|
83 |
0
| fail("Reinitialized cause over null");
|
|
84 |
| } |
|
85 |
| catch (IllegalStateException success) { |
|
86 |
1
| assertNotNull(success.getMessage());
|
|
87 |
| } |
|
88 |
| |
|
89 |
| } |
|
90 |
| |
|
91 |
| |
|
92 |
1
| public void testNullInitCause() {
|
|
93 |
| |
|
94 |
1
| XPathException ex = new XPathException(null, null);
|
|
95 |
1
| assertNull(ex.getCause());
|
|
96 |
| |
|
97 |
1
| try {
|
|
98 |
1
| ex.initCause(new Exception());
|
|
99 |
0
| fail("Reinitialized cause over null");
|
|
100 |
| } |
|
101 |
| catch (IllegalStateException success) { |
|
102 |
1
| assertNotNull(success.getMessage());
|
|
103 |
| } |
|
104 |
| |
|
105 |
1
| try {
|
|
106 |
1
| ex.initCause(null);
|
|
107 |
0
| fail("Reinitialized cause over null");
|
|
108 |
| } |
|
109 |
| catch (IllegalStateException success) { |
|
110 |
1
| assertNotNull(success.getMessage());
|
|
111 |
| } |
|
112 |
| |
|
113 |
| } |
|
114 |
| |
|
115 |
| |
|
116 |
1
| public void testSelfCause() {
|
|
117 |
| |
|
118 |
1
| try {
|
|
119 |
1
| ex.initCause(ex);
|
|
120 |
0
| fail("Allowed self-causation");
|
|
121 |
| } |
|
122 |
| catch (IllegalArgumentException success) { |
|
123 |
1
| assertNotNull(success.getMessage());
|
|
124 |
| } |
|
125 |
| |
|
126 |
| } |
|
127 |
| |
|
128 |
| |
|
129 |
1
| public void testGetMessage() {
|
|
130 |
1
| Exception ex = new XPathException("testing");
|
|
131 |
1
| assertEquals("testing", ex.getMessage());
|
|
132 |
| } |
|
133 |
| |
|
134 |
1
| public void testGetXPathExpression() {
|
|
135 |
| |
|
136 |
1
| Element parent = new Element("Test");
|
|
137 |
| |
|
138 |
1
| try {
|
|
139 |
1
| parent.query("This is not an XPath expression");
|
|
140 |
0
| fail("Allowed malformed query");
|
|
141 |
| } |
|
142 |
| catch (XPathException success) { |
|
143 |
1
| assertEquals(
|
|
144 |
| "This is not an XPath expression", success.getXPath()); |
|
145 |
| } |
|
146 |
| |
|
147 |
| } |
|
148 |
| |
|
149 |
1
| public void testSerializeXPathTypeException() throws IOException {
|
|
150 |
| |
|
151 |
1
| Element parent = new Element("Test");
|
|
152 |
1
| Element child = new Element("child");
|
|
153 |
1
| parent.appendChild(child);
|
|
154 |
| |
|
155 |
1
| try {
|
|
156 |
1
| parent.query("count(*)");
|
|
157 |
0
| fail("Allowed query to return number");
|
|
158 |
| } |
|
159 |
| catch (XPathTypeException success) { |
|
160 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
161 |
1
| ObjectOutputStream oout = new ObjectOutputStream(out);
|
|
162 |
1
| oout.writeObject(success);
|
|
163 |
1
| oout.close();
|
|
164 |
| } |
|
165 |
| |
|
166 |
| } |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| } |