001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.lang;
018:
019: import junit.framework.Test;
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022:
023: /**
024: * Test cases for the {@link NumberRange} class.
025: *
026: * @author <a href="mailto:chrise@esha.com">Christopher Elkins</a>
027: * @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
028: * @author Tim O'Brien
029: * @version $Revision: 437554 $ $Date: 2006-08-27 23:21:41 -0700 (Sun, 27 Aug 2006) $
030: */
031:
032: public final class NumberRangeTest extends TestCase {
033:
034: private NumberRange tenToTwenty;
035: private NumberRange fifteenToTwentyFive;
036: private NumberRange fiveToNine;
037: private Number five;
038: private Number nine;
039: private Number ten;
040: private Number fifteen;
041: private Number twenty;
042: private Number twentyFive;
043:
044: public NumberRangeTest(String name) {
045: super (name);
046: }
047:
048: public void setUp() {
049: five = new Integer(5);
050: nine = new Double(9.0);
051: ten = new Integer(10);
052: fifteen = new Integer(15);
053: twenty = new Integer(20);
054: twentyFive = new Integer(25);
055:
056: tenToTwenty = new NumberRange(ten, twenty);
057: fifteenToTwentyFive = new NumberRange(fifteen, twentyFive);
058: fiveToNine = new NumberRange(five, nine);
059: }
060:
061: public static Test suite() {
062: TestSuite suite = new TestSuite(NumberRangeTest.class);
063: suite.setName("NumberRange Tests");
064: return suite;
065: }
066:
067: public void testMaxMin() {
068: boolean expected = true;
069: boolean result = tenToTwenty.getMaximum().equals(twenty);
070: assertEquals(expected, result);
071:
072: expected = true;
073: result = tenToTwenty.getMinimum().equals(ten);
074: assertEquals(expected, result);
075: }
076:
077: public void testEquals() {
078: boolean expected = false;
079: boolean result = tenToTwenty.equals(new NumberRange(five, ten));
080: assertEquals(expected, result);
081:
082: expected = true;
083: result = tenToTwenty.equals(new NumberRange(ten, twenty));
084: assertEquals(expected, result);
085:
086: expected = false;
087: result = tenToTwenty.equals(new NumberRange(ten, fifteen));
088: assertEquals(expected, result);
089:
090: expected = false;
091: result = tenToTwenty.equals(new NumberRange(fifteen, twenty));
092: assertEquals(expected, result);
093: }
094:
095: public void testEqualsWithOtherObject() {
096: assertEquals("A NumberRange should not equals a String object",
097: false, fiveToNine.equals("TEST"));
098: }
099:
100: public void testEqualsWithSameReference() {
101: assertEquals("A NumberRange should equal itself", true,
102: fiveToNine.equals(fiveToNine));
103: }
104:
105: public void testEqualsNull() {
106: assertEquals("A NumberRange should not equal null", false,
107: fiveToNine.equals(null));
108: }
109:
110: public void testHashCode() {
111: NumberRange nr = new NumberRange(new Integer(5),
112: new Double(9.0));
113: assertEquals(
114: "The hashCode of 5-9 should equals the hashcode of another NumberRange of the same min/max",
115: fiveToNine.hashCode(), nr.hashCode());
116: assertTrue(
117: "The hashCode of 10-20 should not equal the hashCode of 5-9",
118: fiveToNine.hashCode() != tenToTwenty.hashCode());
119: }
120:
121: public void testIncludesNumber() {
122: boolean expected = false;
123: boolean result = tenToTwenty.includesNumber(five);
124: assertEquals(expected, result);
125:
126: expected = true;
127: result = tenToTwenty.includesNumber(ten);
128: assertEquals(expected, result);
129:
130: expected = true;
131: result = tenToTwenty.includesNumber(fifteen);
132: assertEquals(expected, result);
133:
134: expected = true;
135: result = tenToTwenty.includesNumber(twenty);
136: assertEquals(expected, result);
137:
138: expected = false;
139: result = tenToTwenty.includesNumber(twentyFive);
140: assertEquals(expected, result);
141: }
142:
143: public void testIncludesNumberNull() {
144: boolean result = tenToTwenty.includesNumber(null);
145: assertEquals(
146: "Includes number should return false for null values",
147: false, result);
148: }
149:
150: public void testIncludesRange() {
151: boolean expected = false;
152: boolean result = tenToTwenty.includesRange(new NumberRange(
153: five, ten));
154: assertEquals(expected, result);
155:
156: expected = false;
157: result = tenToTwenty.includesRange(new NumberRange(five,
158: fifteen));
159: assertEquals(expected, result);
160:
161: expected = true;
162: result = tenToTwenty
163: .includesRange(new NumberRange(ten, fifteen));
164: assertEquals(expected, result);
165:
166: expected = true;
167: result = tenToTwenty
168: .includesRange(new NumberRange(ten, twenty));
169: assertEquals(expected, result);
170:
171: expected = true;
172: result = tenToTwenty.includesRange(new NumberRange(fifteen,
173: twenty));
174: assertEquals(expected, result);
175:
176: expected = false;
177: result = tenToTwenty.includesRange(new NumberRange(fifteen,
178: twentyFive));
179: assertEquals(expected, result);
180:
181: expected = false;
182: result = tenToTwenty.includesRange(new NumberRange(twenty,
183: twentyFive));
184: assertEquals(expected, result);
185: }
186:
187: public void testIncludesRangeNull() {
188: boolean result = tenToTwenty.includesRange(null);
189: assertEquals(
190: "Includes range should return false for null values",
191: false, result);
192: }
193:
194: public void testConstructor() {
195: NumberRange nr = new NumberRange(new Double(2.0));
196: assertEquals("Unexpected min on NumberRange", 2.0, nr
197: .getMinimum().doubleValue(), Double.MIN_VALUE);
198: assertEquals("Unexpected max on NumberRange", 2.0, nr
199: .getMaximum().doubleValue(), Double.MIN_VALUE);
200: }
201:
202: public void testConstructorNullParameters() {
203: try {
204: NumberRange nr = new NumberRange(null);
205: fail("NumberRange(null) did not throw an exception.");
206: } catch (Exception e) {
207: assertTrue("NumberRange(null)",
208: e instanceof NullPointerException);
209: }
210:
211: try {
212: NumberRange nr = new NumberRange(five, null);
213: fail("NumberRange(five, null) did not throw an exception.");
214: } catch (Exception e) {
215: assertTrue("NumberRange(five, null)",
216: e instanceof NullPointerException);
217: }
218:
219: try {
220: NumberRange nr = new NumberRange(null, five);
221: fail("NumberRange(null, five) did not throw an exception.");
222: } catch (Exception e) {
223: assertTrue("NumberRange(null, five)",
224: e instanceof NullPointerException);
225: }
226: }
227:
228: public void testConstructorWithMaxLessThanMin() {
229: NumberRange nr = new NumberRange(new Double(2.0), new Double(
230: 1.0));
231: assertEquals("Unexpected min on NumberRange", 2.0, nr
232: .getMinimum().doubleValue(), Double.MIN_VALUE);
233: assertEquals("Unexpected max on NumberRange", 2.0, nr
234: .getMaximum().doubleValue(), Double.MIN_VALUE);
235: }
236:
237: public void testOverlap() {
238: assertEquals("5-9 should not overlap 10-20", false, fiveToNine
239: .overlaps(tenToTwenty));
240: assertEquals("10-20 should overlap 15-25", true, tenToTwenty
241: .overlaps(fifteenToTwentyFive));
242: }
243:
244: public void testOverlapNull() {
245: assertEquals("5-9 should not overlap null", false, fiveToNine
246: .overlaps(null));
247: }
248:
249: public void testToString() {
250: String expected = "10-20";
251: String result = tenToTwenty.toString();
252: assertEquals(expected, result);
253: }
254:
255: public void testToStringWithNegatives() {
256: String expected = "(-20)-(-10)";
257: NumberRange nr = new NumberRange(new Integer(-20), new Integer(
258: -10));
259: String result = nr.toString();
260: assertEquals(expected, result);
261:
262: expected = "(-20)-10";
263: nr = new NumberRange(new Integer(-20), new Integer(10));
264: result = nr.toString();
265: assertEquals(expected, result);
266: }
267:
268: }
|