View Javadoc
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  
18  package org.apache.commons.lang3;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.assertSame;
24  import static org.junit.Assert.assertTrue;
25  import static org.junit.Assert.fail;
26  
27  import java.util.Comparator;
28  
29  import org.junit.Before;
30  import org.junit.Test;
31  
32  /**
33   * <p>
34   * Tests the methods in the {@link org.apache.commons.lang3.Range} class.
35   * </p>
36   * 
37   * @version $Id: RangeTest.java 1436770 2013-01-22 07:09:45Z ggregory $
38   */
39  @SuppressWarnings("boxing")
40  public class RangeTest {
41  
42      private Range<Byte> byteRange;
43      private Range<Byte> byteRange2;
44      private Range<Byte> byteRange3;
45  
46      private Range<Integer> intRange;
47      private Range<Long> longRange;
48      private Range<Float> floatRange;
49      private Range<Double> doubleRange;
50  
51      @SuppressWarnings("cast") // intRange
52      @Before
53      public void setUp() {
54          byteRange   = Range.between((byte) 0, (byte) 5);
55          byteRange2  = Range.between((byte) 0, (byte) 5);
56          byteRange3  = Range.between((byte) 0, (byte) 10);
57  
58          intRange    = Range.between((int) 10, (int) 20);
59          longRange   = Range.between((long) 10, (long) 20);
60          floatRange  = Range.between((float) 10, (float) 20);
61          doubleRange = Range.between((double) 10, (double) 20);
62      }
63  
64      //-----------------------------------------------------------------------
65      @SuppressWarnings({ "rawtypes", "unchecked" })
66      @Test
67      public void testComparableConstructors() {
68          final Comparable c = 
69              new Comparable() { 
70                  @Override
71                  public int compareTo(final Object other) {
72                      return 1;
73                  }
74              };
75          final Range r1 = Range.is(c);
76          final Range r2 = Range.between(c, c);
77          assertEquals(true, r1.isNaturalOrdering());
78          assertEquals(true, r2.isNaturalOrdering());
79      }
80  
81      @Test
82      public void testIsWithCompare(){
83          final Comparator<Integer> c = new Comparator<Integer>(){
84              @Override
85              public int compare(final Integer o1, final Integer o2) {
86                  return 0; // all integers are equal
87              }
88          };
89          Range<Integer> ri = Range.is(10);
90          assertFalse("should not contain null",ri.contains(null));
91          assertTrue("should contain 10",ri.contains(10));
92          assertFalse("should not contain 11",ri.contains(11));
93          ri = Range.is(10,c);
94          assertFalse("should not contain null",ri.contains(null));
95          assertTrue("should contain 10",ri.contains(10));
96          assertTrue("should contain 11",ri.contains(11));
97      }
98  
99      @Test
100     public void testBetweenWithCompare(){
101         // TODO add tests with a better comparator
102         final Comparator<Integer> c = new Comparator<Integer>(){
103             @Override
104             public int compare(final Integer o1, final Integer o2) {
105                 return 0; // all integers are equal
106             }
107         };
108         Range<Integer> rb = Range.between(-10,20);
109         assertFalse("should not contain null",rb.contains(null));
110         assertTrue("should contain 10",rb.contains(10));
111         assertTrue("should contain -10",rb.contains(-10));
112         assertFalse("should not contain 21",rb.contains(21));
113         assertFalse("should not contain -11",rb.contains(-11));
114         rb = Range.between(-10,20,c);
115         assertFalse("should not contain null",rb.contains(null));
116         assertTrue("should contain 10",rb.contains(10));
117         assertTrue("should contain -10",rb.contains(-10));
118         assertTrue("should contain 21",rb.contains(21));
119         assertTrue("should contain -11",rb.contains(-11));
120     }
121 
122     //-----------------------------------------------------------------------
123     @Test
124     public void testRangeOfChars() {
125         final Range<Character> chars = Range.between('a', 'z');
126         assertTrue(chars.contains('b'));
127         assertFalse(chars.contains('B'));
128     }
129 
130     //-----------------------------------------------------------------------
131     @Test
132     public void testEqualsObject() {
133         assertEquals(byteRange, byteRange);
134         assertEquals(byteRange, byteRange2);
135         assertEquals(byteRange2, byteRange2);
136         assertTrue(byteRange.equals(byteRange));
137         assertTrue(byteRange2.equals(byteRange2));
138         assertTrue(byteRange3.equals(byteRange3));
139         assertFalse(byteRange2.equals(byteRange3));
140         assertFalse(byteRange2.equals(null));
141         assertFalse(byteRange2.equals("Ni!"));
142     }
143 
144     @Test
145     public void testHashCode() {
146         assertEquals(byteRange.hashCode(), byteRange2.hashCode());
147         assertFalse(byteRange.hashCode() == byteRange3.hashCode());
148         
149         assertEquals(intRange.hashCode(), intRange.hashCode());
150         assertTrue(intRange.hashCode() != 0);
151     }
152 
153     @Test
154     public void testToString() {
155         assertNotNull(byteRange.toString());
156         
157         final String str = intRange.toString();
158         assertEquals("[10..20]", str);
159         assertEquals("[-20..-10]", Range.between(-20, -10).toString());
160     }
161 
162     @Test
163     public void testToStringFormat() {
164         final String str = intRange.toString("From %1$s to %2$s");
165         assertEquals("From 10 to 20", str);
166     }
167 
168     //-----------------------------------------------------------------------
169     @Test
170     public void testGetMinimum() {
171         assertEquals(10, (int) intRange.getMinimum());
172         assertEquals(10L, (long) longRange.getMinimum());
173         assertEquals(10f, floatRange.getMinimum(), 0.00001f);
174         assertEquals(10d, doubleRange.getMinimum(), 0.00001d);
175     }
176 
177     @Test
178     public void testGetMaximum() {
179         assertEquals(20, (int) intRange.getMaximum());
180         assertEquals(20L, (long) longRange.getMaximum());
181         assertEquals(20f, floatRange.getMaximum(), 0.00001f);
182         assertEquals(20d, doubleRange.getMaximum(), 0.00001d);
183     }
184 
185     @Test
186     public void testContains() {
187         assertFalse(intRange.contains(null));
188         
189         assertFalse(intRange.contains(5));
190         assertTrue(intRange.contains(10));
191         assertTrue(intRange.contains(15));
192         assertTrue(intRange.contains(20));
193         assertFalse(intRange.contains(25));
194     }
195 
196     @Test
197     public void testIsAfter() {
198         assertFalse(intRange.isAfter(null));
199         
200         assertTrue(intRange.isAfter(5));
201         assertFalse(intRange.isAfter(10));
202         assertFalse(intRange.isAfter(15));
203         assertFalse(intRange.isAfter(20));
204         assertFalse(intRange.isAfter(25));
205     }
206 
207     @Test
208     public void testIsStartedBy() {
209         assertFalse(intRange.isStartedBy(null));
210         
211         assertFalse(intRange.isStartedBy(5));
212         assertTrue(intRange.isStartedBy(10));
213         assertFalse(intRange.isStartedBy(15));
214         assertFalse(intRange.isStartedBy(20));
215         assertFalse(intRange.isStartedBy(25));
216     }
217 
218     @Test
219     public void testIsEndedBy() {
220         assertFalse(intRange.isEndedBy(null));
221         
222         assertFalse(intRange.isEndedBy(5));
223         assertFalse(intRange.isEndedBy(10));
224         assertFalse(intRange.isEndedBy(15));
225         assertTrue(intRange.isEndedBy(20));
226         assertFalse(intRange.isEndedBy(25));
227     }
228 
229     @Test
230     public void testIsBefore() {
231         assertFalse(intRange.isBefore(null));
232         
233         assertFalse(intRange.isBefore(5));
234         assertFalse(intRange.isBefore(10));
235         assertFalse(intRange.isBefore(15));
236         assertFalse(intRange.isBefore(20));
237         assertTrue(intRange.isBefore(25));
238     }
239 
240     @Test
241     public void testElementCompareTo() {
242         try {
243             intRange.elementCompareTo(null);
244             fail("NullPointerException should have been thrown");
245         } catch(final NullPointerException npe) {
246             // expected
247         }
248         
249         assertEquals(-1, intRange.elementCompareTo(5));
250         assertEquals(0, intRange.elementCompareTo(10));
251         assertEquals(0, intRange.elementCompareTo(15));
252         assertEquals(0, intRange.elementCompareTo(20));
253         assertEquals(1, intRange.elementCompareTo(25));
254     }
255 
256     //-----------------------------------------------------------------------
257     @Test
258     public void testContainsRange() {
259 
260         // null handling
261         assertFalse(intRange.containsRange(null));
262 
263         // easy inside range
264         assertTrue(intRange.containsRange(Range.between(12, 18)));
265 
266         // outside range on each side
267         assertFalse(intRange.containsRange(Range.between(32, 45)));
268         assertFalse(intRange.containsRange(Range.between(2, 8)));
269 
270         // equals range
271         assertTrue(intRange.containsRange(Range.between(10, 20)));
272 
273         // overlaps
274         assertFalse(intRange.containsRange(Range.between(9, 14)));
275         assertFalse(intRange.containsRange(Range.between(16, 21)));
276 
277         // touches lower boundary
278         assertTrue(intRange.containsRange(Range.between(10, 19)));
279         assertFalse(intRange.containsRange(Range.between(10, 21)));
280 
281         // touches upper boundary
282         assertTrue(intRange.containsRange(Range.between(11, 20)));
283         assertFalse(intRange.containsRange(Range.between(9, 20)));
284         
285         // negative
286         assertFalse(intRange.containsRange(Range.between(-11, -18)));
287     }
288 
289     @Test
290     public void testIsAfterRange() {
291         assertFalse(intRange.isAfterRange(null));
292         
293         assertTrue(intRange.isAfterRange(Range.between(5, 9)));
294         
295         assertFalse(intRange.isAfterRange(Range.between(5, 10)));
296         assertFalse(intRange.isAfterRange(Range.between(5, 20)));
297         assertFalse(intRange.isAfterRange(Range.between(5, 25)));
298         assertFalse(intRange.isAfterRange(Range.between(15, 25)));
299         
300         assertFalse(intRange.isAfterRange(Range.between(21, 25)));
301         
302         assertFalse(intRange.isAfterRange(Range.between(10, 20)));
303     }
304 
305     @Test
306     public void testIsOverlappedBy() {
307 
308         // null handling
309         assertFalse(intRange.isOverlappedBy(null));
310 
311         // easy inside range
312         assertTrue(intRange.isOverlappedBy(Range.between(12, 18)));
313 
314         // outside range on each side
315         assertFalse(intRange.isOverlappedBy(Range.between(32, 45)));
316         assertFalse(intRange.isOverlappedBy(Range.between(2, 8)));
317 
318         // equals range
319         assertTrue(intRange.isOverlappedBy(Range.between(10, 20)));
320 
321         // overlaps
322         assertTrue(intRange.isOverlappedBy(Range.between(9, 14)));
323         assertTrue(intRange.isOverlappedBy(Range.between(16, 21)));
324 
325         // touches lower boundary
326         assertTrue(intRange.isOverlappedBy(Range.between(10, 19)));
327         assertTrue(intRange.isOverlappedBy(Range.between(10, 21)));
328 
329         // touches upper boundary
330         assertTrue(intRange.isOverlappedBy(Range.between(11, 20)));
331         assertTrue(intRange.isOverlappedBy(Range.between(9, 20)));
332         
333         // negative
334         assertFalse(intRange.isOverlappedBy(Range.between(-11, -18)));
335     }
336 
337     @Test
338     public void testIsBeforeRange() {
339         assertFalse(intRange.isBeforeRange(null));
340         
341         assertFalse(intRange.isBeforeRange(Range.between(5, 9)));
342         
343         assertFalse(intRange.isBeforeRange(Range.between(5, 10)));
344         assertFalse(intRange.isBeforeRange(Range.between(5, 20)));
345         assertFalse(intRange.isBeforeRange(Range.between(5, 25)));
346         assertFalse(intRange.isBeforeRange(Range.between(15, 25)));
347         
348         assertTrue(intRange.isBeforeRange(Range.between(21, 25)));
349         
350         assertFalse(intRange.isBeforeRange(Range.between(10, 20)));
351     }
352 
353     @Test
354     public void testIntersectionWith() {
355         assertSame(intRange, intRange.intersectionWith(intRange));
356         assertSame(byteRange, byteRange.intersectionWith(byteRange));
357         assertSame(longRange, longRange.intersectionWith(longRange));
358         assertSame(floatRange, floatRange.intersectionWith(floatRange));
359         assertSame(doubleRange, doubleRange.intersectionWith(doubleRange));
360 
361         assertEquals(Range.between(10, 15), intRange.intersectionWith(Range.between(5, 15)));
362     }
363 
364     @Test(expected = IllegalArgumentException.class)
365     public void testIntersectionWithNull() {
366         intRange.intersectionWith(null);
367     }
368 
369     @Test(expected = IllegalArgumentException.class)
370     public void testIntersectionWithNonOverlapping() {
371         intRange.intersectionWith(Range.between(0, 9));
372     }
373 
374     //-----------------------------------------------------------------------
375     @Test
376     public void testSerializing() {
377         SerializationUtils.clone(intRange);
378     }
379 
380 }