1 /*
2 * $Source$
3 * $Revision$
4 *
5 * Copyright (C) 2003 Tim Joyce
6 *
7 * Part of Melati (http://melati.org), a framework for the rapid
8 * development of clean, maintainable web applications.
9 *
10 * Melati is free software; Permission is granted to copy, distribute
11 * and/or modify this software under the terms either:
12 *
13 * a) the GNU General Public License as published by the Free Software
14 * Foundation; either version 2 of the License, or (at your option)
15 * any later version,
16 *
17 * or
18 *
19 * b) any version of the Melati Software License, as published
20 * at http://melati.org
21 *
22 * You should have received a copy of the GNU General Public License and
23 * the Melati Software License along with this program;
24 * if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
26 * GNU General Public License and visit http://melati.org to obtain the
27 * Melati Software License.
28 *
29 * Feel free to contact the Developers of Melati (http://melati.org),
30 * if you would like to work out a different arrangement than the options
31 * outlined here. It is our intention to allow Melati to be used by as
32 * wide an audience as possible.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * Contact details for copyright holder:
40 *
41 * Tim Joyce <timj At paneris.org>
42 *
43 */
44 package org.melati.poem.util.test;
45
46 import junit.framework.TestCase;
47
48 import org.melati.poem.util.StringUtils;
49
50 /**
51 * A test for the org.melati.util.StringUtil class.
52 *
53 * @author TimJ@paneris.org
54 */
55 public class StringUtilsTest extends TestCase {
56
57 /**
58 * Test method for {@link org.melati.poem.util.StringUtils#split}.
59 */
60 public void testSplit() {
61 String them = "one,two,three";
62 assertEquals(3, StringUtils.split(them, ',').length);
63 assertEquals("two", StringUtils.split(them, ',')[1]);
64 }
65
66 /**
67 * Test method for {@link org.melati.poem.util.StringUtils#appendEscaped(StringBuffer, String, char)}.
68 */
69 public void testAppendEscaped() {
70 StringBuffer buff = new StringBuffer();
71 assertEquals("",buff.toString());
72 StringUtils.appendEscaped(buff, "a$b", '$');
73 assertEquals("a\\$b",buff.toString());
74 StringUtils.appendEscaped(buff, "a$a$a$a", '$');
75 assertEquals("a\\$ba\\$a\\$a\\$a",buff.toString());
76 StringUtils.appendEscaped(buff, "c\\d", '$');
77 assertEquals("a\\$ba\\$a\\$a\\$ac\\\\d",buff.toString());
78 }
79
80 /**
81 * Test method for {@link org.melati.poem.util.StringUtils#appendQuoted(StringBuffer, String, char)}.
82 */
83 public void testAppendQuoted() {
84 StringBuffer buff = new StringBuffer();
85 assertEquals("",buff.toString());
86 StringUtils.appendQuoted(buff, "a$b", '$');
87 assertEquals("$a\\$b$",buff.toString());
88 StringUtils.appendQuoted(buff, "c\\d", '$');
89 assertEquals("$a\\$b$$c\\\\d$",buff.toString());
90 }
91
92 /**
93 * Test method for {@link org.melati.poem.util.StringUtils#quoted(String, char)}.
94 */
95 public void testQuoted() {
96 assertEquals("\"a\"", StringUtils.quoted("a",'"'));
97 assertEquals("'a'", StringUtils.quoted("a",'\''));
98 }
99
100 /**
101 * Test method for {@link org.melati.poem.util.StringUtils#escaped(String, char)}.
102 */
103 public void testEscaped() {
104 // deprecated
105 // assertEquals("Eureka\\!", StringUtils.escaped("Eureka!",'!'));
106 }
107
108 /**
109 * Test method for {@link org.melati.poem.util.StringUtils#capitalised(String)}.
110 */
111 public void testCapitalised() {
112 assertEquals("Capitalised", StringUtils.capitalised("capitalised"));
113 }
114
115 /**
116 * Test method for {@link org.melati.poem.util.StringUtils#uncapitalised(String)}.
117 */
118 public void testUncapitalised() {
119 assertEquals("capitalised", StringUtils.uncapitalised("Capitalised"));
120 }
121
122
123 /**
124 * Test method for {@link org.melati.poem.util.StringUtils#tr(String, String, String)}.
125 */
126 public void testTrStringStringString() {
127 assertEquals("Muther", StringUtils.tr("Mother","o","u"));
128 }
129
130 /**
131 * Test method for {@link org.melati.poem.util.StringUtils#tr(String, char, char)}.
132 */
133 @SuppressWarnings("deprecation")
134 public void testTrStringCharChar() {
135 assertEquals("Muther", StringUtils.tr("Mother",'o','u'));
136 }
137
138 /**
139 * Test method for {@link org.melati.poem.util.StringUtils#concatenated(String, String[])}.
140 */
141 public void testConcatenated() {
142 String[] them = {"one", "two", "three"};
143 assertEquals("one,two,three", StringUtils.concatenated(",",them));
144 assertEquals("onetwothree", StringUtils.concatenated(null,them));
145 assertEquals("", StringUtils.concatenated(null,new String[] {}));
146 }
147
148 /**
149 * Test method for {@link org.melati.poem.util.StringUtils#randomString(int)}.
150 */
151 public void testRandomString() {
152 assertEquals(5, StringUtils.randomString(5).length());
153 }
154
155 /**
156 * Test method for {@link org.melati.poem.util.StringUtils#nulled(String)}.
157 */
158 public void testNulled() {
159 assertEquals(null, StringUtils.nulled(""));
160 assertEquals("null", StringUtils.nulled("null"));
161 }
162
163
164 /**
165 * Test method for {@link org.melati.poem.util.StringUtils#unNulled(String)}.
166 */
167 public void testUnNulled() {
168 String expected = "a";
169 String actual = StringUtils.unNulled("a");
170 assertEquals(expected, actual);
171 }
172
173 /**
174 * Test method for {@link org.melati.poem.util.StringUtils#unNulled(String)}.
175 */
176 public void testUnNulledNull() {
177 String expected = "";
178 String actual = StringUtils.unNulled(null);
179 assertEquals(expected, actual);
180 }
181
182 /**
183 * Test hex encoding and decoding.
184 * Test method for {@link org.melati.poem.util.StringUtils#hexEncoding(byte[])}
185 *
186 */
187 public void testHexEncoding() {
188 String[] strings = {"01234567", "abcdef", "f1234bcd"};
189 for (int i = 0; i<strings.length; i++){
190 assertEquals(new String(StringUtils.hexDecoding(
191 StringUtils.hexEncoding(strings[i].getBytes()))),
192 strings[i]);
193 }
194 }
195
196
197 /**
198 * Test method for {@link org.melati.poem.util.StringUtils#hexDecoding(char)}.
199 */
200 public void testHexDecodingChar() {
201 assertEquals(new Integer(10), new Integer(StringUtils.hexDecoding('A')));
202 assertEquals(new Integer(10), new Integer(StringUtils.hexDecoding('a')));
203 assertEquals(new Integer(15), new Integer(StringUtils.hexDecoding('F')));
204 assertEquals(new Integer(15), new Integer(StringUtils.hexDecoding('f')));
205 try {
206 StringUtils.hexDecoding('g');
207 fail("should have bombed");
208 } catch (IllegalArgumentException e) {
209 e = null;
210 }
211 }
212
213 /**
214 * Test method for {@link org.melati.poem.util.StringUtils#hexDecoding(String)}.
215 */
216 public void testHexDecodingString() {
217 byte[] b = StringUtils.hexDecoding("41");
218 assertEquals(new Integer(65), new Integer(b[0]));
219 try {
220 StringUtils.hexDecoding("411");
221 fail("should have bombed");
222 } catch (IllegalArgumentException e) {
223 e = null;
224 }
225 }
226
227 /**
228 * Test method for {@link org.melati.poem.util.StringUtils#isQuoted(String)}.
229 */
230 public void testIsQuoted() {
231 assertTrue(StringUtils.isQuoted("\"a\""));
232 }
233
234 /**
235 * Test method for {@link org.melati.poem.util.StringUtils#isQuoted(String)}.
236 */
237 public void testIsQuotedNull() {
238 assertTrue(!StringUtils.isQuoted(null));
239 }
240
241 /**
242 * Test method for {@link org.melati.poem.util.StringUtils#isQuoted(String)}.
243 */
244 public void testIsQuotedBlank() {
245 assertTrue(!StringUtils.isQuoted(""));
246 }
247
248 /**
249 * Test method for {@link org.melati.poem.util.StringUtils#isQuoted(String)}.
250 */
251 public void testIsQuotedNot() {
252 assertTrue(!StringUtils.isQuoted("a"));
253 }
254
255 /**
256 * Test method for {@link org.melati.poem.util.StringUtils#isQuoted(String)}.
257 */
258 public void testIsQuotedDouble() {
259 assertTrue(StringUtils.isQuoted("\"a\""));
260 }
261
262 /**
263 * Test method for {@link org.melati.poem.util.StringUtils#isQuoted(String)}.
264 */
265 public void testIsQuotedSingle() {
266 assertTrue(StringUtils.isQuoted("\'a\'"));
267 }
268
269
270
271 }