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.HashMap ; 22 import java.util.Map ; 23 24 import junit.framework.Test; 25 import junit.framework.TestSuite; 26 import junit.textui.TestRunner; 27 28 import org.apache.commons.collections.functors.NOPClosure; 29 30 38 public class TestClosureUtils extends junit.framework.TestCase { 39 40 private static final Object cObject = new Object (); 41 private static final Object cString = "Hello"; 42 private static final Object cInteger = new Integer (6); 43 44 47 public TestClosureUtils(String name) { 48 super(name); 49 } 50 51 55 public static void main(String [] args) { 56 TestRunner.run(suite()); 57 } 58 59 62 public static Test suite() { 63 return new TestSuite(TestClosureUtils.class); 64 } 65 66 69 public void setUp() { 70 } 71 72 75 public void tearDown() { 76 } 77 78 static class MockClosure implements Closure { 79 int count = 0; 80 81 public void execute(Object object) { 82 count++; 83 } 84 } 85 static class MockTransformer implements Transformer { 86 int count = 0; 87 88 public Object transform(Object object) { 89 count++; 90 return object; 91 } 92 } 93 94 97 public void testExceptionClosure() { 98 assertNotNull(ClosureUtils.exceptionClosure()); 99 assertSame(ClosureUtils.exceptionClosure(), ClosureUtils.exceptionClosure()); 100 try { 101 ClosureUtils.exceptionClosure().execute(null); 102 } catch (FunctorException ex) { 103 try { 104 ClosureUtils.exceptionClosure().execute(cString); 105 } catch (FunctorException ex2) { 106 return; 107 } 108 } 109 fail(); 110 } 111 112 115 public void testNopClosure() { 116 StringBuffer buf = new StringBuffer ("Hello"); 117 ClosureUtils.nopClosure().execute(null); 118 assertEquals("Hello", buf.toString()); 119 ClosureUtils.nopClosure().execute("Hello"); 120 assertEquals("Hello", buf.toString()); 121 } 122 123 126 public void testInvokeClosure() { 127 StringBuffer buf = new StringBuffer ("Hello"); 128 ClosureUtils.invokerClosure("reverse").execute(buf); 129 assertEquals("olleH", buf.toString()); 130 buf = new StringBuffer ("Hello"); 131 ClosureUtils.invokerClosure("setLength", new Class [] {Integer.TYPE}, new Object [] {new Integer (2)}).execute(buf); 132 assertEquals("He", buf.toString()); 133 } 134 135 138 public void testForClosure() { 139 MockClosure cmd = new MockClosure(); 140 ClosureUtils.forClosure(5, cmd).execute(null); 141 assertEquals(5, cmd.count); 142 assertSame(NOPClosure.INSTANCE, ClosureUtils.forClosure(0, new MockClosure())); 143 assertSame(NOPClosure.INSTANCE, ClosureUtils.forClosure(-1, new MockClosure())); 144 assertSame(NOPClosure.INSTANCE, ClosureUtils.forClosure(1, null)); 145 assertSame(NOPClosure.INSTANCE, ClosureUtils.forClosure(3, null)); 146 assertSame(cmd, ClosureUtils.forClosure(1, cmd)); 147 } 148 149 152 public void testWhileClosure() { 153 MockClosure cmd = new MockClosure(); 154 ClosureUtils.whileClosure(PredicateUtils.falsePredicate(), cmd).execute(null); 155 assertEquals(0, cmd.count); 156 157 cmd = new MockClosure(); 158 ClosureUtils.whileClosure(PredicateUtils.uniquePredicate(), cmd).execute(null); 159 assertEquals(1, cmd.count); 160 161 try { 162 ClosureUtils.whileClosure(null, ClosureUtils.nopClosure()); 163 fail(); 164 } catch (IllegalArgumentException ex) {} 165 try { 166 ClosureUtils.whileClosure(PredicateUtils.falsePredicate(), null); 167 fail(); 168 } catch (IllegalArgumentException ex) {} 169 try { 170 ClosureUtils.whileClosure(null, null); 171 fail(); 172 } catch (IllegalArgumentException ex) {} 173 } 174 175 178 public void testDoWhileClosure() { 179 MockClosure cmd = new MockClosure(); 180 ClosureUtils.doWhileClosure(cmd, PredicateUtils.falsePredicate()).execute(null); 181 assertEquals(1, cmd.count); 182 183 cmd = new MockClosure(); 184 ClosureUtils.doWhileClosure(cmd, PredicateUtils.uniquePredicate()).execute(null); 185 assertEquals(2, cmd.count); 186 187 try { 188 ClosureUtils.doWhileClosure(null, null); 189 fail(); 190 } catch (IllegalArgumentException ex) {} 191 } 192 193 196 public void testChainedClosure() { 197 MockClosure a = new MockClosure(); 198 MockClosure b = new MockClosure(); 199 ClosureUtils.chainedClosure(a, b).execute(null); 200 assertEquals(1, a.count); 201 assertEquals(1, b.count); 202 203 a = new MockClosure(); 204 b = new MockClosure(); 205 ClosureUtils.chainedClosure(new Closure[] {a, b, a}).execute(null); 206 assertEquals(2, a.count); 207 assertEquals(1, b.count); 208 209 a = new MockClosure(); 210 b = new MockClosure(); 211 Collection coll = new ArrayList (); 212 coll.add(b); 213 coll.add(a); 214 coll.add(b); 215 ClosureUtils.chainedClosure(coll).execute(null); 216 assertEquals(1, a.count); 217 assertEquals(2, b.count); 218 219 assertSame(NOPClosure.INSTANCE, ClosureUtils.chainedClosure(new Closure[0])); 220 assertSame(NOPClosure.INSTANCE, ClosureUtils.chainedClosure(Collections.EMPTY_LIST)); 221 222 try { 223 ClosureUtils.chainedClosure(null, null); 224 fail(); 225 } catch (IllegalArgumentException ex) {} 226 try { 227 ClosureUtils.chainedClosure((Closure[]) null); 228 fail(); 229 } catch (IllegalArgumentException ex) {} 230 try { 231 ClosureUtils.chainedClosure((Collection ) null); 232 fail(); 233 } catch (IllegalArgumentException ex) {} 234 try { 235 ClosureUtils.chainedClosure(new Closure[] {null, null}); 236 fail(); 237 } catch (IllegalArgumentException ex) {} 238 try { 239 coll = new ArrayList (); 240 coll.add(null); 241 coll.add(null); 242 ClosureUtils.chainedClosure(coll); 243 fail(); 244 } catch (IllegalArgumentException ex) {} 245 } 246 247 250 public void testSwitchClosure() { 251 MockClosure a = new MockClosure(); 252 MockClosure b = new MockClosure(); 253 ClosureUtils.ifClosure(PredicateUtils.truePredicate(), a, b).execute(null); 254 assertEquals(1, a.count); 255 assertEquals(0, b.count); 256 257 a = new MockClosure(); 258 b = new MockClosure(); 259 ClosureUtils.ifClosure(PredicateUtils.falsePredicate(), a, b).execute(null); 260 assertEquals(0, a.count); 261 assertEquals(1, b.count); 262 263 a = new MockClosure(); 264 b = new MockClosure(); 265 ClosureUtils.switchClosure( 266 new Predicate[] {PredicateUtils.equalPredicate("HELLO"), PredicateUtils.equalPredicate("THERE")}, 267 new Closure[] {a, b}).execute("WELL"); 268 assertEquals(0, a.count); 269 assertEquals(0, b.count); 270 271 a = new MockClosure(); 272 b = new MockClosure(); 273 ClosureUtils.switchClosure( 274 new Predicate[] {PredicateUtils.equalPredicate("HELLO"), PredicateUtils.equalPredicate("THERE")}, 275 new Closure[] {a, b}).execute("HELLO"); 276 assertEquals(1, a.count); 277 assertEquals(0, b.count); 278 279 a = new MockClosure(); 280 b = new MockClosure(); 281 MockClosure c = new MockClosure(); 282 ClosureUtils.switchClosure( 283 new Predicate[] {PredicateUtils.equalPredicate("HELLO"), PredicateUtils.equalPredicate("THERE")}, 284 new Closure[] {a, b}, c).execute("WELL"); 285 assertEquals(0, a.count); 286 assertEquals(0, b.count); 287 assertEquals(1, c.count); 288 289 a = new MockClosure(); 290 b = new MockClosure(); 291 Map map = new HashMap (); 292 map.put(PredicateUtils.equalPredicate("HELLO"), a); 293 map.put(PredicateUtils.equalPredicate("THERE"), b); 294 ClosureUtils.switchClosure(map).execute(null); 295 assertEquals(0, a.count); 296 assertEquals(0, b.count); 297 298 a = new MockClosure(); 299 b = new MockClosure(); 300 map = new HashMap (); 301 map.put(PredicateUtils.equalPredicate("HELLO"), a); 302 map.put(PredicateUtils.equalPredicate("THERE"), b); 303 ClosureUtils.switchClosure(map).execute("THERE"); 304 assertEquals(0, a.count); 305 assertEquals(1, b.count); 306 307 a = new MockClosure(); 308 b = new MockClosure(); 309 c = new MockClosure(); 310 map = new HashMap (); 311 map.put(PredicateUtils.equalPredicate("HELLO"), a); 312 map.put(PredicateUtils.equalPredicate("THERE"), b); 313 map.put(null, c); 314 ClosureUtils.switchClosure(map).execute("WELL"); 315 assertEquals(0, a.count); 316 assertEquals(0, b.count); 317 assertEquals(1, c.count); 318 319 assertSame(NOPClosure.INSTANCE, ClosureUtils.switchClosure(new Predicate[0], new Closure[0])); 320 assertSame(NOPClosure.INSTANCE, ClosureUtils.switchClosure(new HashMap ())); 321 map = new HashMap (); 322 map.put(null, null); 323 assertSame(NOPClosure.INSTANCE, ClosureUtils.switchClosure(map)); 324 325 try { 326 ClosureUtils.switchClosure(null, null); 327 fail(); 328 } catch (IllegalArgumentException ex) {} 329 try { 330 ClosureUtils.switchClosure((Predicate[]) null, (Closure[]) null); 331 fail(); 332 } catch (IllegalArgumentException ex) {} 333 try { 334 ClosureUtils.switchClosure((Map ) null); 335 fail(); 336 } catch (IllegalArgumentException ex) {} 337 try { 338 ClosureUtils.switchClosure(new Predicate[2], new Closure[2]); 339 fail(); 340 } catch (IllegalArgumentException ex) {} 341 try { 342 ClosureUtils.switchClosure( 343 new Predicate[] {PredicateUtils.truePredicate()}, 344 new Closure[] {a,b}); 345 fail(); 346 } catch (IllegalArgumentException ex) {} 347 } 348 349 352 public void testSwitchMapClosure() { 353 MockClosure a = new MockClosure(); 354 MockClosure b = new MockClosure(); 355 Map map = new HashMap (); 356 map.put("HELLO", a); 357 map.put("THERE", b); 358 ClosureUtils.switchMapClosure(map).execute(null); 359 assertEquals(0, a.count); 360 assertEquals(0, b.count); 361 362 a = new MockClosure(); 363 b = new MockClosure(); 364 map = new HashMap (); 365 map.put("HELLO", a); 366 map.put("THERE", b); 367 ClosureUtils.switchMapClosure(map).execute("THERE"); 368 assertEquals(0, a.count); 369 assertEquals(1, b.count); 370 371 a = new MockClosure(); 372 b = new MockClosure(); 373 MockClosure c = new MockClosure(); 374 map = new HashMap (); 375 map.put("HELLO", a); 376 map.put("THERE", b); 377 map.put(null, c); 378 ClosureUtils.switchMapClosure(map).execute("WELL"); 379 assertEquals(0, a.count); 380 assertEquals(0, b.count); 381 assertEquals(1, c.count); 382 383 assertSame(NOPClosure.INSTANCE, ClosureUtils.switchMapClosure(new HashMap ())); 384 385 try { 386 ClosureUtils.switchMapClosure(null); 387 fail(); 388 } catch (IllegalArgumentException ex) {} 389 } 390 391 394 public void testTransformerClosure() { 395 MockTransformer mock = new MockTransformer(); 396 Closure closure = ClosureUtils.asClosure(mock); 397 closure.execute(null); 398 assertEquals(1, mock.count); 399 closure.execute(null); 400 assertEquals(2, mock.count); 401 402 assertSame(ClosureUtils.nopClosure(), ClosureUtils.asClosure(null)); 403 } 404 405 } 406 | Popular Tags |