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.assertTrue;
21  
22  import org.apache.commons.functor.BaseFunctorTest;
23  import org.apache.commons.functor.NullaryFunction;
24  import org.apache.commons.functor.Function;
25  import org.junit.Test;
26  
27  /**
28   * Tests for TransformedNullaryFunction.
29   * 
30   * @version $Revision: $ $Date: $
31   */
32  public class TestTransformedNullaryFunction extends BaseFunctorTest {
33  
34      private static class One implements NullaryFunction<Integer> {
35          public Integer evaluate() {
36              return Integer.valueOf(1);
37          }
38  
39          @Override
40          public boolean equals(Object obj) {
41              return obj == this || obj != null && obj instanceof One;
42          }
43  
44          @Override
45          public int hashCode() {
46              return "One".hashCode();
47          }
48      };
49  
50      private static class AddOne implements Function<Integer, Integer> {
51          public Integer evaluate(Integer obj) {
52              return obj + 1;
53          }
54  
55          @Override
56          public boolean equals(Object obj) {
57              return obj == this || obj != null && obj instanceof AddOne;
58          }
59  
60          @Override
61          public int hashCode() {
62              return "AddOne".hashCode();
63          }
64      };
65  
66      private One one = new One();
67      private AddOne addOne = new AddOne();
68  
69      @Override
70      protected Object makeFunctor() throws Exception {
71          return new TransformedNullaryFunction<Integer>(one, addOne);
72      }
73  
74      @Test
75      public void testRun() {
76          TransformedNullaryFunction<Integer> p = new TransformedNullaryFunction<Integer>(one, addOne);
77          assertEquals(Integer.valueOf(2), p.evaluate());
78      }
79  
80      @Test
81      public void testEquals() {
82          TransformedNullaryFunction<Integer> t = new TransformedNullaryFunction<Integer>(one, addOne);
83          NullaryFunction<Integer> f = new NullaryFunction<Integer>() {
84              public Integer evaluate() {
85                  return Integer.valueOf(2);
86              }
87          };
88          Function<Integer, Integer> p = new Function<Integer, Integer>() {
89              public Integer evaluate(Integer obj) {
90                  return obj + 2;
91              }
92          };
93          assertEquals(t, t);
94          assertObjectsAreEqual(t, new TransformedNullaryFunction<Integer>(one, addOne));
95          assertObjectsAreNotEqual(t, new TransformedNullaryFunction<Integer>(f, addOne));
96          assertObjectsAreNotEqual(t, new TransformedNullaryFunction<Integer>(one, p));
97          assertTrue(!t.equals(null));
98      }
99  
100 }