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