📄 testpredicateutils.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import junit.framework.Test;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* Tests the org.apache.commons.collections.PredicateUtils class.
*
* @since Commons Collections 3.0
* @version $Revision: 646780 $ $Date: 2008-04-10 13:48:07 +0100 (Thu, 10 Apr 2008) $
*
* @author Stephen Colebourne
* @author Matt Benson
*/
public class TestPredicateUtils extends junit.framework.TestCase {
private static final Object cObject = new Object();
private static final Object cString = "Hello";
private static final Object cInteger = new Integer(6);
/**
* Construct
*/
public TestPredicateUtils(String name) {
super(name);
}
/**
* Main.
* @param args
*/
public static void main(String[] args) {
TestRunner.run(suite());
}
/**
* Return class as a test suite.
*/
public static Test suite() {
return new TestSuite(TestPredicateUtils.class);
}
/**
* Set up instance variables required by this test case.
*/
public void setUp() {
}
/**
* Tear down instance variables required by this test case.
*/
public void tearDown() {
}
// exceptionPredicate
//------------------------------------------------------------------
public void testExceptionPredicate() {
assertNotNull(PredicateUtils.exceptionPredicate());
assertSame(PredicateUtils.exceptionPredicate(), PredicateUtils.exceptionPredicate());
try {
PredicateUtils.exceptionPredicate().evaluate(null);
} catch (FunctorException ex) {
try {
PredicateUtils.exceptionPredicate().evaluate(cString);
} catch (FunctorException ex2) {
return;
}
}
fail();
}
// nullPredicate
//------------------------------------------------------------------
public void testNullPredicate() {
assertNotNull(PredicateUtils.nullPredicate());
assertSame(PredicateUtils.nullPredicate(), PredicateUtils.nullPredicate());
assertEquals(true, PredicateUtils.nullPredicate().evaluate(null));
assertEquals(false, PredicateUtils.nullPredicate().evaluate(cObject));
assertEquals(false, PredicateUtils.nullPredicate().evaluate(cString));
assertEquals(false, PredicateUtils.nullPredicate().evaluate(cInteger));
}
// notNullPredicate
//------------------------------------------------------------------
public void testIsNotNullPredicate() {
assertNotNull(PredicateUtils.notNullPredicate());
assertSame(PredicateUtils.notNullPredicate(), PredicateUtils.notNullPredicate());
assertEquals(false, PredicateUtils.notNullPredicate().evaluate(null));
assertEquals(true, PredicateUtils.notNullPredicate().evaluate(cObject));
assertEquals(true, PredicateUtils.notNullPredicate().evaluate(cString));
assertEquals(true, PredicateUtils.notNullPredicate().evaluate(cInteger));
}
// equalPredicate
//------------------------------------------------------------------
public void testEqualPredicate() {
assertSame(PredicateUtils.nullPredicate(), PredicateUtils.equalPredicate(null));
assertNotNull(PredicateUtils.equalPredicate(new Integer(6)));
assertEquals(false, PredicateUtils.equalPredicate(new Integer(6)).evaluate(null));
assertEquals(false, PredicateUtils.equalPredicate(new Integer(6)).evaluate(cObject));
assertEquals(false, PredicateUtils.equalPredicate(new Integer(6)).evaluate(cString));
assertEquals(true, PredicateUtils.equalPredicate(new Integer(6)).evaluate(cInteger));
}
// identityPredicate
//------------------------------------------------------------------
public void testIdentityPredicate() {
assertSame(PredicateUtils.nullPredicate(), PredicateUtils.identityPredicate(null));
assertNotNull(PredicateUtils.identityPredicate(new Integer(6)));
assertEquals(false, PredicateUtils.identityPredicate(new Integer(6)).evaluate(null));
assertEquals(false, PredicateUtils.identityPredicate(new Integer(6)).evaluate(cObject));
assertEquals(false, PredicateUtils.identityPredicate(new Integer(6)).evaluate(cString));
assertEquals(false, PredicateUtils.identityPredicate(new Integer(6)).evaluate(cInteger));
assertEquals(true, PredicateUtils.identityPredicate(cInteger).evaluate(cInteger));
}
// truePredicate
//------------------------------------------------------------------
public void testTruePredicate() {
assertNotNull(PredicateUtils.truePredicate());
assertSame(PredicateUtils.truePredicate(), PredicateUtils.truePredicate());
assertEquals(true, PredicateUtils.truePredicate().evaluate(null));
assertEquals(true, PredicateUtils.truePredicate().evaluate(cObject));
assertEquals(true, PredicateUtils.truePredicate().evaluate(cString));
assertEquals(true, PredicateUtils.truePredicate().evaluate(cInteger));
}
// falsePredicate
//------------------------------------------------------------------
public void testFalsePredicate() {
assertNotNull(PredicateUtils.falsePredicate());
assertSame(PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate());
assertEquals(false, PredicateUtils.falsePredicate().evaluate(null));
assertEquals(false, PredicateUtils.falsePredicate().evaluate(cObject));
assertEquals(false, PredicateUtils.falsePredicate().evaluate(cString));
assertEquals(false, PredicateUtils.falsePredicate().evaluate(cInteger));
}
// notPredicate
//------------------------------------------------------------------
public void testNotPredicate() {
assertNotNull(PredicateUtils.notPredicate(PredicateUtils.truePredicate()));
assertEquals(false, PredicateUtils.notPredicate(PredicateUtils.truePredicate()).evaluate(null));
assertEquals(false, PredicateUtils.notPredicate(PredicateUtils.truePredicate()).evaluate(cObject));
assertEquals(false, PredicateUtils.notPredicate(PredicateUtils.truePredicate()).evaluate(cString));
assertEquals(false, PredicateUtils.notPredicate(PredicateUtils.truePredicate()).evaluate(cInteger));
}
public void testNotPredicateEx() {
try {
PredicateUtils.notPredicate(null);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
// andPredicate
//------------------------------------------------------------------
public void testAndPredicate() {
assertEquals(true, PredicateUtils.andPredicate(PredicateUtils.truePredicate(), PredicateUtils.truePredicate()).evaluate(null));
assertEquals(false, PredicateUtils.andPredicate(PredicateUtils.truePredicate(), PredicateUtils.falsePredicate()).evaluate(null));
assertEquals(false, PredicateUtils.andPredicate(PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()).evaluate(null));
assertEquals(false, PredicateUtils.andPredicate(PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate()).evaluate(null));
}
public void testAndPredicateEx() {
try {
PredicateUtils.andPredicate(null, null);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
// allPredicate
//------------------------------------------------------------------
public void testAllPredicate() {
assertTrue(PredicateUtils.allPredicate(
new Predicate[] {}).evaluate(null));
assertEquals(true, PredicateUtils.allPredicate(new Predicate[] {
PredicateUtils.truePredicate(), PredicateUtils.truePredicate(), PredicateUtils.truePredicate()}).evaluate(null));
assertEquals(false, PredicateUtils.allPredicate(new Predicate[] {
PredicateUtils.truePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()}).evaluate(null));
assertEquals(false, PredicateUtils.allPredicate(new Predicate[] {
PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()}).evaluate(null));
assertEquals(false, PredicateUtils.allPredicate(new Predicate[] {
PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate()}).evaluate(null));
Collection coll = new ArrayList();
coll.add(PredicateUtils.truePredicate());
coll.add(PredicateUtils.truePredicate());
coll.add(PredicateUtils.truePredicate());
assertEquals(true, PredicateUtils.allPredicate(coll).evaluate(null));
coll.clear();
coll.add(PredicateUtils.truePredicate());
coll.add(PredicateUtils.falsePredicate());
coll.add(PredicateUtils.truePredicate());
assertEquals(false, PredicateUtils.allPredicate(coll).evaluate(null));
coll.clear();
coll.add(PredicateUtils.falsePredicate());
coll.add(PredicateUtils.falsePredicate());
coll.add(PredicateUtils.truePredicate());
assertEquals(false, PredicateUtils.allPredicate(coll).evaluate(null));
coll.clear();
coll.add(PredicateUtils.falsePredicate());
coll.add(PredicateUtils.falsePredicate());
coll.add(PredicateUtils.falsePredicate());
assertEquals(false, PredicateUtils.allPredicate(coll).evaluate(null));
coll.clear();
coll.add(PredicateUtils.falsePredicate());
assertFalse(PredicateUtils.allPredicate(coll).evaluate(null));
coll.clear();
coll.add(PredicateUtils.truePredicate());
assertTrue(PredicateUtils.allPredicate(coll).evaluate(null));
coll.clear();
assertTrue(PredicateUtils.allPredicate(coll).evaluate(null));
}
public void testAllPredicateEx1() {
try {
PredicateUtils.allPredicate((Predicate[]) null);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
public void testAllPredicateEx2() {
try {
PredicateUtils.allPredicate(new Predicate[] {null});
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
public void testAllPredicateEx3() {
try {
PredicateUtils.allPredicate(new Predicate[] {null, null});
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
public void testAllPredicateEx4() {
try {
PredicateUtils.allPredicate((Collection) null);
} catch (IllegalArgumentException ex) {
return;
}
fail();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -