From 4c124cea2831d2579bd78895c132f6750c8f9915 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Fri, 13 Nov 2009 08:05:11 +0000 Subject: [PATCH] Adding @throws and test for the ClassCastException thrown by the ComparableComparator git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@835771 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/commons/lang/Range.java | 10 ++++++---- .../org/apache/commons/lang/RangeTest.java | 20 +++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/java/org/apache/commons/lang/Range.java b/src/java/org/apache/commons/lang/Range.java index 07861ca76..abfe5f248 100644 --- a/src/java/org/apache/commons/lang/Range.java +++ b/src/java/org/apache/commons/lang/Range.java @@ -41,10 +41,11 @@ public class Range { * * @param element the value to use for this range, must not be null * @throws IllegalArgumentException if the value is null + * @throws ClassCastException if the value is not Comparable */ -// TODO: Ideally this would only support > + // TODO: Ideally the ClassCastException would be compile-time via generics public Range(T element) { - this(element, element); + this( element, element); } /** @@ -59,10 +60,11 @@ public class Range { * @param element1 first value that defines the edge of the range, inclusive * @param element2 second value that defines the edge of the range, inclusive * @throws IllegalArgumentException if either value is null + * @throws ClassCastException if either value is not Comparable */ -// TODO: Ideally this would only support > + // TODO: Ideally the ClassCastException would be compile-time via generics public Range(T element1, T element2) { - this(element1, element2, ComparableComparator.INSTANCE); + this( element1, element2, ComparableComparator.INSTANCE); } /** diff --git a/src/test/org/apache/commons/lang/RangeTest.java b/src/test/org/apache/commons/lang/RangeTest.java index a7ab9ec53..b9418e740 100644 --- a/src/test/org/apache/commons/lang/RangeTest.java +++ b/src/test/org/apache/commons/lang/RangeTest.java @@ -51,6 +51,26 @@ public class RangeTest extends TestCase { doubleRange = new Range((double) 10, (double) 20); } + // -------------------------------------------------------------------------- + + public void testComparableConstructors() { + try { + Range range = new Range(new Object()); + fail("IllegalArgumentException expected"); + } catch(ClassCastException cce) { + // expected + } + + try { + Range range = new Range(new Object(), new Object()); + fail("ClassCastException expected"); + } catch(ClassCastException cce) { + // expected + } + } + + // -------------------------------------------------------------------------- + public void testEqualsObject() { assertEquals(byteRange, byteRange); assertEquals(byteRange, byteRange2);