1   /*
2    * $Header$
3    * $Revision$
4    * $Date$
5    *
6    * ====================================================================
7    *
8    * Copyright 2005 Elliotte Rusty Harold.
9    * All rights reserved.
10   *
11   * Redistribution and use in source and binary forms, with or without
12   * modification, are permitted provided that the following conditions are
13   * met:
14   * 
15   *   * Redistributions of source code must retain the above copyright
16   *     notice, this list of conditions and the following disclaimer.
17   * 
18   *   * Redistributions in binary form must reproduce the above copyright
19   *     notice, this list of conditions and the following disclaimer in the
20   *     documentation and/or other materials provided with the distribution.
21   * 
22   *   * Neither the name of the Jaxen Project nor the names of its
23   *     contributors may be used to endorse or promote products derived 
24   *     from this software without specific prior written permission.
25   * 
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28   * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
29   * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
30   * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   *
38   * ====================================================================
39   * This software consists of voluntary contributions made by many 
40   * individuals on behalf of the Jaxen Project and was originally 
41   * created by bob mcwhirter <bob@werken.com> and 
42   * James Strachan <jstrachan@apache.org>.  For more information on the 
43   * Jaxen Project, please see <http://www.jaxen.org/>.
44   * 
45   * $Id$
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      // Unusual tests from XPath spec
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 }