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;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertNull;
22  import static org.junit.Assert.assertSame;
23  import static org.junit.Assert.assertTrue;
24  
25  import org.apache.commons.functor.BaseFunctorTest;
26  import org.apache.commons.functor.BinaryFunction;
27  import org.apache.commons.functor.BinaryPredicate;
28  import org.junit.Test;
29  
30  /**
31   * @version $Revision: 1541658 $ $Date: 2013-11-13 19:54:05 +0100 (Mi, 13 Nov 2013) $
32   */
33  public class TestRightIdentity extends BaseFunctorTest {
34  
35      // Functor Testing Framework
36      // ------------------------------------------------------------------------
37  
38      @Override
39      protected Object makeFunctor() {
40          return RightIdentity.FUNCTION;
41      }
42  
43      // Tests
44      // ------------------------------------------------------------------------
45  
46      @Test
47      public void testJavabeanConstructor() {
48          assertNotNull(new RightIdentity()); // Public constructor for JavaBean
49      }
50  
51      @Test
52      public void testEvaluate() throws Exception {
53          BinaryFunction<Object, Object, Object> f = RightIdentity.FUNCTION;
54          assertNull(f.evaluate(null,null));
55          assertNull(f.evaluate("xyzzy",null));
56          assertEquals("xyzzy",f.evaluate("abcdefg","xyzzy"));
57          assertEquals("xyzzy",f.evaluate(null,"xyzzy"));
58          assertEquals(Integer.valueOf(3),f.evaluate(null,Integer.valueOf(3)));
59          Object obj = Long.valueOf(12345L);
60          assertSame(obj,f.evaluate(null,obj));
61          assertSame(obj,f.evaluate(obj,obj));
62      }
63  
64      @Test(expected=NullPointerException.class)
65      public void testTest() throws Exception {
66          BinaryPredicate<Object, Boolean> p = RightIdentity.PREDICATE;
67          assertTrue(p.test(null,Boolean.TRUE));
68          assertTrue(!p.test(null,Boolean.FALSE));
69          p.test(null,null);
70      }
71  
72      @Test
73      public void testEquals() throws Exception {
74          BinaryFunction<Object, Object, Object> f = RightIdentity.FUNCTION;
75          assertEquals(f,f);
76          assertObjectsAreEqual(f,RightIdentity.function());
77          assertObjectsAreNotEqual(f,new Identity<Object>());
78          assertObjectsAreNotEqual(f,LeftIdentity.function());
79          assertObjectsAreNotEqual(f,Constant.TRUE);
80          assertObjectsAreNotEqual(f,Constant.of("abcde"));
81      }
82  
83      @Test
84      public void testConstant() throws Exception {
85          assertEquals(RightIdentity.function(),RightIdentity.function());
86      }
87  }