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