1 16 package org.apache.commons.collections; 17 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.Collections ; 21 import java.util.Date ; 22 import java.util.HashMap ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import junit.framework.Test; 27 import junit.framework.TestSuite; 28 import junit.textui.TestRunner; 29 30 import org.apache.commons.collections.functors.ConstantTransformer; 31 import org.apache.commons.collections.functors.NOPTransformer; 32 33 42 public class TestTransformerUtils extends junit.framework.TestCase { 43 44 private static final Object cObject = new Object (); 45 private static final Object cString = "Hello"; 46 private static final Object cInteger = new Integer (6); 47 48 51 public TestTransformerUtils(String name) { 52 super(name); 53 } 54 55 59 public static void main(String [] args) { 60 TestRunner.run(suite()); 61 } 62 63 66 public static Test suite() { 67 return new TestSuite(TestTransformerUtils.class); 68 } 69 70 73 public void setUp() { 74 } 75 76 79 public void tearDown() { 80 } 81 82 85 public void testExceptionTransformer() { 86 assertNotNull(TransformerUtils.exceptionTransformer()); 87 assertSame(TransformerUtils.exceptionTransformer(), TransformerUtils.exceptionTransformer()); 88 try { 89 TransformerUtils.exceptionTransformer().transform(null); 90 } catch (FunctorException ex) { 91 try { 92 TransformerUtils.exceptionTransformer().transform(cString); 93 } catch (FunctorException ex2) { 94 return; 95 } 96 } 97 fail(); 98 } 99 100 103 public void testNullTransformer() { 104 assertNotNull(TransformerUtils.nullTransformer()); 105 assertSame(TransformerUtils.nullTransformer(), TransformerUtils.nullTransformer()); 106 assertEquals(null, TransformerUtils.nullTransformer().transform(null)); 107 assertEquals(null, TransformerUtils.nullTransformer().transform(cObject)); 108 assertEquals(null, TransformerUtils.nullTransformer().transform(cString)); 109 assertEquals(null, TransformerUtils.nullTransformer().transform(cInteger)); 110 } 111 112 115 public void testNopTransformer() { 116 assertNotNull(TransformerUtils.nullTransformer()); 117 assertSame(TransformerUtils.nullTransformer(), TransformerUtils.nullTransformer()); 118 assertEquals(null, TransformerUtils.nopTransformer().transform(null)); 119 assertEquals(cObject, TransformerUtils.nopTransformer().transform(cObject)); 120 assertEquals(cString, TransformerUtils.nopTransformer().transform(cString)); 121 assertEquals(cInteger, TransformerUtils.nopTransformer().transform(cInteger)); 122 } 123 124 127 public void testConstantTransformer() { 128 assertEquals(cObject, TransformerUtils.constantTransformer(cObject).transform(null)); 129 assertEquals(cObject, TransformerUtils.constantTransformer(cObject).transform(cObject)); 130 assertEquals(cObject, TransformerUtils.constantTransformer(cObject).transform(cString)); 131 assertEquals(cObject, TransformerUtils.constantTransformer(cObject).transform(cInteger)); 132 assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.constantTransformer(null)); 133 } 134 135 138 public void testCloneTransformer() { 139 assertEquals(null, TransformerUtils.cloneTransformer().transform(null)); 140 assertEquals(cString, TransformerUtils.cloneTransformer().transform(cString)); 141 assertEquals(cInteger, TransformerUtils.cloneTransformer().transform(cInteger)); 142 try { 143 assertEquals(cObject, TransformerUtils.cloneTransformer().transform(cObject)); 144 } catch (IllegalArgumentException ex) { 145 return; 146 } 147 fail(); 148 } 149 150 153 public void testMapTransformer() { 154 Map map = new HashMap (); 155 map.put(null, new Integer (0)); 156 map.put(cObject, new Integer (1)); 157 map.put(cString, new Integer (2)); 158 assertEquals(new Integer (0), TransformerUtils.mapTransformer(map).transform(null)); 159 assertEquals(new Integer (1), TransformerUtils.mapTransformer(map).transform(cObject)); 160 assertEquals(new Integer (2), TransformerUtils.mapTransformer(map).transform(cString)); 161 assertEquals(null, TransformerUtils.mapTransformer(map).transform(cInteger)); 162 assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.mapTransformer(null)); 163 } 164 165 168 public void testExecutorTransformer() { 169 assertEquals(null, TransformerUtils.asTransformer(ClosureUtils.nopClosure()).transform(null)); 170 assertEquals(cObject, TransformerUtils.asTransformer(ClosureUtils.nopClosure()).transform(cObject)); 171 assertEquals(cString, TransformerUtils.asTransformer(ClosureUtils.nopClosure()).transform(cString)); 172 assertEquals(cInteger, TransformerUtils.asTransformer(ClosureUtils.nopClosure()).transform(cInteger)); 173 try { 174 TransformerUtils.asTransformer((Closure) null); 175 } catch (IllegalArgumentException ex) { 176 return; 177 } 178 fail(); 179 } 180 181 184 public void testPredicateTransformer() { 185 assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(PredicateUtils.truePredicate()).transform(null)); 186 assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(PredicateUtils.truePredicate()).transform(cObject)); 187 assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(PredicateUtils.truePredicate()).transform(cString)); 188 assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(PredicateUtils.truePredicate()).transform(cInteger)); 189 try { 190 TransformerUtils.asTransformer((Predicate) null); 191 } catch (IllegalArgumentException ex) { 192 return; 193 } 194 fail(); 195 } 196 197 200 public void testFactoryTransformer() { 201 assertEquals(null, TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(null)); 202 assertEquals(null, TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(cObject)); 203 assertEquals(null, TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(cString)); 204 assertEquals(null, TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(cInteger)); 205 try { 206 TransformerUtils.asTransformer((Factory) null); 207 } catch (IllegalArgumentException ex) { 208 return; 209 } 210 fail(); 211 } 212 213 216 public void testChainedTransformer() { 217 Transformer a = TransformerUtils.constantTransformer("A"); 218 Transformer b = TransformerUtils.constantTransformer("B"); 219 220 assertEquals("A", TransformerUtils.chainedTransformer(b, a).transform(null)); 221 assertEquals("B", TransformerUtils.chainedTransformer(a, b).transform(null)); 222 assertEquals("A", TransformerUtils.chainedTransformer(new Transformer[] {b, a}).transform(null)); 223 Collection coll = new ArrayList (); 224 coll.add(b); 225 coll.add(a); 226 assertEquals("A", TransformerUtils.chainedTransformer(coll).transform(null)); 227 228 assertSame(NOPTransformer.INSTANCE, TransformerUtils.chainedTransformer(new Transformer[0])); 229 assertSame(NOPTransformer.INSTANCE, TransformerUtils.chainedTransformer(Collections.EMPTY_LIST)); 230 231 try { 232 TransformerUtils.chainedTransformer(null, null); 233 fail(); 234 } catch (IllegalArgumentException ex) {} 235 try { 236 TransformerUtils.chainedTransformer((Transformer[]) null); 237 fail(); 238 } catch (IllegalArgumentException ex) {} 239 try { 240 TransformerUtils.chainedTransformer((Collection ) null); 241 fail(); 242 } catch (IllegalArgumentException ex) {} 243 try { 244 TransformerUtils.chainedTransformer(new Transformer[] {null, null}); 245 fail(); 246 } catch (IllegalArgumentException ex) {} 247 try { 248 coll = new ArrayList (); 249 coll.add(null); 250 coll.add(null); 251 TransformerUtils.chainedTransformer(coll); 252 fail(); 253 } catch (IllegalArgumentException ex) {} 254 } 255 256 259 public void testSwitchTransformer() { 260 Transformer a = TransformerUtils.constantTransformer("A"); 261 Transformer b = TransformerUtils.constantTransformer("B"); 262 Transformer c = TransformerUtils.constantTransformer("C"); 263 264 assertEquals("A", TransformerUtils.switchTransformer(PredicateUtils.truePredicate(), a, b).transform(null)); 265 assertEquals("B", TransformerUtils.switchTransformer(PredicateUtils.falsePredicate(), a, b).transform(null)); 266 267 assertEquals(null, TransformerUtils.switchTransformer( 268 new Predicate[] {PredicateUtils.equalPredicate("HELLO"), PredicateUtils.equalPredicate("THERE")}, 269 new Transformer[] {a, b}).transform("WELL")); 270 assertEquals("A", TransformerUtils.switchTransformer( 271 new Predicate[] {PredicateUtils.equalPredicate("HELLO"), PredicateUtils.equalPredicate("THERE")}, 272 new Transformer[] {a, b}).transform("HELLO")); 273 assertEquals("B", TransformerUtils.switchTransformer( 274 new Predicate[] {PredicateUtils.equalPredicate("HELLO"), PredicateUtils.equalPredicate("THERE")}, 275 new Transformer[] {a, b}).transform("THERE")); 276 277 assertEquals("C", TransformerUtils.switchTransformer( 278 new Predicate[] {PredicateUtils.equalPredicate("HELLO"), PredicateUtils.equalPredicate("THERE")}, 279 new Transformer[] {a, b}, c).transform("WELL")); 280 281 Map map = new HashMap (); 282 map.put(PredicateUtils.equalPredicate("HELLO"), a); 283 map.put(PredicateUtils.equalPredicate("THERE"), b); 284 assertEquals(null, TransformerUtils.switchTransformer(map).transform("WELL")); 285 assertEquals("A", TransformerUtils.switchTransformer(map).transform("HELLO")); 286 assertEquals("B", TransformerUtils.switchTransformer(map).transform("THERE")); 287 map.put(null, c); 288 assertEquals("C", TransformerUtils.switchTransformer(map).transform("WELL")); 289 290 assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchTransformer(new Predicate[0], new Transformer[0])); 291 assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchTransformer(new HashMap ())); 292 map = new HashMap (); 293 map.put(null, null); 294 assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchTransformer(map)); 295 296 try { 297 TransformerUtils.switchTransformer(null, null); 298 fail(); 299 } catch (IllegalArgumentException ex) {} 300 try { 301 TransformerUtils.switchTransformer((Predicate[]) null, (Transformer[]) null); 302 fail(); 303 } catch (IllegalArgumentException ex) {} 304 try { 305 TransformerUtils.switchTransformer((Map ) null); 306 fail(); 307 } catch (IllegalArgumentException ex) {} 308 try { 309 TransformerUtils.switchTransformer(new Predicate[2], new Transformer[2]); 310 fail(); 311 } catch (IllegalArgumentException ex) {} 312 try { 313 TransformerUtils.switchTransformer( 314 new Predicate[] {PredicateUtils.truePredicate()}, 315 new Transformer[] {a,b}); 316 fail(); 317 } catch (IllegalArgumentException ex) {} 318 } 319 320 323 public void testSwitchMapTransformer() { 324 Transformer a = TransformerUtils.constantTransformer("A"); 325 Transformer b = TransformerUtils.constantTransformer("B"); 326 Transformer c = TransformerUtils.constantTransformer("C"); 327 328 Map map = new HashMap (); 329 map.put("HELLO", a); 330 map.put("THERE", b); 331 assertEquals(null, TransformerUtils.switchMapTransformer(map).transform("WELL")); 332 assertEquals("A", TransformerUtils.switchMapTransformer(map).transform("HELLO")); 333 assertEquals("B", TransformerUtils.switchMapTransformer(map).transform("THERE")); 334 map.put(null, c); 335 assertEquals("C", TransformerUtils.switchMapTransformer(map).transform("WELL")); 336 337 assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchMapTransformer(new HashMap ())); 338 map = new HashMap (); 339 map.put(null, null); 340 assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchMapTransformer(map)); 341 342 try { 343 TransformerUtils.switchMapTransformer(null); 344 fail(); 345 } catch (IllegalArgumentException ex) {} 346 } 347 348 351 public void testInvokerTransformer() { 352 List list = new ArrayList (); 353 assertEquals(new Integer (0), TransformerUtils.invokerTransformer("size").transform(list)); 354 list.add(new Object ()); 355 assertEquals(new Integer (1), TransformerUtils.invokerTransformer("size").transform(list)); 356 assertEquals(null, TransformerUtils.invokerTransformer("size").transform(null)); 357 358 try { 359 TransformerUtils.invokerTransformer(null); 360 fail(); 361 } catch (IllegalArgumentException ex) {} 362 try { 363 TransformerUtils.invokerTransformer("noSuchMethod").transform(new Object ()); 364 fail(); 365 } catch (FunctorException ex) {} 366 } 367 368 371 public void testInvokerTransformer2() { 372 List list = new ArrayList (); 373 assertEquals(Boolean.FALSE, TransformerUtils.invokerTransformer( 374 "contains", new Class [] {Object .class}, new Object [] {cString}).transform(list)); 375 list.add(cString); 376 assertEquals(Boolean.TRUE, TransformerUtils.invokerTransformer( 377 "contains", new Class [] {Object .class}, new Object [] {cString}).transform(list)); 378 assertEquals(null, TransformerUtils.invokerTransformer( 379 "contains", new Class [] {Object .class}, new Object [] {cString}).transform(null)); 380 381 try { 382 TransformerUtils.invokerTransformer(null, null, null); 383 fail(); 384 } catch (IllegalArgumentException ex) {} 385 try { 386 TransformerUtils.invokerTransformer( 387 "noSuchMethod", new Class [] {Object .class}, new Object [] {cString}).transform(new Object ()); 388 fail(); 389 } catch (FunctorException ex) {} 390 try { 391 TransformerUtils.invokerTransformer("badArgs", null, new Object [] { cString }); 392 fail(); 393 } catch (IllegalArgumentException ex) {} 394 try { 395 TransformerUtils.invokerTransformer("badArgs", new Class [] {Object .class}, null); 396 fail(); 397 } catch (IllegalArgumentException ex) {} 398 try { 399 TransformerUtils.invokerTransformer("badArgs", new Class [] {}, new Object [] { cString }); 400 fail(); 401 } catch (IllegalArgumentException ex) {} 402 } 403 404 407 public void testStringValueTransformer() { 408 assertNotNull( "StringValueTransformer should NEVER return a null value.", 409 TransformerUtils.stringValueTransformer().transform(null)); 410 assertEquals( "StringValueTransformer should return \"null\" when given a null argument.", "null", 411 TransformerUtils.stringValueTransformer().transform(null)); 412 assertEquals( "StringValueTransformer should return toString value", "6", 413 TransformerUtils.stringValueTransformer().transform(new Integer (6))); 414 } 415 416 419 public void testInstantiateTransformerNull() { 420 try { 421 Transformer trans = TransformerUtils.instantiateTransformer(null, new Object [] {"str"}); 422 fail(); 423 } catch (IllegalArgumentException ex) {} 424 try { 425 Transformer trans = TransformerUtils.instantiateTransformer(new Class [] {}, new Object [] {"str"}); 426 fail(); 427 } catch (IllegalArgumentException ex) {} 428 429 Transformer trans = TransformerUtils.instantiateTransformer(new Class [] {Long .class}, new Object [] {null}); 430 try { 431 trans.transform(String .class); 432 fail(); 433 } catch (FunctorException ex) {} 434 435 trans = TransformerUtils.instantiateTransformer(); 436 assertEquals("", trans.transform(String .class)); 437 438 trans = TransformerUtils.instantiateTransformer(new Class [] {Long.TYPE}, new Object [] {new Long (1000L)}); 439 assertEquals(new Date (1000L), trans.transform(Date .class)); 440 } 441 442 } 443 | Popular Tags |