1 16 package org.apache.commons.lang; 17 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import junit.framework.Test; 25 import junit.framework.TestCase; 26 import junit.framework.TestSuite; 27 import junit.textui.TestRunner; 28 35 public class ValidateTest extends TestCase { 36 37 public ValidateTest(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(ValidateTest.class); 47 suite.setName("Validate 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 testIsTrue1() { 61 Validate.isTrue(true); 62 try { 63 Validate.isTrue(false); 64 fail("Expecting IllegalArgumentException"); 65 } catch (IllegalArgumentException ex) { 66 assertEquals("The validated expression is false", ex.getMessage()); 67 } 68 } 69 70 public void testIsTrue2() { 72 Validate.isTrue(true, "MSG"); 73 try { 74 Validate.isTrue(false, "MSG"); 75 fail("Expecting IllegalArgumentException"); 76 } catch (IllegalArgumentException ex) { 77 assertEquals("MSG", ex.getMessage()); 78 } 79 } 80 81 public void testIsTrue3() { 83 Validate.isTrue(true, "MSG", new Integer (6)); 84 try { 85 Validate.isTrue(false, "MSG", new Integer (6)); 86 fail("Expecting IllegalArgumentException"); 87 } catch (IllegalArgumentException ex) { 88 assertEquals("MSG6", ex.getMessage()); 89 } 90 } 91 92 public void testIsTrue4() { 94 Validate.isTrue(true, "MSG", 7); 95 try { 96 Validate.isTrue(false, "MSG", 7); 97 fail("Expecting IllegalArgumentException"); 98 } catch (IllegalArgumentException ex) { 99 assertEquals("MSG7", ex.getMessage()); 100 } 101 } 102 103 public void testIsTrue5() { 105 Validate.isTrue(true, "MSG", 7.4d); 106 try { 107 Validate.isTrue(false, "MSG", 7.4d); 108 fail("Expecting IllegalArgumentException"); 109 } catch (IllegalArgumentException ex) { 110 assertEquals("MSG7.4", ex.getMessage()); 111 } 112 } 113 114 public void testNotNull1() { 116 Validate.notNull(new Object ()); 117 try { 118 Validate.notNull(null); 119 fail("Expecting IllegalArgumentException"); 120 } catch (IllegalArgumentException ex) { 121 assertEquals("The validated object is null", ex.getMessage()); 122 } 123 } 124 125 public void testNotNull2() { 127 Validate.notNull(new Object (), "MSG"); 128 try { 129 Validate.notNull(null, "MSG"); 130 fail("Expecting IllegalArgumentException"); 131 } catch (IllegalArgumentException ex) { 132 assertEquals("MSG", ex.getMessage()); 133 } 134 } 135 136 public void testNotEmptyArray1() { 138 Validate.notEmpty(new Object [] {null}); 139 try { 140 Validate.notEmpty((Object []) null); 141 fail("Expecting IllegalArgumentException"); 142 } catch (IllegalArgumentException ex) { 143 assertEquals("The validated array is empty", ex.getMessage()); 144 } 145 try { 146 Validate.notEmpty(new Object [0]); 147 fail("Expecting IllegalArgumentException"); 148 } catch (IllegalArgumentException ex) { 149 assertEquals("The validated array is empty", ex.getMessage()); 150 } 151 } 152 153 public void testNotEmptyArray2() { 155 Validate.notEmpty(new Object [] {null}, "MSG"); 156 try { 157 Validate.notEmpty((Object []) null, "MSG"); 158 fail("Expecting IllegalArgumentException"); 159 } catch (IllegalArgumentException ex) { 160 assertEquals("MSG", ex.getMessage()); 161 } 162 try { 163 Validate.notEmpty(new Object [0], "MSG"); 164 fail("Expecting IllegalArgumentException"); 165 } catch (IllegalArgumentException ex) { 166 assertEquals("MSG", ex.getMessage()); 167 } 168 } 169 170 public void testNotEmptyCollection1() { 172 Collection coll = new ArrayList (); 173 try { 174 Validate.notEmpty((Collection ) null); 175 fail("Expecting IllegalArgumentException"); 176 } catch (IllegalArgumentException ex) { 177 assertEquals("The validated collection is empty", ex.getMessage()); 178 } 179 try { 180 Validate.notEmpty(coll); 181 fail("Expecting IllegalArgumentException"); 182 } catch (IllegalArgumentException ex) { 183 assertEquals("The validated collection is empty", ex.getMessage()); 184 } 185 coll.add(new Integer (8)); 186 Validate.notEmpty(coll); 187 } 188 189 public void testNotEmptyCollection2() { 191 Collection coll = new ArrayList (); 192 try { 193 Validate.notEmpty((Collection ) null, "MSG"); 194 fail("Expecting IllegalArgumentException"); 195 } catch (IllegalArgumentException ex) { 196 assertEquals("MSG", ex.getMessage()); 197 } 198 try { 199 Validate.notEmpty(coll, "MSG"); 200 fail("Expecting IllegalArgumentException"); 201 } catch (IllegalArgumentException ex) { 202 assertEquals("MSG", ex.getMessage()); 203 } 204 coll.add(new Integer (8)); 205 Validate.notEmpty(coll, "MSG"); 206 } 207 208 public void testNotEmptyMap1() { 210 Map map = new HashMap (); 211 try { 212 Validate.notEmpty((Map ) null); 213 fail("Expecting IllegalArgumentException"); 214 } catch (IllegalArgumentException ex) { 215 assertEquals("The validated map is empty", ex.getMessage()); 216 } 217 try { 218 Validate.notEmpty(map); 219 fail("Expecting IllegalArgumentException"); 220 } catch (IllegalArgumentException ex) { 221 assertEquals("The validated map is empty", ex.getMessage()); 222 } 223 map.put("ll", new Integer (8)); 224 Validate.notEmpty(map); 225 } 226 227 public void testNotEmptyMap2() { 229 Map map = new HashMap (); 230 try { 231 Validate.notEmpty((Map ) null, "MSG"); 232 fail("Expecting IllegalArgumentException"); 233 } catch (IllegalArgumentException ex) { 234 assertEquals("MSG", ex.getMessage()); 235 } 236 try { 237 Validate.notEmpty(map, "MSG"); 238 fail("Expecting IllegalArgumentException"); 239 } catch (IllegalArgumentException ex) { 240 assertEquals("MSG", ex.getMessage()); 241 } 242 map.put("ll", new Integer (8)); 243 Validate.notEmpty(map, "MSG"); 244 } 245 246 public void testNotEmptyString1() { 248 Validate.notEmpty("hjl"); 249 try { 250 Validate.notEmpty((String ) null); 251 fail("Expecting IllegalArgumentException"); 252 } catch (IllegalArgumentException ex) { 253 assertEquals("The validated string is empty", ex.getMessage()); 254 } 255 try { 256 Validate.notEmpty(""); 257 fail("Expecting IllegalArgumentException"); 258 } catch (IllegalArgumentException ex) { 259 assertEquals("The validated string is empty", ex.getMessage()); 260 } 261 } 262 263 public void testNotEmptyString2() { 265 Validate.notEmpty("a", "MSG"); 266 try { 267 Validate.notEmpty((String ) null, "MSG"); 268 fail("Expecting IllegalArgumentException"); 269 } catch (IllegalArgumentException ex) { 270 assertEquals("MSG", ex.getMessage()); 271 } 272 try { 273 Validate.notEmpty("", "MSG"); 274 fail("Expecting IllegalArgumentException"); 275 } catch (IllegalArgumentException ex) { 276 assertEquals("MSG", ex.getMessage()); 277 } 278 } 279 280 public void testNoNullElementsArray1() { 282 String [] array = new String [] {"a", "b"}; 283 Validate.noNullElements(array); 284 try { 285 Validate.noNullElements((Object []) null); 286 fail("Expecting IllegalArgumentException"); 287 } catch (IllegalArgumentException ex) { 288 assertEquals("The validated object is null", ex.getMessage()); 289 } 290 array[1] = null; 291 try { 292 Validate.noNullElements(array); 293 fail("Expecting IllegalArgumentException"); 294 } catch (IllegalArgumentException ex) { 295 assertEquals("The validated array contains null element at index: 1", ex.getMessage()); 296 } 297 } 298 299 public void testNoNullElementsArray2() { 301 String [] array = new String [] {"a", "b"}; 302 Validate.noNullElements(array, "MSG"); 303 try { 304 Validate.noNullElements((Object []) null, "MSG"); 305 fail("Expecting IllegalArgumentException"); 306 } catch (IllegalArgumentException ex) { 307 assertEquals("The validated object is null", ex.getMessage()); 308 } 309 array[1] = null; 310 try { 311 Validate.noNullElements(array, "MSG"); 312 fail("Expecting IllegalArgumentException"); 313 } catch (IllegalArgumentException ex) { 314 assertEquals("MSG", ex.getMessage()); 315 } 316 } 317 318 public void testNoNullElementsCollection1() { 320 List coll = new ArrayList (); 321 coll.add("a"); 322 coll.add("b"); 323 Validate.noNullElements(coll); 324 try { 325 Validate.noNullElements((Collection ) null); 326 fail("Expecting IllegalArgumentException"); 327 } catch (IllegalArgumentException ex) { 328 assertEquals("The validated object is null", ex.getMessage()); 329 } 330 coll.set(1, null); 331 try { 332 Validate.noNullElements(coll); 333 fail("Expecting IllegalArgumentException"); 334 } catch (IllegalArgumentException ex) { 335 assertEquals("The validated collection contains null element at index: 1", ex.getMessage()); 336 } 337 } 338 339 public void testNoNullElementsCollection2() { 341 List coll = new ArrayList (); 342 coll.add("a"); 343 coll.add("b"); 344 Validate.noNullElements(coll, "MSG"); 345 try { 346 Validate.noNullElements((Collection ) null, "MSG"); 347 fail("Expecting IllegalArgumentException"); 348 } catch (IllegalArgumentException ex) { 349 assertEquals("The validated object is null", ex.getMessage()); 350 } 351 coll.set(1, null); 352 try { 353 Validate.noNullElements(coll, "MSG"); 354 fail("Expecting IllegalArgumentException"); 355 } catch (IllegalArgumentException ex) { 356 assertEquals("MSG", ex.getMessage()); 357 } 358 } 359 360 public void testAllElementsOfType() { 362 List coll = new ArrayList (); 363 coll.add("a"); 364 coll.add("b"); 365 Validate.allElementsOfType(coll, String .class, "MSG"); 366 try { 367 Validate.allElementsOfType(coll, Integer .class, "MSG"); 368 fail("Expecting IllegalArgumentException"); 369 } catch (IllegalArgumentException ex) { 370 assertEquals("MSG", ex.getMessage()); 371 } 372 coll.set(1, Boolean.FALSE); 373 try { 374 Validate.allElementsOfType(coll, String .class); 375 fail("Expecting IllegalArgumentException"); 376 } catch (IllegalArgumentException ex) { 377 assertEquals("The validated collection contains an element not of type java.lang.String at index: 1", ex.getMessage()); 378 } 379 380 coll = new ArrayList (); 381 coll.add(new Integer (5)); 382 coll.add(new Double (2.0d)); 383 Validate.allElementsOfType(coll, Number .class, "MSG"); 384 try { 385 Validate.allElementsOfType(coll, Integer .class, "MSG"); 386 fail("Expecting IllegalArgumentException"); 387 } catch (IllegalArgumentException ex) { 388 assertEquals("MSG", ex.getMessage()); 389 } 390 try { 391 Validate.allElementsOfType(coll, Double .class, "MSG"); 392 fail("Expecting IllegalArgumentException"); 393 } catch (IllegalArgumentException ex) { 394 assertEquals("MSG", ex.getMessage()); 395 } 396 } 397 398 } 399 | Popular Tags |