KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002,2004 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.math;
17
18 import junit.framework.Test;
19 import junit.framework.TestSuite;
20
21 /**
22  * Test cases for the {@link NumberRange} class.
23  *
24  * @author <a HREF="mailto:chrise@esha.com">Christopher Elkins</a>
25  * @author <a HREF="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
26  * @author Stephen Colebourne
27  * @version $Id: NumberRangeTest.java 155423 2005-02-26 13:08:30Z dirkv $
28  */

29 public final class NumberRangeTest extends AbstractRangeTest {
30
31     public NumberRangeTest(String JavaDoc name) {
32         super(name);
33     }
34
35     public static Test suite() {
36         TestSuite suite = new TestSuite(NumberRangeTest.class);
37         suite.setName("NumberRange Tests");
38         return suite;
39     }
40     
41     public void setUp() {
42         super.setUp();
43         tenToTwenty = new NumberRange(ten, twenty);
44         otherRange = new IntRange(ten, twenty);
45     }
46
47     protected Range createRange(Integer JavaDoc integer1, Integer JavaDoc integer2) {
48         return new NumberRange(integer1, integer2);
49     }
50     protected Range createRange(Integer JavaDoc integer) {
51         return new NumberRange(integer);
52     }
53
54     //--------------------------------------------------------------------------
55

56     public void testConstructor1() {
57         NumberRange nr = new NumberRange(five);
58         assertSame(five, nr.getMinimumNumber());
59         assertSame(five, nr.getMaximumNumber());
60         
61         try {
62             new NumberRange(null);
63             fail();
64         } catch (IllegalArgumentException JavaDoc ex) {}
65         try {
66             new NumberRange(nonComparable);
67             fail();
68         } catch (IllegalArgumentException JavaDoc ex) {}
69     }
70     
71     public void testConstructor2() {
72         NumberRange nr = new NumberRange(five, ten);
73         assertSame(five, nr.getMinimumNumber());
74         assertSame(ten, nr.getMaximumNumber());
75         
76         nr = new NumberRange(ten, five);
77         assertSame(five, nr.getMinimumNumber());
78         assertSame(ten, nr.getMaximumNumber());
79         
80         // not null
81
try {
82             new NumberRange(five, null);
83             fail();
84         } catch (IllegalArgumentException JavaDoc ex) {}
85         try {
86             new NumberRange(null, five);
87             fail();
88         } catch (IllegalArgumentException JavaDoc ex) {}
89         try {
90             new NumberRange(null, null);
91             fail();
92         } catch (IllegalArgumentException JavaDoc ex) {}
93         
94         // no mixed types
95
try {
96             new NumberRange(five, long21);
97             fail();
98         } catch (IllegalArgumentException JavaDoc ex) {}
99         
100         // must be comparable
101
try {
102             new NumberRange(nonComparable, nonComparable);
103             fail();
104         } catch (IllegalArgumentException JavaDoc ex) {}
105         
106         // no double NaN
107
try {
108             new NumberRange(new Double JavaDoc(0), new Double JavaDoc(Double.NaN));
109             fail();
110         } catch (IllegalArgumentException JavaDoc ex) {}
111         
112         try {
113             new NumberRange(new Double JavaDoc(Double.NaN), new Double JavaDoc(0));
114             fail();
115         } catch (IllegalArgumentException JavaDoc ex) {}
116         
117         // no float NaN
118
try {
119             new NumberRange(new Float JavaDoc(0), new Float JavaDoc(Float.NaN));
120             fail();
121         } catch (IllegalArgumentException JavaDoc ex) {}
122         
123         try {
124             new NumberRange(new Float JavaDoc(Float.NaN), new Float JavaDoc(0));
125             fail();
126         } catch (IllegalArgumentException JavaDoc ex) {}
127     }
128
129     //--------------------------------------------------------------------------
130

131     public void testContainsNumber() {
132         assertEquals(false, tenToTwenty.containsNumber(null));
133         assertEquals(false, tenToTwenty.containsNumber(five));
134         assertEquals(true, tenToTwenty.containsNumber(ten));
135         assertEquals(true, tenToTwenty.containsNumber(fifteen));
136         assertEquals(true, tenToTwenty.containsNumber(twenty));
137         assertEquals(false, tenToTwenty.containsNumber(twentyFive));
138         
139         try {
140             tenToTwenty.containsNumber(long21);
141             fail();
142         } catch (IllegalArgumentException JavaDoc ex) {}
143     }
144
145     public void testContainsLongBig() {
146         // original NumberRange class failed this test
147
NumberRange big = new NumberRange(new Long JavaDoc(Long.MAX_VALUE), new Long JavaDoc(Long.MAX_VALUE- 2));
148         assertEquals(true, big.containsLong(Long.MAX_VALUE - 1));
149         assertEquals(false, big.containsLong(Long.MAX_VALUE - 3));
150     }
151
152     //--------------------------------------------------------------------------
153

154 }
155
Popular Tags