KickJava   Java API By Example, From Geeks To Geeks.

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


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.Date JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
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 /**
34  * Tests the org.apache.commons.collections.TransformerUtils class.
35  *
36  * @since Commons Collections 3.0
37  * @version $Revision: 1.10 $ $Date: 2004/02/18 01:20:35 $
38  *
39  * @author Stephen Colebourne
40  * @author James Carman
41  */

42 public class TestTransformerUtils extends junit.framework.TestCase {
43
44     private static final Object JavaDoc cObject = new Object JavaDoc();
45     private static final Object JavaDoc cString = "Hello";
46     private static final Object JavaDoc cInteger = new Integer JavaDoc(6);
47
48     /**
49      * Construct
50      */

51     public TestTransformerUtils(String JavaDoc name) {
52         super(name);
53     }
54
55     /**
56      * Main.
57      * @param args
58      */

59     public static void main(String JavaDoc[] args) {
60         TestRunner.run(suite());
61     }
62
63     /**
64      * Return class as a test suite.
65      */

66     public static Test suite() {
67         return new TestSuite(TestTransformerUtils.class);
68     }
69
70     /**
71      * Set up instance variables required by this test case.
72      */

73     public void setUp() {
74     }
75
76     /**
77      * Tear down instance variables required by this test case.
78      */

79     public void tearDown() {
80     }
81
82     // exceptionTransformer
83
//------------------------------------------------------------------
84

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     // nullTransformer
101
//------------------------------------------------------------------
102

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

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     // constantTransformer
125
//------------------------------------------------------------------
126

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     // cloneTransformer
136
//------------------------------------------------------------------
137

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 JavaDoc ex) {
145             return;
146         }
147         fail();
148     }
149
150     // mapTransformer
151
//------------------------------------------------------------------
152

153     public void testMapTransformer() {
154         Map JavaDoc map = new HashMap JavaDoc();
155         map.put(null, new Integer JavaDoc(0));
156         map.put(cObject, new Integer JavaDoc(1));
157         map.put(cString, new Integer JavaDoc(2));
158         assertEquals(new Integer JavaDoc(0), TransformerUtils.mapTransformer(map).transform(null));
159         assertEquals(new Integer JavaDoc(1), TransformerUtils.mapTransformer(map).transform(cObject));
160         assertEquals(new Integer JavaDoc(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     // commandTransformer
166
//------------------------------------------------------------------
167

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 JavaDoc ex) {
176             return;
177         }
178         fail();
179     }
180
181     // predicateTransformer
182
//------------------------------------------------------------------
183

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 JavaDoc ex) {
192             return;
193         }
194         fail();
195     }
196
197     // factoryTransformer
198
//------------------------------------------------------------------
199

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 JavaDoc ex) {
208             return;
209         }
210         fail();
211     }
212
213     // chainedTransformer
214
//------------------------------------------------------------------
215

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 JavaDoc coll = new ArrayList JavaDoc();
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 JavaDoc ex) {}
235         try {
236             TransformerUtils.chainedTransformer((Transformer[]) null);
237             fail();
238         } catch (IllegalArgumentException JavaDoc ex) {}
239         try {
240             TransformerUtils.chainedTransformer((Collection JavaDoc) null);
241             fail();
242         } catch (IllegalArgumentException JavaDoc ex) {}
243         try {
244             TransformerUtils.chainedTransformer(new Transformer[] {null, null});
245             fail();
246         } catch (IllegalArgumentException JavaDoc ex) {}
247         try {
248             coll = new ArrayList JavaDoc();
249             coll.add(null);
250             coll.add(null);
251             TransformerUtils.chainedTransformer(coll);
252             fail();
253         } catch (IllegalArgumentException JavaDoc ex) {}
254     }
255     
256     // switchTransformer
257
//------------------------------------------------------------------
258

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 JavaDoc map = new HashMap JavaDoc();
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 JavaDoc()));
292         map = new HashMap JavaDoc();
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 JavaDoc ex) {}
300         try {
301             TransformerUtils.switchTransformer((Predicate[]) null, (Transformer[]) null);
302             fail();
303         } catch (IllegalArgumentException JavaDoc ex) {}
304         try {
305             TransformerUtils.switchTransformer((Map JavaDoc) null);
306             fail();
307         } catch (IllegalArgumentException JavaDoc ex) {}
308         try {
309             TransformerUtils.switchTransformer(new Predicate[2], new Transformer[2]);
310             fail();
311         } catch (IllegalArgumentException JavaDoc ex) {}
312         try {
313             TransformerUtils.switchTransformer(
314                     new Predicate[] {PredicateUtils.truePredicate()},
315                     new Transformer[] {a,b});
316             fail();
317         } catch (IllegalArgumentException JavaDoc ex) {}
318     }
319     
320     // switchMapTransformer
321
//------------------------------------------------------------------
322

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 JavaDoc map = new HashMap JavaDoc();
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 JavaDoc()));
338         map = new HashMap JavaDoc();
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 JavaDoc ex) {}
346     }
347     
348     // invokerTransformer
349
//------------------------------------------------------------------
350

