1 16 package org.apache.commons.lang; 17 18 import junit.framework.Test; 19 import junit.framework.TestCase; 20 import junit.framework.TestSuite; 21 import junit.textui.TestRunner; 22 23 30 public class StringUtilsTrimEmptyTest extends TestCase { 31 private static final String FOO = "foo"; 32 33 public StringUtilsTrimEmptyTest(String name) { 34 super(name); 35 } 36 37 public static void main(String [] args) { 38 TestRunner.run(suite()); 39 } 40 41 public static Test suite() { 42 TestSuite suite = new TestSuite(StringUtilsTrimEmptyTest.class); 43 suite.setName("StringUtilsTrimEmpty Tests"); 44 return suite; 45 } 46 47 protected void setUp() throws Exception { 48 super.setUp(); 49 } 50 51 protected void tearDown() throws Exception { 52 super.tearDown(); 53 } 54 55 public void testIsEmpty() { 57 assertEquals(true, StringUtils.isEmpty(null)); 58 assertEquals(true, StringUtils.isEmpty("")); 59 assertEquals(false, StringUtils.isEmpty(" ")); 60 assertEquals(false, StringUtils.isEmpty("foo")); 61 assertEquals(false, StringUtils.isEmpty(" foo ")); 62 } 63 64 public void testIsNotEmpty() { 65 assertEquals(false, StringUtils.isNotEmpty(null)); 66 assertEquals(false, StringUtils.isNotEmpty("")); 67 assertEquals(true, StringUtils.isNotEmpty(" ")); 68 assertEquals(true, StringUtils.isNotEmpty("foo")); 69 assertEquals(true, StringUtils.isNotEmpty(" foo ")); 70 } 71 72 public void testIsBlank() { 73 assertEquals(true, StringUtils.isBlank(null)); 74 assertEquals(true, StringUtils.isBlank("")); 75 assertEquals(true, StringUtils.isBlank(StringUtilsTest.WHITESPACE)); 76 assertEquals(false, StringUtils.isBlank("foo")); 77 assertEquals(false, StringUtils.isBlank(" foo ")); 78 } 79 80 public void testIsNotBlank() { 81 assertEquals(false, StringUtils.isNotBlank(null)); 82 assertEquals(false, StringUtils.isNotBlank("")); 83 assertEquals(false, StringUtils.isNotBlank(StringUtilsTest.WHITESPACE)); 84 assertEquals(true, StringUtils.isNotBlank("foo")); 85 assertEquals(true, StringUtils.isNotBlank(" foo ")); 86 } 87 88 public void testClean() { 90 assertEquals(FOO, StringUtils.clean(FOO + " ")); 91 assertEquals(FOO, StringUtils.clean(" " + FOO + " ")); 92 assertEquals(FOO, StringUtils.clean(" " + FOO)); 93 assertEquals(FOO, StringUtils.clean(FOO + "")); 94 assertEquals("", StringUtils.clean(" \t\r\n\b ")); 95 assertEquals("", StringUtils.clean("")); 96 assertEquals("", StringUtils.clean(null)); 97 } 98 99 public void testTrim() { 100 assertEquals(FOO, StringUtils.trim(FOO + " ")); 101 assertEquals(FOO, StringUtils.trim(" " + FOO + " ")); 102 assertEquals(FOO, StringUtils.trim(" " + FOO)); 103 assertEquals(FOO, StringUtils.trim(FOO + "")); 104 assertEquals("", StringUtils.trim(" \t\r\n\b ")); 105 assertEquals("", StringUtils.trim(StringUtilsTest.TRIMMABLE)); 106 assertEquals(StringUtilsTest.NON_TRIMMABLE, StringUtils.trim(StringUtilsTest.NON_TRIMMABLE)); 107 assertEquals("", StringUtils.trim("")); 108 assertEquals(null, StringUtils.trim(null)); 109 } 110 111 public void testTrimToNull() { 112 assertEquals(FOO, StringUtils.trimToNull(FOO + " ")); 113 assertEquals(FOO, StringUtils.trimToNull(" " + FOO + " ")); 114 assertEquals(FOO, StringUtils.trimToNull(" " + FOO)); 115 assertEquals(FOO, StringUtils.trimToNull(FOO + "")); 116 assertEquals(null, StringUtils.trimToNull(" \t\r\n\b ")); 117 assertEquals(null, StringUtils.trimToNull(StringUtilsTest.TRIMMABLE)); 118 assertEquals(StringUtilsTest.NON_TRIMMABLE, StringUtils.trimToNull(StringUtilsTest.NON_TRIMMABLE)); 119 assertEquals(null, StringUtils.trimToNull("")); 120 assertEquals(null, StringUtils.trimToNull(null)); 121 } 122 123 public void testTrimToEmpty() { 124 assertEquals(FOO, StringUtils.trimToEmpty(FOO + " ")); 125 assertEquals(FOO, StringUtils.trimToEmpty(" " + FOO + " ")); 126 assertEquals(FOO, StringUtils.trimToEmpty(" " + FOO)); 127 assertEquals(FOO, StringUtils.trimToEmpty(FOO + "")); 128 assertEquals("", StringUtils.trimToEmpty(" \t\r\n\b ")); 129 assertEquals("", StringUtils.trimToEmpty(StringUtilsTest.TRIMMABLE)); 130 assertEquals(StringUtilsTest.NON_TRIMMABLE, StringUtils.trimToEmpty(StringUtilsTest.NON_TRIMMABLE)); 131 assertEquals("", StringUtils.trimToEmpty("")); 132 assertEquals("", StringUtils.trimToEmpty(null)); 133 } 134 135 public void testStrip_String() { 137 assertEquals(null, StringUtils.strip(null)); 138 assertEquals("", StringUtils.strip("")); 139 assertEquals("", StringUtils.strip(" ")); 140 assertEquals("abc", StringUtils.strip(" abc ")); 141 assertEquals(StringUtilsTest.NON_WHITESPACE, 142 StringUtils.strip(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE + StringUtilsTest.WHITESPACE)); 143 } 144 145 public void testStripToNull_String() { 146 assertEquals(null, StringUtils.stripToNull(null)); 147 assertEquals(null, StringUtils.stripToNull("")); 148 assertEquals(null, StringUtils.stripToNull(" ")); 149 assertEquals(null, StringUtils.stripToNull(StringUtilsTest.WHITESPACE)); 150 assertEquals("ab c", StringUtils.stripToNull(" ab c ")); 151 assertEquals(StringUtilsTest.NON_WHITESPACE, 152 StringUtils.stripToNull(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE + StringUtilsTest.WHITESPACE)); 153 } 154 155 public void testStripToEmpty_String() { 156 assertEquals("", StringUtils.stripToEmpty(null)); 157 assertEquals("", StringUtils.stripToEmpty("")); 158 assertEquals("", StringUtils.stripToEmpty(" ")); 159 assertEquals("", StringUtils.stripToEmpty(StringUtilsTest.WHITESPACE)); 160 assertEquals("ab c", StringUtils.stripToEmpty(" ab c ")); 161 assertEquals(StringUtilsTest.NON_WHITESPACE, 162 StringUtils.stripToEmpty(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE + StringUtilsTest.WHITESPACE)); 163 } 164 165 public void testStrip_StringString() { 166 assertEquals(null, StringUtils.strip(null, null)); 168 assertEquals("", StringUtils.strip("", null)); 169 assertEquals("", StringUtils.strip(" ", null)); 170 assertEquals("abc", StringUtils.strip(" abc ", null)); 171 assertEquals(StringUtilsTest.NON_WHITESPACE, 172 StringUtils.strip(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE + StringUtilsTest.WHITESPACE, null)); 173 174 assertEquals(null, StringUtils.strip(null, "")); 176 assertEquals("", StringUtils.strip("", "")); 177 assertEquals(" ", StringUtils.strip(" ", "")); 178 assertEquals(" abc ", StringUtils.strip(" abc ", "")); 179 assertEquals(StringUtilsTest.WHITESPACE, StringUtils.strip(StringUtilsTest.WHITESPACE, "")); 180 181 assertEquals(null, StringUtils.strip(null, " ")); 183 assertEquals("", StringUtils.strip("", " ")); 184 assertEquals("", StringUtils.strip(" ", " ")); 185 assertEquals("abc", StringUtils.strip(" abc ", " ")); 186 187 assertEquals(null, StringUtils.strip(null, "ab")); 189 assertEquals("", StringUtils.strip("", "ab")); 190 assertEquals(" ", StringUtils.strip(" ", "ab")); 191 assertEquals(" abc ", StringUtils.strip(" abc ", "ab")); 192 assertEquals("c", StringUtils.strip("abcabab", "ab")); 193 assertEquals(StringUtilsTest.WHITESPACE, StringUtils.strip(StringUtilsTest.WHITESPACE, "")); 194 } 195 196 public void testStripStart_StringString() { 197 assertEquals(null, StringUtils.stripStart(null, null)); 199 assertEquals("", StringUtils.stripStart("", null)); 200 assertEquals("", StringUtils.stripStart(" ", null)); 201 assertEquals("abc ", StringUtils.stripStart(" abc ", null)); 202 assertEquals(StringUtilsTest.NON_WHITESPACE + StringUtilsTest.WHITESPACE, 203 StringUtils.stripStart(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE + StringUtilsTest.WHITESPACE, null)); 204 205 assertEquals(null, StringUtils.stripStart(null, "")); 207 assertEquals("", StringUtils.stripStart("", "")); 208 assertEquals(" ", StringUtils.stripStart(" ", "")); 209 assertEquals(" abc ", StringUtils.stripStart(" abc ", "")); 210 assertEquals(StringUtilsTest.WHITESPACE, StringUtils.stripStart(StringUtilsTest.WHITESPACE, "")); 211 212 assertEquals(null, StringUtils.stripStart(null, " ")); 214 assertEquals("", StringUtils.stripStart("", " ")); 215 assertEquals("", StringUtils.stripStart(" ", " ")); 216 assertEquals("abc ", StringUtils.stripStart(" abc ", " ")); 217 218 assertEquals(null, StringUtils.stripStart(null, "ab")); 220 assertEquals("", StringUtils.stripStart("", "ab")); 221 assertEquals(" ", StringUtils.stripStart(" ", "ab")); 222 assertEquals(" abc ", StringUtils.stripStart(" abc ", "ab")); 223 assertEquals("cabab", StringUtils.stripStart("abcabab", "ab")); 224 assertEquals(StringUtilsTest.WHITESPACE, StringUtils.stripStart(StringUtilsTest.WHITESPACE, "")); 225 } 226 227 public void testStripEnd_StringString() { 228 assertEquals(null, StringUtils.stripEnd(null, null)); 230 assertEquals("", StringUtils.stripEnd("", null)); 231 assertEquals("", StringUtils.stripEnd(" ", null)); 232 assertEquals(" abc", StringUtils.stripEnd(" abc ", null)); 233 assertEquals(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE, 234 StringUtils.stripEnd(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE + StringUtilsTest.WHITESPACE, null)); 235 236 assertEquals(null, StringUtils.stripEnd(null, "")); 238 assertEquals("", StringUtils.stripEnd("", "")); 239 assertEquals(" ", StringUtils.stripEnd(" ", "")); 240 assertEquals(" abc ", StringUtils.stripEnd(" abc ", "")); 241 assertEquals(StringUtilsTest.WHITESPACE, StringUtils.stripEnd(StringUtilsTest.WHITESPACE, "")); 242 243 assertEquals(null, StringUtils.stripEnd(null, " ")); 245 assertEquals("", StringUtils.stripEnd("", " ")); 246 assertEquals("", StringUtils.stripEnd(" ", " ")); 247 assertEquals(" abc", StringUtils.stripEnd(" abc ", " ")); 248 249 assertEquals(null, StringUtils.stripEnd(null, "ab")); 251 assertEquals("", StringUtils.stripEnd("", "ab")); 252 assertEquals(" ", StringUtils.stripEnd(" ", "ab")); 253 assertEquals(" abc ", StringUtils.stripEnd(" abc ", "ab")); 254 assertEquals("abc", StringUtils.stripEnd("abcabab", "ab")); 255 assertEquals(StringUtilsTest.WHITESPACE, StringUtils.stripEnd(StringUtilsTest.WHITESPACE, "")); 256 } 257 258 public void testStripAll() { 259 String [] empty = new String [0]; 261 String [] fooSpace = new String [] { " "+FOO+" ", " "+FOO, FOO+" " }; 262 String [] fooDots = new String [] { ".."+FOO+"..", ".."+FOO, FOO+".." }; 263 String [] foo = new String [] { FOO, FOO, FOO }; 264 265 assertEquals(null, StringUtils.stripAll(null)); 266 assertArrayEquals(empty, StringUtils.stripAll(empty)); 267 assertArrayEquals(foo, StringUtils.stripAll(fooSpace)); 268 269 assertEquals(null, StringUtils.stripAll(null, null)); 270 assertArrayEquals(foo, StringUtils.stripAll(fooSpace, null)); 271 assertArrayEquals(foo, StringUtils.stripAll(fooDots, ".")); 272 } 273 274 private void assertArrayEquals(Object [] o1, Object [] o2) { 275 if(o1 == null) { 276 assertEquals(o1,o2); 277 return; 278 } 279 assertEquals("Length not equal. ", o1.length, o2.length); 280 int sz = o1.length; 281 for(int i=0; i<sz; i++) { 282 if(o1[i] instanceof Object []) { 283 assertArrayEquals( (Object []) o1[i], (Object []) o2[i] ); 285 } else { 286 assertEquals(o1[i], o2[i]); 287 } 288 } 289 } 290 291 } 292 | Popular Tags |