| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.util; |
| 34 | | | |
| 35 | | | import java.io.UnsupportedEncodingException; |
| 36 | | | import junit.framework.TestCase; |
| 37 | | | import org.jcoderz.commons.ArgumentMalformedException; |
| 38 | | | |
| 39 | | | |
| 40 | | | |
| 41 | | | |
| 42 | | | @author |
| 43 | | | |
| 44 | 100 | | public class StringUtilTest |
| 45 | | | extends TestCase |
| 46 | | | { |
| 47 | | | private static final String FOO_STRING = "foo"; |
| 48 | | | private static final String NULL_SHOULD_PRODUCE_NULL |
| 49 | | | = "Null should produce null."; |
| 50 | | | private static final int MIN_LENGTH = 10; |
| 51 | | | private static final int MAX_LENGTH = 20; |
| 52 | | | |
| 53 | | | |
| 54 | | | public void testAsciiToStringNull () |
| 55 | | | { |
| 56 | 100 | | assertEquals(NULL_SHOULD_PRODUCE_NULL, null, |
| 57 | | | StringUtil.asciiToString(null)); |
| 58 | 100 | | assertEquals(NULL_SHOULD_PRODUCE_NULL, null, |
| 59 | | | StringUtil.asciiToString(null, 0, 0)); |
| 60 | 100 | | } |
| 61 | | | |
| 62 | | | |
| 63 | | | public void testAsciiToString () |
| 64 | | | { |
| 65 | 100 | | final String testString = "ABCDE"; |
| 66 | 100 | | assertEquals("Teststring unexpected result.", testString, |
| 67 | | | StringUtil.asciiToString(testString.getBytes())); |
| 68 | 100 | | assertNull("null byte[] should return a null string", |
| 69 | | | StringUtil.asciiToString(null)); |
| 70 | 100 | | assertEquals("Teststring unexpected result.", testString, |
| 71 | | | StringUtil.asciiToString( |
| 72 | | | testString.getBytes(), 0, testString.length())); |
| 73 | 100 | | assertNull("null byte[] should return a null string", |
| 74 | | | StringUtil.asciiToString(null, 0, 0)); |
| 75 | 100 | | } |
| 76 | | | |
| 77 | | | |
| 78 | | | {@link } |
| 79 | | | |
| 80 | | | |
| 81 | | | public void testAsciiToStringWithNonAsciiChars () |
| 82 | | | { |
| 83 | 100 | | final String testString = "\u00c4\u00d6\u00dc"; |
| 84 | | | try |
| 85 | | | { |
| 86 | 100 | | assertEquals("String should contain '?' only.", |
| 87 | | | "???", StringUtil.asciiToString( |
| 88 | | | testString.getBytes(Constants.ENCODING_ASCII))); |
| 89 | | | } |
| 90 | 0 | | catch (UnsupportedEncodingException e) |
| 91 | | | { |
| 92 | 0 | | fail("Ups, ASCII encoding not supported?" + e); |
| 93 | 100 | | } |
| 94 | 100 | | } |
| 95 | | | |
| 96 | | | |
| 97 | | | public void testToStringNull () |
| 98 | | | { |
| 99 | 100 | | assertEquals(NULL_SHOULD_PRODUCE_NULL, null, |
| 100 | | | StringUtil.toString(null)); |
| 101 | 100 | | assertEquals(NULL_SHOULD_PRODUCE_NULL, null, |
| 102 | | | StringUtil.toString(null, 0 , 0)); |
| 103 | 100 | | } |
| 104 | | | |
| 105 | | | |
| 106 | | | public void testToString () |
| 107 | | | { |
| 108 | 100 | | final String testString = "ABCDE"; |
| 109 | 100 | | assertEquals("Teststring unexpected result.", testString, |
| 110 | | | StringUtil.toString(testString.getBytes())); |
| 111 | 100 | | } |
| 112 | | | |
| 113 | | | |
| 114 | | | public void testToStringUmlaute () |
| 115 | | | { |
| 116 | 100 | | final String testString = "ABCDE\u00c4\u00d6\u00dc\u00e4\u00f6\u00fc" |
| 117 | | | + "\u00df\u00e1\u00b5"; |
| 118 | 100 | | assertEquals("Teststring with umlauts unexpected result.", testString, |
| 119 | | | StringUtil.toString(StringUtil.toBytes(testString))); |
| 120 | 100 | | } |
| 121 | | | |
| 122 | | | |
| 123 | | | {@link } |
| 124 | | | |
| 125 | | | public void testIsAscii () |
| 126 | | | { |
| 127 | | | |
| 128 | 100 | | assertTrue("test with valid ASCII char", |
| 129 | | | StringUtil.isAscii('A')); |
| 130 | 100 | | assertTrue("test with valid ASCII string", |
| 131 | | | StringUtil.isAscii("The brown fox.")); |
| 132 | | | |
| 133 | 100 | | assertFalse("test with invalid ASCII char", |
| 134 | | | StringUtil.isAscii('\u00dc')); |
| 135 | 100 | | assertFalse("test with invalid ASCII string", |
| 136 | | | StringUtil.isAscii("The brown \u00e4fox.")); |
| 137 | 100 | | } |
| 138 | | | |
| 139 | | | |
| 140 | | | {@link } |
| 141 | | | |
| 142 | | | public void testIsNullOrEmpty () |
| 143 | | | { |
| 144 | 100 | | assertTrue("null string should be true", StringUtil.isNullOrEmpty(null)); |
| 145 | 100 | | assertTrue("empty string should be true", StringUtil.isNullOrEmpty("")); |
| 146 | 100 | (1) | assertFalse("any string should be false", |
| 147 | | | StringUtil.isNullOrEmpty(FOO_STRING)); |
| 148 | 100 | | } |
| 149 | | | |
| 150 | | | |
| 151 | | | {@link } |
| 152 | | | |
| 153 | | | public void testIsEmptyOrNull () |
| 154 | | | { |
| 155 | 100 | | assertTrue("null string should be true", StringUtil.isEmptyOrNull(null)); |
| 156 | 100 | | assertTrue("empty string should be true", StringUtil.isEmptyOrNull("")); |
| 157 | 100 | | assertFalse("any string should be false", |
| 158 | | | StringUtil.isEmptyOrNull(FOO_STRING)); |
| 159 | 100 | | } |
| 160 | | | |
| 161 | | | |
| 162 | | | {@link } |
| 163 | | | |
| 164 | | | public void testIsNullOrBlank () |
| 165 | | | { |
| 166 | 100 | | assertTrue("null string should be true", StringUtil.isNullOrBlank(null)); |
| 167 | 100 | | assertTrue("empty string should be true", StringUtil.isNullOrBlank("")); |
| 168 | 100 | | assertTrue("tab string should be true", StringUtil.isNullOrBlank("\t")); |
| 169 | 100 | (2) | assertTrue("whitespace string should be true", StringUtil.isNullOrBlank(" ")); |
| 170 | 100 | | assertFalse("any string should be false", |
| 171 | | | StringUtil.isNullOrBlank(FOO_STRING)); |
| 172 | 100 | | assertFalse("any string should be false", |
| 173 | | | StringUtil.isNullOrBlank(" " + FOO_STRING + " ")); |
| 174 | 100 | | assertFalse("'x ' string should be false", |
| 175 | | | StringUtil.isNullOrBlank("x ")); |
| 176 | 100 | | assertFalse("' x' string should be false", |
| 177 | | | StringUtil.isNullOrBlank(" x")); |
| 178 | 100 | | } |
| 179 | | | |
| 180 | | | |
| 181 | | | {@link } |
| 182 | | | |
| 183 | | | public void testEquals () |
| 184 | | | { |
| 185 | | | |
| 186 | 100 | | assertTrue("two null string references should be equal", |
| 187 | | | StringUtil.equals(null, null)); |
| 188 | 100 | | assertTrue("same string reference should be equals", |
| 189 | | | StringUtil.equals(FOO_STRING, FOO_STRING)); |
| 190 | | | |
| 191 | | | |
| 192 | 100 | | assertFalse("string reference equals null reference should be false", |
| 193 | | | StringUtil.equals(null, FOO_STRING)); |
| 194 | 100 | | assertFalse("string reference equals null reference should be false", |
| 195 | | | StringUtil.equals(FOO_STRING, null)); |
| 196 | 100 | | assertFalse("different string reference should not be equals", |
| 197 | | | StringUtil.equals(FOO_STRING, "bar")); |
| 198 | 100 | | } |
| 199 | | | |
| 200 | | | |
| 201 | | | {@link } |
| 202 | | | @throws |
| 203 | | | |
| 204 | | | public void testFitToLength () |
| 205 | | | throws Exception |
| 206 | | | { |
| 207 | 0 | | final String smallStr = "xxx"; |
| 208 | 0 | | final String mediumStr = "xxxxxxxxxxxxxxx"; |
| 209 | | | |
| 210 | 0 | | final String bigStr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; |
| 211 | | | |
| 212 | | | |
| 213 | | | |
| 214 | 0 | | assertEquals("Result should be " + MIN_LENGTH + " chars long", MIN_LENGTH, |
| 215 | | | StringUtil.fitToLength(smallStr, MIN_LENGTH, MAX_LENGTH).length()); |
| 216 | 0 | | assertEquals("Result should be " + MAX_LENGTH + " chars long", MAX_LENGTH, |
| 217 | | | StringUtil.fitToLength(bigStr, MIN_LENGTH, MAX_LENGTH).length()); |
| 218 | 0 | | assertEquals("String that fits between MIN_LENGTH and MAX_LENGTH " |
| 219 | | | + "should be returned unmodified", mediumStr, |
| 220 | | | StringUtil.fitToLength(mediumStr, MIN_LENGTH, MAX_LENGTH)); |
| 221 | | | |
| 222 | | | |
| 223 | 0 | | StringUtil.fitToLength(smallStr, MIN_LENGTH, MIN_LENGTH); |
| 224 | | | try |
| 225 | | | { |
| 226 | 0 | | StringUtil.fitToLength(smallStr, MAX_LENGTH, MIN_LENGTH); |
| 227 | 0 | | fail("Should throw exception if minLength is bigger than maxLength"); |
| 228 | | | } |
| 229 | 100 | | catch (ArgumentMalformedException x) |
| 230 | | | { |
| 231 | | | |
| 232 | 0 | | } |
| 233 | | | try |
| 234 | | | { |
| 235 | 0 | | StringUtil.fitToLength(null, MIN_LENGTH, MAX_LENGTH); |
| 236 | 0 | | fail("Should throw exception if string argument is null"); |
| 237 | | | } |
| 238 | 100 | | catch (ArgumentMalformedException x) |
| 239 | | | { |
| 240 | | | |
| 241 | 0 | | } |
| 242 | 100 | | } |
| 243 | | | |
| 244 | | | |
| 245 | | | {@link } |
| 246 | | | |
| 247 | | | public void testTrimLeft () |
| 248 | | | { |
| 249 | 100 | | final String trimmed = StringUtil.trimLengthLeft("12345", 1); |
| 250 | 100 | | assertEquals("Unexpected string length.", |
| 251 | | | 1, trimmed.length()); |
| 252 | 100 | | assertEquals("Unexpected result from trimLength", "5", trimmed); |
| 253 | 100 | | } |
| 254 | | | |
| 255 | | | |
| 256 | | | {@link } |
| 257 | | | |
| 258 | | | public void testPadLeft () |
| 259 | | | { |
| 260 | 100 | | final String paddedString = StringUtil.padLeft("", '0', MAX_LENGTH); |
| 261 | 100 | | assertEquals("Modified string length should be " + MAX_LENGTH, |
| 262 | | | MAX_LENGTH, paddedString.length()); |
| 263 | 100 | | assertTrue("Modified string should contain only zeros, but was " |
| 264 | | | + paddedString, paddedString.matches("[0]{" + MAX_LENGTH + "}")); |
| 265 | 100 | | } |
| 266 | | | |
| 267 | | | |
| 268 | | | {@link } |
| 269 | | | |
| 270 | | | public void testContains () |
| 271 | | | { |
| 272 | 100 | | assertTrue("'aaa' is contained in 'bbbaaaa'.", |
| 273 | | | StringUtil.contains("bbbbaaaa", "aaa")); |
| 274 | 100 | | assertFalse("'ccc' is not contained in 'bbbaaaa'.", |
| 275 | | | StringUtil.contains("bbbbaaaa", "ccc")); |
| 276 | 100 | | } |
| 277 | | | |
| 278 | | | } |