View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.beanutils;
19  
20  import junit.framework.TestCase;
21  
22  import org.apache.commons.collections.functors.EqualPredicate;
23  import org.apache.commons.collections.functors.InstanceofPredicate;
24  import org.apache.commons.collections.functors.NotPredicate;
25  import org.apache.commons.collections.functors.NullPredicate;
26  
27  /**
28   * @version $Id$
29   */
30  public class BeanPredicateTestCase extends TestCase {
31  
32      public BeanPredicateTestCase(final String name) {
33          super(name);
34      }
35  
36      public void testEqual() {
37          final BeanPredicate predicate =
38              new BeanPredicate("stringProperty",new EqualPredicate("foo"));
39          assertTrue(predicate.evaluate(new TestBean("foo")));
40          assertTrue(!predicate.evaluate(new TestBean("bar")));
41      }
42  
43      public void testNotEqual() {
44          final BeanPredicate predicate =
45              new BeanPredicate("stringProperty",new NotPredicate( new EqualPredicate("foo")));
46          assertTrue(!predicate.evaluate(new TestBean("foo")));
47          assertTrue(predicate.evaluate(new TestBean("bar")));
48      }
49  
50      public void testInstanceOf() {
51          final BeanPredicate predicate =
52              new BeanPredicate("stringProperty",new InstanceofPredicate( String.class ));
53          assertTrue(predicate.evaluate(new TestBean("foo")));
54          assertTrue(predicate.evaluate(new TestBean("bar")));
55      }
56  
57      public void testNull() {
58          final BeanPredicate predicate =
59              new BeanPredicate("stringProperty", NullPredicate.INSTANCE);
60          final String nullString = null;
61          assertTrue(predicate.evaluate(new TestBean(nullString)));
62          assertTrue(!predicate.evaluate(new TestBean("bar")));
63      }
64  
65  }