KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > TestClosureUtils


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
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 /**
31  * Tests the org.apache.commons.collections.ClosureUtils class.
32  *
33  * @since Commons Collections 3.0
34  * @version $Revision: 1.9 $ $Date: 2004/02/18 01:20:35 $
35  *
36  * @author Stephen Colebourne
37  */

38 public class TestClosureUtils extends junit.framework.TestCase {
39
40     private static final Object JavaDoc cObject = new Object JavaDoc();
41     private static final Object JavaDoc cString = "Hello";
42     private static final Object JavaDoc cInteger = new Integer JavaDoc(6);
43
44     /**
45      * Construct
46      */

47     public TestClosureUtils(String JavaDoc name) {
48         super(name);
49     }
50
51     /**
52      * Main.
53      * @param args
54      */

55     public static void main(String JavaDoc[] args) {
56         TestRunner.run(suite());
57     }
58
59     /**
60      * Return class as a test suite.
61      */

62     public static Test suite() {
63         return new TestSuite(TestClosureUtils.class);
64     }
65
66     /**
67      * Set up instance variables required by this test case.
68      */

69     public void setUp() {
70     }
71
72     /**
73      * Tear down instance variables required by this test case.
74      */

75     public void tearDown() {
76     }
77     
78     static class MockClosure implements Closure {
79         int count = 0;
80         
81         public void execute(Object JavaDoc object) {
82             count++;
83         }
84     }
85     static class MockTransformer implements Transformer {
86         int count = 0;
87         
88         public Object JavaDoc transform(Object JavaDoc object) {
89             count++;
90             return object;
91         }
92     }
93
94     // exceptionClosure
95
//------------------------------------------------------------------
96

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     // nopClosure
113
//------------------------------------------------------------------
114

115     public void testNopClosure() {
116         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("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 JavaDoc buf = new StringBuffer JavaDoc("Hello");
128         ClosureUtils.invokerClosure("reverse").execute(buf);
129         assertEquals("olleH", buf.toString());
130         buf = new StringBuffer JavaDoc("Hello");
131         ClosureUtils.invokerClosure("setLength", new Class JavaDoc[] {Integer.TYPE}, new Object JavaDoc[] {new Integer JavaDoc(2)}).execute(buf);
132         assertEquals("He", buf.toString());
133     }
134
135     // forClosure
136
//------------------------------------------------------------------
137

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     // whileClosure
150
//------------------------------------------------------------------
151

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 JavaDoc ex) {}
165         try {
166             ClosureUtils.whileClosure(PredicateUtils.falsePredicate(), null);
167             fail();
168         } catch (IllegalArgumentException JavaDoc ex) {}
169         try {
170             ClosureUtils.whileClosure(null, null);
171             fail();
172         } catch (IllegalArgumentException JavaDoc ex) {}
173     }
174
175     // doWhileClosure
176
//------------------------------------------------------------------
177

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 JavaDoc ex) {}
191     }
192
193     // chainedClosure
194
//------------------------------------------------------------------
195

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 JavaDoc coll = new ArrayList JavaDoc();
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 JavaDoc ex) {}
226         try {
227             ClosureUtils.chainedClosure((Closure[]) null);
228             fail();
229         } catch (IllegalArgumentException JavaDoc ex) {}
230         try {
231             ClosureUtils.chainedClosure((Collection JavaDoc) null);
232             fail();
233         } catch (IllegalArgumentException JavaDoc ex) {}
234         try {
235             ClosureUtils.chainedClosure(new Closure[] {null, null});
236             fail();
237         } catch (IllegalArgumentException JavaDoc ex) {}
238         try {
239             coll = new ArrayList JavaDoc();
240             coll.add(null);
241             coll.add(null);
242             ClosureUtils.chainedClosure(coll);
243             fail();
244         } catch (IllegalArgumentException JavaDoc ex) {}
245     }
246     
247     // switchClosure
248
//------------------------------------------------------------------
249

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 JavaDoc map = new HashMap JavaDoc();
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 JavaDoc();
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 JavaDoc();
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 JavaDoc()));
321         map = new HashMap JavaDoc();
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 JavaDoc ex) {}
329         try {
330             ClosureUtils.switchClosure((Predicate[]) null, (Closure[]) null);
331             fail();
332         } catch (IllegalArgumentException JavaDoc ex) {}
333         try {
334             ClosureUtils.switchClosure((Map JavaDoc) null);
335             fail();
336         } catch (IllegalArgumentException JavaDoc ex) {}
337         try {
338             ClosureUtils.switchClosure(new Predicate[2], new Closure[2]);
339             fail();
340         } catch (IllegalArgumentException JavaDoc ex) {}
341         try {
342             ClosureUtils.switchClosure(
343                     new Predicate[] {PredicateUtils.truePredicate()},
344                     new Closure[] {a,b});
345             fail();
346         } catch (IllegalArgumentException JavaDoc ex) {}
347     }
348     
349     // switchMapClosure
350
//------------------------------------------------------------------
351

352     public void testSwitchMapClosure() {
353         MockClosure a = new MockClosure();
354         MockClosure b = new MockClosure();
355         Map JavaDoc map = new HashMap JavaDoc();
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 JavaDoc();
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 JavaDoc();
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 JavaDoc()));
384         
385         try {
386             ClosureUtils.switchMapClosure(null);
387             fail();
388         } catch (IllegalArgumentException JavaDoc ex) {}
389     }
390     
391     // asClosure
392
//------------------------------------------------------------------
393

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