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