KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > util > test > StringToolsTest


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.util.test;
8
9
10 import java.util.Arrays JavaDoc;
11
12 import junit.framework.TestCase;
13
14 import com.inversoft.util.StringTools;
15
16
17 /**
18  * This tests the com.inversoft.util.StringTools class
19  */

20 public class StringToolsTest extends TestCase {
21
22     public StringToolsTest(String JavaDoc name) {
23         super(name);
24     }
25
26     public void testIsEmpty() {
27         assertTrue("Null should be empty", StringTools.isEmpty(null));
28         assertTrue("Length zero should be empty", StringTools.isEmpty(""));
29     }
30
31     public void testIsTrimmedEmpty() {
32         assertTrue("Null should be empty", StringTools.isTrimmedEmpty(null));
33         assertTrue("Length zero should be empty", StringTools.isTrimmedEmpty(""));
34         assertTrue("Whitespace should be empty", StringTools.isTrimmedEmpty(" "));
35     }
36
37     public void testIsValidBoolean() {
38         assertTrue("Null should not be valid", !StringTools.isValidBoolean(null));
39         assertTrue("Empty should not be valid", !StringTools.isValidBoolean(""));
40         assertTrue("false should be valid", StringTools.isValidBoolean("false"));
41         assertTrue("False should be valid", StringTools.isValidBoolean("False"));
42         assertTrue("fAlse should be valid", StringTools.isValidBoolean("fAlse"));
43         assertTrue("true should be valid", StringTools.isValidBoolean("true"));
44         assertTrue("True should be valid", StringTools.isValidBoolean("True"));
45         assertTrue("tRue should be valid", StringTools.isValidBoolean("tRue"));
46         assertTrue("yes should not be valid", !StringTools.isValidBoolean("yes"));
47         assertTrue("no should not be valid", !StringTools.isValidBoolean("no"));
48     }
49
50     public void testToHex() {
51         assertEquals("Should be 0", StringTools.toHex((byte) 0), '0');
52         assertEquals("Should be 1", StringTools.toHex((byte) 1), '1');
53         assertEquals("Should be 2", StringTools.toHex((byte) 2), '2');
54         assertEquals("Should be 3", StringTools.toHex((byte) 3), '3');
55         assertEquals("Should be 4", StringTools.toHex((byte) 4), '4');
56         assertEquals("Should be 5", StringTools.toHex((byte) 5), '5');
57         assertEquals("Should be 6", StringTools.toHex((byte) 6), '6');
58         assertEquals("Should be 7", StringTools.toHex((byte) 7), '7');
59         assertEquals("Should be 8", StringTools.toHex((byte) 8), '8');
60         assertEquals("Should be 9", StringTools.toHex((byte) 9), '9');
61         assertEquals("Should be a", StringTools.toHex((byte) 10), 'a');
62         assertEquals("Should be b", StringTools.toHex((byte) 11), 'b');
63         assertEquals("Should be c", StringTools.toHex((byte) 12), 'c');
64         assertEquals("Should be d", StringTools.toHex((byte) 13), 'd');
65         assertEquals("Should be e", StringTools.toHex((byte) 14), 'e');
66         assertEquals("Should be f", StringTools.toHex((byte) 15), 'f');
67         assertEquals("Should be 0", StringTools.toHex(new int[]{0}), "0");
68         assertEquals("Should be c8", StringTools.toHex(new int[]{200}), "c8");
69         assertEquals("Should be ea5d", StringTools.toHex(new int[]{234, 93}), "ea5d");
70
71         try {
72             StringTools.toHex((byte) 16);
73             fail("Should have failed");
74         } catch (IllegalArgumentException JavaDoc iae) {
75             // expected
76
}
77     }
78
79     public void testFromHex() {
80         assertEquals("Should be 0", StringTools.fromHex('0'), 0);
81         assertEquals("Should be 1", StringTools.fromHex('1'), 1);
82         assertEquals("Should be 2", StringTools.fromHex('2'), 2);
83         assertEquals("Should be 3", StringTools.fromHex('3'), 3);
84         assertEquals("Should be 4", StringTools.fromHex('4'), 4);
85         assertEquals("Should be 5", StringTools.fromHex('5'), 5);
86         assertEquals("Should be 6", StringTools.fromHex('6'), 6);
87         assertEquals("Should be 7", StringTools.fromHex('7'), 7);
88         assertEquals("Should be 8", StringTools.fromHex('8'), 8);
89         assertEquals("Should be 9", StringTools.fromHex('9'), 9);
90         assertEquals("Should be A", StringTools.fromHex('A'), 10);
91         assertEquals("Should be B", StringTools.fromHex('B'), 11);
92         assertEquals("Should be C", StringTools.fromHex('C'), 12);
93         assertEquals("Should be D", StringTools.fromHex('D'), 13);
94         assertEquals("Should be E", StringTools.fromHex('E'), 14);
95         assertEquals("Should be F", StringTools.fromHex('F'), 15);
96         assertTrue("Should be 00", Arrays.equals(StringTools.fromHex("00"), new byte[]{(byte) 0}));
97         assertTrue("Should be C8", Arrays.equals(StringTools.fromHex("C8"), new byte[]{(byte) 200}));
98         assertTrue("Should be EA5D", Arrays.equals(StringTools.fromHex("EA5D"), new byte[]{(byte) 234, (byte) 93}));
99
100         try {
101             StringTools.fromHex('G');
102             fail("Should have failed");
103         } catch (IllegalArgumentException JavaDoc iae) {
104             // expected
105
}
106     }
107
108     /**
109      * Tests the clean method
110      */

111     public void testClean() {
112         String JavaDoc str = StringTools.cleanString(null);
113         assertEquals("", str);
114
115         str = StringTools.cleanString("f");
116         assertEquals("f", str);
117     }
118
119     public void testConvert() {
120         String JavaDoc[] values = StringTools.convertEmpty(new String JavaDoc[]{""});
121         assertNull(values);
122
123         values = StringTools.convertEmpty(null);
124         assertNull(values);
125
126         values = StringTools.convertEmpty(new String JavaDoc[]{" "});
127         assertNotNull(values);
128         assertEquals(" ", values[0]);
129
130         values = StringTools.convertEmpty(new String JavaDoc[]{"a",""});
131         assertNotNull(values);
132         assertEquals("a", values[0]);
133
134         values = StringTools.convertEmpty(new String JavaDoc[]{"",""});
135         assertNotNull(values);
136         assertEquals("", values[0]);
137     }
138 }
Popular Tags