1 52 53 package org.jivesoftware.smack.util; 54 55 import junit.framework.TestCase; 56 57 60 public class StringUtilsTest extends TestCase { 61 62 public void testEscapeForXML() { 63 String input = null; 64 65 assertNull(StringUtils.escapeForXML(null)); 66 67 input = "<b>"; 68 assertEquals("<b>", StringUtils.escapeForXML(input)); 69 70 input = "\""; 71 assertEquals(""", StringUtils.escapeForXML(input)); 72 73 input = "&"; 74 assertEquals("&", StringUtils.escapeForXML(input)); 75 76 input = "<b>\n\t\r</b>"; 77 assertEquals("<b>\n\t\r</b>", StringUtils.escapeForXML(input)); 78 79 input = " & "; 80 assertEquals(" & ", StringUtils.escapeForXML(input)); 81 82 input = " \" "; 83 assertEquals(" " ", StringUtils.escapeForXML(input)); 84 85 input = "> of me <"; 86 assertEquals("> of me <", StringUtils.escapeForXML(input)); 87 88 input = "> of me & you<"; 89 assertEquals("> of me & you<", StringUtils.escapeForXML(input)); 90 91 input = "& <"; 92 assertEquals("& <", StringUtils.escapeForXML(input)); 93 94 input = "&"; 95 assertEquals("&", StringUtils.escapeForXML(input)); 96 } 97 98 public void testHash() { 99 try { 102 StringUtils.hash(null); 103 fail(); 104 } 105 catch (NullPointerException npe) { 106 assertTrue(true); 107 } 108 109 String result = StringUtils.hash(""); 111 assertEquals("da39a3ee5e6b4b0d3255bfef95601890afd80709", result); 112 113 String adminInHash = "d033e22ae348aeb5660fc2140aec35850c4da997"; 115 result = StringUtils.hash("admin"); 116 assertEquals(adminInHash, result); 117 118 String random = "jive software blah and stuff this is pretty cool"; 121 result = StringUtils.hash(random); 122 assertTrue(isValidHash(result)); 123 124 String junk = "\n\n\t\b\r!@(!)^(#)@+_-\u2031\u09291\u00A9\u00BD\u0394\u00F8"; 126 result = StringUtils.hash(junk); 127 assertTrue(isValidHash(result)); 128 } 129 130 131 132 private final String HASH_CHARS = "0123456789abcdef"; 133 134 137 private boolean isValidHash(String 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 input = ""; 150 String output = ""; 151 assertEquals(new String (StringUtils.encodeHex(input.getBytes())), 152 new String (output.getBytes())); 153 154 input = "foo bar 123"; 155 output = "666f6f2062617220313233"; 156 assertEquals(new String (StringUtils.encodeHex(input.getBytes())), 157 new String (output.getBytes())); 158 } 159 160 163 public void testEncodeBase64() { 164 String input = ""; 165 String 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 184 203 204 public void testRandomString() { 205 String result = StringUtils.randomString(-1); 207 assertNull(result); 208 209 result = StringUtils.randomString(0); 211 assertNull(result); 212 213 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 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 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 |