KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smack > util > StringUtilsTest


1 /**
2  * $RCSfile$
3  * $Revision: 2485 $
4  * $Date: 2005-04-14 21:56:37 -0300 (Thu, 14 Apr 2005) $
5  *
6  * Copyright (C) 2002-2003 Jive Software. All rights reserved.
7  * ====================================================================
8  * The Jive Software License (based on Apache Software License, Version 1.1)
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in
19  * the documentation and/or other materials provided with the
20  * distribution.
21  *
22  * 3. The end-user documentation included with the redistribution,
23  * if any, must include the following acknowledgment:
24  * "This product includes software developed by
25  * Jive Software (http://www.jivesoftware.com)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Smack" and "Jive Software" must not be used to
30  * endorse or promote products derived from this software without
31  * prior written permission. For written permission, please
32  * contact webmaster@jivesoftware.com.
33  *
34  * 5. Products derived from this software may not be called "Smack",
35  * nor may "Smack" appear in their name, without prior written
36  * permission of Jive Software.
37  *
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  * DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  * ====================================================================
51  */

52
53 package org.jivesoftware.smack.util;
54
55 import junit.framework.TestCase;
56
57 /**
58  * A test case for the StringUtils class.
59  */

60 public class StringUtilsTest extends TestCase {
61
62     public void testEscapeForXML() {
63         String JavaDoc input = null;
64
65         assertNull(StringUtils.escapeForXML(null));
66
67         input = "<b>";
68         assertEquals("&lt;b&gt;", StringUtils.escapeForXML(input));
69
70         input = "\"";
71         assertEquals("&quot;", StringUtils.escapeForXML(input));
72
73         input = "&";
74         assertEquals("&amp;", StringUtils.escapeForXML(input));
75
76         input = "<b>\n\t\r</b>";
77         assertEquals("&lt;b&gt;\n\t\r&lt;/b&gt;", StringUtils.escapeForXML(input));
78
79         input = " & ";
80         assertEquals(" &amp; ", StringUtils.escapeForXML(input));
81
82         input = " \" ";
83         assertEquals(" &quot; ", StringUtils.escapeForXML(input));
84
85         input = "> of me <";
86         assertEquals("&gt; of me &lt;", StringUtils.escapeForXML(input));
87
88         input = "> of me & you<";
89         assertEquals("&gt; of me &amp; you&lt;", StringUtils.escapeForXML(input));
90
91         input = "& <";
92         assertEquals("&amp; &lt;", StringUtils.escapeForXML(input));
93
94         input = "&";
95         assertEquals("&amp;", StringUtils.escapeForXML(input));
96     }
97
98     public void testHash() {
99         // Test null
100
// @TODO - should the StringUtils.hash(String) method be fixed to handle null input?
101
try {
102             StringUtils.hash(null);
103             fail();
104         }
105         catch (NullPointerException JavaDoc npe) {
106             assertTrue(true);
107         }
108
109         // Test empty String
110
String JavaDoc result = StringUtils.hash("");
111         assertEquals("da39a3ee5e6b4b0d3255bfef95601890afd80709", result);
112
113         // Test a known hash
114
String JavaDoc adminInHash = "d033e22ae348aeb5660fc2140aec35850c4da997";
115         result = StringUtils.hash("admin");
116         assertEquals(adminInHash, result);
117
118         // Test a random String - make sure all resulting characters are valid hash characters
119
// and that the returned string is 32 characters long.
120
String JavaDoc random = "jive software blah and stuff this is pretty cool";
121         result = StringUtils.hash(random);
122         assertTrue(isValidHash(result));
123
124         // Test junk input:
125
String JavaDoc junk = "\n\n\t\b\r!@(!)^(#)@+_-\u2031\u09291\u00A9\u00BD\u0394\u00F8";
126         result = StringUtils.hash(junk);
127         assertTrue(isValidHash(result));
128     }
129
130     /* ----- Utility methods and vars ----- */
131
132     private final String JavaDoc HASH_CHARS = "0123456789abcdef";
133
134     /**
135      * Returns true if the input string is valid md5 hash, false otherwise.
136      */

137     private boolean isValidHash(String JavaDoc result) {
138         boolean valid = true;
139         for (int i=0; i<result.length(); i++) {
140             char c = result.charAt(i);
141             if (HASH_CHARS.indexOf(c) < 0) {
142                 valid = false;
143             }
144         }
145         return valid;
146     }
147
148     public void testEncodeHex() {
149         String JavaDoc input = "";
150         String JavaDoc output = "";
151         assertEquals(new String JavaDoc(StringUtils.encodeHex(input.getBytes())),
152                 new String JavaDoc(output.getBytes()));
153
154         input = "foo bar 123";
155         output = "666f6f2062617220313233";
156         assertEquals(new String JavaDoc(StringUtils.encodeHex(input.getBytes())),
157                 new String JavaDoc(output.getBytes()));
158     }
159
160     /**
161      * This method tests 2 StringUtil methods - encodeBase64(String) and encodeBase64(byte[]).
162      */

163     public void testEncodeBase64() {
164         String JavaDoc input = "";
165         String JavaDoc output = "";
166         assertEquals(StringUtils.encodeBase64(input), output);
167
168         input = "foo bar 123";
169         output = "Zm9vIGJhciAxMjM=";
170         assertEquals(StringUtils.encodeBase64(input), output);
171
172         input = "=";
173         output = "PQ==";
174         assertEquals(StringUtils.encodeBase64(input), output);
175
176         input = "abcdefghijklmnopqrstuvwxyz0123456789\n\t\"?!.@{}[]();',./<>#$%^&*";
177         output = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5CgkiPyEuQHt9W10oKTsnLC4vPD4jJCVeJio=";
178         assertEquals(StringUtils.encodeBase64(input), output);
179     }
180
181     /***
182      * This method tests 2 StringUtil methods - decodeBase64(String) and decodeBase64(byte[]).
183      */

184     /*
185     public void testDecodeBase64() {
186         String input = "";
187         String output = "";
188         assertEquals(StringUtils.decodeBase64(input), output);
189
190         input = "Zm9vIGJhciAxMjM=";
191         output = "foo bar 123";
192         assertEquals(StringUtils.decodeBase64(input), output);
193
194         input = "PQ==";
195         output = "=";
196         assertEquals(StringUtils.decodeBase64(input), output);
197
198         input = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5CgkiPyEuQHt9W10oKTsnLC4vPD4jJCVeJio=";
199         output = "abcdefghijklmnopqrstuvwxyz0123456789\n\t\"?!.@{}[]();',./<>#$%^&*";
200         assertEquals(StringUtils.decodeBase64(input), output);
201     }
202     */

203
204     public void testRandomString() {
205         // Boundary test
206
String JavaDoc result = StringUtils.randomString(-1);
207         assertNull(result);
208
209         // Zero length string test
210
result = StringUtils.randomString(0);
211         assertNull(result);
212
213         // Test various lengths - make sure the same length is returned
214
result = StringUtils.randomString(4);
215         assertTrue(result.length() == 4);
216         result = StringUtils.randomString(16);
217         assertTrue(result.length() == 16);
218         result = StringUtils.randomString(128);
219         assertTrue(result.length() == 128);
220     }
221
222     public void testParsing() {
223         String JavaDoc error = "Error parsing node name";
224         assertEquals(error, "", StringUtils.parseName("yahoo.myjabber.net"));
225         assertEquals(error, "", StringUtils.parseName("yahoo.myjabber.net/registred"));
226         assertEquals(error, "user", StringUtils.parseName("user@yahoo.myjabber.net/registred"));
227         assertEquals(error, "user", StringUtils.parseName("user@yahoo.myjabber.net"));
228
229         error = "Error parsing server name";
230         String JavaDoc result = "yahoo.myjabber.net";
231         assertEquals(error, result, StringUtils.parseServer("yahoo.myjabber.net"));
232         assertEquals(error, result, StringUtils.parseServer("yahoo.myjabber.net/registred"));
233         assertEquals(error, result, StringUtils.parseServer("user@yahoo.myjabber.net/registred"));
234         assertEquals(error, result, StringUtils.parseServer("user@yahoo.myjabber.net"));
235     }
236 }
237
Popular Tags