KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > StringUtilTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.util;
5
6 import junit.framework.TestCase;
7
8 public final class StringUtilTest extends TestCase {
9
10   public void testExists() {
11     String JavaDoc[] testVals = new String JavaDoc[] { "test1", "test2", "test3", null };
12     assertTrue(StringUtil.exists(testVals, "test1"));
13     assertTrue(StringUtil.exists(testVals, null));
14     assertFalse(StringUtil.exists(testVals, "Not in test vals"));
15     assertFalse(StringUtil.exists(null, "can't examine null list"));
16   }
17
18   public void testToStringObjectArrayStringStringString() {
19     // Test 1, all nulls
20
String JavaDoc expected = StringUtil.NULL_STRING;
21     String JavaDoc rv = StringUtil.toString((Object JavaDoc[]) null, null, null, null);
22     assertNotNull("StringUtil.toString(Object[],String,String,String) returned null", rv);
23     assertEquals("Returned string was not the same as expected", expected, rv);
24
25     // Test 2, objects with no formatting
26
Object JavaDoc[] arr = new Object JavaDoc[] { "one", "two", "three" };
27     expected = "onetwothree";
28     rv = StringUtil.toString(arr, null, null, null);
29     assertNotNull("StringUtil.toString(Object[],String,String,String) returned null", rv);
30     assertEquals("Returned string was not the same as expected", expected, rv);
31
32     // Test 3, objects with some formatting
33
expected = "[one:[two:[three";
34     rv = StringUtil.toString(arr, ":", "[", null);
35     assertNotNull("StringUtil.toString(Object[],String,String,String) returned null", rv);
36     assertEquals("Returned string was not the same as expected", expected, rv);
37
38     // Test 4, objects with all formatting
39
expected = "<one>,<two>,<three>";
40     rv = StringUtil.toString(arr, ",", "<", ">");
41     assertNotNull("StringUtil.toString(Object[],String,String,String) returned null", rv);
42     assertEquals("Returned string was not the same as expected", expected, rv);
43
44     // Test 5, objects with all formatting and null elements
45
arr = new Object JavaDoc[] { "one", null, null, "four" };
46     expected = "<one>,<null>,<null>,<four>";
47     rv = StringUtil.toString(arr, ",", "<", ">");
48     assertNotNull("StringUtil.toString(Object[],String,String,String) returned null", rv);
49     assertEquals("Returned string was not the same as expected", expected, rv);
50   }
51
52   public void testOrdinal() throws Exception JavaDoc {
53     final String JavaDoc[] ordinals = { "0th", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th",
54         "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th",
55         "26th", "27th", "28th", "29th", "30th", "31st", "32nd", "33rd", "34th", "35th", "36th", "37th", "38th", "39th",
56         "40th", "41st", "42nd", "43rd" };
57     for (int pos = 0; pos < ordinals.length; ++pos) {
58       assertEquals(ordinals[pos], StringUtil.ordinal(pos));
59     }
60     // Some one off awkward values
61
assertEquals("111th", StringUtil.ordinal(111));
62     assertEquals("1011th", StringUtil.ordinal(1011));
63     assertEquals("1911th", StringUtil.ordinal(1911));
64     assertEquals("10011th", StringUtil.ordinal(10011));
65     assertEquals("10911th", StringUtil.ordinal(10911));
66
67     assertEquals("112th", StringUtil.ordinal(112));
68     assertEquals("1012th", StringUtil.ordinal(1012));
69     assertEquals("1912th", StringUtil.ordinal(1912));
70     assertEquals("10012th", StringUtil.ordinal(10012));
71     assertEquals("10912th", StringUtil.ordinal(10912));
72
73     assertEquals("113th", StringUtil.ordinal(113));
74     assertEquals("1013th", StringUtil.ordinal(1013));
75     assertEquals("1913th", StringUtil.ordinal(1913));
76     assertEquals("10013th", StringUtil.ordinal(10013));
77     assertEquals("10913th", StringUtil.ordinal(10913));
78   }
79
80 }
81
Popular Tags