KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > StringUtilsSubstringTest


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang;
17
18 import junit.framework.Test;
19 import junit.framework.TestCase;
20 import junit.framework.TestSuite;
21 import junit.textui.TestRunner;
22
23 /**
24  * Unit tests {@link org.apache.commons.lang.StringUtils} - Substring methods
25  *
26  * @author <a HREF="mailto:scolebourne@joda.org">Stephen Colebourne</a>
27  * @author <a HREF="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
28  * @author Phil Steitz
29  * @version $Id: StringUtilsSubstringTest.java 161244 2005-04-14 06:16:36Z ggregory $
30  */

31 public class StringUtilsSubstringTest extends TestCase {
32     private static final String JavaDoc FOO = "foo";
33     private static final String JavaDoc BAR = "bar";
34     private static final String JavaDoc BAZ = "baz";
35     private static final String JavaDoc FOOBAR = "foobar";
36     private static final String JavaDoc SENTENCE = "foo bar baz";
37
38     public StringUtilsSubstringTest(String JavaDoc name) {
39         super(name);
40     }
41
42     public static void main(String JavaDoc[] args) {
43         TestRunner.run(suite());
44     }
45
46     public static Test suite() {
47         TestSuite suite = new TestSuite(StringUtilsSubstringTest.class);
48         suite.setName("StringUtilsSubstring Tests");
49         return suite;
50     }
51
52     protected void setUp() throws Exception JavaDoc {
53         super.setUp();
54     }
55
56     protected void tearDown() throws Exception JavaDoc {
57         super.tearDown();
58     }
59
60     //-----------------------------------------------------------------------
61

62
63     public void testSubstring_StringInt() {
64         assertEquals(null, StringUtils.substring(null, 0));
65         assertEquals("", StringUtils.substring("", 0));
66         assertEquals("", StringUtils.substring("", 2));
67         
68         assertEquals("", StringUtils.substring(SENTENCE, 80));
69         assertEquals(BAZ, StringUtils.substring(SENTENCE, 8));
70         assertEquals(BAZ, StringUtils.substring(SENTENCE, -3));
71         assertEquals(SENTENCE, StringUtils.substring(SENTENCE, 0));
72         assertEquals("abc", StringUtils.substring("abc", -4));
73         assertEquals("abc", StringUtils.substring("abc", -3));
74         assertEquals("bc", StringUtils.substring("abc", -2));
75         assertEquals("c", StringUtils.substring("abc", -1));
76         assertEquals("abc", StringUtils.substring("abc", 0));
77         assertEquals("bc", StringUtils.substring("abc", 1));
78         assertEquals("c", StringUtils.substring("abc", 2));
79         assertEquals("", StringUtils.substring("abc", 3));
80         assertEquals("", StringUtils.substring("abc", 4));
81     }
82     
83     public void testSubstring_StringIntInt() {
84         assertEquals(null, StringUtils.substring(null, 0, 0));
85         assertEquals(null, StringUtils.substring(null, 1, 2));
86         assertEquals("", StringUtils.substring("", 0, 0));
87         assertEquals("", StringUtils.substring("", 1, 2));
88         assertEquals("", StringUtils.substring("", -2, -1));
89         
90         assertEquals("", StringUtils.substring(SENTENCE, 8, 6));
91         assertEquals(FOO, StringUtils.substring(SENTENCE, 0, 3));
92         assertEquals("o", StringUtils.substring(SENTENCE, -9, 3));
93         assertEquals(FOO, StringUtils.substring(SENTENCE, 0, -8));
94         assertEquals("o", StringUtils.substring(SENTENCE, -9, -8));
95         assertEquals(SENTENCE, StringUtils.substring(SENTENCE, 0, 80));
96         assertEquals("", StringUtils.substring(SENTENCE, 2, 2));
97         assertEquals("b",StringUtils.substring("abc", -2, -1));
98     }
99            
100     public void testLeft_String() {
101         assertSame(null, StringUtils.left(null, -1));
102         assertSame(null, StringUtils.left(null, 0));
103         assertSame(null, StringUtils.left(null, 2));
104         
105         assertEquals("", StringUtils.left("", -1));
106         assertEquals("", StringUtils.left("", 0));
107         assertEquals("", StringUtils.left("", 2));
108         
109         assertEquals("", StringUtils.left(FOOBAR, -1));
110         assertEquals("", StringUtils.left(FOOBAR, 0));
111         assertEquals(FOO, StringUtils.left(FOOBAR, 3));
112         assertSame(FOOBAR, StringUtils.left(FOOBAR, 80));
113     }
114     
115     public void testRight_String() {
116         assertSame(null, StringUtils.right(null, -1));
117         assertSame(null, StringUtils.right(null, 0));
118         assertSame(null, StringUtils.right(null, 2));
119         
120         assertEquals("", StringUtils.right("", -1));
121         assertEquals("", StringUtils.right("", 0));
122         assertEquals("", StringUtils.right("", 2));
123         
124         assertEquals("", StringUtils.right(FOOBAR, -1));
125         assertEquals("", StringUtils.right(FOOBAR, 0));
126         assertEquals(BAR, StringUtils.right(FOOBAR, 3));
127         assertSame(FOOBAR, StringUtils.right(FOOBAR, 80));
128     }
129     
130     public void testMid_String() {
131         assertSame(null, StringUtils.mid(null, -1, 0));
132         assertSame(null, StringUtils.mid(null, 0, -1));
133         assertSame(null, StringUtils.mid(null, 3, 0));
134         assertSame(null, StringUtils.mid(null, 3, 2));
135         
136         assertEquals("", StringUtils.mid("", 0, -1));
137         assertEquals("", StringUtils.mid("", 0, 0));
138         assertEquals("", StringUtils.mid("", 0, 2));
139         
140         assertEquals("", StringUtils.mid(FOOBAR, 3, -1));
141         assertEquals("", StringUtils.mid(FOOBAR, 3, 0));
142         assertEquals("b", StringUtils.mid(FOOBAR, 3, 1));
143         assertEquals(FOO, StringUtils.mid(FOOBAR, 0, 3));
144         assertEquals(BAR, StringUtils.mid(FOOBAR, 3, 3));
145         assertEquals(FOOBAR, StringUtils.mid(FOOBAR, 0, 80));
146         assertEquals(BAR, StringUtils.mid(FOOBAR, 3, 80));
147         assertEquals("", StringUtils.mid(FOOBAR, 9, 3));
148         assertEquals(FOO, StringUtils.mid(FOOBAR, -1, 3));
149     }
150     
151     //-----------------------------------------------------------------------
152
public void testSubstringBefore_StringString() {
153         assertEquals("foo", StringUtils.substringBefore("fooXXbarXXbaz", "XX"));
154
155         assertEquals(null, StringUtils.substringBefore(null, null));
156         assertEquals(null, StringUtils.substringBefore(null, ""));
157         assertEquals(null, StringUtils.substringBefore(null, "XX"));
158         assertEquals("", StringUtils.substringBefore("", null));
159         assertEquals("", StringUtils.substringBefore("", ""));
160         assertEquals("", StringUtils.substringBefore("", "XX"));
161         
162         assertEquals("foo", StringUtils.substringBefore("foo", null));
163         assertEquals("foo", StringUtils.substringBefore("foo", "b"));
164         assertEquals("f", StringUtils.substringBefore("foot", "o"));
165         assertEquals("", StringUtils.substringBefore("abc", "a"));
166         assertEquals("a", StringUtils.substringBefore("abcba", "b"));
167         assertEquals("ab", StringUtils.substringBefore("abc", "c"));
168         assertEquals("", StringUtils.substringBefore("abc", ""));
169     }
170     
171     public void testSubstringAfter_StringString() {
172         assertEquals("barXXbaz", StringUtils.substringAfter("fooXXbarXXbaz", "XX"));
173         
174         assertEquals(null, StringUtils.substringAfter(null, null));
175         assertEquals(null, StringUtils.substringAfter(null, ""));
176         assertEquals(null, StringUtils.substringAfter(null, "XX"));
177         assertEquals("", StringUtils.substringAfter("", null));
178         assertEquals("", StringUtils.substringAfter("", ""));
179         assertEquals("", StringUtils.substringAfter("", "XX"));
180         
181         assertEquals("", StringUtils.substringAfter("foo", null));
182         assertEquals("ot", StringUtils.substringAfter("foot", "o"));
183         assertEquals("bc", StringUtils.substringAfter("abc", "a"));
184         assertEquals("cba", StringUtils.substringAfter("abcba", "b"));
185         assertEquals("", StringUtils.substringAfter("abc", "c"));
186         assertEquals("abc", StringUtils.substringAfter("abc", ""));
187         assertEquals("", StringUtils.substringAfter("abc", "d"));
188     }
189
190     public void testSubstringBeforeLast_StringString() {
191         assertEquals("fooXXbar", StringUtils.substringBeforeLast("fooXXbarXXbaz", "XX"));
192
193         assertEquals(null, StringUtils.substringBeforeLast(null, null));
194         assertEquals(null, StringUtils.substringBeforeLast(null, ""));
195         assertEquals(null, StringUtils.substringBeforeLast(null, "XX"));
196         assertEquals("", StringUtils.substringBeforeLast("", null));
197         assertEquals("", StringUtils.substringBeforeLast("", ""));
198         assertEquals("", StringUtils.substringBeforeLast("", "XX"));
199
200         assertEquals("foo", StringUtils.substringBeforeLast("foo", null));
201         assertEquals("foo", StringUtils.substringBeforeLast("foo", "b"));
202         assertEquals("fo", StringUtils.substringBeforeLast("foo", "o"));
203         assertEquals("abc\r\n", StringUtils.substringBeforeLast("abc\r\n", "d"));
204         assertEquals("abc", StringUtils.substringBeforeLast("abcdabc", "d"));
205         assertEquals("abcdabc", StringUtils.substringBeforeLast("abcdabcd", "d"));
206         assertEquals("a", StringUtils.substringBeforeLast("abc", "b"));
207         assertEquals("abc ", StringUtils.substringBeforeLast("abc \n", "\n"));
208         assertEquals("a", StringUtils.substringBeforeLast("a", null));
209         assertEquals("a", StringUtils.substringBeforeLast("a", ""));
210         assertEquals("", StringUtils.substringBeforeLast("a", "a"));
211     }
212     
213     public void testSubstringAfterLast_StringString() {
214         assertEquals("baz", StringUtils.substringAfterLast("fooXXbarXXbaz", "XX"));
215
216         assertEquals(null, StringUtils.substringAfterLast(null, null));
217         assertEquals(null, StringUtils.substringAfterLast(null, ""));
218         assertEquals(null, StringUtils.substringAfterLast(null, "XX"));
219         assertEquals("", StringUtils.substringAfterLast("", null));
220         assertEquals("", StringUtils.substringAfterLast("", ""));
221         assertEquals("", StringUtils.substringAfterLast("", "a"));
222
223         assertEquals("", StringUtils.substringAfterLast("foo", null));
224         assertEquals("", StringUtils.substringAfterLast("foo", "b"));
225         assertEquals("t", StringUtils.substringAfterLast("foot", "o"));
226         assertEquals("bc", StringUtils.substringAfterLast("abc", "a"));
227         assertEquals("a", StringUtils.substringAfterLast("abcba", "b"));
228         assertEquals("", StringUtils.substringAfterLast("abc", "c"));
229         assertEquals("", StringUtils.substringAfterLast("", "d"));
230         assertEquals("", StringUtils.substringAfterLast("abc", ""));
231     }
232         
233     //-----------------------------------------------------------------------
234
public void testSubstringBetween_StringString() {
235         assertEquals(null, StringUtils.substringBetween(null, "tag"));
236         assertEquals("", StringUtils.substringBetween("", ""));
237         assertEquals(null, StringUtils.substringBetween("", "abc"));
238         assertEquals("", StringUtils.substringBetween(" ", " "));
239         assertEquals(null, StringUtils.substringBetween("abc", null));
240         assertEquals("", StringUtils.substringBetween("abc", ""));
241         assertEquals(null, StringUtils.substringBetween("abc", "a"));
242         assertEquals("bc", StringUtils.substringBetween("abca", "a"));
243         assertEquals("bc", StringUtils.substringBetween("abcabca", "a"));
244         assertEquals("bar", StringUtils.substringBetween("\nbar\n", "\n"));
245     }
246             
247     public void testSubstringBetween_StringStringString() {
248         assertEquals(null, StringUtils.substringBetween(null, "", ""));
249         assertEquals("", StringUtils.substringBetween("", "", ""));
250         assertEquals("", StringUtils.substringBetween(" ", " ", " "));
251         assertEquals("bar", StringUtils.substringBetween("<foo>bar</foo>", "<foo>", "</foo>") );
252     }
253
254     //-----------------------------------------------------------------------
255
public void testCountMatches_String() {
256         assertEquals(0, StringUtils.countMatches(null, null));
257         assertEquals(0, StringUtils.countMatches("blah", null));
258         assertEquals(0, StringUtils.countMatches(null, "DD"));
259
260         assertEquals(0, StringUtils.countMatches("x", ""));
261         assertEquals(0, StringUtils.countMatches("", ""));
262
263         assertEquals(3,
264              StringUtils.countMatches("one long someone sentence of one", "one"));
265         assertEquals(0,
266              StringUtils.countMatches("one long someone sentence of one", "two"));
267         assertEquals(4,
268              StringUtils.countMatches("oooooooooooo", "ooo"));
269     }
270
271     public void testGetNestedString_StringString() {
272         assertEquals(null, StringUtils.getNestedString(null, "tag"));
273         assertEquals("", StringUtils.getNestedString("", ""));
274         assertEquals(null, StringUtils.getNestedString("", "abc"));
275         assertEquals("", StringUtils.getNestedString(" ", " "));
276         assertEquals(null, StringUtils.getNestedString("abc", null));
277         assertEquals("", StringUtils.getNestedString("abc", ""));
278         assertEquals(null, StringUtils.getNestedString("abc", "a"));
279         assertEquals("bc", StringUtils.getNestedString("abca", "a"));
280         assertEquals("bc", StringUtils.getNestedString("abcabca", "a"));
281         assertEquals("bar", StringUtils.getNestedString("\nbar\n", "\n"));
282     }
283             
284     public void testGetNestedString_StringStringString() {
285         assertEquals(null, StringUtils.getNestedString(null, "", ""));
286         assertEquals("", StringUtils.getNestedString("", "", ""));
287         assertEquals("", StringUtils.getNestedString(" ", " ", " "));
288         assertEquals("bar", StringUtils.getNestedString("<foo>bar</foo>", "<foo>", "</foo>") );
289     }
290
291 }
292
Popular Tags