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  package org.apache.commons.functor.core.composite;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertTrue;
22  
23  import org.apache.commons.functor.BaseFunctorTest;
24  import org.apache.commons.functor.core.Constant;
25  import org.junit.Test;
26  
27  /**
28   * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $
29   */
30  public class TestCompositePredicate extends BaseFunctorTest {
31  
32      // Functor Testing Framework
33      // ------------------------------------------------------------------------
34  
35      @Override
36      protected Object makeFunctor() {
37          return Composite.predicate(Constant.TRUE);
38      }
39  
40      // Tests
41      // ------------------------------------------------------------------------
42  
43      @Test
44      public void testTest() throws Exception {
45          assertTrue(Composite.predicate(Constant.TRUE).test(null));
46          assertTrue(Composite.predicate(Constant.TRUE, Constant.of(3)).test("xyzzy"));
47          assertFalse(Composite.predicate(Constant.FALSE, Constant.of(4)).test("xyzzy"));
48      }
49  
50      @Test(expected = NullPointerException.class)
51      public void testNullNotAllowed() throws Exception {
52          new CompositePredicate<Object>(null);
53      }
54  
55      @Test(expected = NullPointerException.class)
56      public void testNullNotAllowed2() throws Exception {
57          Composite.function(Constant.TRUE, null);
58      }
59  
60      @Test
61      public void testOf() throws Exception {
62          CompositePredicate<Object> f = new CompositePredicate<Object>(Constant.TRUE);
63          assertTrue(f.test(null));
64          for (int i=0;i<10;i++) {
65              f = f.of(Constant.FALSE);
66              assertTrue(f.test(null));
67          }
68      }
69  
70      @Test
71      public void testEquals() throws Exception {
72          CompositePredicate<Object> f = new CompositePredicate<Object>(Constant.TRUE);
73          assertEquals(f,f);
74          CompositePredicate<Object> g = new CompositePredicate<Object>(Constant.TRUE);
75          assertObjectsAreEqual(f,g);
76  
77          for (int i=0;i<3;i++) {
78              f = f.of(Constant.of("x"));
79              assertObjectsAreNotEqual(f,g);
80              g = g.of(Constant.of("x"));
81              assertObjectsAreEqual(f,g);
82              f = f.of(Constant.of("y")).of(Constant.of("z"));
83              assertObjectsAreNotEqual(f,g);
84              g = g.of(Constant.of("y")).of(Constant.of("z"));
85              assertObjectsAreEqual(f,g);
86          }
87  
88          assertObjectsAreNotEqual(f,Constant.FALSE);
89          assertTrue(!f.equals(null));
90      }
91  
92  }