Source Code Cross Referenced for TestTransformerUtils.java in  » Library » Apache-common-Collections » org » apache » commons » collections » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache common Collections » org.apache.commons.collections 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Copyright 2001-2004 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.Date;
022:        import java.util.HashMap;
023:        import java.util.List;
024:        import java.util.Map;
025:
026:        import junit.framework.Test;
027:        import junit.framework.TestSuite;
028:        import junit.textui.TestRunner;
029:
030:        import org.apache.commons.collections.functors.ConstantTransformer;
031:        import org.apache.commons.collections.functors.NOPTransformer;
032:
033:        /**
034:         * Tests the org.apache.commons.collections.TransformerUtils class.
035:         * 
036:         * @since Commons Collections 3.0
037:         * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
038:         *
039:         * @author Stephen Colebourne
040:         * @author James Carman
041:         */
042:        public class TestTransformerUtils extends junit.framework.TestCase {
043:
044:            private static final Object cObject = new Object();
045:            private static final Object cString = "Hello";
046:            private static final Object cInteger = new Integer(6);
047:
048:            /**
049:             * Construct
050:             */
051:            public TestTransformerUtils(String name) {
052:                super (name);
053:            }
054:
055:            /**
056:             * Main.
057:             * @param args
058:             */
059:            public static void main(String[] args) {
060:                TestRunner.run(suite());
061:            }
062:
063:            /**
064:             * Return class as a test suite.
065:             */
066:            public static Test suite() {
067:                return new TestSuite(TestTransformerUtils.class);
068:            }
069:
070:            /**
071:             * Set up instance variables required by this test case.
072:             */
073:            public void setUp() {
074:            }
075:
076:            /**
077:             * Tear down instance variables required by this test case.
078:             */
079:            public void tearDown() {
080:            }
081:
082:            // exceptionTransformer
083:            //------------------------------------------------------------------
084:
085:            public void testExceptionTransformer() {
086:                assertNotNull(TransformerUtils.exceptionTransformer());
087:                assertSame(TransformerUtils.exceptionTransformer(),
088:                        TransformerUtils.exceptionTransformer());
089:                try {
090:                    TransformerUtils.exceptionTransformer().transform(null);
091:                } catch (FunctorException ex) {
092:                    try {
093:                        TransformerUtils.exceptionTransformer().transform(
094:                                cString);
095:                    } catch (FunctorException ex2) {
096:                        return;
097:                    }
098:                }
099:                fail();
100:            }
101:
102:            // nullTransformer
103:            //------------------------------------------------------------------
104:
105:            public void testNullTransformer() {
106:                assertNotNull(TransformerUtils.nullTransformer());
107:                assertSame(TransformerUtils.nullTransformer(), TransformerUtils
108:                        .nullTransformer());
109:                assertEquals(null, TransformerUtils.nullTransformer()
110:                        .transform(null));
111:                assertEquals(null, TransformerUtils.nullTransformer()
112:                        .transform(cObject));
113:                assertEquals(null, TransformerUtils.nullTransformer()
114:                        .transform(cString));
115:                assertEquals(null, TransformerUtils.nullTransformer()
116:                        .transform(cInteger));
117:            }
118:
119:            // nopTransformer
120:            //------------------------------------------------------------------
121:
122:            public void testNopTransformer() {
123:                assertNotNull(TransformerUtils.nullTransformer());
124:                assertSame(TransformerUtils.nullTransformer(), TransformerUtils
125:                        .nullTransformer());
126:                assertEquals(null, TransformerUtils.nopTransformer().transform(
127:                        null));
128:                assertEquals(cObject, TransformerUtils.nopTransformer()
129:                        .transform(cObject));
130:                assertEquals(cString, TransformerUtils.nopTransformer()
131:                        .transform(cString));
132:                assertEquals(cInteger, TransformerUtils.nopTransformer()
133:                        .transform(cInteger));
134:            }
135:
136:            // constantTransformer
137:            //------------------------------------------------------------------
138:
139:            public void testConstantTransformer() {
140:                assertEquals(cObject, TransformerUtils.constantTransformer(
141:                        cObject).transform(null));
142:                assertEquals(cObject, TransformerUtils.constantTransformer(
143:                        cObject).transform(cObject));
144:                assertEquals(cObject, TransformerUtils.constantTransformer(
145:                        cObject).transform(cString));
146:                assertEquals(cObject, TransformerUtils.constantTransformer(
147:                        cObject).transform(cInteger));
148:                assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils
149:                        .constantTransformer(null));
150:            }
151:
152:            // cloneTransformer
153:            //------------------------------------------------------------------
154:
155:            public void testCloneTransformer() {
156:                assertEquals(null, TransformerUtils.cloneTransformer()
157:                        .transform(null));
158:                assertEquals(cString, TransformerUtils.cloneTransformer()
159:                        .transform(cString));
160:                assertEquals(cInteger, TransformerUtils.cloneTransformer()
161:                        .transform(cInteger));
162:                try {
163:                    assertEquals(cObject, TransformerUtils.cloneTransformer()
164:                            .transform(cObject));
165:                } catch (IllegalArgumentException ex) {
166:                    return;
167:                }
168:                fail();
169:            }
170:
171:            // mapTransformer
172:            //------------------------------------------------------------------
173:
174:            public void testMapTransformer() {
175:                Map map = new HashMap();
176:                map.put(null, new Integer(0));
177:                map.put(cObject, new Integer(1));
178:                map.put(cString, new Integer(2));
179:                assertEquals(new Integer(0), TransformerUtils.mapTransformer(
180:                        map).transform(null));
181:                assertEquals(new Integer(1), TransformerUtils.mapTransformer(
182:                        map).transform(cObject));
183:                assertEquals(new Integer(2), TransformerUtils.mapTransformer(
184:                        map).transform(cString));
185:                assertEquals(null, TransformerUtils.mapTransformer(map)
186:                        .transform(cInteger));
187:                assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils
188:                        .mapTransformer(null));
189:            }
190:
191:            // commandTransformer
192:            //------------------------------------------------------------------
193:
194:            public void testExecutorTransformer() {
195:                assertEquals(null, TransformerUtils.asTransformer(
196:                        ClosureUtils.nopClosure()).transform(null));
197:                assertEquals(cObject, TransformerUtils.asTransformer(
198:                        ClosureUtils.nopClosure()).transform(cObject));
199:                assertEquals(cString, TransformerUtils.asTransformer(
200:                        ClosureUtils.nopClosure()).transform(cString));
201:                assertEquals(cInteger, TransformerUtils.asTransformer(
202:                        ClosureUtils.nopClosure()).transform(cInteger));
203:                try {
204:                    TransformerUtils.asTransformer((Closure) null);
205:                } catch (IllegalArgumentException ex) {
206:                    return;
207:                }
208:                fail();
209:            }
210:
211:            // predicateTransformer
212:            //------------------------------------------------------------------
213:
214:            public void testPredicateTransformer() {
215:                assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(
216:                        PredicateUtils.truePredicate()).transform(null));
217:                assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(
218:                        PredicateUtils.truePredicate()).transform(cObject));
219:                assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(
220:                        PredicateUtils.truePredicate()).transform(cString));
221:                assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(
222:                        PredicateUtils.truePredicate()).transform(cInteger));
223:                try {
224:                    TransformerUtils.asTransformer((Predicate) null);
225:                } catch (IllegalArgumentException ex) {
226:                    return;
227:                }
228:                fail();
229:            }
230:
231:            // factoryTransformer
232:            //------------------------------------------------------------------
233:
234:            public void testFactoryTransformer() {
235:                assertEquals(null, TransformerUtils.asTransformer(
236:                        FactoryUtils.nullFactory()).transform(null));
237:                assertEquals(null, TransformerUtils.asTransformer(
238:                        FactoryUtils.nullFactory()).transform(cObject));
239:                assertEquals(null, TransformerUtils.asTransformer(
240:                        FactoryUtils.nullFactory()).transform(cString));
241:                assertEquals(null, TransformerUtils.asTransformer(
242:                        FactoryUtils.nullFactory()).transform(cInteger));
243:                try {
244:                    TransformerUtils.asTransformer((Factory) null);
245:                } catch (IllegalArgumentException ex) {
246:                    return;
247:                }
248:                fail();
249:            }
250:
251:            // chainedTransformer
252:            //------------------------------------------------------------------
253:
254:            public void testChainedTransformer() {
255:                Transformer a = TransformerUtils.constantTransformer("A");
256:                Transformer b = TransformerUtils.constantTransformer("B");
257:
258:                assertEquals("A", TransformerUtils.chainedTransformer(b, a)
259:                        .transform(null));
260:                assertEquals("B", TransformerUtils.chainedTransformer(a, b)
261:                        .transform(null));
262:                assertEquals("A", TransformerUtils.chainedTransformer(
263:                        new Transformer[] { b, a }).transform(null));
264:                Collection coll = new ArrayList();
265:                coll.add(b);
266:                coll.add(a);
267:                assertEquals("A", TransformerUtils.chainedTransformer(coll)
268:                        .transform(null));
269:
270:                assertSame(NOPTransformer.INSTANCE, TransformerUtils
271:                        .chainedTransformer(new Transformer[0]));
272:                assertSame(NOPTransformer.INSTANCE, TransformerUtils
273:                        .chainedTransformer(Collections.EMPTY_LIST));
274:
275:                try {
276:                    TransformerUtils.chainedTransformer(null, null);
277:                    fail();
278:                } catch (IllegalArgumentException ex) {
279:                }
280:                try {
281:                    TransformerUtils.chainedTransformer((Transformer[]) null);
282:                    fail();
283:                } catch (IllegalArgumentException ex) {
284:                }
285:                try {
286:                    TransformerUtils.chainedTransformer((Collection) null);
287:                    fail();
288:                } catch (IllegalArgumentException ex) {
289:                }
290:                try {
291:                    TransformerUtils.chainedTransformer(new Transformer[] {
292:                            null, null });
293:                    fail();
294:                } catch (IllegalArgumentException ex) {
295:                }
296:                try {
297:                    coll = new ArrayList();
298:                    coll.add(null);
299:                    coll.add(null);
300:                    TransformerUtils.chainedTransformer(coll);
301:                    fail();
302:                } catch (IllegalArgumentException ex) {
303:                }
304:            }
305:
306:            // switchTransformer
307:            //------------------------------------------------------------------
308:
309:            public void testSwitchTransformer() {
310:                Transformer a = TransformerUtils.constantTransformer("A");
311:                Transformer b = TransformerUtils.constantTransformer("B");
312:                Transformer c = TransformerUtils.constantTransformer("C");
313:
314:                assertEquals("A", TransformerUtils.switchTransformer(
315:                        PredicateUtils.truePredicate(), a, b).transform(null));
316:                assertEquals("B", TransformerUtils.switchTransformer(
317:                        PredicateUtils.falsePredicate(), a, b).transform(null));
318:
319:                assertEquals(null, TransformerUtils.switchTransformer(
320:                        new Predicate[] {
321:                                PredicateUtils.equalPredicate("HELLO"),
322:                                PredicateUtils.equalPredicate("THERE") },
323:                        new Transformer[] { a, b }).transform("WELL"));
324:                assertEquals("A", TransformerUtils.switchTransformer(
325:                        new Predicate[] {
326:                                PredicateUtils.equalPredicate("HELLO"),
327:                                PredicateUtils.equalPredicate("THERE") },
328:                        new Transformer[] { a, b }).transform("HELLO"));
329:                assertEquals("B", TransformerUtils.switchTransformer(
330:                        new Predicate[] {
331:                                PredicateUtils.equalPredicate("HELLO"),
332:                                PredicateUtils.equalPredicate("THERE") },
333:                        new Transformer[] { a, b }).transform("THERE"));
334:
335:                assertEquals("C", TransformerUtils.switchTransformer(
336:                        new Predicate[] {
337:                                PredicateUtils.equalPredicate("HELLO"),
338:                                PredicateUtils.equalPredicate("THERE") },
339:                        new Transformer[] { a, b }, c).transform("WELL"));
340:
341:                Map map = new HashMap();
342:                map.put(PredicateUtils.equalPredicate("HELLO"), a);
343:                map.put(PredicateUtils.equalPredicate("THERE"), b);
344:                assertEquals(null, TransformerUtils.switchTransformer(map)
345:                        .transform("WELL"));
346:                assertEquals("A", TransformerUtils.switchTransformer(map)
347:                        .transform("HELLO"));
348:                assertEquals("B", TransformerUtils.switchTransformer(map)
349:                        .transform("THERE"));
350:                map.put(null, c);
351:                assertEquals("C", TransformerUtils.switchTransformer(map)
352:                        .transform("WELL"));
353:
354:                assertSame(ConstantTransformer.NULL_INSTANCE,
355:                        TransformerUtils.switchTransformer(new Predicate[0],
356:                                new Transformer[0]));
357:                assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils
358:                        .switchTransformer(new HashMap()));
359:                map = new HashMap();
360:                map.put(null, null);
361:                assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils
362:                        .switchTransformer(map));
363:
364:                try {
365:                    TransformerUtils.switchTransformer(null, null);
366:                    fail();
367:                } catch (IllegalArgumentException ex) {
368:                }
369:                try {
370:                    TransformerUtils.switchTransformer((Predicate[]) null,
371:                            (Transformer[]) null);
372:                    fail();
373:                } catch (IllegalArgumentException ex) {
374:                }
375:                try {
376:                    TransformerUtils.switchTransformer((Map) null);
377:                    fail();
378:                } catch (IllegalArgumentException ex) {
379:                }
380:                try {
381:                    TransformerUtils.switchTransformer(new Predicate[2],
382:                            new Transformer[2]);
383:                    fail();
384:                } catch (IllegalArgumentException ex) {
385:                }
386:                try {
387:                    TransformerUtils.switchTransformer(
388:                            new Predicate[] { PredicateUtils.truePredicate() },
389:                            new Transformer[] { a, b });
390:                    fail();
391:                } catch (IllegalArgumentException ex) {
392:                }
393:            }
394:
395:            // switchMapTransformer
396:            //------------------------------------------------------------------
397:
398:            public void testSwitchMapTransformer() {
399:                Transformer a = TransformerUtils.constantTransformer("A");
400:                Transformer b = TransformerUtils.constantTransformer("B");
401:                Transformer c = TransformerUtils.constantTransformer("C");
402:
403:                Map map = new HashMap();
404:                map.put("HELLO", a);
405:                map.put("THERE", b);
406:                assertEquals(null, TransformerUtils.switchMapTransformer(map)
407:                        .transform("WELL"));
408:                assertEquals("A", TransformerUtils.switchMapTransformer(map)
409:                        .transform("HELLO"));
410:                assertEquals("B", TransformerUtils.switchMapTransformer(map)
411:                        .transform("THERE"));
412:                map.put(null, c);
413:                assertEquals("C", TransformerUtils.switchMapTransformer(map)
414:                        .transform("WELL"));
415:
416:                assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils
417:                        .switchMapTransformer(new HashMap()));
418:                map = new HashMap();
419:                map.put(null, null);
420:                assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils
421:                        .switchMapTransformer(map));
422:
423:                try {
424:                    TransformerUtils.switchMapTransformer(null);
425:                    fail();
426:                } catch (IllegalArgumentException ex) {
427:                }
428:            }
429:
430:            // invokerTransformer
431:            //------------------------------------------------------------------
432:
433:            public void testInvokerTransformer() {
434:                List list = new ArrayList();
435:                assertEquals(new Integer(0), TransformerUtils
436:                        .invokerTransformer("size").transform(list));
437:                list.add(new Object());
438:                assertEquals(new Integer(1), TransformerUtils
439:                        .invokerTransformer("size").transform(list));
440:                assertEquals(null, TransformerUtils.invokerTransformer("size")
441:                        .transform(null));
442:
443:                try {
444:                    TransformerUtils.invokerTransformer(null);
445:                    fail();
446:                } catch (IllegalArgumentException ex) {
447:                }
448:                try {
449:                    TransformerUtils.invokerTransformer("noSuchMethod")
450:                            .transform(new Object());
451:                    fail();
452:                } catch (FunctorException ex) {
453:                }
454:            }
455:
456:            // invokerTransformer2
457:            //------------------------------------------------------------------
458:
459:            public void testInvokerTransformer2() {
460:                List list = new ArrayList();
461:                assertEquals(Boolean.FALSE, TransformerUtils
462:                        .invokerTransformer("contains",
463:                                new Class[] { Object.class },
464:                                new Object[] { cString }).transform(list));
465:                list.add(cString);
466:                assertEquals(Boolean.TRUE, TransformerUtils.invokerTransformer(
467:                        "contains", new Class[] { Object.class },
468:                        new Object[] { cString }).transform(list));
469:                assertEquals(null, TransformerUtils.invokerTransformer(
470:                        "contains", new Class[] { Object.class },
471:                        new Object[] { cString }).transform(null));
472:
473:                try {
474:                    TransformerUtils.invokerTransformer(null, null, null);
475:                    fail();
476:                } catch (IllegalArgumentException ex) {
477:                }
478:                try {
479:                    TransformerUtils.invokerTransformer("noSuchMethod",
480:                            new Class[] { Object.class },
481:                            new Object[] { cString }).transform(new Object());
482:                    fail();
483:                } catch (FunctorException ex) {
484:                }
485:                try {
486:                    TransformerUtils.invokerTransformer("badArgs", null,
487:                            new Object[] { cString });
488:                    fail();
489:                } catch (IllegalArgumentException ex) {
490:                }
491:                try {
492:                    TransformerUtils.invokerTransformer("badArgs",
493:                            new Class[] { Object.class }, null);
494:                    fail();
495:                } catch (IllegalArgumentException ex) {
496:                }
497:                try {
498:                    TransformerUtils.invokerTransformer("badArgs",
499:                            new Class[] {}, new Object[] { cString });
500:                    fail();
501:                } catch (IllegalArgumentException ex) {
502:                }
503:            }
504:
505:            // stringValueTransformer
506:            //------------------------------------------------------------------
507:
508:            public void testStringValueTransformer() {
509:                assertNotNull(
510:                        "StringValueTransformer should NEVER return a null value.",
511:                        TransformerUtils.stringValueTransformer().transform(
512:                                null));
513:                assertEquals(
514:                        "StringValueTransformer should return \"null\" when given a null argument.",
515:                        "null", TransformerUtils.stringValueTransformer()
516:                                .transform(null));
517:                assertEquals(
518:                        "StringValueTransformer should return toString value",
519:                        "6", TransformerUtils.stringValueTransformer()
520:                                .transform(new Integer(6)));
521:            }
522:
523:            // instantiateFactory
524:            //------------------------------------------------------------------
525:
526:            public void testInstantiateTransformerNull() {
527:                try {
528:                    Transformer trans = TransformerUtils
529:                            .instantiateTransformer(null,
530:                                    new Object[] { "str" });
531:                    fail();
532:                } catch (IllegalArgumentException ex) {
533:                }
534:                try {
535:                    Transformer trans = TransformerUtils
536:                            .instantiateTransformer(new Class[] {},
537:                                    new Object[] { "str" });
538:                    fail();
539:                } catch (IllegalArgumentException ex) {
540:                }
541:
542:                Transformer trans = TransformerUtils.instantiateTransformer(
543:                        new Class[] { Long.class }, new Object[] { null });
544:                try {
545:                    trans.transform(String.class);
546:                    fail();
547:                } catch (FunctorException ex) {
548:                }
549:
550:                trans = TransformerUtils.instantiateTransformer();
551:                assertEquals("", trans.transform(String.class));
552:
553:                trans = TransformerUtils.instantiateTransformer(
554:                        new Class[] { Long.TYPE }, new Object[] { new Long(
555:                                1000L) });
556:                assertEquals(new Date(1000L), trans.transform(Date.class));
557:            }
558:
559:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.