0001: /*
0002: * Copyright 2001-2004,2006 The Apache Software Foundation
0003: *
0004: * Licensed under the Apache License, Version 2.0 (the "License");
0005: * you may not use this file except in compliance with the License.
0006: * You may obtain a copy of the License at
0007: *
0008: * http://www.apache.org/licenses/LICENSE-2.0
0009: *
0010: * Unless required by applicable law or agreed to in writing, software
0011: * distributed under the License is distributed on an "AS IS" BASIS,
0012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013: * See the License for the specific language governing permissions and
0014: * limitations under the License.
0015: */
0016: package org.apache.commons.collections;
0017:
0018: import java.util.ArrayList;
0019: import java.util.Collection;
0020: import java.util.Collections;
0021: import java.util.HashMap;
0022: import java.util.List;
0023: import java.util.Map;
0024:
0025: import junit.framework.Test;
0026: import junit.framework.TestSuite;
0027: import junit.textui.TestRunner;
0028:
0029: /**
0030: * Tests the org.apache.commons.collections.PredicateUtils class.
0031: *
0032: * @since Commons Collections 3.0
0033: * @version $Revision: 382470 $ $Date: 2006-03-02 19:02:36 +0000 (Thu, 02 Mar 2006) $
0034: *
0035: * @author Stephen Colebourne
0036: * @author Matt Benson
0037: */
0038: public class TestPredicateUtils extends junit.framework.TestCase {
0039:
0040: private static final Object cObject = new Object();
0041: private static final Object cString = "Hello";
0042: private static final Object cInteger = new Integer(6);
0043:
0044: /**
0045: * Construct
0046: */
0047: public TestPredicateUtils(String name) {
0048: super (name);
0049: }
0050:
0051: /**
0052: * Main.
0053: * @param args
0054: */
0055: public static void main(String[] args) {
0056: TestRunner.run(suite());
0057: }
0058:
0059: /**
0060: * Return class as a test suite.
0061: */
0062: public static Test suite() {
0063: return new TestSuite(TestPredicateUtils.class);
0064: }
0065:
0066: /**
0067: * Set up instance variables required by this test case.
0068: */
0069: public void setUp() {
0070: }
0071:
0072: /**
0073: * Tear down instance variables required by this test case.
0074: */
0075: public void tearDown() {
0076: }
0077:
0078: // exceptionPredicate
0079: //------------------------------------------------------------------
0080:
0081: public void testExceptionPredicate() {
0082: assertNotNull(PredicateUtils.exceptionPredicate());
0083: assertSame(PredicateUtils.exceptionPredicate(), PredicateUtils
0084: .exceptionPredicate());
0085: try {
0086: PredicateUtils.exceptionPredicate().evaluate(null);
0087: } catch (FunctorException ex) {
0088: try {
0089: PredicateUtils.exceptionPredicate().evaluate(cString);
0090: } catch (FunctorException ex2) {
0091: return;
0092: }
0093: }
0094: fail();
0095: }
0096:
0097: // nullPredicate
0098: //------------------------------------------------------------------
0099:
0100: public void testNullPredicate() {
0101: assertNotNull(PredicateUtils.nullPredicate());
0102: assertSame(PredicateUtils.nullPredicate(), PredicateUtils
0103: .nullPredicate());
0104: assertEquals(true, PredicateUtils.nullPredicate()
0105: .evaluate(null));
0106: assertEquals(false, PredicateUtils.nullPredicate().evaluate(
0107: cObject));
0108: assertEquals(false, PredicateUtils.nullPredicate().evaluate(
0109: cString));
0110: assertEquals(false, PredicateUtils.nullPredicate().evaluate(
0111: cInteger));
0112: }
0113:
0114: // notNullPredicate
0115: //------------------------------------------------------------------
0116:
0117: public void testIsNotNullPredicate() {
0118: assertNotNull(PredicateUtils.notNullPredicate());
0119: assertSame(PredicateUtils.notNullPredicate(), PredicateUtils
0120: .notNullPredicate());
0121: assertEquals(false, PredicateUtils.notNullPredicate().evaluate(
0122: null));
0123: assertEquals(true, PredicateUtils.notNullPredicate().evaluate(
0124: cObject));
0125: assertEquals(true, PredicateUtils.notNullPredicate().evaluate(
0126: cString));
0127: assertEquals(true, PredicateUtils.notNullPredicate().evaluate(
0128: cInteger));
0129: }
0130:
0131: // equalPredicate
0132: //------------------------------------------------------------------
0133:
0134: public void testEqualPredicate() {
0135: assertSame(PredicateUtils.nullPredicate(), PredicateUtils
0136: .equalPredicate(null));
0137: assertNotNull(PredicateUtils.equalPredicate(new Integer(6)));
0138: assertEquals(false, PredicateUtils.equalPredicate(
0139: new Integer(6)).evaluate(null));
0140: assertEquals(false, PredicateUtils.equalPredicate(
0141: new Integer(6)).evaluate(cObject));
0142: assertEquals(false, PredicateUtils.equalPredicate(
0143: new Integer(6)).evaluate(cString));
0144: assertEquals(true, PredicateUtils
0145: .equalPredicate(new Integer(6)).evaluate(cInteger));
0146: }
0147:
0148: // identityPredicate
0149: //------------------------------------------------------------------
0150:
0151: public void testIdentityPredicate() {
0152: assertSame(PredicateUtils.nullPredicate(), PredicateUtils
0153: .identityPredicate(null));
0154: assertNotNull(PredicateUtils.identityPredicate(new Integer(6)));
0155: assertEquals(false, PredicateUtils.identityPredicate(
0156: new Integer(6)).evaluate(null));
0157: assertEquals(false, PredicateUtils.identityPredicate(
0158: new Integer(6)).evaluate(cObject));
0159: assertEquals(false, PredicateUtils.identityPredicate(
0160: new Integer(6)).evaluate(cString));
0161: assertEquals(false, PredicateUtils.identityPredicate(
0162: new Integer(6)).evaluate(cInteger));
0163: assertEquals(true, PredicateUtils.identityPredicate(cInteger)
0164: .evaluate(cInteger));
0165: }
0166:
0167: // truePredicate
0168: //------------------------------------------------------------------
0169:
0170: public void testTruePredicate() {
0171: assertNotNull(PredicateUtils.truePredicate());
0172: assertSame(PredicateUtils.truePredicate(), PredicateUtils
0173: .truePredicate());
0174: assertEquals(true, PredicateUtils.truePredicate()
0175: .evaluate(null));
0176: assertEquals(true, PredicateUtils.truePredicate().evaluate(
0177: cObject));
0178: assertEquals(true, PredicateUtils.truePredicate().evaluate(
0179: cString));
0180: assertEquals(true, PredicateUtils.truePredicate().evaluate(
0181: cInteger));
0182: }
0183:
0184: // falsePredicate
0185: //------------------------------------------------------------------
0186:
0187: public void testFalsePredicate() {
0188: assertNotNull(PredicateUtils.falsePredicate());
0189: assertSame(PredicateUtils.falsePredicate(), PredicateUtils
0190: .falsePredicate());
0191: assertEquals(false, PredicateUtils.falsePredicate().evaluate(
0192: null));
0193: assertEquals(false, PredicateUtils.falsePredicate().evaluate(
0194: cObject));
0195: assertEquals(false, PredicateUtils.falsePredicate().evaluate(
0196: cString));
0197: assertEquals(false, PredicateUtils.falsePredicate().evaluate(
0198: cInteger));
0199: }
0200:
0201: // notPredicate
0202: //------------------------------------------------------------------
0203:
0204: public void testNotPredicate() {
0205: assertNotNull(PredicateUtils.notPredicate(PredicateUtils
0206: .truePredicate()));
0207: assertEquals(false, PredicateUtils.notPredicate(
0208: PredicateUtils.truePredicate()).evaluate(null));
0209: assertEquals(false, PredicateUtils.notPredicate(
0210: PredicateUtils.truePredicate()).evaluate(cObject));
0211: assertEquals(false, PredicateUtils.notPredicate(
0212: PredicateUtils.truePredicate()).evaluate(cString));
0213: assertEquals(false, PredicateUtils.notPredicate(
0214: PredicateUtils.truePredicate()).evaluate(cInteger));
0215: }
0216:
0217: public void testNotPredicateEx() {
0218: try {
0219: PredicateUtils.notPredicate(null);
0220: } catch (IllegalArgumentException ex) {
0221: return;
0222: }
0223: fail();
0224: }
0225:
0226: // andPredicate
0227: //------------------------------------------------------------------
0228:
0229: public void testAndPredicate() {
0230: assertEquals(true, PredicateUtils.andPredicate(
0231: PredicateUtils.truePredicate(),
0232: PredicateUtils.truePredicate()).evaluate(null));
0233: assertEquals(false, PredicateUtils.andPredicate(
0234: PredicateUtils.truePredicate(),
0235: PredicateUtils.falsePredicate()).evaluate(null));
0236: assertEquals(false, PredicateUtils.andPredicate(
0237: PredicateUtils.falsePredicate(),
0238: PredicateUtils.truePredicate()).evaluate(null));
0239: assertEquals(false, PredicateUtils.andPredicate(
0240: PredicateUtils.falsePredicate(),
0241: PredicateUtils.falsePredicate()).evaluate(null));
0242: }
0243:
0244: public void testAndPredicateEx() {
0245: try {
0246: PredicateUtils.andPredicate(null, null);
0247: } catch (IllegalArgumentException ex) {
0248: return;
0249: }
0250: fail();
0251: }
0252:
0253: // allPredicate
0254: //------------------------------------------------------------------
0255:
0256: public void testAllPredicate() {
0257: assertTrue(PredicateUtils.allPredicate(new Predicate[] {})
0258: .evaluate(null));
0259: assertEquals(true, PredicateUtils.allPredicate(
0260: new Predicate[] { PredicateUtils.truePredicate(),
0261: PredicateUtils.truePredicate(),
0262: PredicateUtils.truePredicate() })
0263: .evaluate(null));
0264: assertEquals(false, PredicateUtils.allPredicate(
0265: new Predicate[] { PredicateUtils.truePredicate(),
0266: PredicateUtils.falsePredicate(),
0267: PredicateUtils.truePredicate() })
0268: .evaluate(null));
0269: assertEquals(false, PredicateUtils.allPredicate(
0270: new Predicate[] { PredicateUtils.falsePredicate(),
0271: PredicateUtils.falsePredicate(),
0272: PredicateUtils.truePredicate() })
0273: .evaluate(null));
0274: assertEquals(false, PredicateUtils.allPredicate(
0275: new Predicate[] { PredicateUtils.falsePredicate(),
0276: PredicateUtils.falsePredicate(),
0277: PredicateUtils.falsePredicate() }).evaluate(
0278: null));
0279: Collection coll = new ArrayList();
0280: coll.add(PredicateUtils.truePredicate());
0281: coll.add(PredicateUtils.truePredicate());
0282: coll.add(PredicateUtils.truePredicate());
0283: assertEquals(true, PredicateUtils.allPredicate(coll).evaluate(
0284: null));
0285: coll.clear();
0286: coll.add(PredicateUtils.truePredicate());
0287: coll.add(PredicateUtils.falsePredicate());
0288: coll.add(PredicateUtils.truePredicate());
0289: assertEquals(false, PredicateUtils.allPredicate(coll).evaluate(
0290: null));
0291: coll.clear();
0292: coll.add(PredicateUtils.falsePredicate());
0293: coll.add(PredicateUtils.falsePredicate());
0294: coll.add(PredicateUtils.truePredicate());
0295: assertEquals(false, PredicateUtils.allPredicate(coll).evaluate(
0296: null));
0297: coll.clear();
0298: coll.add(PredicateUtils.falsePredicate());
0299: coll.add(PredicateUtils.falsePredicate());
0300: coll.add(PredicateUtils.falsePredicate());
0301: assertEquals(false, PredicateUtils.allPredicate(coll).evaluate(
0302: null));
0303: coll.clear();
0304: coll.add(PredicateUtils.falsePredicate());
0305: assertFalse(PredicateUtils.allPredicate(coll).evaluate(null));
0306: coll.clear();
0307: coll.add(PredicateUtils.truePredicate());
0308: assertTrue(PredicateUtils.allPredicate(coll).evaluate(null));
0309: coll.clear();
0310: assertTrue(PredicateUtils.allPredicate(coll).evaluate(null));
0311: }
0312:
0313: public void testAllPredicateEx1() {
0314: try {
0315: PredicateUtils.allPredicate((Predicate[]) null);
0316: } catch (IllegalArgumentException ex) {
0317: return;
0318: }
0319: fail();
0320: }
0321:
0322: public void testAllPredicateEx2() {
0323: try {
0324: PredicateUtils.allPredicate(new Predicate[] { null });
0325: } catch (IllegalArgumentException ex) {
0326: return;
0327: }
0328: fail();
0329: }
0330:
0331: public void testAllPredicateEx3() {
0332: try {
0333: PredicateUtils.allPredicate(new Predicate[] { null, null });
0334: } catch (IllegalArgumentException ex) {
0335: return;
0336: }
0337: fail();
0338: }
0339:
0340: public void testAllPredicateEx4() {
0341: try {
0342: PredicateUtils.allPredicate((Collection) null);
0343: } catch (IllegalArgumentException ex) {
0344: return;
0345: }
0346: fail();
0347: }
0348:
0349: public void testAllPredicateEx5() {
0350: PredicateUtils.allPredicate(Collections.EMPTY_LIST);
0351: }
0352:
0353: public void testAllPredicateEx6() {
0354: try {
0355: Collection coll = new ArrayList();
0356: coll.add(null);
0357: coll.add(null);
0358: PredicateUtils.allPredicate(coll);
0359: } catch (IllegalArgumentException ex) {
0360: return;
0361: }
0362: fail();
0363: }
0364:
0365: // orPredicate
0366: //------------------------------------------------------------------
0367:
0368: public void testOrPredicate() {
0369: assertEquals(true, PredicateUtils.orPredicate(
0370: PredicateUtils.truePredicate(),
0371: PredicateUtils.truePredicate()).evaluate(null));
0372: assertEquals(true, PredicateUtils.orPredicate(
0373: PredicateUtils.truePredicate(),
0374: PredicateUtils.falsePredicate()).evaluate(null));
0375: assertEquals(true, PredicateUtils.orPredicate(
0376: PredicateUtils.falsePredicate(),
0377: PredicateUtils.truePredicate()).evaluate(null));
0378: assertEquals(false, PredicateUtils.orPredicate(
0379: PredicateUtils.falsePredicate(),
0380: PredicateUtils.falsePredicate()).evaluate(null));
0381: }
0382:
0383: public void testOrPredicateEx() {
0384: try {
0385: PredicateUtils.orPredicate(null, null);
0386: } catch (IllegalArgumentException ex) {
0387: return;
0388: }
0389: fail();
0390: }
0391:
0392: // anyPredicate
0393: //------------------------------------------------------------------
0394:
0395: public void testAnyPredicate() {
0396: assertFalse(PredicateUtils.anyPredicate(new Predicate[] {})
0397: .evaluate(null));
0398: assertEquals(true, PredicateUtils.anyPredicate(
0399: new Predicate[] { PredicateUtils.truePredicate(),
0400: PredicateUtils.truePredicate(),
0401: PredicateUtils.truePredicate() })
0402: .evaluate(null));
0403: assertEquals(true, PredicateUtils.anyPredicate(
0404: new Predicate[] { PredicateUtils.truePredicate(),
0405: PredicateUtils.falsePredicate(),
0406: PredicateUtils.truePredicate() })
0407: .evaluate(null));
0408: assertEquals(true, PredicateUtils.anyPredicate(
0409: new Predicate[] { PredicateUtils.falsePredicate(),
0410: PredicateUtils.falsePredicate(),
0411: PredicateUtils.truePredicate() })
0412: .evaluate(null));
0413: assertEquals(false, PredicateUtils.anyPredicate(
0414: new Predicate[] { PredicateUtils.falsePredicate(),
0415: PredicateUtils.falsePredicate(),
0416: PredicateUtils.falsePredicate() }).evaluate(
0417: null));
0418: Collection coll = new ArrayList();
0419: coll.add(PredicateUtils.truePredicate());
0420: coll.add(PredicateUtils.truePredicate());
0421: coll.add(PredicateUtils.truePredicate());
0422: assertEquals(true, PredicateUtils.anyPredicate(coll).evaluate(
0423: null));
0424: coll.clear();
0425: coll.add(PredicateUtils.truePredicate());
0426: coll.add(PredicateUtils.falsePredicate());
0427: coll.add(PredicateUtils.truePredicate());
0428: assertEquals(true, PredicateUtils.anyPredicate(coll).evaluate(
0429: null));
0430: coll.clear();
0431: coll.add(PredicateUtils.falsePredicate());
0432: coll.add(PredicateUtils.falsePredicate());
0433: coll.add(PredicateUtils.truePredicate());
0434: assertEquals(true, PredicateUtils.anyPredicate(coll).evaluate(
0435: null));
0436: coll.clear();
0437: coll.add(PredicateUtils.falsePredicate());
0438: coll.add(PredicateUtils.falsePredicate());
0439: coll.add(PredicateUtils.falsePredicate());
0440: assertEquals(false, PredicateUtils.anyPredicate(coll).evaluate(
0441: null));
0442: coll.clear();
0443: coll.add(PredicateUtils.falsePredicate());
0444: assertFalse(PredicateUtils.anyPredicate(coll).evaluate(null));
0445: coll.clear();
0446: coll.add(PredicateUtils.truePredicate());
0447: assertTrue(PredicateUtils.anyPredicate(coll).evaluate(null));
0448: coll.clear();
0449: assertFalse(PredicateUtils.anyPredicate(coll).evaluate(null));
0450: }
0451:
0452: public void testAnyPredicateEx1() {
0453: try {
0454: PredicateUtils.anyPredicate((Predicate[]) null);
0455: } catch (IllegalArgumentException ex) {
0456: return;
0457: }
0458: fail();
0459: }
0460:
0461: public void testAnyPredicateEx2() {
0462: try {
0463: PredicateUtils.anyPredicate(new Predicate[] { null });
0464: } catch (IllegalArgumentException ex) {
0465: return;
0466: }
0467: fail();
0468: }
0469:
0470: public void testAnyPredicateEx3() {
0471: try {
0472: PredicateUtils.anyPredicate(new Predicate[] { null, null });
0473: } catch (IllegalArgumentException ex) {
0474: return;
0475: }
0476: fail();
0477: }
0478:
0479: public void testAnyPredicateEx4() {
0480: try {
0481: PredicateUtils.anyPredicate((Collection) null);
0482: } catch (IllegalArgumentException ex) {
0483: return;
0484: }
0485: fail();
0486: }
0487:
0488: public void testAnyPredicateEx5() {
0489: PredicateUtils.anyPredicate(Collections.EMPTY_LIST);
0490: }
0491:
0492: public void testAnyPredicateEx6() {
0493: try {
0494: Collection coll = new ArrayList();
0495: coll.add(null);
0496: coll.add(null);
0497: PredicateUtils.anyPredicate(coll);
0498: } catch (IllegalArgumentException ex) {
0499: return;
0500: }
0501: fail();
0502: }
0503:
0504: // eitherPredicate
0505: //------------------------------------------------------------------
0506:
0507: public void testEitherPredicate() {
0508: assertEquals(false, PredicateUtils.eitherPredicate(
0509: PredicateUtils.truePredicate(),
0510: PredicateUtils.truePredicate()).evaluate(null));
0511: assertEquals(true, PredicateUtils.eitherPredicate(
0512: PredicateUtils.truePredicate(),
0513: PredicateUtils.falsePredicate()).evaluate(null));
0514: assertEquals(true, PredicateUtils.eitherPredicate(
0515: PredicateUtils.falsePredicate(),
0516: PredicateUtils.truePredicate()).evaluate(null));
0517: assertEquals(false, PredicateUtils.eitherPredicate(
0518: PredicateUtils.falsePredicate(),
0519: PredicateUtils.falsePredicate()).evaluate(null));
0520: }
0521:
0522: public void testEitherPredicateEx() {
0523: try {
0524: PredicateUtils.eitherPredicate(null, null);
0525: } catch (IllegalArgumentException ex) {
0526: return;
0527: }
0528: fail();
0529: }
0530:
0531: // onePredicate
0532: //------------------------------------------------------------------
0533:
0534: public void testOnePredicate() {
0535: assertFalse(PredicateUtils.onePredicate(new Predicate[] {})
0536: .evaluate(null));
0537: assertEquals(false, PredicateUtils.onePredicate(
0538: new Predicate[] { PredicateUtils.truePredicate(),
0539: PredicateUtils.truePredicate(),
0540: PredicateUtils.truePredicate() })
0541: .evaluate(null));
0542: assertEquals(false, PredicateUtils.onePredicate(
0543: new Predicate[] { PredicateUtils.truePredicate(),
0544: PredicateUtils.falsePredicate(),
0545: PredicateUtils.truePredicate() })
0546: .evaluate(null));
0547: assertEquals(true, PredicateUtils.onePredicate(
0548: new Predicate[] { PredicateUtils.truePredicate(),
0549: PredicateUtils.falsePredicate(),
0550: PredicateUtils.falsePredicate() }).evaluate(
0551: null));
0552: assertEquals(true, PredicateUtils.onePredicate(
0553: new Predicate[] { PredicateUtils.falsePredicate(),
0554: PredicateUtils.truePredicate(),
0555: PredicateUtils.falsePredicate() }).evaluate(
0556: null));
0557: assertEquals(true, PredicateUtils.onePredicate(
0558: new Predicate[] { PredicateUtils.falsePredicate(),
0559: PredicateUtils.falsePredicate(),
0560: PredicateUtils.truePredicate() })
0561: .evaluate(null));
0562: assertEquals(false, PredicateUtils.onePredicate(
0563: new Predicate[] { PredicateUtils.falsePredicate(),
0564: PredicateUtils.falsePredicate(),
0565: PredicateUtils.falsePredicate() }).evaluate(
0566: null));
0567: Collection coll = new ArrayList();
0568: coll.add(PredicateUtils.truePredicate());
0569: coll.add(PredicateUtils.truePredicate());
0570: coll.add(PredicateUtils.truePredicate());
0571: assertEquals(false, PredicateUtils.onePredicate(coll).evaluate(
0572: null));
0573: coll.clear();
0574: coll.add(PredicateUtils.truePredicate());
0575: coll.add(PredicateUtils.falsePredicate());
0576: coll.add(PredicateUtils.truePredicate());
0577: assertEquals(false, PredicateUtils.onePredicate(coll).evaluate(
0578: null));
0579: coll.clear();
0580: coll.add(PredicateUtils.falsePredicate());
0581: coll.add(PredicateUtils.falsePredicate());
0582: coll.add(PredicateUtils.truePredicate());
0583: assertEquals(true, PredicateUtils.onePredicate(coll).evaluate(
0584: null));
0585: coll.clear();
0586: coll.add(PredicateUtils.falsePredicate());
0587: coll.add(PredicateUtils.falsePredicate());
0588: coll.add(PredicateUtils.falsePredicate());
0589: assertEquals(false, PredicateUtils.onePredicate(coll).evaluate(
0590: null));
0591: coll.clear();
0592: coll.add(PredicateUtils.falsePredicate());
0593: assertFalse(PredicateUtils.onePredicate(coll).evaluate(null));
0594: coll.clear();
0595: coll.add(PredicateUtils.truePredicate());
0596: assertTrue(PredicateUtils.onePredicate(coll).evaluate(null));
0597: coll.clear();
0598: assertFalse(PredicateUtils.onePredicate(coll).evaluate(null));
0599: }
0600:
0601: public void testOnePredicateEx1() {
0602: try {
0603: PredicateUtils.onePredicate((Predicate[]) null);
0604: } catch (IllegalArgumentException ex) {
0605: return;
0606: }
0607: fail();
0608: }
0609:
0610: public void testOnePredicateEx2() {
0611: try {
0612: PredicateUtils.onePredicate(new Predicate[] { null });
0613: } catch (IllegalArgumentException ex) {
0614: return;
0615: }
0616: fail();
0617: }
0618:
0619: public void testOnePredicateEx3() {
0620: try {
0621: PredicateUtils.onePredicate(new Predicate[] { null, null });
0622: } catch (IllegalArgumentException ex) {
0623: return;
0624: }
0625: fail();
0626: }
0627:
0628: public void testOnePredicateEx4() {
0629: try {
0630: PredicateUtils.onePredicate((Collection) null);
0631: } catch (IllegalArgumentException ex) {
0632: return;
0633: }
0634: fail();
0635: }
0636:
0637: public void testOnePredicateEx5() {
0638: PredicateUtils.onePredicate(Collections.EMPTY_LIST);
0639: }
0640:
0641: public void testOnePredicateEx6() {
0642: try {
0643: Collection coll = new ArrayList();
0644: coll.add(null);
0645: coll.add(null);
0646: PredicateUtils.onePredicate(coll);
0647: } catch (IllegalArgumentException ex) {
0648: return;
0649: }
0650: fail();
0651: }
0652:
0653: // neitherPredicate
0654: //------------------------------------------------------------------
0655:
0656: public void testNeitherPredicate() {
0657: assertEquals(false, PredicateUtils.neitherPredicate(
0658: PredicateUtils.truePredicate(),
0659: PredicateUtils.truePredicate()).evaluate(null));
0660: assertEquals(false, PredicateUtils.neitherPredicate(
0661: PredicateUtils.truePredicate(),
0662: PredicateUtils.falsePredicate()).evaluate(null));
0663: assertEquals(false, PredicateUtils.neitherPredicate(
0664: PredicateUtils.falsePredicate(),
0665: PredicateUtils.truePredicate()).evaluate(null));
0666: assertEquals(true, PredicateUtils.neitherPredicate(
0667: PredicateUtils.falsePredicate(),
0668: PredicateUtils.falsePredicate()).evaluate(null));
0669: }
0670:
0671: public void testNeitherPredicateEx() {
0672: try {
0673: PredicateUtils.neitherPredicate(null, null);
0674: } catch (IllegalArgumentException ex) {
0675: return;
0676: }
0677: fail();
0678: }
0679:
0680: // nonePredicate
0681: //------------------------------------------------------------------
0682:
0683: public void testNonePredicate() {
0684: assertTrue(PredicateUtils.nonePredicate(new Predicate[] {})
0685: .evaluate(null));
0686: assertEquals(false, PredicateUtils.nonePredicate(
0687: new Predicate[] { PredicateUtils.truePredicate(),
0688: PredicateUtils.truePredicate(),
0689: PredicateUtils.truePredicate() })
0690: .evaluate(null));
0691: assertEquals(false, PredicateUtils.nonePredicate(
0692: new Predicate[] { PredicateUtils.truePredicate(),
0693: PredicateUtils.falsePredicate(),
0694: PredicateUtils.truePredicate() })
0695: .evaluate(null));
0696: assertEquals(false, PredicateUtils.nonePredicate(
0697: new Predicate[] { PredicateUtils.falsePredicate(),
0698: PredicateUtils.falsePredicate(),
0699: PredicateUtils.truePredicate() })
0700: .evaluate(null));
0701: assertEquals(true, PredicateUtils.nonePredicate(
0702: new Predicate[] { PredicateUtils.falsePredicate(),
0703: PredicateUtils.falsePredicate(),
0704: PredicateUtils.falsePredicate() }).evaluate(
0705: null));
0706: Collection coll = new ArrayList();
0707: coll.add(PredicateUtils.truePredicate());
0708: coll.add(PredicateUtils.truePredicate());
0709: coll.add(PredicateUtils.truePredicate());
0710: assertEquals(false, PredicateUtils.nonePredicate(coll)
0711: .evaluate(null));
0712: coll.clear();
0713: coll.add(PredicateUtils.truePredicate());
0714: coll.add(PredicateUtils.falsePredicate());
0715: coll.add(PredicateUtils.truePredicate());
0716: assertEquals(false, PredicateUtils.nonePredicate(coll)
0717: .evaluate(null));
0718: coll.clear();
0719: coll.add(PredicateUtils.falsePredicate());
0720: coll.add(PredicateUtils.falsePredicate());
0721: coll.add(PredicateUtils.truePredicate());
0722: assertEquals(false, PredicateUtils.nonePredicate(coll)
0723: .evaluate(null));
0724: coll.clear();
0725: coll.add(PredicateUtils.falsePredicate());
0726: coll.add(PredicateUtils.falsePredicate());
0727: coll.add(PredicateUtils.falsePredicate());
0728: assertEquals(true, PredicateUtils.nonePredicate(coll).evaluate(
0729: null));
0730: coll.clear();
0731: coll.add(PredicateUtils.falsePredicate());
0732: assertTrue(PredicateUtils.nonePredicate(coll).evaluate(null));
0733: coll.clear();
0734: coll.add(PredicateUtils.truePredicate());
0735: assertFalse(PredicateUtils.nonePredicate(coll).evaluate(null));
0736: coll.clear();
0737: assertTrue(PredicateUtils.nonePredicate(coll).evaluate(null));
0738: }
0739:
0740: public void testNonePredicateEx1() {
0741: try {
0742: PredicateUtils.nonePredicate((Predicate[]) null);
0743: } catch (IllegalArgumentException ex) {
0744: return;
0745: }
0746: fail();
0747: }
0748:
0749: public void testNonePredicateEx2() {
0750: try {
0751: PredicateUtils.nonePredicate(new Predicate[] { null });
0752: } catch (IllegalArgumentException ex) {
0753: return;
0754: }
0755: fail();
0756: }
0757:
0758: public void testNonePredicateEx3() {
0759: try {
0760: PredicateUtils
0761: .nonePredicate(new Predicate[] { null, null });
0762: } catch (IllegalArgumentException ex) {
0763: return;
0764: }
0765: fail();
0766: }
0767:
0768: public void testNonePredicateEx4() {
0769: try {
0770: PredicateUtils.nonePredicate((Collection) null);
0771: } catch (IllegalArgumentException ex) {
0772: return;
0773: }
0774: fail();
0775: }
0776:
0777: public void testNonePredicateEx5() {
0778: PredicateUtils.nonePredicate(Collections.EMPTY_LIST);
0779: }
0780:
0781: public void testNonePredicateEx6() {
0782: try {
0783: Collection coll = new ArrayList();
0784: coll.add(null);
0785: coll.add(null);
0786: PredicateUtils.nonePredicate(coll);
0787: } catch (IllegalArgumentException ex) {
0788: return;
0789: }
0790: fail();
0791: }
0792:
0793: // instanceofPredicate
0794: //------------------------------------------------------------------
0795:
0796: public void testInstanceOfPredicate() {
0797: assertNotNull(PredicateUtils.instanceof Predicate(String.class));
0798: assertEquals(false, PredicateUtils.instanceof Predicate(
0799: String.class).evaluate(null));
0800: assertEquals(false, PredicateUtils.instanceof Predicate(
0801: String.class).evaluate(cObject));
0802: assertEquals(true, PredicateUtils.instanceof Predicate(
0803: String.class).evaluate(cString));
0804: assertEquals(false, PredicateUtils.instanceof Predicate(
0805: String.class).evaluate(cInteger));
0806: }
0807:
0808: // uniquePredicate
0809: //------------------------------------------------------------------
0810:
0811: public void testUniquePredicate() {
0812: Predicate p = PredicateUtils.uniquePredicate();
0813: assertEquals(true, p.evaluate(new Object()));
0814: assertEquals(true, p.evaluate(new Object()));
0815: assertEquals(true, p.evaluate(new Object()));
0816: assertEquals(true, p.evaluate(cString));
0817: assertEquals(false, p.evaluate(cString));
0818: assertEquals(false, p.evaluate(cString));
0819: }
0820:
0821: // asPredicate(Transformer)
0822: //------------------------------------------------------------------
0823:
0824: public void testAsPredicateTransformer() {
0825: assertEquals(false, PredicateUtils.asPredicate(
0826: TransformerUtils.nopTransformer()).evaluate(
0827: Boolean.FALSE));
0828: assertEquals(true, PredicateUtils.asPredicate(
0829: TransformerUtils.nopTransformer()).evaluate(
0830: Boolean.TRUE));
0831: }
0832:
0833: public void testAsPredicateTransformerEx1() {
0834: try {
0835: PredicateUtils.asPredicate(null);
0836: } catch (IllegalArgumentException ex) {
0837: return;
0838: }
0839: fail();
0840: }
0841:
0842: public void testAsPredicateTransformerEx2() {
0843: try {
0844: PredicateUtils.asPredicate(
0845: TransformerUtils.nopTransformer()).evaluate(null);
0846: } catch (FunctorException ex) {
0847: return;
0848: }
0849: fail();
0850: }
0851:
0852: // invokerPredicate
0853: //------------------------------------------------------------------
0854:
0855: public void testInvokerPredicate() {
0856: List list = new ArrayList();
0857: assertEquals(true, PredicateUtils.invokerPredicate("isEmpty")
0858: .evaluate(list));
0859: list.add(new Object());
0860: assertEquals(false, PredicateUtils.invokerPredicate("isEmpty")
0861: .evaluate(list));
0862: }
0863:
0864: public void testInvokerPredicateEx1() {
0865: try {
0866: PredicateUtils.invokerPredicate(null);
0867: } catch (IllegalArgumentException ex) {
0868: return;
0869: }
0870: fail();
0871: }
0872:
0873: public void testInvokerPredicateEx2() {
0874: try {
0875: PredicateUtils.invokerPredicate("isEmpty").evaluate(null);
0876: } catch (FunctorException ex) {
0877: return;
0878: }
0879: fail();
0880: }
0881:
0882: public void testInvokerPredicateEx3() {
0883: try {
0884: PredicateUtils.invokerPredicate("noSuchMethod").evaluate(
0885: new Object());
0886: } catch (FunctorException ex) {
0887: return;
0888: }
0889: fail();
0890: }
0891:
0892: // invokerPredicate2
0893: //------------------------------------------------------------------
0894:
0895: public void testInvokerPredicate2() {
0896: List list = new ArrayList();
0897: assertEquals(false, PredicateUtils.invokerPredicate("contains",
0898: new Class[] { Object.class }, new Object[] { cString })
0899: .evaluate(list));
0900: list.add(cString);
0901: assertEquals(true, PredicateUtils.invokerPredicate("contains",
0902: new Class[] { Object.class }, new Object[] { cString })
0903: .evaluate(list));
0904: }
0905:
0906: public void testInvokerPredicate2Ex1() {
0907: try {
0908: PredicateUtils.invokerPredicate(null, null, null);
0909: } catch (IllegalArgumentException ex) {
0910: return;
0911: }
0912: fail();
0913: }
0914:
0915: public void testInvokerPredicate2Ex2() {
0916: try {
0917: PredicateUtils.invokerPredicate("contains",
0918: new Class[] { Object.class },
0919: new Object[] { cString }).evaluate(null);
0920: } catch (FunctorException ex) {
0921: return;
0922: }
0923: fail();
0924: }
0925:
0926: public void testInvokerPredicate2Ex3() {
0927: try {
0928: PredicateUtils.invokerPredicate("noSuchMethod",
0929: new Class[] { Object.class },
0930: new Object[] { cString }).evaluate(new Object());
0931: } catch (FunctorException ex) {
0932: return;
0933: }
0934: fail();
0935: }
0936:
0937: // nullIsException
0938: //------------------------------------------------------------------
0939:
0940: public void testNullIsExceptionPredicate() {
0941: assertEquals(true, PredicateUtils.nullIsExceptionPredicate(
0942: PredicateUtils.truePredicate()).evaluate(new Object()));
0943: try {
0944: PredicateUtils.nullIsExceptionPredicate(
0945: PredicateUtils.truePredicate()).evaluate(null);
0946: } catch (FunctorException ex) {
0947: return;
0948: }
0949: fail();
0950: }
0951:
0952: public void testNullIsExceptionPredicateEx1() {
0953: try {
0954: PredicateUtils.nullIsExceptionPredicate(null);
0955: } catch (IllegalArgumentException ex) {
0956: return;
0957: }
0958: fail();
0959: }
0960:
0961: // nullIsTrue
0962: //------------------------------------------------------------------
0963:
0964: public void testNullIsTruePredicate() {
0965: assertEquals(true, PredicateUtils.nullIsTruePredicate(
0966: PredicateUtils.truePredicate()).evaluate(null));
0967: assertEquals(true, PredicateUtils.nullIsTruePredicate(
0968: PredicateUtils.truePredicate()).evaluate(new Object()));
0969: assertEquals(false, PredicateUtils.nullIsTruePredicate(
0970: PredicateUtils.falsePredicate()).evaluate(new Object()));
0971: }
0972:
0973: public void testNullIsTruePredicateEx1() {
0974: try {
0975: PredicateUtils.nullIsTruePredicate(null);
0976: } catch (IllegalArgumentException ex) {
0977: return;
0978: }
0979: fail();
0980: }
0981:
0982: // nullIsFalse
0983: //------------------------------------------------------------------
0984:
0985: public void testNullIsFalsePredicate() {
0986: assertEquals(false, PredicateUtils.nullIsFalsePredicate(
0987: PredicateUtils.truePredicate()).evaluate(null));
0988: assertEquals(true, PredicateUtils.nullIsFalsePredicate(
0989: PredicateUtils.truePredicate()).evaluate(new Object()));
0990: assertEquals(false, PredicateUtils.nullIsFalsePredicate(
0991: PredicateUtils.falsePredicate()).evaluate(new Object()));
0992: }
0993:
0994: public void testNullIsFalsePredicateEx1() {
0995: try {
0996: PredicateUtils.nullIsFalsePredicate(null);
0997: } catch (IllegalArgumentException ex) {
0998: return;
0999: }
1000: fail();
1001: }
1002:
1003: // transformed
1004: //------------------------------------------------------------------
1005:
1006: public void testTransformedPredicate() {
1007: assertEquals(true, PredicateUtils.transformedPredicate(
1008: TransformerUtils.nopTransformer(),
1009: PredicateUtils.truePredicate()).evaluate(new Object()));
1010:
1011: Map map = new HashMap();
1012: map.put(Boolean.TRUE, "Hello");
1013: Transformer t = TransformerUtils.mapTransformer(map);
1014: Predicate p = PredicateUtils.equalPredicate("Hello");
1015: assertEquals(false, PredicateUtils.transformedPredicate(t, p)
1016: .evaluate(null));
1017: assertEquals(true, PredicateUtils.transformedPredicate(t, p)
1018: .evaluate(Boolean.TRUE));
1019: try {
1020: PredicateUtils.transformedPredicate(null, null);
1021: fail();
1022: } catch (IllegalArgumentException ex) {
1023: }
1024: }
1025:
1026: }
|