001: /*
002: * Copyright 2001-2004,2006 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Collections;
021: import java.util.HashMap;
022: import java.util.Map;
023:
024: import junit.framework.Test;
025: import junit.framework.TestSuite;
026: import junit.textui.TestRunner;
027:
028: import org.apache.commons.collections.functors.NOPClosure;
029:
030: /**
031: * Tests the org.apache.commons.collections.ClosureUtils class.
032: *
033: * @since Commons Collections 3.0
034: * @version $Revision: 375766 $ $Date: 2006-02-07 23:10:36 +0000 (Tue, 07 Feb 2006) $
035: *
036: * @author Stephen Colebourne
037: */
038: public class TestClosureUtils extends junit.framework.TestCase {
039:
040: private static final Object cString = "Hello";
041:
042: /**
043: * Construct
044: */
045: public TestClosureUtils(String name) {
046: super (name);
047: }
048:
049: /**
050: * Main.
051: * @param args
052: */
053: public static void main(String[] args) {
054: TestRunner.run(suite());
055: }
056:
057: /**
058: * Return class as a test suite.
059: */
060: public static Test suite() {
061: return new TestSuite(TestClosureUtils.class);
062: }
063:
064: /**
065: * Set up instance variables required by this test case.
066: */
067: public void setUp() {
068: }
069:
070: /**
071: * Tear down instance variables required by this test case.
072: */
073: public void tearDown() {
074: }
075:
076: static class MockClosure implements Closure {
077: int count = 0;
078:
079: public void execute(Object object) {
080: count++;
081: }
082: }
083:
084: static class MockTransformer implements Transformer {
085: int count = 0;
086:
087: public Object transform(Object object) {
088: count++;
089: return object;
090: }
091: }
092:
093: // exceptionClosure
094: //------------------------------------------------------------------
095:
096: public void testExceptionClosure() {
097: assertNotNull(ClosureUtils.exceptionClosure());
098: assertSame(ClosureUtils.exceptionClosure(), ClosureUtils
099: .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: // nopClosure
113: //------------------------------------------------------------------
114:
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: // invokeClosure
124: //------------------------------------------------------------------
125:
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",
132: new Class[] { Integer.TYPE },
133: new Object[] { new Integer(2) }).execute(buf);
134: assertEquals("He", buf.toString());
135: }
136:
137: // forClosure
138: //------------------------------------------------------------------
139:
140: public void testForClosure() {
141: MockClosure cmd = new MockClosure();
142: ClosureUtils.forClosure(5, cmd).execute(null);
143: assertEquals(5, cmd.count);
144: assertSame(NOPClosure.INSTANCE, ClosureUtils.forClosure(0,
145: new MockClosure()));
146: assertSame(NOPClosure.INSTANCE, ClosureUtils.forClosure(-1,
147: new MockClosure()));
148: assertSame(NOPClosure.INSTANCE, ClosureUtils
149: .forClosure(1, null));
150: assertSame(NOPClosure.INSTANCE, ClosureUtils
151: .forClosure(3, null));
152: assertSame(cmd, ClosureUtils.forClosure(1, cmd));
153: }
154:
155: // whileClosure
156: //------------------------------------------------------------------
157:
158: public void testWhileClosure() {
159: MockClosure cmd = new MockClosure();
160: ClosureUtils.whileClosure(PredicateUtils.falsePredicate(), cmd)
161: .execute(null);
162: assertEquals(0, cmd.count);
163:
164: cmd = new MockClosure();
165: ClosureUtils
166: .whileClosure(PredicateUtils.uniquePredicate(), cmd)
167: .execute(null);
168: assertEquals(1, cmd.count);
169:
170: try {
171: ClosureUtils.whileClosure(null, ClosureUtils.nopClosure());
172: fail();
173: } catch (IllegalArgumentException ex) {
174: }
175: try {
176: ClosureUtils.whileClosure(PredicateUtils.falsePredicate(),
177: null);
178: fail();
179: } catch (IllegalArgumentException ex) {
180: }
181: try {
182: ClosureUtils.whileClosure(null, null);
183: fail();
184: } catch (IllegalArgumentException ex) {
185: }
186: }
187:
188: // doWhileClosure
189: //------------------------------------------------------------------
190:
191: public void testDoWhileClosure() {
192: MockClosure cmd = new MockClosure();
193: ClosureUtils.doWhileClosure(cmd,
194: PredicateUtils.falsePredicate()).execute(null);
195: assertEquals(1, cmd.count);
196:
197: cmd = new MockClosure();
198: ClosureUtils.doWhileClosure(cmd,
199: PredicateUtils.uniquePredicate()).execute(null);
200: assertEquals(2, cmd.count);
201:
202: try {
203: ClosureUtils.doWhileClosure(null, null);
204: fail();
205: } catch (IllegalArgumentException ex) {
206: }
207: }
208:
209: // chainedClosure
210: //------------------------------------------------------------------
211:
212: public void testChainedClosure() {
213: MockClosure a = new MockClosure();
214: MockClosure b = new MockClosure();
215: ClosureUtils.chainedClosure(a, b).execute(null);
216: assertEquals(1, a.count);
217: assertEquals(1, b.count);
218:
219: a = new MockClosure();
220: b = new MockClosure();
221: ClosureUtils.chainedClosure(new Closure[] { a, b, a }).execute(
222: null);
223: assertEquals(2, a.count);
224: assertEquals(1, b.count);
225:
226: a = new MockClosure();
227: b = new MockClosure();
228: Collection coll = new ArrayList();
229: coll.add(b);
230: coll.add(a);
231: coll.add(b);
232: ClosureUtils.chainedClosure(coll).execute(null);
233: assertEquals(1, a.count);
234: assertEquals(2, b.count);
235:
236: assertSame(NOPClosure.INSTANCE, ClosureUtils
237: .chainedClosure(new Closure[0]));
238: assertSame(NOPClosure.INSTANCE, ClosureUtils
239: .chainedClosure(Collections.EMPTY_LIST));
240:
241: try {
242: ClosureUtils.chainedClosure(null, null);
243: fail();
244: } catch (IllegalArgumentException ex) {
245: }
246: try {
247: ClosureUtils.chainedClosure((Closure[]) null);
248: fail();
249: } catch (IllegalArgumentException ex) {
250: }
251: try {
252: ClosureUtils.chainedClosure((Collection) null);
253: fail();
254: } catch (IllegalArgumentException ex) {
255: }
256: try {
257: ClosureUtils.chainedClosure(new Closure[] { null, null });
258: fail();
259: } catch (IllegalArgumentException ex) {
260: }
261: try {
262: coll = new ArrayList();
263: coll.add(null);
264: coll.add(null);
265: ClosureUtils.chainedClosure(coll);
266: fail();
267: } catch (IllegalArgumentException ex) {
268: }
269: }
270:
271: // ifClosure
272: //------------------------------------------------------------------
273:
274: public void testIfClosure() {
275: MockClosure a = new MockClosure();
276: MockClosure b = null;
277: ClosureUtils.ifClosure(PredicateUtils.truePredicate(), a)
278: .execute(null);
279: assertEquals(1, a.count);
280:
281: a = new MockClosure();
282: ClosureUtils.ifClosure(PredicateUtils.falsePredicate(), a)
283: .execute(null);
284: assertEquals(0, a.count);
285:
286: a = new MockClosure();
287: b = new MockClosure();
288: ClosureUtils.ifClosure(PredicateUtils.truePredicate(), a, b)
289: .execute(null);
290: assertEquals(1, a.count);
291: assertEquals(0, b.count);
292:
293: a = new MockClosure();
294: b = new MockClosure();
295: ClosureUtils.ifClosure(PredicateUtils.falsePredicate(), a, b)
296: .execute(null);
297: assertEquals(0, a.count);
298: assertEquals(1, b.count);
299: }
300:
301: // switchClosure
302: //------------------------------------------------------------------
303:
304: public void testSwitchClosure() {
305: MockClosure a = new MockClosure();
306: MockClosure b = new MockClosure();
307: ClosureUtils.switchClosure(
308: new Predicate[] {
309: PredicateUtils.equalPredicate("HELLO"),
310: PredicateUtils.equalPredicate("THERE") },
311: new Closure[] { a, b }).execute("WELL");
312: assertEquals(0, a.count);
313: assertEquals(0, b.count);
314:
315: a = new MockClosure();
316: b = new MockClosure();
317: ClosureUtils.switchClosure(
318: new Predicate[] {
319: PredicateUtils.equalPredicate("HELLO"),
320: PredicateUtils.equalPredicate("THERE") },
321: new Closure[] { a, b }).execute("HELLO");
322: assertEquals(1, a.count);
323: assertEquals(0, b.count);
324:
325: a = new MockClosure();
326: b = new MockClosure();
327: MockClosure c = new MockClosure();
328: ClosureUtils.switchClosure(
329: new Predicate[] {
330: PredicateUtils.equalPredicate("HELLO"),
331: PredicateUtils.equalPredicate("THERE") },
332: new Closure[] { a, b }, c).execute("WELL");
333: assertEquals(0, a.count);
334: assertEquals(0, b.count);
335: assertEquals(1, c.count);
336:
337: a = new MockClosure();
338: b = new MockClosure();
339: Map map = new HashMap();
340: map.put(PredicateUtils.equalPredicate("HELLO"), a);
341: map.put(PredicateUtils.equalPredicate("THERE"), b);
342: ClosureUtils.switchClosure(map).execute(null);
343: assertEquals(0, a.count);
344: assertEquals(0, b.count);
345:
346: a = new MockClosure();
347: b = new MockClosure();
348: map = new HashMap();
349: map.put(PredicateUtils.equalPredicate("HELLO"), a);
350: map.put(PredicateUtils.equalPredicate("THERE"), b);
351: ClosureUtils.switchClosure(map).execute("THERE");
352: assertEquals(0, a.count);
353: assertEquals(1, b.count);
354:
355: a = new MockClosure();
356: b = new MockClosure();
357: c = new MockClosure();
358: map = new HashMap();
359: map.put(PredicateUtils.equalPredicate("HELLO"), a);
360: map.put(PredicateUtils.equalPredicate("THERE"), b);
361: map.put(null, c);
362: ClosureUtils.switchClosure(map).execute("WELL");
363: assertEquals(0, a.count);
364: assertEquals(0, b.count);
365: assertEquals(1, c.count);
366:
367: assertSame(NOPClosure.INSTANCE, ClosureUtils.switchClosure(
368: new Predicate[0], new Closure[0]));
369: assertSame(NOPClosure.INSTANCE, ClosureUtils
370: .switchClosure(new HashMap()));
371: map = new HashMap();
372: map.put(null, null);
373: assertSame(NOPClosure.INSTANCE, ClosureUtils.switchClosure(map));
374:
375: try {
376: ClosureUtils.switchClosure(null, null);
377: fail();
378: } catch (IllegalArgumentException ex) {
379: }
380: try {
381: ClosureUtils.switchClosure((Predicate[]) null,
382: (Closure[]) null);
383: fail();
384: } catch (IllegalArgumentException ex) {
385: }
386: try {
387: ClosureUtils.switchClosure((Map) null);
388: fail();
389: } catch (IllegalArgumentException ex) {
390: }
391: try {
392: ClosureUtils
393: .switchClosure(new Predicate[2], new Closure[2]);
394: fail();
395: } catch (IllegalArgumentException ex) {
396: }
397: try {
398: ClosureUtils.switchClosure(new Predicate[] { PredicateUtils
399: .truePredicate() }, new Closure[] { a, b });
400: fail();
401: } catch (IllegalArgumentException ex) {
402: }
403: }
404:
405: // switchMapClosure
406: //------------------------------------------------------------------
407:
408: public void testSwitchMapClosure() {
409: MockClosure a = new MockClosure();
410: MockClosure b = new MockClosure();
411: Map map = new HashMap();
412: map.put("HELLO", a);
413: map.put("THERE", b);
414: ClosureUtils.switchMapClosure(map).execute(null);
415: assertEquals(0, a.count);
416: assertEquals(0, b.count);
417:
418: a = new MockClosure();
419: b = new MockClosure();
420: map = new HashMap();
421: map.put("HELLO", a);
422: map.put("THERE", b);
423: ClosureUtils.switchMapClosure(map).execute("THERE");
424: assertEquals(0, a.count);
425: assertEquals(1, b.count);
426:
427: a = new MockClosure();
428: b = new MockClosure();
429: MockClosure c = new MockClosure();
430: map = new HashMap();
431: map.put("HELLO", a);
432: map.put("THERE", b);
433: map.put(null, c);
434: ClosureUtils.switchMapClosure(map).execute("WELL");
435: assertEquals(0, a.count);
436: assertEquals(0, b.count);
437: assertEquals(1, c.count);
438:
439: assertSame(NOPClosure.INSTANCE, ClosureUtils
440: .switchMapClosure(new HashMap()));
441:
442: try {
443: ClosureUtils.switchMapClosure(null);
444: fail();
445: } catch (IllegalArgumentException ex) {
446: }
447: }
448:
449: // asClosure
450: //------------------------------------------------------------------
451:
452: public void testTransformerClosure() {
453: MockTransformer mock = new MockTransformer();
454: Closure closure = ClosureUtils.asClosure(mock);
455: closure.execute(null);
456: assertEquals(1, mock.count);
457: closure.execute(null);
458: assertEquals(2, mock.count);
459:
460: assertSame(ClosureUtils.nopClosure(), ClosureUtils
461: .asClosure(null));
462: }
463:
464: }
|