1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 package org.jaxen.test;
49
50 import javax.xml.parsers.DocumentBuilder;
51 import javax.xml.parsers.DocumentBuilderFactory;
52 import javax.xml.parsers.ParserConfigurationException;
53
54 import junit.framework.TestCase;
55
56 import org.jaxen.FunctionCallException;
57 import org.jaxen.JaxenException;
58 import org.jaxen.XPath;
59 import org.jaxen.dom.DOMXPath;
60 import org.w3c.dom.Document;
61
62 /***
63 * @author Elliotte Rusty Harold
64 *
65 */
66 public class SubstringTest extends TestCase {
67
68 private Document doc;
69
70 public void setUp() throws ParserConfigurationException
71 {
72 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
73 factory.setNamespaceAware(true);
74 DocumentBuilder builder = factory.newDocumentBuilder();
75 doc = builder.newDocument();
76 doc.appendChild(doc.createElement("root"));
77 }
78
79
80 public SubstringTest(String name) {
81 super(name);
82 }
83
84 public void testSubstringOfNumber() throws JaxenException
85 {
86 XPath xpath = new DOMXPath( "substring(1234, 3)" );
87 String result = (String) xpath.evaluate( doc );
88 assertEquals("34", result);
89 }
90
91 public void testSubstringOfNumber2() throws JaxenException
92 {
93 XPath xpath = new DOMXPath( "substring(1234, 2, 3)" );
94 String result = (String) xpath.evaluate( doc );
95 assertEquals("234", result);
96 }
97
98
99
100 public void testUnusualSubstring1() throws JaxenException
101 {
102 XPath xpath = new DOMXPath( "substring('12345', 1.5, 2.6)" );
103 String result = (String) xpath.evaluate( doc );
104 assertEquals("234", result);
105 }
106
107
108 public void testUnusualSubstring2() throws JaxenException
109 {
110 XPath xpath = new DOMXPath( "substring('12345', 0, 3)" );
111 String result = (String) xpath.evaluate( doc );
112 assertEquals("12", result);
113 }
114
115
116 public void testUnusualSubstring3() throws JaxenException
117 {
118 XPath xpath = new DOMXPath( "substring('12345', 0 div 0, 3)" );
119 String result = (String) xpath.evaluate( doc );
120 assertEquals("", result);
121 }
122
123 public void testUnusualSubstring4() throws JaxenException
124 {
125 XPath xpath = new DOMXPath( "substring('12345', 1, 0 div 0)" );
126 String result = (String) xpath.evaluate( doc );
127 assertEquals("", result);
128 }
129
130 public void testUnusualSubstring5() throws JaxenException
131 {
132 XPath xpath = new DOMXPath( "substring('12345', -42, 1 div 0)" );
133 String result = (String) xpath.evaluate( doc );
134 assertEquals("12345", result);
135 }
136
137 public void testUnusualSubstring6() throws JaxenException
138 {
139 XPath xpath = new DOMXPath( "substring('12345', -1 div 0, 1 div 0)" );
140 String result = (String) xpath.evaluate( doc );
141 assertEquals("", result);
142 }
143
144
145 public void testSubstringOfNaN() throws JaxenException
146 {
147 XPath xpath = new DOMXPath( "substring(0 div 0, 2)" );
148 String result = (String) xpath.evaluate( doc );
149 assertEquals("aN", result);
150 }
151
152
153 public void testSubstringOfEmptyString() throws JaxenException
154 {
155 XPath xpath = new DOMXPath( "substring('', 2)" );
156 String result = (String) xpath.evaluate( doc );
157 assertEquals("", result);
158 }
159
160
161 public void testSubstringWithNegativeLength() throws JaxenException
162 {
163 XPath xpath = new DOMXPath( "substring('12345', 2, -3)" );
164 String result = (String) xpath.evaluate( doc );
165 assertEquals("", result);
166 }
167
168
169 public void testSubstringWithExcessiveLength() throws JaxenException
170 {
171 XPath xpath = new DOMXPath( "substring('12345', 2, 32)" );
172 String result = (String) xpath.evaluate( doc );
173 assertEquals("2345", result);
174 }
175
176
177 public void testSubstringWithNegativeLength2() throws JaxenException
178 {
179 XPath xpath = new DOMXPath( "substring('12345', 2, -1)" );
180 String result = (String) xpath.evaluate( doc );
181 assertEquals("", result);
182 }
183
184
185 public void testSubstringFunctionRequiresAtLeastTwoArguments()
186 throws JaxenException {
187
188 XPath xpath = new DOMXPath("substring('a')");
189
190 try {
191 xpath.selectNodes(doc);
192 fail("Allowed substring function with one argument");
193 }
194 catch (FunctionCallException ex) {
195 assertNotNull(ex.getMessage());
196 }
197
198 }
199
200 public void testNegativeStartNoLength()
201 throws JaxenException {
202
203 XPath xpath = new DOMXPath("substring('Hello', -50)");
204 String result = (String) xpath.evaluate( doc );
205 assertEquals("Hello", result);
206
207 }
208
209 public void testNegativeStartWithLength()
210 throws JaxenException {
211
212 XPath xpath = new DOMXPath("substring('Hello', -50, 20)");
213 String result = (String) xpath.evaluate( doc );
214 assertEquals("", result);
215
216 }
217
218 public void testSubstringFunctionRequiresAtMostThreeArguments()
219 throws JaxenException {
220
221 XPath xpath = new DOMXPath("substring('a', 1, 1, 4)");
222
223 try {
224 xpath.selectNodes(doc);
225 fail("Allowed substring function with four arguments");
226 }
227 catch (FunctionCallException ex) {
228 assertNotNull(ex.getMessage());
229 }
230
231 }
232
233 public void testStringLengthCountsUnicodeCharactersNotJavaChars()
234 throws JaxenException {
235
236 XPath xpath = new DOMXPath("substring('A\uD834\uDD00', 1, 2)");
237 String result = (String) xpath.evaluate( doc );
238 assertEquals("A\uD834\uDD00", result);
239
240 }
241
242
243 public void testStringLengthIndexesUnicodeCharactersNotJavaChars()
244 throws JaxenException {
245
246 XPath xpath = new DOMXPath("substring('A\uD834\uDD00', 3, 1)");
247 String result = (String) xpath.evaluate( doc );
248 assertEquals("", result);
249
250 }
251
252
253 public void testStringLengthIndexesAndCountsUnicodeCharactersNotJavaChars()
254 throws JaxenException {
255
256 XPath xpath = new DOMXPath("substring('A\uD834\uDD00123', 3, 2)");
257 String result = (String) xpath.evaluate( doc );
258 assertEquals("12", result);
259
260 }
261
262
263 }