KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25
26 import junit.framework.Test;
27 import junit.framework.TestSuite;
28 import junit.textui.TestRunner;
29
30 /**
31  * Tests the org.apache.commons.collections.PredicateUtils class.
32  *
33  * @since Commons Collections 3.0
34  * @version $Revision: 1.8 $ $Date: 2004/03/13 16:34:46 $
35  *
36  * @author Stephen Colebourne
37  */

38 public class TestPredicateUtils 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 TestPredicateUtils(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(TestPredicateUtils.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     // exceptionPredicate
79
//------------------------------------------------------------------
80

81     public void testExceptionPredicate() {
82         assertNotNull(PredicateUtils.exceptionPredicate());
83         assertSame(PredicateUtils.exceptionPredicate(), PredicateUtils.exceptionPredicate());
84         try {
85             PredicateUtils.exceptionPredicate().evaluate(null);
86         } catch (FunctorException ex) {
87             try {
88                 PredicateUtils.exceptionPredicate().evaluate(cString);
89             } catch (FunctorException ex2) {
90                 return;
91             }
92         }
93         fail();
94     }
95     
96     // nullPredicate
97
//------------------------------------------------------------------
98

99     public void testNullPredicate() {
100         assertNotNull(PredicateUtils.nullPredicate());
101         assertSame(PredicateUtils.nullPredicate(), PredicateUtils.nullPredicate());
102         assertEquals(true, PredicateUtils.nullPredicate().evaluate(null));
103         assertEquals(false, PredicateUtils.nullPredicate().evaluate(cObject));
104         assertEquals(false, PredicateUtils.nullPredicate().evaluate(cString));
105         assertEquals(false, PredicateUtils.nullPredicate().evaluate(cInteger));
106     }
107
108     // notNullPredicate
109
//------------------------------------------------------------------
110

111     public void testIsNotNullPredicate() {
112         assertNotNull(PredicateUtils.notNullPredicate());
113         assertSame(PredicateUtils.notNullPredicate(), PredicateUtils.notNullPredicate());
114         assertEquals(false, PredicateUtils.notNullPredicate().evaluate(null));
115         assertEquals(true, PredicateUtils.notNullPredicate().evaluate(cObject));
116         assertEquals(true, PredicateUtils.notNullPredicate().evaluate(cString));
117         assertEquals(true, PredicateUtils.notNullPredicate().evaluate(cInteger));
118     }
119
120     // equalPredicate
121
//------------------------------------------------------------------
122

123     public void testEqualPredicate() {
124         assertSame(PredicateUtils.nullPredicate(), PredicateUtils.equalPredicate(null));
125         assertNotNull(PredicateUtils.equalPredicate(new Integer JavaDoc(6)));
126         assertEquals(false, PredicateUtils.equalPredicate(new Integer JavaDoc(6)).evaluate(null));
127         assertEquals(false, PredicateUtils.equalPredicate(new Integer JavaDoc(6)).evaluate(cObject));
128         assertEquals(false, PredicateUtils.equalPredicate(new Integer JavaDoc(6)).evaluate(cString));
129         assertEquals(true, PredicateUtils.equalPredicate(new Integer JavaDoc(6)).evaluate(cInteger));
130     }
131
132     // identityPredicate
133
//------------------------------------------------------------------
134

135     public void testIdentityPredicate() {
136         assertSame(PredicateUtils.nullPredicate(), PredicateUtils.identityPredicate(null));
137         assertNotNull(PredicateUtils.identityPredicate(new Integer JavaDoc(6)));
138         assertEquals(false, PredicateUtils.identityPredicate(new Integer JavaDoc(6)).evaluate(null));
139         assertEquals(false, PredicateUtils.identityPredicate(new Integer JavaDoc(6)).evaluate(cObject));
140         assertEquals(false, PredicateUtils.identityPredicate(new Integer JavaDoc(6)).evaluate(cString));
141         assertEquals(false, PredicateUtils.identityPredicate(new Integer JavaDoc(6)).evaluate(cInteger));
142         assertEquals(true, PredicateUtils.identityPredicate(cInteger).evaluate(cInteger));
143     }
144
145     // truePredicate
146
//------------------------------------------------------------------
147

148     public void testTruePredicate() {
149         assertNotNull(PredicateUtils.truePredicate());
150         assertSame(PredicateUtils.truePredicate(), PredicateUtils.truePredicate());
151         assertEquals(true, PredicateUtils.truePredicate().evaluate(null));
152         assertEquals(true, PredicateUtils.truePredicate().evaluate(cObject));
153         assertEquals(true, PredicateUtils.truePredicate().evaluate(cString));
154         assertEquals(true, PredicateUtils.truePredicate().evaluate(cInteger));
155     }
156
157     // falsePredicate
158
//------------------------------------------------------------------
159

160     public void testFalsePredicate() {
161         assertNotNull(PredicateUtils.falsePredicate());
162         assertSame(PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate());
163         assertEquals(false, PredicateUtils.falsePredicate().evaluate(null));
164         assertEquals(false, PredicateUtils.falsePredicate().evaluate(cObject));
165         assertEquals(false, PredicateUtils.falsePredicate().evaluate(cString));
166         assertEquals(false, PredicateUtils.falsePredicate().evaluate(cInteger));
167     }
168
169     // notPredicate
170
//------------------------------------------------------------------
171

172     public void testNotPredicate() {
173         assertNotNull(PredicateUtils.notPredicate(PredicateUtils.truePredicate()));
174         assertEquals(false, PredicateUtils.notPredicate(PredicateUtils.truePredicate()).evaluate(null));
175         assertEquals(false, PredicateUtils.notPredicate(PredicateUtils.truePredicate()).evaluate(cObject));
176         assertEquals(false, PredicateUtils.notPredicate(PredicateUtils.truePredicate()).evaluate(cString));
177         assertEquals(false, PredicateUtils.notPredicate(PredicateUtils.truePredicate()).evaluate(cInteger));
178     }
179     
180     public void testNotPredicateEx() {
181         try {
182             PredicateUtils.notPredicate(null);
183         } catch (IllegalArgumentException JavaDoc ex) {
184             return;
185         }
186         fail();
187     }
188     
189     // andPredicate
190
//------------------------------------------------------------------
191

192     public void testAndPredicate() {
193         assertEquals(true, PredicateUtils.andPredicate(PredicateUtils.truePredicate(), PredicateUtils.truePredicate()).evaluate(null));
194         assertEquals(false, PredicateUtils.andPredicate(PredicateUtils.truePredicate(), PredicateUtils.falsePredicate()).evaluate(null));
195         assertEquals(false, PredicateUtils.andPredicate(PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()).evaluate(null));
196         assertEquals(false, PredicateUtils.andPredicate(PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate()).evaluate(null));
197     }
198
199     public void testAndPredicateEx() {
200         try {
201             PredicateUtils.andPredicate(null, null);
202         } catch (IllegalArgumentException JavaDoc ex) {
203             return;
204         }
205         fail();
206     }
207     
208     // allPredicate
209
//------------------------------------------------------------------
210

211     public void testAllPredicate() {
212         assertEquals(true, PredicateUtils.allPredicate(new Predicate[] {
213             PredicateUtils.truePredicate(), PredicateUtils.truePredicate(), PredicateUtils.truePredicate()}).evaluate(null));
214         assertEquals(false, PredicateUtils.allPredicate(new Predicate[] {
215             PredicateUtils.truePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()}).evaluate(null));
216         assertEquals(false, PredicateUtils.allPredicate(new Predicate[] {
217             PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()}).evaluate(null));
218         assertEquals(false, PredicateUtils.allPredicate(new Predicate[] {
219             PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate()}).evaluate(null));
220         Collection JavaDoc coll = new ArrayList JavaDoc();
221         coll.add(PredicateUtils.truePredicate());
222         coll.add(PredicateUtils.truePredicate());
223         coll.add(PredicateUtils.truePredicate());
224         assertEquals(true, PredicateUtils.allPredicate(coll).evaluate(null));
225         coll.clear();
226         coll.add(PredicateUtils.truePredicate());
227         coll.add(PredicateUtils.falsePredicate());
228         coll.add(PredicateUtils.truePredicate());
229         assertEquals(false, PredicateUtils.allPredicate(coll).evaluate(null));
230         coll.clear();
231         coll.add(PredicateUtils.falsePredicate());
232         coll.add(PredicateUtils.falsePredicate());
233         coll.add(PredicateUtils.truePredicate());
234         assertEquals(false, PredicateUtils.allPredicate(coll).evaluate(null));
235         coll.clear();
236         coll.add(PredicateUtils.falsePredicate());
237         coll.add(PredicateUtils.falsePredicate());
238         coll.add(PredicateUtils.falsePredicate());
239         assertEquals(false, PredicateUtils.allPredicate(coll).evaluate(null));
240     }
241
242     public void testAllPredicateEx1() {
243         try {
244             PredicateUtils.allPredicate((Predicate[]) null);
245         } catch (IllegalArgumentException JavaDoc ex) {
246             return;
247         }
248         fail();
249     }
250     
251     public void testAllPredicateEx2() {
252         try {
253             PredicateUtils.allPredicate(new Predicate[] {null});
254         } catch (IllegalArgumentException JavaDoc ex) {
255             return;
256         }
257         fail();
258     }
259     
260     public void testAllPredicateEx3() {
261         try {
262             PredicateUtils.allPredicate(new Predicate[] {null, null});
263         } catch (IllegalArgumentException JavaDoc ex) {
264             return;
265         }
266         fail();
267     }
268     
269     public void testAllPredicateEx4() {
270         try {
271             PredicateUtils.allPredicate((Collection JavaDoc) null);
272         } catch (IllegalArgumentException JavaDoc ex) {
273             return;
274         }
275         fail();
276     }
277     
278     public void testAllPredicateEx5() {
279         try {
280             PredicateUtils.allPredicate(Collections.EMPTY_LIST);
281         } catch (IllegalArgumentException JavaDoc ex) {
282             return;
283         }
284         fail();
285     }
286     
287     public void testAllPredicateEx6() {
288         try {
289             Collection JavaDoc coll = new ArrayList JavaDoc();
290             coll.add(null);
291             coll.add(null);
292             PredicateUtils.allPredicate(coll);
293         } catch (IllegalArgumentException JavaDoc ex) {
294             return;
295         }
296         fail();
297     }
298     
299     // orPredicate
300
//------------------------------------------------------------------
301

302     public void testOrPredicate() {
303         assertEquals(true, PredicateUtils.orPredicate(PredicateUtils.truePredicate(), PredicateUtils.truePredicate()).evaluate(null));
304         assertEquals(true, PredicateUtils.orPredicate(PredicateUtils.truePredicate(), PredicateUtils.falsePredicate()).evaluate(null));
305         assertEquals(true, PredicateUtils.orPredicate(PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()).evaluate(null));
306         assertEquals(false, PredicateUtils.orPredicate(PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate()).evaluate(null));
307     }
308     
309     public void testOrPredicateEx() {
310         try {
311             PredicateUtils.orPredicate(null, null);
312         } catch (IllegalArgumentException JavaDoc ex) {
313             return;
314         }
315         fail();
316     }
317     
318     // anyPredicate
319
//------------------------------------------------------------------
320

321     public void testAnyPredicate() {
322         assertEquals(true, PredicateUtils.anyPredicate(new Predicate[] {
323             PredicateUtils.truePredicate(), PredicateUtils.truePredicate(), PredicateUtils.truePredicate()}).evaluate(null));
324         assertEquals(true, PredicateUtils.anyPredicate(new Predicate[] {
325             PredicateUtils.truePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()}).evaluate(null));
326         assertEquals(true, PredicateUtils.anyPredicate(new Predicate[] {
327             PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()}).evaluate(null));
328         assertEquals(false, PredicateUtils.anyPredicate(new Predicate[] {
329             PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate()}).evaluate(null));
330         Collection JavaDoc coll = new ArrayList JavaDoc();
331         coll.add(PredicateUtils.truePredicate());
332         coll.add(PredicateUtils.truePredicate());
333         coll.add(PredicateUtils.truePredicate());
334         assertEquals(true, PredicateUtils.anyPredicate(coll).evaluate(null));
335         coll.clear();
336         coll.add(PredicateUtils.truePredicate());
337         coll.add(PredicateUtils.falsePredicate());
338         coll.add(PredicateUtils.truePredicate());
339         assertEquals(true, PredicateUtils.anyPredicate(coll).evaluate(null));
340         coll.clear();
341         coll.add(PredicateUtils.falsePredicate());
342         coll.add(PredicateUtils.falsePredicate());
343         coll.add(PredicateUtils.truePredicate());
344         assertEquals(true, PredicateUtils.anyPredicate(coll).evaluate(null));
345         coll.clear();
346         coll.add(PredicateUtils.falsePredicate());
347         coll.add(PredicateUtils.falsePredicate());
348         coll.add(PredicateUtils.falsePredicate());
349         assertEquals(false, PredicateUtils.anyPredicate(coll).evaluate(null));
350     }
351
352     public void testAnyPredicateEx1() {
353         try {
354             PredicateUtils.anyPredicate((Predicate[]) null);
355         } catch (IllegalArgumentException JavaDoc ex) {
356             return;
357         }
358         fail();
359     }
360     
361     public void testAnyPredicateEx2() {
362         try {
363             PredicateUtils.anyPredicate(new Predicate[] {null});
364         } catch (IllegalArgumentException JavaDoc ex) {
365             return;
366         }
367         fail();
368     }
369     
370     public void testAnyPredicateEx3() {
371         try {
372             PredicateUtils.anyPredicate(new Predicate[] {null, null});
373         } catch (IllegalArgumentException JavaDoc ex) {
374             return;
375         }
376         fail();
377     }
378     
379     public void testAnyPredicateEx4() {
380         try {
381             PredicateUtils.anyPredicate((Collection JavaDoc) null);
382         } catch (IllegalArgumentException JavaDoc ex) {
383             return;
384         }
385         fail();
386     }
387     
388     public void testAnyPredicateEx5() {
389         try {
390             PredicateUtils.anyPredicate(Collections.EMPTY_LIST);
391         } catch (IllegalArgumentException JavaDoc ex) {
392             return;
393         }
394         fail();
395     }
396     
397     public void testAnyPredicateEx6() {
398         try {
399             Collection JavaDoc coll = new ArrayList JavaDoc();
400             coll.add(null);
401             coll.add(null);
402             PredicateUtils.anyPredicate(coll);
403         } catch (IllegalArgumentException JavaDoc ex) {
404             return;
405         }
406         fail();
407     }
408     
409     // eitherPredicate
410
//------------------------------------------------------------------
411

412     public void testEitherPredicate() {
413         assertEquals(false, PredicateUtils.eitherPredicate(PredicateUtils.truePredicate(), PredicateUtils.truePredicate()).evaluate(null));
414         assertEquals(true, PredicateUtils.eitherPredicate(PredicateUtils.truePredicate(), PredicateUtils.falsePredicate()).evaluate(null));
415         assertEquals(true, PredicateUtils.eitherPredicate(PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()).evaluate(null));
416         assertEquals(false, PredicateUtils.eitherPredicate(PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate()).evaluate(null));
417     }
418
419     public void testEitherPredicateEx() {
420         try {
421             PredicateUtils.eitherPredicate(null, null);
422         } catch (IllegalArgumentException JavaDoc ex) {
423             return;
424         }
425         fail();
426     }
427     
428     // onePredicate
429
//------------------------------------------------------------------
430

431     public void testOnePredicate() {
432         assertEquals(false, PredicateUtils.onePredicate(new Predicate[] {
433             PredicateUtils.truePredicate(), PredicateUtils.truePredicate(), PredicateUtils.truePredicate()}).evaluate(null));
434         assertEquals(false, PredicateUtils.onePredicate(new Predicate[] {
435             PredicateUtils.truePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()}).evaluate(null));
436         assertEquals(true, PredicateUtils.onePredicate(new Predicate[] {
437             PredicateUtils.truePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate()}).evaluate(null));
438         assertEquals(true, PredicateUtils.onePredicate(new Predicate[] {
439             PredicateUtils.falsePredicate(), PredicateUtils.truePredicate(), PredicateUtils.falsePredicate()}).evaluate(null));
440         assertEquals(true, PredicateUtils.onePredicate(new Predicate[] {
441             PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()}).evaluate(null));
442         assertEquals(false, PredicateUtils.onePredicate(new Predicate[] {
443             PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate()}).evaluate(null));
444         Collection JavaDoc coll = new ArrayList JavaDoc();
445         coll.add(PredicateUtils.truePredicate());
446         coll.add(PredicateUtils.truePredicate());
447         coll.add(PredicateUtils.truePredicate());
448         assertEquals(false, PredicateUtils.onePredicate(coll).evaluate(null));
449         coll.clear();
450         coll.add(PredicateUtils.truePredicate());
451         coll.add(PredicateUtils.falsePredicate());
452         coll.add(PredicateUtils.truePredicate());
453         assertEquals(false, PredicateUtils.onePredicate(coll).evaluate(null));
454         coll.clear();
455         coll.add(PredicateUtils.falsePredicate());
456         coll.add(PredicateUtils.falsePredicate());
457         coll.add(PredicateUtils.truePredicate());
458         assertEquals(true, PredicateUtils.onePredicate(coll).evaluate(null));
459         coll.clear();
460         coll.add(PredicateUtils.falsePredicate());
461         coll.add(PredicateUtils.falsePredicate());
462         coll.add(PredicateUtils.falsePredicate());
463         assertEquals(false, PredicateUtils.onePredicate(coll).evaluate(null));
464     }
465
466     public void testOnePredicateEx1() {
467         try {
468             PredicateUtils.onePredicate((Predicate[]) null);
469         } catch (IllegalArgumentException JavaDoc ex) {
470             return;
471         }
472         fail();
473     }
474     
475     public void testOnePredicateEx2() {
476         try {
477             PredicateUtils.onePredicate(new Predicate[] {null});
478         } catch (IllegalArgumentException JavaDoc ex) {
479             return;
480         }
481         fail();
482     }
483     
484     public void testOnePredicateEx3() {
485         try {
486             PredicateUtils.onePredicate(new Predicate[] {null, null});
487         } catch (IllegalArgumentException JavaDoc ex) {
488             return;
489         }
490         fail();
491     }
492     
493     public void testOnePredicateEx4() {
494         try {
495             PredicateUtils.onePredicate((Collection JavaDoc) null);
496         } catch (IllegalArgumentException JavaDoc ex) {
497             return;
498         }
499         fail();
500     }
501     
502     public void testOnePredicateEx5() {
503         try {
504             PredicateUtils.onePredicate(Collections.EMPTY_LIST);
505         } catch (IllegalArgumentException JavaDoc ex) {
506             return;
507         }
508         fail();
509     }
510     
511     public void testOnePredicateEx6() {
512         try {
513             Collection JavaDoc coll = new ArrayList JavaDoc();
514             coll.add(null);
515             coll.add(null);
516             PredicateUtils.onePredicate(coll);
517         } catch (IllegalArgumentException JavaDoc ex) {
518             return;
519         }
520         fail();
521     }
522     
523     // neitherPredicate
524
//------------------------------------------------------------------
525

526     public void testNeitherPredicate() {
527         assertEquals(false, PredicateUtils.neitherPredicate(PredicateUtils.truePredicate(), PredicateUtils.truePredicate()).evaluate(null));
528         assertEquals(false, PredicateUtils.neitherPredicate(PredicateUtils.truePredicate(), PredicateUtils.falsePredicate()).evaluate(null));
529         assertEquals(false, PredicateUtils.neitherPredicate(PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()).evaluate(null));
530         assertEquals(true, PredicateUtils.neitherPredicate(PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate()).evaluate(null));
531     }
532
533     public void testNeitherPredicateEx() {
534         try {
535             PredicateUtils.neitherPredicate(null, null);
536         } catch (IllegalArgumentException JavaDoc ex) {
537             return;
538         }
539         fail();
540     }
541     
542     // nonePredicate
543
//------------------------------------------------------------------
544

545     public void testNonePredicate() {
546         assertEquals(false, PredicateUtils.nonePredicate(new Predicate[] {
547             PredicateUtils.truePredicate(), PredicateUtils.truePredicate(), PredicateUtils.truePredicate()}).evaluate(null));
548         assertEquals(false, PredicateUtils.nonePredicate(new Predicate[] {
549             PredicateUtils.truePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()}).evaluate(null));
550         assertEquals(false, PredicateUtils.nonePredicate(new Predicate[] {
551             PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()}).evaluate(null));
552         assertEquals(true, PredicateUtils.nonePredicate(new Predicate[] {
553             PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate()}).evaluate(null));
554         Collection JavaDoc coll = new ArrayList JavaDoc();
555         coll.add(PredicateUtils.truePredicate());
556         coll.add(PredicateUtils.truePredicate());
557         coll.add(PredicateUtils.truePredicate());
558         assertEquals(false, PredicateUtils.nonePredicate(coll).evaluate(null));
559         coll.clear();
560         coll.add(PredicateUtils.truePredicate());
561         coll.add(PredicateUtils.falsePredicate());
562         coll.add(PredicateUtils.truePredicate());
563         assertEquals(false, PredicateUtils.nonePredicate(coll).evaluate(null));
564         coll.clear();
565         coll.add(PredicateUtils.falsePredicate());
566         coll.add(PredicateUtils.falsePredicate());
567         coll.add(PredicateUtils.truePredicate());
568         assertEquals(false, PredicateUtils.nonePredicate(coll).evaluate(null));
569         coll.clear();
570         coll.add(PredicateUtils.falsePredicate());
571         coll.add(PredicateUtils.falsePredicate());
572         coll.add(PredicateUtils.falsePredicate());
573         assertEquals(true, PredicateUtils.nonePredicate(coll).evaluate(null));
574     }
575
576     public void testNonePredicateEx1() {
577         try {
578             PredicateUtils.nonePredicate((Predicate[]) null);
579         } catch (IllegalArgumentException JavaDoc ex) {
580             return;
581         }
582         fail();
583     }
584     
585     public void testNonePredicateEx2() {
586         try {
587             PredicateUtils.nonePredicate(new Predicate[] {null});
588         } catch (IllegalArgumentException JavaDoc ex) {
589             return;
590         }
591         fail();
592     }
593     
594     public void testNonePredicateEx3() {
595         try {
596             PredicateUtils.nonePredicate(new Predicate[] {null, null});
597         } catch (IllegalArgumentException JavaDoc ex) {
598             return;
599         }
600         fail();
601     }
602     
603     public void testNonePredicateEx4() {
604         try {
605             PredicateUtils.nonePredicate((Collection JavaDoc) null);
606         } catch (IllegalArgumentException JavaDoc ex) {
607             return;
608         }
609         fail();
610     }
611     
612     public void testNonePredicateEx5() {
613         try {
614             PredicateUtils.nonePredicate(Collections.EMPTY_LIST);
615         } catch (IllegalArgumentException JavaDoc ex) {
616             return;
617         }
618         fail();
619     }
620     
621     public void testNonePredicateEx6() {
622         try {
623             Collection JavaDoc coll = new ArrayList JavaDoc();
624             coll.add(null);
625             coll.add(null);
626             PredicateUtils.nonePredicate(coll);
627         } catch (IllegalArgumentException JavaDoc ex) {
628             return;
629         }
630         fail();
631     }
632     
633     // instanceofPredicate
634
//------------------------------------------------------------------
635

636     public void testInstanceOfPredicate() {
637         assertNotNull(PredicateUtils.instanceofPredicate(String JavaDoc.class));
638         assertEquals(false, PredicateUtils.instanceofPredicate(String JavaDoc.class).evaluate(null));
639         assertEquals(false, PredicateUtils.instanceofPredicate(String JavaDoc.class).evaluate(cObject));
640         assertEquals(true, PredicateUtils.instanceofPredicate(String JavaDoc.class).evaluate(cString));
641         assertEquals(false, PredicateUtils.instanceofPredicate(String JavaDoc.class).evaluate(cInteger));
642     }
643
644     // uniquePredicate
645
//------------------------------------------------------------------
646

647     public void testUniquePredicate() {
648         Predicate p = PredicateUtils.uniquePredicate();
649         assertEquals(true, p.evaluate(new Object JavaDoc()));
650         assertEquals(true, p.evaluate(new Object JavaDoc()));
651         assertEquals(true, p.evaluate(new Object JavaDoc()));
652         assertEquals(true, p.evaluate(cString));
653         assertEquals(false, p.evaluate(cString));
654         assertEquals(false, p.evaluate(cString));
655     }
656     
657     // asPredicate(Transformer)
658
//------------------------------------------------------------------
659

660     public void testAsPredicateTransformer() {
661         assertEquals(false, PredicateUtils.asPredicate(TransformerUtils.nopTransformer()).evaluate(Boolean.FALSE));
662         assertEquals(true, PredicateUtils.asPredicate(TransformerUtils.nopTransformer()).evaluate(Boolean.TRUE));
663     }
664
665     public void testAsPredicateTransformerEx1() {
666         try {
667             PredicateUtils.asPredicate(null);
668         } catch (IllegalArgumentException JavaDoc ex) {
669             return;
670         }
671         fail();
672     }
673     
674     public void testAsPredicateTransformerEx2() {
675         try {
676             PredicateUtils.asPredicate(TransformerUtils.nopTransformer()).evaluate(null);
677         } catch (FunctorException ex) {
678             return;
679         }
680         fail();
681     }
682     
683     // invokerPredicate
684
//------------------------------------------------------------------
685

686     public void testInvokerPredicate() {
687         List JavaDoc list = new ArrayList JavaDoc();
688         assertEquals(true, PredicateUtils.invokerPredicate("isEmpty").evaluate(list));
689         list.add(new Object JavaDoc());
690         assertEquals(false, PredicateUtils.invokerPredicate("isEmpty").evaluate(list));
691     }
692
693     public void testInvokerPredicateEx1() {
694         try {
695             PredicateUtils.invokerPredicate(null);
696         } catch (IllegalArgumentException JavaDoc ex) {
697             return;
698         }
699         fail();
700     }
701     
702     public void testInvokerPredicateEx2() {
703         try {
704             PredicateUtils.invokerPredicate("isEmpty").evaluate(null);
705         } catch (FunctorException ex) {
706             return;
707         }
708         fail();
709     }
710     
711     public void testInvokerPredicateEx3() {
712         try {
713             PredicateUtils.invokerPredicate("noSuchMethod").evaluate(new Object JavaDoc());
714         } catch (FunctorException ex) {
715             return;
716         }
717         fail();
718     }
719     
720     // invokerPredicate2
721
//------------------------------------------------------------------
722

723     public void testInvokerPredicate2() {
724         List JavaDoc list = new ArrayList JavaDoc();
725         assertEquals(false, PredicateUtils.invokerPredicate(
726             "contains", new Class JavaDoc[] {Object JavaDoc.class}, new Object JavaDoc[] {cString}).evaluate(list));
727         list.add(cString);
728         assertEquals(true, PredicateUtils.invokerPredicate(
729             "contains", new Class JavaDoc[] {Object JavaDoc.class}, new Object JavaDoc[] {cString}).evaluate(list));
730     }
731
732     public void testInvokerPredicate2Ex1() {
733         try {
734             PredicateUtils.invokerPredicate(null, null, null);
735         } catch (IllegalArgumentException JavaDoc ex) {
736             return;
737         }
738         fail();
739     }
740     
741     public void testInvokerPredicate2Ex2() {
742         try {
743             PredicateUtils.invokerPredicate("contains", new Class JavaDoc[] {Object JavaDoc.class}, new Object JavaDoc[] {cString}).evaluate(null);
744         } catch (FunctorException ex) {
745             return;
746         }
747         fail();
748     }
749     
750     public void testInvokerPredicate2Ex3() {
751         try {
752             PredicateUtils.invokerPredicate(
753                 "noSuchMethod", new Class JavaDoc[] {Object JavaDoc.class}, new Object JavaDoc[] {cString}).evaluate(new Object JavaDoc());
754         } catch (FunctorException ex) {
755             return;
756         }
757         fail();
758     }
759     
760     // nullIsException
761
//------------------------------------------------------------------
762

763     public void testNullIsExceptionPredicate() {
764         assertEquals(true, PredicateUtils.nullIsExceptionPredicate(PredicateUtils.truePredicate()).evaluate(new Object JavaDoc()));
765         try {
766             PredicateUtils.nullIsExceptionPredicate(PredicateUtils.truePredicate()).evaluate(null);
767         } catch (FunctorException ex) {
768             return;
769         }
770         fail();
771     }
772
773     public void testNullIsExceptionPredicateEx1() {
774         try {
775             PredicateUtils.nullIsExceptionPredicate(null);
776         } catch (IllegalArgumentException JavaDoc ex) {
777             return;
778         }
779         fail();
780     }
781     
782     // nullIsTrue
783
//------------------------------------------------------------------
784

785     public void testNullIsTruePredicate() {
786         assertEquals(true, PredicateUtils.nullIsTruePredicate(PredicateUtils.truePredicate()).evaluate(null));
787         assertEquals(true, PredicateUtils.nullIsTruePredicate(PredicateUtils.truePredicate()).evaluate(new Object JavaDoc()));
788         assertEquals(false, PredicateUtils.nullIsTruePredicate(PredicateUtils.falsePredicate()).evaluate(new Object JavaDoc()));
789     }
790
791     public void testNullIsTruePredicateEx1() {
792         try {
793             PredicateUtils.nullIsTruePredicate(null);
794         } catch (IllegalArgumentException JavaDoc ex) {
795             return;
796         }
797         fail();
798     }
799     
800     // nullIsFalse
801
//------------------------------------------------------------------
802

803     public void testNullIsFalsePredicate() {
804         assertEquals(false, PredicateUtils.nullIsFalsePredicate(PredicateUtils.truePredicate()).evaluate(null));
805         assertEquals(true, PredicateUtils.nullIsFalsePredicate(PredicateUtils.truePredicate()).evaluate(new Object JavaDoc()));
806         assertEquals(false, PredicateUtils.nullIsFalsePredicate(PredicateUtils.falsePredicate()).evaluate(new Object JavaDoc()));
807     }
808
809     public void testNullIsFalsePredicateEx1() {
810         try {
811             PredicateUtils.nullIsFalsePredicate(null);
812         } catch (IllegalArgumentException JavaDoc ex) {
813             return;
814         }
815         fail();
816     }
817     
818     // transformed
819
//------------------------------------------------------------------
820

821     public void testTransformedPredicate() {
822         assertEquals(true, PredicateUtils.transformedPredicate(
823                 TransformerUtils.nopTransformer(),
824                 PredicateUtils.truePredicate()).evaluate(new Object JavaDoc()));
825                 
826         Map JavaDoc map = new HashMap JavaDoc();
827         map.put(Boolean.TRUE, "Hello");
828         Transformer t = TransformerUtils.mapTransformer(map);
829         Predicate p = PredicateUtils.equalPredicate("Hello");
830         assertEquals(false, PredicateUtils.transformedPredicate(t, p).evaluate(null));
831         assertEquals(true, PredicateUtils.transformedPredicate(t, p).evaluate(Boolean.TRUE));
832         try {
833             PredicateUtils.transformedPredicate(null, null);
834             fail();
835         } catch (IllegalArgumentException JavaDoc ex) {}
836     }
837
838 }
839
Popular Tags