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.comparator;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertTrue;
21  import static org.junit.Assert.fail;
22  
23  import org.apache.commons.functor.BaseFunctorTest;
24  import org.junit.Test;
25  
26  /**
27   * @version $Revision: 1171255 $ $Date: 2011-09-15 22:27:39 +0200 (Thu, 15 Sep 2011) $
28   * @author Rodney Waldhoff
29   * @author Jason Horman
30   */
31  public class TestIsWithinRange extends BaseFunctorTest {
32  
33      // Functor Testing Framework
34      // ------------------------------------------------------------------------
35  
36      protected Object makeFunctor() {
37          return new IsWithinRange<Integer>(new Integer(5), new Integer(10));
38      }
39  
40      // Tests
41      // ------------------------------------------------------------------------
42  
43      @Test
44      public void testTest() throws Exception {
45          IsWithinRange<Integer> p = new IsWithinRange<Integer>(new Integer(5), new Integer(10));
46          assertTrue(p.test(new Integer(5)));
47          assertTrue(p.test(new Integer(6)));
48          assertTrue(p.test(new Integer(7)));
49          assertTrue(p.test(new Integer(8)));
50          assertTrue(p.test(new Integer(9)));
51          assertTrue(p.test(new Integer(10)));
52  
53          assertTrue(!p.test(new Integer(4)));
54          assertTrue(!p.test(new Integer(11)));
55  
56      }
57  
58      @Test
59      public void testInvalidRange() {
60          try {
61              new IsWithinRange<Integer>(new Integer(5), new Integer(4));
62              fail("should have thrown IllegalArgumentException");
63          } catch (IllegalArgumentException e) {
64              // good
65          } catch (Exception e) {
66              fail("should have thrown IllegalArgumentException, not " + e);
67          }
68  
69          try {
70              new IsWithinRange<Integer>(new Integer(5), null);
71              fail("should have thrown IllegalArgumentException");
72          } catch (IllegalArgumentException e) {
73              // good
74          } catch (Exception e) {
75              fail("should have thrown IllegalArgumentException, not " + e);
76          }
77      }
78  
79      @Test
80      public void testEquals() throws Exception {
81          IsWithinRange<Integer> p1 = new IsWithinRange<Integer>(new Integer(5), new Integer(10));
82          IsWithinRange<Integer> p2 = new IsWithinRange<Integer>(new Integer(5), new Integer(10));
83          assertEquals(p1, p2);
84          p2 = new IsWithinRange<Integer>(new Integer(5), new Integer(11));
85          assertTrue(!p1.equals(p2));
86          p2 = new IsWithinRange<Integer>(new Integer(6), new Integer(10));
87          assertTrue(!p1.equals(p2));
88      }
89  
90      @Test
91      public void testFactory() throws Exception {
92          IsWithinRange<Integer> p = IsWithinRange.instance(new Integer(5), new Integer(10));
93          assertTrue(p.test(new Integer(5)));
94          assertTrue(p.test(new Integer(6)));
95          assertTrue(p.test(new Integer(7)));
96          assertTrue(p.test(new Integer(8)));
97          assertTrue(p.test(new Integer(9)));
98          assertTrue(p.test(new Integer(10)));
99  
100         assertTrue(!p.test(new Integer(4)));
101         assertTrue(!p.test(new Integer(11)));
102 
103     }
104 }