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.assertTrue;
21  
22  import org.apache.commons.functor.BaseFunctorTest;
23  import org.apache.commons.functor.Predicate;
24  import org.apache.commons.functor.core.Constant;
25  import org.junit.Test;
26  
27  /**
28   * @version $Revision: 1171255 $ $Date: 2011-09-15 22:27:39 +0200 (Thu, 15 Sep 2011) $
29   * @author Rodney Waldhoff
30   */
31  public class TestAnd extends BaseFunctorTest {
32  
33      // Functor Testing Framework
34      // ------------------------------------------------------------------------
35  
36      protected Object makeFunctor() {
37          return new And(Constant.TRUE, Constant.TRUE);
38      }
39  
40      // Tests
41      // ------------------------------------------------------------------------
42  
43      @Test
44      public void testTrue() throws Exception {
45          assertTrue((new And()).test());
46          assertTrue((new And(Constant.TRUE)).test());
47          assertTrue((new And(Constant.TRUE,Constant.TRUE)).test());
48          assertTrue((new And(Constant.TRUE,Constant.TRUE,Constant.TRUE)).test());
49  
50          And p = new And(Constant.TRUE);
51          assertTrue(p.test());
52          for (int i=0;i<10;i++) {
53              p.and(Constant.TRUE);
54              assertTrue(p.test());
55          }
56  
57          And q = new And(Constant.TRUE);
58          assertTrue(q.test());
59          for (int i=0;i<10;i++) {
60              q.and(Constant.TRUE);
61              assertTrue(q.test());
62          }
63  
64          And r = new And(p,q);
65          assertTrue(r.test());
66      }
67  
68      @Test
69      public void testFalse() throws Exception {
70          assertTrue(!(new And(Constant.FALSE)).test());
71          assertTrue(!(new And(Constant.TRUE,Constant.FALSE)).test());
72          assertTrue(!(new And(Constant.TRUE,Constant.TRUE,Constant.FALSE)).test());
73  
74          And p = new And(Constant.FALSE);
75          assertTrue(!p.test());
76          for (int i=0;i<10;i++) {
77              p.and(Constant.FALSE);
78              assertTrue(!p.test());
79          }
80  
81          And q = new And(Constant.TRUE);
82          assertTrue(q.test());
83          for (int i=0;i<10;i++) {
84              q.and(Constant.TRUE);
85              assertTrue(q.test());
86          }
87  
88          And r = new And(p,q);
89          assertTrue(!r.test());
90      }
91  
92      @Test
93      public void testDuplicateAdd() throws Exception {
94          Predicate p = Constant.TRUE;
95          And q = new And(p,p);
96          assertTrue(q.test());
97          for (int i=0;i<10;i++) {
98              q.and(p);
99              assertTrue(q.test());
100         }
101     }
102 
103     @Test
104     public void testEquals() throws Exception {
105         And p = new And();
106         assertEquals(p,p);
107         And q = new And();
108         assertObjectsAreEqual(p,q);
109 
110         for (int i=0;i<3;i++) {
111             p.and(Constant.TRUE);
112             assertObjectsAreNotEqual(p,q);
113             q.and(Constant.truePredicate());
114             assertObjectsAreEqual(p,q);
115             p.and(new And(Constant.TRUE,Constant.FALSE));
116             assertObjectsAreNotEqual(p,q);
117             q.and(new And(Constant.TRUE,Constant.FALSE));
118             assertObjectsAreEqual(p,q);
119         }
120 
121         assertObjectsAreNotEqual(p,Constant.TRUE);
122     }
123 
124 }