1 16 package org.apache.commons.lang; 17 18 import java.lang.reflect.Constructor ; 19 import java.lang.reflect.Modifier ; 20 21 import junit.framework.Test; 22 import junit.framework.TestCase; 23 import junit.framework.TestSuite; 24 import junit.textui.TestRunner; 25 26 35 public class CharSetUtilsTest extends TestCase { 36 37 public CharSetUtilsTest(String name) { 38 super(name); 39 } 40 41 public static void main(String [] args) { 42 TestRunner.run(suite()); 43 } 44 45 public static Test suite() { 46 TestSuite suite = new TestSuite(CharSetUtilsTest.class); 47 suite.setName("CharSetUtils Tests"); 48 return suite; 49 } 50 51 protected void setUp() throws Exception { 52 super.setUp(); 53 } 54 55 protected void tearDown() throws Exception { 56 super.tearDown(); 57 } 58 59 public void testConstructor() { 61 assertNotNull(new CharSetUtils()); 62 Constructor [] cons = CharSetUtils.class.getDeclaredConstructors(); 63 assertEquals(1, cons.length); 64 assertEquals(true, Modifier.isPublic(cons[0].getModifiers())); 65 assertEquals(true, Modifier.isPublic(CharSetUtils.class.getModifiers())); 66 assertEquals(false, Modifier.isFinal(CharSetUtils.class.getModifiers())); 67 } 68 69 public void testEvaluateSet_Stringarray() { 71 assertEquals(null, CharSetUtils.evaluateSet((String []) null)); 72 assertEquals("[]", CharSetUtils.evaluateSet(new String [0]).toString()); 73 assertEquals("[]", CharSetUtils.evaluateSet(new String [] {null}).toString()); 74 assertEquals("[a-e]", CharSetUtils.evaluateSet(new String [] {"a-e"}).toString()); 75 } 76 77 public void testSqueeze_StringString() { 79 assertEquals(null, CharSetUtils.squeeze(null, (String ) null)); 80 assertEquals(null, CharSetUtils.squeeze(null, "")); 81 82 assertEquals("", CharSetUtils.squeeze("", (String ) null)); 83 assertEquals("", CharSetUtils.squeeze("", "")); 84 assertEquals("", CharSetUtils.squeeze("", "a-e")); 85 86 assertEquals("hello", CharSetUtils.squeeze("hello", (String ) null)); 87 assertEquals("hello", CharSetUtils.squeeze("hello", "")); 88 assertEquals("hello", CharSetUtils.squeeze("hello", "a-e")); 89 assertEquals("helo", CharSetUtils.squeeze("hello", "l-p")); 90 assertEquals("heloo", CharSetUtils.squeeze("helloo", "l")); 91 assertEquals("hello", CharSetUtils.squeeze("helloo", "^l")); 92 } 93 94 public void testSqueeze_StringStringarray() { 95 assertEquals(null, CharSetUtils.squeeze(null, (String []) null)); 96 assertEquals(null, CharSetUtils.squeeze(null, new String [0])); 97 assertEquals(null, CharSetUtils.squeeze(null, new String [] {null})); 98 assertEquals(null, CharSetUtils.squeeze(null, new String [] {"el"})); 99 100 assertEquals("", CharSetUtils.squeeze("", (String []) null)); 101 assertEquals("", CharSetUtils.squeeze("", new String [0])); 102 assertEquals("", CharSetUtils.squeeze("", new String [] {null})); 103 assertEquals("", CharSetUtils.squeeze("", new String [] {"a-e"})); 104 105 assertEquals("hello", CharSetUtils.squeeze("hello", (String []) null)); 106 assertEquals("hello", CharSetUtils.squeeze("hello", new String [0])); 107 assertEquals("hello", CharSetUtils.squeeze("hello", new String [] {null})); 108 assertEquals("hello", CharSetUtils.squeeze("hello", new String [] {"a-e"})); 109 110 assertEquals("helo", CharSetUtils.squeeze("hello", new String [] { "el" })); 111 assertEquals("hello", CharSetUtils.squeeze("hello", new String [] { "e" })); 112 assertEquals("fofof", CharSetUtils.squeeze("fooffooff", new String [] { "of" })); 113 assertEquals("fof", CharSetUtils.squeeze("fooooff", new String [] { "fo" })); 114 } 115 116 public void testCount_StringString() { 118 assertEquals(0, CharSetUtils.count(null, (String ) null)); 119 assertEquals(0, CharSetUtils.count(null, "")); 120 121 assertEquals(0, CharSetUtils.count("", (String ) null)); 122 assertEquals(0, CharSetUtils.count("", "")); 123 assertEquals(0, CharSetUtils.count("", "a-e")); 124 125 assertEquals(0, CharSetUtils.count("hello", (String ) null)); 126 assertEquals(0, CharSetUtils.count("hello", "")); 127 assertEquals(1, CharSetUtils.count("hello", "a-e")); 128 assertEquals(3, CharSetUtils.count("hello", "l-p")); 129 } 130 131 public void testCount_StringStringarray() { 132 assertEquals(0, CharSetUtils.count(null, (String []) null)); 133 assertEquals(0, CharSetUtils.count(null, new String [0])); 134 assertEquals(0, CharSetUtils.count(null, new String [] {null})); 135 assertEquals(0, CharSetUtils.count(null, new String [] {"a-e"})); 136 137 assertEquals(0, CharSetUtils.count("", (String []) null)); 138 assertEquals(0, CharSetUtils.count("", new String [0])); 139 assertEquals(0, CharSetUtils.count("", new String [] {null})); 140 assertEquals(0, CharSetUtils.count("", new String [] {"a-e"})); 141 142 assertEquals(0, CharSetUtils.count("hello", (String []) null)); 143 assertEquals(0, CharSetUtils.count("hello", new String [0])); 144 assertEquals(0, CharSetUtils.count("hello", new String [] {null})); 145 assertEquals(1, CharSetUtils.count("hello", new String [] {"a-e"})); 146 147 assertEquals(3, CharSetUtils.count("hello", new String [] { "el" })); 148 assertEquals(0, CharSetUtils.count("hello", new String [] { "x" })); 149 assertEquals(2, CharSetUtils.count("hello", new String [] { "e-i" })); 150 assertEquals(5, CharSetUtils.count("hello", new String [] { "a-z" })); 151 assertEquals(0, CharSetUtils.count("hello", new String [] { "" })); 152 } 153 154 public void testKeep_StringString() { 156 assertEquals(null, CharSetUtils.keep(null, (String ) null)); 157 assertEquals(null, CharSetUtils.keep(null, "")); 158 159 assertEquals("", CharSetUtils.keep("", (String ) null)); 160 assertEquals("", CharSetUtils.keep("", "")); 161 assertEquals("", CharSetUtils.keep("", "a-e")); 162 163 assertEquals("", CharSetUtils.keep("hello", (String ) null)); 164 assertEquals("", CharSetUtils.keep("hello", "")); 165 assertEquals("", CharSetUtils.keep("hello", "xyz")); 166 assertEquals("hello", CharSetUtils.keep("hello", "a-z")); 167 assertEquals("hello", CharSetUtils.keep("hello", "oleh")); 168 assertEquals("ell", CharSetUtils.keep("hello", "el")); 169 } 170 171 public void testKeep_StringStringarray() { 172 assertEquals(null, CharSetUtils.keep(null, (String []) null)); 173 assertEquals(null, CharSetUtils.keep(null, new String [0])); 174 assertEquals(null, CharSetUtils.keep(null, new String [] {null})); 175 assertEquals(null, CharSetUtils.keep(null, new String [] {"a-e"})); 176 177 assertEquals("", CharSetUtils.keep("", (String []) null)); 178 assertEquals("", CharSetUtils.keep("", new String [0])); 179 assertEquals("", CharSetUtils.keep("", new String [] {null})); 180 assertEquals("", CharSetUtils.keep("", new String [] {"a-e"})); 181 182 assertEquals("", CharSetUtils.keep("hello", (String []) null)); 183 assertEquals("", CharSetUtils.keep("hello", new String [0])); 184 assertEquals("", CharSetUtils.keep("hello", new String [] {null})); 185 assertEquals("e", CharSetUtils.keep("hello", new String [] {"a-e"})); 186 187 assertEquals("e", CharSetUtils.keep("hello", new String [] { "a-e" })); 188 assertEquals("ell", CharSetUtils.keep("hello", new String [] { "el" })); 189 assertEquals("hello", CharSetUtils.keep("hello", new String [] { "elho" })); 190 assertEquals("hello", CharSetUtils.keep("hello", new String [] { "a-z" })); 191 assertEquals("----", CharSetUtils.keep("----", new String [] { "-" })); 192 assertEquals("ll", CharSetUtils.keep("hello", new String [] { "l" })); 193 } 194 195 public void testDelete_StringString() { 197 assertEquals(null, CharSetUtils.delete(null, (String ) null)); 198 assertEquals(null, CharSetUtils.delete(null, "")); 199 200 assertEquals("", CharSetUtils.delete("", (String ) null)); 201 assertEquals("", CharSetUtils.delete("", "")); 202 assertEquals("", CharSetUtils.delete("", "a-e")); 203 204 assertEquals("hello", CharSetUtils.delete("hello", (String ) null)); 205 assertEquals("hello", CharSetUtils.delete("hello", "")); 206 assertEquals("hllo", CharSetUtils.delete("hello", "a-e")); 207 assertEquals("he", CharSetUtils.delete("hello", "l-p")); 208 assertEquals("hello", CharSetUtils.delete("hello", "z")); 209 } 210 211 public void testDelete_StringStringarray() { 212 assertEquals(null, CharSetUtils.delete(null, (String []) null)); 213 assertEquals(null, CharSetUtils.delete(null, new String [0])); 214 assertEquals(null, CharSetUtils.delete(null, new String [] {null})); 215 assertEquals(null, CharSetUtils.delete(null, new String [] {"el"})); 216 217 assertEquals("", CharSetUtils.delete("", (String []) null)); 218 assertEquals("", CharSetUtils.delete("", new String [0])); 219 assertEquals("", CharSetUtils.delete("", new String [] {null})); 220 assertEquals("", CharSetUtils.delete("", new String [] {"a-e"})); 221 222 assertEquals("hello", CharSetUtils.delete("hello", (String []) null)); 223 assertEquals("hello", CharSetUtils.delete("hello", new String [0])); 224 assertEquals("hello", CharSetUtils.delete("hello", new String [] {null})); 225 assertEquals("hello", CharSetUtils.delete("hello", new String [] {"xyz"})); 226 227 assertEquals("ho", CharSetUtils.delete("hello", new String [] { "el" })); 228 assertEquals("", CharSetUtils.delete("hello", new String [] { "elho" })); 229 assertEquals("hello", CharSetUtils.delete("hello", new String [] { "" })); 230 assertEquals("hello", CharSetUtils.delete("hello", "")); 231 assertEquals("", CharSetUtils.delete("hello", new String [] { "a-z" })); 232 assertEquals("", CharSetUtils.delete("----", new String [] { "-" })); 233 assertEquals("heo", CharSetUtils.delete("hello", new String [] { "l" })); 234 } 235 236 237 public void testTranslate() { 238 assertEquals(null, CharSetUtils.translate(null, null, null)); 239 assertEquals("", CharSetUtils.translate("", "a", "b")); 240 assertEquals("jelly", CharSetUtils.translate("hello", "ho", "jy")); 241 assertEquals("jellj", CharSetUtils.translate("hello", "ho", "j")); 242 assertEquals("jelly", CharSetUtils.translate("hello", "ho", "jyx")); 243 assertEquals("\rhello\r", CharSetUtils.translate("\nhello\n", "\n", "\r")); 244 assertEquals("hello", CharSetUtils.translate("hello", "", "x")); 245 assertEquals("hello", CharSetUtils.translate("hello", "", "")); 246 assertEquals("hello", CharSetUtils.translate("hello", "", "")); 247 assertEquals("q651.506bera", CharSetUtils.translate("d216.102oren", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789", 249 "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM567891234")); 250 } 251 252 public void testTranslateNullPointerException() { 253 try { 254 CharSetUtils.translate("hello", null, null); 255 fail("Expecting NullPointerException"); 256 } catch (NullPointerException ex) { 257 } 258 try { 259 CharSetUtils.translate("hello", "h", null); 260 fail("Expecting NullPointerException"); 261 } catch (NullPointerException ex) { 262 } 263 try { 264 CharSetUtils.translate("hello", null, "a"); 265 fail("Expecting NullPointerException"); 266 } catch (NullPointerException ex) { 267 } 268 try { 269 CharSetUtils.translate("hello", "h", ""); 270 fail("Expecting ArrayIndexOutOfBoundsException"); 271 } catch (ArrayIndexOutOfBoundsException ex) { 272 } 273 } 274 275 276 } 277 | Popular Tags |