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.algorithm;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.util.Arrays;
24  import java.util.List;
25  import java.util.NoSuchElementException;
26  
27  import org.apache.commons.functor.BaseFunctorTest;
28  import org.apache.commons.functor.Predicate;
29  import org.apache.commons.functor.adapter.LeftBoundPredicate;
30  import org.apache.commons.functor.core.IsEqual;
31  import org.apache.commons.functor.generator.loop.IteratorToGeneratorAdapter;
32  import org.junit.Test;
33  
34  /**
35   * Tests {@link FindWithinGenerator} algorithm.
36   */
37  public class TestFindWithinGenerator extends BaseFunctorTest {
38  
39      @Override
40      protected Object makeFunctor() throws Exception {
41          return new FindWithinGenerator<Object>();
42      }
43  
44      @Test
45      public void testObjectEqualsWithIfNone() throws Exception {
46          Object obj = new FindWithinGenerator<Object>(1);
47          assertEquals("equals must be reflexive", obj, obj);
48          assertEquals("hashCode must be reflexive", obj.hashCode(), obj.hashCode());
49          assertTrue(!obj.equals(null)); // should be able to compare to null
50  
51          Object obj2 = new FindWithinGenerator<Object>(1);
52          if (obj.equals(obj2)) {
53              assertEquals("equals implies hash equals", obj.hashCode(), obj2.hashCode());
54              assertEquals("equals must be symmetric", obj2, obj);
55          } else {
56              assertTrue("equals must be symmetric", !obj2.equals(obj));
57          }
58      }
59  
60      @Test
61      public void testObjectEqualsWithNullDefault() throws Exception {
62          Object obj = new FindWithinGenerator<Object>(null);
63          assertEquals("equals must be reflexive", obj, obj);
64          assertEquals("hashCode must be reflexive", obj.hashCode(), obj.hashCode());
65          assertTrue(!obj.equals(null)); // should be able to compare to null
66  
67          Object obj2 = new FindWithinGenerator<Object>(null);
68          if (obj.equals(obj2)) {
69              assertEquals("equals implies hash equals", obj.hashCode(), obj2.hashCode());
70              assertEquals("equals must be symmetric", obj2, obj);
71          } else {
72              assertTrue("equals must be symmetric", !obj2.equals(obj));
73          }
74      }
75  
76      @Test
77      public void testEquals() {
78          FindWithinGenerator<Object> f = new FindWithinGenerator<Object>();
79          assertEquals(f, f);
80  
81          assertObjectsAreEqual(f, new FindWithinGenerator<Object>());
82          assertObjectsAreEqual(new FindWithinGenerator<Object>(Double.valueOf(0)), new FindWithinGenerator<Object>(
83              Double.valueOf(0)));
84          assertObjectsAreNotEqual(f, new FindWithinGenerator<Object>(Integer.valueOf(0)));
85      }
86  
87      @Test(expected = NoSuchElementException.class)
88      public void testDetect() {
89          assertEquals(Integer.valueOf(3), new FindWithinGenerator<Integer>().evaluate(
90              IteratorToGeneratorAdapter.adapt(numbers.iterator()), equalsThree));
91          new FindWithinGenerator<Integer>().evaluate(IteratorToGeneratorAdapter.adapt(numbers.iterator()),
92              equalsTwentyThree);
93      }
94  
95      @Test
96      public void testDetectIfNone() {
97          assertEquals(
98              Integer.valueOf(3),
99              new FindWithinGenerator<Integer>(Integer.valueOf(3)).evaluate(
100                 IteratorToGeneratorAdapter.adapt(numbers.iterator()), equalsThree));
101         assertEquals("Xyzzy", new FindWithinGenerator<String>("Xyzzy").evaluate(
102             IteratorToGeneratorAdapter.adapt(strings.iterator()), equalsXyZ));
103     }
104 
105     @Test
106     public void testInstance() {
107         assertNotNull("FindWithinGenerator instance must not be null", FindWithinGenerator.instance());
108     }
109 
110     // Attributes
111     // ------------------------------------------------------------------------
112 
113     private List<Integer> numbers = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
114     private List<String> strings = Arrays.asList("Zyx", "xxyZ");
115     private Predicate<Integer> equalsThree = LeftBoundPredicate.bind(IsEqual.instance(), Integer.valueOf(3));
116     private Predicate<Integer> equalsTwentyThree = LeftBoundPredicate.bind(IsEqual.instance(), Integer.valueOf(23));
117     private Predicate<String> equalsXyZ = LeftBoundPredicate.bind(IsEqual.instance(), "xyZ");
118 
119 }