KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > NumberRangeTest


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang;
17
18 import junit.framework.Test;
19 import junit.framework.TestCase;
20 import junit.framework.TestSuite;
21
22 /**
23  * Test cases for the {@link NumberRange} class.
24  *
25  * @author <a HREF="mailto:chrise@esha.com">Christopher Elkins</a>
26  * @author <a HREF="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
27  * @author Tim O'Brien
28  * @version $Revision: 161244 $ $Date: 2005-04-14 02:16:36 -0400 (Thu, 14 Apr 2005) $
29  */

30
31 public final class NumberRangeTest extends TestCase {
32
33
34     private NumberRange tenToTwenty;
35     private NumberRange fifteenToTwentyFive;
36     private NumberRange fiveToNine;
37     private Number JavaDoc five;
38     private Number JavaDoc nine;
39     private Number JavaDoc ten;
40     private Number JavaDoc fifteen;
41     private Number JavaDoc twenty;
42     private Number JavaDoc twentyFive;
43
44     public NumberRangeTest(String JavaDoc name) {
45         super(name);
46     }
47
48     public void setUp() {
49         five = new Integer JavaDoc(5);
50         nine = new Double JavaDoc(9.0);
51         ten = new Integer JavaDoc(10);
52         fifteen = new Integer JavaDoc(15);
53         twenty = new Integer JavaDoc(20);
54         twentyFive = new Integer JavaDoc(25);
55
56         tenToTwenty = new NumberRange(ten, twenty);
57         fifteenToTwentyFive = new NumberRange( fifteen, twentyFive);
58         fiveToNine = new NumberRange( five, nine );
59     }
60
61
62     public static Test suite() {
63         TestSuite suite = new TestSuite(NumberRangeTest.class);
64         suite.setName("NumberRange Tests");
65         return suite;
66     }
67
68     public void testMaxMin() {
69         boolean expected = true;
70         boolean result = tenToTwenty.getMaximum().equals(twenty);
71         assertEquals(expected, result);
72
73         expected = true;
74         result = tenToTwenty.getMinimum().equals(ten);
75         assertEquals(expected, result);
76     }
77
78     public void testEquals() {
79         boolean expected = false;
80         boolean result = tenToTwenty.equals(new NumberRange(five, ten));
81         assertEquals(expected, result);
82
83         expected = true;
84         result = tenToTwenty.equals(new NumberRange(ten, twenty));
85         assertEquals(expected, result);
86
87         expected = false;
88         result = tenToTwenty.equals(new NumberRange(ten, fifteen));
89         assertEquals(expected, result);
90
91         expected = false;
92         result = tenToTwenty.equals(new NumberRange(fifteen, twenty));
93         assertEquals(expected, result);
94     }
95     
96     public void testEqualsWithOtherObject() {
97         assertEquals( "A NumberRange should not equals a String object", false, fiveToNine.equals("TEST"));
98     }
99
100     public void testEqualsWithSameReference() {
101         assertEquals( "A NumberRange should equal itself", true, fiveToNine.equals(fiveToNine));
102     }
103
104     public void testEqualsNull() {
105         assertEquals( "A NumberRange should not equal null", false, fiveToNine.equals(null));
106     }
107
108     public void testHashCode() {
109         NumberRange nr = new NumberRange( new Integer JavaDoc(5), new Double JavaDoc(9.0));
110         assertEquals( "The hashCode of 5-9 should equals the hashcode of another NumberRange of the same min/max",
111                                 fiveToNine.hashCode(), nr.hashCode());
112         assertTrue( "The hashCode of 10-20 should not equal the hashCode of 5-9",
113                             fiveToNine.hashCode() != tenToTwenty.hashCode());
114     }
115
116     public void testIncludesNumber() {
117         boolean expected = false;
118         boolean result = tenToTwenty.includesNumber(five);
119         assertEquals(expected, result);
120
121         expected = true;
122         result = tenToTwenty.includesNumber(ten);
123         assertEquals(expected, result);
124
125         expected = true;
126         result = tenToTwenty.includesNumber(fifteen);
127         assertEquals(expected, result);
128
129         expected = true;
130         result = tenToTwenty.includesNumber(twenty);
131         assertEquals(expected, result);
132
133         expected = false;
134         result = tenToTwenty.includesNumber(twentyFive);
135         assertEquals(expected, result);
136     }
137
138     public void testIncludesNumberNull() {
139         boolean result = tenToTwenty.includesNumber(null);
140         assertEquals("Includes number should return false for null values", false, result);
141     }
142
143     public void testIncludesRange() {
144         boolean expected = false;
145         boolean result = tenToTwenty.includesRange(new NumberRange(five, ten));
146         assertEquals(expected, result);
147
148         expected = false;
149         result = tenToTwenty.includesRange(new NumberRange(five, fifteen));
150         assertEquals(expected, result);
151
152         expected = true;
153         result = tenToTwenty.includesRange(new NumberRange(ten, fifteen));
154         assertEquals(expected, result);
155
156         expected = true;
157         result = tenToTwenty.includesRange(new NumberRange(ten, twenty));
158         assertEquals(expected, result);
159
160         expected = true;
161         result = tenToTwenty.includesRange(new NumberRange(fifteen, twenty));
162         assertEquals(expected, result);
163
164         expected = false;
165         result =
166             tenToTwenty.includesRange(new NumberRange(fifteen, twentyFive));
167         assertEquals(expected, result);
168
169         expected = false;
170         result =
171             tenToTwenty.includesRange(new NumberRange(twenty, twentyFive));
172         assertEquals(expected, result);
173     }
174
175     public void testIncludesRangeNull() {
176         boolean result = tenToTwenty.includesRange(null);
177         assertEquals("Includes range should return false for null values", false, result);
178     }
179
180     public void testConstructor() {
181         NumberRange nr = new NumberRange( new Double JavaDoc(2.0));
182         assertEquals("Unexpected min on NumberRange", 2.0, nr.getMinimum().doubleValue(), Double.MIN_VALUE);
183         assertEquals("Unexpected max on NumberRange", 2.0, nr.getMaximum().doubleValue(), Double.MIN_VALUE);
184     }
185
186     public void testConstructorNullParameters() {
187         try {
188             NumberRange nr = new NumberRange(null);
189             fail("NumberRange(null) did not throw an exception.");
190         } catch (Exception JavaDoc e) {
191             assertTrue( "NumberRange(null)", e instanceof NullPointerException JavaDoc);
192         }
193
194         try {
195             NumberRange nr = new NumberRange(five, null);
196             fail("NumberRange(five, null) did not throw an exception.");
197         } catch (Exception JavaDoc e) {
198             assertTrue("NumberRange(five, null)", e instanceof NullPointerException JavaDoc);
199         }
200
201         try {
202             NumberRange nr = new NumberRange(null, five);
203             fail("NumberRange(null, five) did not throw an exception.");
204         } catch (Exception JavaDoc e) {
205             assertTrue("NumberRange(null, five)", e instanceof NullPointerException JavaDoc);
206         }
207     }
208
209     public void testConstructorWithMaxLessThanMin() {
210         NumberRange nr = new NumberRange( new Double JavaDoc(2.0), new Double JavaDoc(1.0));
211         assertEquals("Unexpected min on NumberRange", 2.0, nr.getMinimum().doubleValue(), Double.MIN_VALUE);
212         assertEquals("Unexpected max on NumberRange", 2.0, nr.getMaximum().doubleValue(), Double.MIN_VALUE);
213     }
214
215     public void testOverlap() {
216         assertEquals( "5-9 should not overlap 10-20", false, fiveToNine.overlaps( tenToTwenty ));
217         assertEquals( "10-20 should overlap 15-25", true, tenToTwenty.overlaps( fifteenToTwentyFive ));
218     }
219
220     public void testOverlapNull() {
221         assertEquals( "5-9 should not overlap null", false, fiveToNine.overlaps( null ));
222     }
223
224     public void testToString() {
225         String JavaDoc expected = "10-20";
226         String JavaDoc result = tenToTwenty.toString();
227         assertEquals(expected, result);
228     }
229
230     public void testToStringWithNegatives() {
231         String JavaDoc expected = "(-20)-(-10)";
232         NumberRange nr = new NumberRange( new Integer JavaDoc(-20), new Integer JavaDoc(-10));
233         String JavaDoc result = nr.toString();
234         assertEquals(expected, result);
235
236         expected = "(-20)-10";
237         nr = new NumberRange( new Integer JavaDoc(-20), new Integer JavaDoc(10));
238         result = nr.toString();
239         assertEquals(expected, result);
240     }
241
242
243 }
244
245
Popular Tags