351     public void testInvokerTransformer() {
352         List JavaDoc list = new ArrayList JavaDoc();
353         assertEquals(new Integer JavaDoc(0), TransformerUtils.invokerTransformer("size").transform(list));
354         list.add(new Object JavaDoc());
355         assertEquals(new Integer JavaDoc(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 JavaDoc ex) {}
362         try {
363             TransformerUtils.invokerTransformer("noSuchMethod").transform(new Object JavaDoc());
364             fail();
365         } catch (FunctorException ex) {}
366     }
367     
368     // invokerTransformer2
369
//------------------------------------------------------------------
370

371     public void testInvokerTransformer2() {
372         List JavaDoc list = new ArrayList JavaDoc();
373         assertEquals(Boolean.FALSE, TransformerUtils.invokerTransformer(
374             "contains", new Class JavaDoc[] {Object JavaDoc.class}, new Object JavaDoc[] {cString}).transform(list));
375         list.add(cString);
376         assertEquals(Boolean.TRUE, TransformerUtils.invokerTransformer(
377             "contains", new Class JavaDoc[] {Object JavaDoc.class}, new Object JavaDoc[] {cString}).transform(list));
378         assertEquals(null, TransformerUtils.invokerTransformer(
379             "contains", new Class JavaDoc[] {Object JavaDoc.class}, new Object JavaDoc[] {cString}).transform(null));
380
381         try {
382             TransformerUtils.invokerTransformer(null, null, null);
383             fail();
384         } catch (IllegalArgumentException JavaDoc ex) {}
385         try {
386             TransformerUtils.invokerTransformer(
387                 "noSuchMethod", new Class JavaDoc[] {Object JavaDoc.class}, new Object JavaDoc[] {cString}).transform(new Object JavaDoc());
388             fail();
389         } catch (FunctorException ex) {}
390         try {
391             TransformerUtils.invokerTransformer("badArgs", null, new Object JavaDoc[] { cString });
392             fail();
393         } catch (IllegalArgumentException JavaDoc ex) {}
394         try {
395             TransformerUtils.invokerTransformer("badArgs", new Class JavaDoc[] {Object JavaDoc.class}, null);
396             fail();
397         } catch (IllegalArgumentException JavaDoc ex) {}
398         try {
399             TransformerUtils.invokerTransformer("badArgs", new Class JavaDoc[] {}, new Object JavaDoc[] { cString });
400             fail();
401         } catch (IllegalArgumentException JavaDoc ex) {}
402     }
403     
404     // stringValueTransformer
405
//------------------------------------------------------------------
406

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 JavaDoc(6)));
414     }
415     
416     // instantiateFactory
417
//------------------------------------------------------------------
418

419     public void testInstantiateTransformerNull() {
420         try {
421             Transformer trans = TransformerUtils.instantiateTransformer(null, new Object JavaDoc[] {"str"});
422             fail();
423         } catch (IllegalArgumentException JavaDoc ex) {}
424         try {
425             Transformer trans = TransformerUtils.instantiateTransformer(new Class JavaDoc[] {}, new Object JavaDoc[] {"str"});
426             fail();
427         } catch (IllegalArgumentException JavaDoc ex) {}
428         
429         Transformer trans = TransformerUtils.instantiateTransformer(new Class JavaDoc[] {Long JavaDoc.class}, new Object JavaDoc[] {null});
430         try {
431             trans.transform(String JavaDoc.class);
432             fail();
433         } catch (FunctorException ex) {}
434         
435         trans = TransformerUtils.instantiateTransformer();
436         assertEquals("", trans.transform(String JavaDoc.class));
437         
438         trans = TransformerUtils.instantiateTransformer(new Class JavaDoc[] {Long.TYPE}, new Object JavaDoc[] {new Long JavaDoc(1000L)});
439         assertEquals(new Date JavaDoc(1000L), trans.transform(Date JavaDoc.class));
440     }
441
442 }
443
Popular Tags