001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.lang;
018:
019: import junit.framework.Test;
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022: import junit.textui.TestRunner;
023:
024: /**
025: * Unit tests {@link org.apache.commons.lang.StringUtils} - Substring methods
026: *
027: * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
028: * @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
029: * @author Phil Steitz
030: * @version $Id: StringUtilsSubstringTest.java 492377 2007-01-04 01:20:30Z scolebourne $
031: */
032: public class StringUtilsSubstringTest extends TestCase {
033: private static final String FOO = "foo";
034: private static final String BAR = "bar";
035: private static final String BAZ = "baz";
036: private static final String FOOBAR = "foobar";
037: private static final String SENTENCE = "foo bar baz";
038:
039: public StringUtilsSubstringTest(String name) {
040: super (name);
041: }
042:
043: public static void main(String[] args) {
044: TestRunner.run(suite());
045: }
046:
047: public static Test suite() {
048: TestSuite suite = new TestSuite(StringUtilsSubstringTest.class);
049: suite.setName("StringUtilsSubstring Tests");
050: return suite;
051: }
052:
053: protected void setUp() throws Exception {
054: super .setUp();
055: }
056:
057: protected void tearDown() throws Exception {
058: super .tearDown();
059: }
060:
061: //-----------------------------------------------------------------------
062:
063: public void testSubstring_StringInt() {
064: assertEquals(null, StringUtils.substring(null, 0));
065: assertEquals("", StringUtils.substring("", 0));
066: assertEquals("", StringUtils.substring("", 2));
067:
068: assertEquals("", StringUtils.substring(SENTENCE, 80));
069: assertEquals(BAZ, StringUtils.substring(SENTENCE, 8));
070: assertEquals(BAZ, StringUtils.substring(SENTENCE, -3));
071: assertEquals(SENTENCE, StringUtils.substring(SENTENCE, 0));
072: assertEquals("abc", StringUtils.substring("abc", -4));
073: assertEquals("abc", StringUtils.substring("abc", -3));
074: assertEquals("bc", StringUtils.substring("abc", -2));
075: assertEquals("c", StringUtils.substring("abc", -1));
076: assertEquals("abc", StringUtils.substring("abc", 0));
077: assertEquals("bc", StringUtils.substring("abc", 1));
078: assertEquals("c", StringUtils.substring("abc", 2));
079: assertEquals("", StringUtils.substring("abc", 3));
080: assertEquals("", StringUtils.substring("abc", 4));
081: }
082:
083: public void testSubstring_StringIntInt() {
084: assertEquals(null, StringUtils.substring(null, 0, 0));
085: assertEquals(null, StringUtils.substring(null, 1, 2));
086: assertEquals("", StringUtils.substring("", 0, 0));
087: assertEquals("", StringUtils.substring("", 1, 2));
088: assertEquals("", StringUtils.substring("", -2, -1));
089:
090: assertEquals("", StringUtils.substring(SENTENCE, 8, 6));
091: assertEquals(FOO, StringUtils.substring(SENTENCE, 0, 3));
092: assertEquals("o", StringUtils.substring(SENTENCE, -9, 3));
093: assertEquals(FOO, StringUtils.substring(SENTENCE, 0, -8));
094: assertEquals("o", StringUtils.substring(SENTENCE, -9, -8));
095: assertEquals(SENTENCE, StringUtils.substring(SENTENCE, 0, 80));
096: assertEquals("", StringUtils.substring(SENTENCE, 2, 2));
097: assertEquals("b", StringUtils.substring("abc", -2, -1));
098: }
099:
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(
154: "fooXXbarXXbaz", "XX"));
155:
156: assertEquals(null, StringUtils.substringBefore(null, null));
157: assertEquals(null, StringUtils.substringBefore(null, ""));
158: assertEquals(null, StringUtils.substringBefore(null, "XX"));
159: assertEquals("", StringUtils.substringBefore("", null));
160: assertEquals("", StringUtils.substringBefore("", ""));
161: assertEquals("", StringUtils.substringBefore("", "XX"));
162:
163: assertEquals("foo", StringUtils.substringBefore("foo", null));
164: assertEquals("foo", StringUtils.substringBefore("foo", "b"));
165: assertEquals("f", StringUtils.substringBefore("foot", "o"));
166: assertEquals("", StringUtils.substringBefore("abc", "a"));
167: assertEquals("a", StringUtils.substringBefore("abcba", "b"));
168: assertEquals("ab", StringUtils.substringBefore("abc", "c"));
169: assertEquals("", StringUtils.substringBefore("abc", ""));
170: }
171:
172: public void testSubstringAfter_StringString() {
173: assertEquals("barXXbaz", StringUtils.substringAfter(
174: "fooXXbarXXbaz", "XX"));
175:
176: assertEquals(null, StringUtils.substringAfter(null, null));
177: assertEquals(null, StringUtils.substringAfter(null, ""));
178: assertEquals(null, StringUtils.substringAfter(null, "XX"));
179: assertEquals("", StringUtils.substringAfter("", null));
180: assertEquals("", StringUtils.substringAfter("", ""));
181: assertEquals("", StringUtils.substringAfter("", "XX"));
182:
183: assertEquals("", StringUtils.substringAfter("foo", null));
184: assertEquals("ot", StringUtils.substringAfter("foot", "o"));
185: assertEquals("bc", StringUtils.substringAfter("abc", "a"));
186: assertEquals("cba", StringUtils.substringAfter("abcba", "b"));
187: assertEquals("", StringUtils.substringAfter("abc", "c"));
188: assertEquals("abc", StringUtils.substringAfter("abc", ""));
189: assertEquals("", StringUtils.substringAfter("abc", "d"));
190: }
191:
192: public void testSubstringBeforeLast_StringString() {
193: assertEquals("fooXXbar", StringUtils.substringBeforeLast(
194: "fooXXbarXXbaz", "XX"));
195:
196: assertEquals(null, StringUtils.substringBeforeLast(null, null));
197: assertEquals(null, StringUtils.substringBeforeLast(null, ""));
198: assertEquals(null, StringUtils.substringBeforeLast(null, "XX"));
199: assertEquals("", StringUtils.substringBeforeLast("", null));
200: assertEquals("", StringUtils.substringBeforeLast("", ""));
201: assertEquals("", StringUtils.substringBeforeLast("", "XX"));
202:
203: assertEquals("foo", StringUtils
204: .substringBeforeLast("foo", null));
205: assertEquals("foo", StringUtils.substringBeforeLast("foo", "b"));
206: assertEquals("fo", StringUtils.substringBeforeLast("foo", "o"));
207: assertEquals("abc\r\n", StringUtils.substringBeforeLast(
208: "abc\r\n", "d"));
209: assertEquals("abc", StringUtils.substringBeforeLast("abcdabc",
210: "d"));
211: assertEquals("abcdabc", StringUtils.substringBeforeLast(
212: "abcdabcd", "d"));
213: assertEquals("a", StringUtils.substringBeforeLast("abc", "b"));
214: assertEquals("abc ", StringUtils.substringBeforeLast("abc \n",
215: "\n"));
216: assertEquals("a", StringUtils.substringBeforeLast("a", null));
217: assertEquals("a", StringUtils.substringBeforeLast("a", ""));
218: assertEquals("", StringUtils.substringBeforeLast("a", "a"));
219: }
220:
221: public void testSubstringAfterLast_StringString() {
222: assertEquals("baz", StringUtils.substringAfterLast(
223: "fooXXbarXXbaz", "XX"));
224:
225: assertEquals(null, StringUtils.substringAfterLast(null, null));
226: assertEquals(null, StringUtils.substringAfterLast(null, ""));
227: assertEquals(null, StringUtils.substringAfterLast(null, "XX"));
228: assertEquals("", StringUtils.substringAfterLast("", null));
229: assertEquals("", StringUtils.substringAfterLast("", ""));
230: assertEquals("", StringUtils.substringAfterLast("", "a"));
231:
232: assertEquals("", StringUtils.substringAfterLast("foo", null));
233: assertEquals("", StringUtils.substringAfterLast("foo", "b"));
234: assertEquals("t", StringUtils.substringAfterLast("foot", "o"));
235: assertEquals("bc", StringUtils.substringAfterLast("abc", "a"));
236: assertEquals("a", StringUtils.substringAfterLast("abcba", "b"));
237: assertEquals("", StringUtils.substringAfterLast("abc", "c"));
238: assertEquals("", StringUtils.substringAfterLast("", "d"));
239: assertEquals("", StringUtils.substringAfterLast("abc", ""));
240: }
241:
242: //-----------------------------------------------------------------------
243: public void testSubstringBetween_StringString() {
244: assertEquals(null, StringUtils.substringBetween(null, "tag"));
245: assertEquals("", StringUtils.substringBetween("", ""));
246: assertEquals(null, StringUtils.substringBetween("", "abc"));
247: assertEquals("", StringUtils.substringBetween(" ", " "));
248: assertEquals(null, StringUtils.substringBetween("abc", null));
249: assertEquals("", StringUtils.substringBetween("abc", ""));
250: assertEquals(null, StringUtils.substringBetween("abc", "a"));
251: assertEquals("bc", StringUtils.substringBetween("abca", "a"));
252: assertEquals("bc", StringUtils.substringBetween("abcabca", "a"));
253: assertEquals("bar", StringUtils.substringBetween("\nbar\n",
254: "\n"));
255: }
256:
257: public void testSubstringBetween_StringStringString() {
258: assertEquals(null, StringUtils.substringBetween(null, "", ""));
259: assertEquals("", StringUtils.substringBetween("", "", ""));
260: assertEquals("", StringUtils.substringBetween("foo", "", ""));
261: assertEquals(null, StringUtils.substringBetween("foo", "", "]"));
262: assertEquals(null, StringUtils
263: .substringBetween("foo", "[", "]"));
264: assertEquals("", StringUtils
265: .substringBetween(" ", " ", " "));
266: assertEquals("bar", StringUtils.substringBetween(
267: "<foo>bar</foo>", "<foo>", "</foo>"));
268: }
269:
270: /**
271: * Tests the substringsBetween method that returns an String Array of substrings.
272: */
273: public void testSubstringsBetween_StringStringString() {
274:
275: String[] results = StringUtils.substringsBetween(
276: "[one], [two], [three]", "[", "]");
277: assertEquals(3, results.length);
278: assertEquals("one", results[0]);
279: assertEquals("two", results[1]);
280: assertEquals("three", results[2]);
281:
282: results = StringUtils.substringsBetween("[one], [two], three",
283: "[", "]");
284: assertEquals(2, results.length);
285: assertEquals("one", results[0]);
286: assertEquals("two", results[1]);
287:
288: results = StringUtils.substringsBetween("[one], [two], three]",
289: "[", "]");
290: assertEquals(2, results.length);
291: assertEquals("one", results[0]);
292: assertEquals("two", results[1]);
293:
294: results = StringUtils.substringsBetween("[one], two], three]",
295: "[", "]");
296: assertEquals(1, results.length);
297: assertEquals("one", results[0]);
298:
299: results = StringUtils.substringsBetween("one], two], [three]",
300: "[", "]");
301: assertEquals(1, results.length);
302: assertEquals("three", results[0]);
303:
304: // 'ab hello ba' will match, but 'ab non ba' won't
305: // this is because the 'a' is shared between the two and can't be matched twice
306: results = StringUtils.substringsBetween("aabhellobabnonba",
307: "ab", "ba");
308: assertEquals(1, results.length);
309: assertEquals("hello", results[0]);
310:
311: results = StringUtils.substringsBetween("one, two, three", "[",
312: "]");
313: assertNull(results);
314:
315: results = StringUtils.substringsBetween("[one, two, three",
316: "[", "]");
317: assertNull(results);
318:
319: results = StringUtils.substringsBetween("one, two, three]",
320: "[", "]");
321: assertNull(results);
322:
323: results = StringUtils.substringsBetween(
324: "[one], [two], [three]", "[", null);
325: assertNull(results);
326:
327: results = StringUtils.substringsBetween(
328: "[one], [two], [three]", null, "]");
329: assertNull(results);
330:
331: results = StringUtils.substringsBetween(
332: "[one], [two], [three]", "", "");
333: assertNull(results);
334:
335: results = StringUtils.substringsBetween(null, "[", "]");
336: assertNull(results);
337:
338: results = StringUtils.substringsBetween("", "[", "]");
339: assertEquals(0, results.length);
340: }
341:
342: //-----------------------------------------------------------------------
343: public void testCountMatches_String() {
344: assertEquals(0, StringUtils.countMatches(null, null));
345: assertEquals(0, StringUtils.countMatches("blah", null));
346: assertEquals(0, StringUtils.countMatches(null, "DD"));
347:
348: assertEquals(0, StringUtils.countMatches("x", ""));
349: assertEquals(0, StringUtils.countMatches("", ""));
350:
351: assertEquals(3, StringUtils.countMatches(
352: "one long someone sentence of one", "one"));
353: assertEquals(0, StringUtils.countMatches(
354: "one long someone sentence of one", "two"));
355: assertEquals(4, StringUtils.countMatches("oooooooooooo", "ooo"));
356: }
357:
358: public void testGetNestedString_StringString() {
359: assertEquals(null, StringUtils.getNestedString(null, "tag"));
360: assertEquals("", StringUtils.getNestedString("", ""));
361: assertEquals(null, StringUtils.getNestedString("", "abc"));
362: assertEquals("", StringUtils.getNestedString(" ", " "));
363: assertEquals(null, StringUtils.getNestedString("abc", null));
364: assertEquals("", StringUtils.getNestedString("abc", ""));
365: assertEquals(null, StringUtils.getNestedString("abc", "a"));
366: assertEquals("bc", StringUtils.getNestedString("abca", "a"));
367: assertEquals("bc", StringUtils.getNestedString("abcabca", "a"));
368: assertEquals("bar", StringUtils
369: .getNestedString("\nbar\n", "\n"));
370: }
371:
372: public void testGetNestedString_StringStringString() {
373: assertEquals(null, StringUtils.getNestedString(null, "", ""));
374: assertEquals("", StringUtils.getNestedString("", "", ""));
375: assertEquals("", StringUtils.getNestedString(" ", " ", " "));
376: assertEquals("bar", StringUtils.getNestedString(
377: "<foo>bar</foo>", "<foo>", "</foo>"));
378: }
379:
380: }
|