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;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNull;
21  import static org.junit.Assert.assertSame;
22  import static org.junit.Assert.assertTrue;
23  
24  import org.apache.commons.functor.BaseFunctorTest;
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 TestConstant extends BaseFunctorTest {
32  
33      // Functor Testing Framework
34      // ------------------------------------------------------------------------
35  
36      protected Object makeFunctor() {
37          return new Constant<Object>("K");
38      }
39  
40      // Tests
41      // ------------------------------------------------------------------------
42  
43      @Test
44      public void testEvaluate() throws Exception {
45          Constant<Object> f = new Constant<Object>("xyzzy");
46          assertEquals("xyzzy",f.evaluate());
47          assertEquals("xyzzy",f.evaluate(null));
48          assertEquals("xyzzy",f.evaluate(null,null));
49          assertEquals("xyzzy",f.evaluate());
50          assertEquals("xyzzy",f.evaluate("foo"));
51          assertEquals("xyzzy",f.evaluate("foo",new Integer(2)));
52      }
53  
54      @Test
55      public void testEvaluateConstantNull() throws Exception {
56          Constant<Object> f = new Constant<Object>(null);
57          assertNull(f.evaluate());
58          assertNull(f.evaluate(null));
59          assertNull(f.evaluate(null,null));
60          assertNull(f.evaluate());
61          assertNull(f.evaluate("foo"));
62          assertNull(f.evaluate("foo",new Integer(2)));
63      }
64  
65      @Test
66      public void testConstantTrue() throws Exception {
67          Constant<Object> truePred = new Constant<Object>(true);
68          assertTrue(truePred.test());
69          assertTrue(truePred.test(null));
70          assertTrue(truePred.test(null,null));
71  
72          assertTrue(truePred.test());
73          assertTrue(truePred.test("foo"));
74          assertTrue(truePred.test("foo",new Integer(2)));
75      }
76  
77      @Test
78      public void testConstantFalse() throws Exception {
79          Constant<Object> falsePred = new Constant<Object>(false);
80          assertTrue(!falsePred.test());
81          assertTrue(!falsePred.test(null));
82          assertTrue(!falsePred.test(null,null));
83  
84          assertTrue(!falsePred.test());
85          assertTrue(!falsePred.test("foo"));
86          assertTrue(!falsePred.test("foo",new Integer(2)));
87      }
88  
89      @Test
90      public void testEquals() throws Exception {
91          Constant<Object> f = new Constant<Object>("xyzzy");
92          assertEquals(f,f);
93  
94          assertObjectsAreEqual(f,new Constant<Object>("xyzzy"));
95          assertObjectsAreNotEqual(f,new Constant<Object>("abcde"));
96          assertObjectsAreNotEqual(f,new Constant<Object>(null));
97      }
98  
99      @Test
100     public void testConstants() throws Exception {
101         assertEquals(Constant.predicate(true),Constant.TRUE);
102 
103         assertEquals(Constant.truePredicate(),Constant.TRUE);
104         assertSame(Constant.truePredicate(),Constant.TRUE);
105 
106         assertEquals(Constant.predicate(true),Constant.TRUE);
107         assertSame(Constant.predicate(true),Constant.TRUE);
108 
109         assertEquals(Constant.falsePredicate(),Constant.FALSE);
110         assertSame(Constant.falsePredicate(),Constant.FALSE);
111 
112         assertEquals(Constant.predicate(false),Constant.FALSE);
113         assertSame(Constant.predicate(false),Constant.FALSE);
114     }
115 
116 
117 }