1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
28
29
30
31 public class TestIsWithinRange extends BaseFunctorTest {
32
33
34
35
36 protected Object makeFunctor() {
37 return new IsWithinRange<Integer>(new Integer(5), new Integer(10));
38 }
39
40
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
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
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 }