From e47b5d8e75e4af0edd747a451b43303391f13d96 Mon Sep 17 00:00:00 2001 From: "Gary D. Gregory" Date: Mon, 1 Aug 2005 00:30:42 +0000 Subject: [PATCH] Better unit test code coverage. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@226708 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/lang/math/AbstractRangeTest.java | 55 +++-- .../commons/lang/math/DoubleRangeTest.java | 4 +- .../commons/lang/math/FloatRangeTest.java | 4 +- .../commons/lang/math/IntRangeTest.java | 4 +- .../commons/lang/math/LongRangeTest.java | 4 +- .../commons/lang/math/NumberRangeTest.java | 217 ++++++++---------- 6 files changed, 129 insertions(+), 159 deletions(-) diff --git a/src/test/org/apache/commons/lang/math/AbstractRangeTest.java b/src/test/org/apache/commons/lang/math/AbstractRangeTest.java index 68573b501..53eb4cf11 100644 --- a/src/test/org/apache/commons/lang/math/AbstractRangeTest.java +++ b/src/test/org/apache/commons/lang/math/AbstractRangeTest.java @@ -63,10 +63,9 @@ public abstract class AbstractRangeTest extends TestCase { public long longValue() { return 12L; } - } - protected InnerNumber nonComparable = new InnerNumber(); + protected InnerNumber nonComparableNumber = new InnerNumber(); public AbstractRangeTest(String name) { @@ -74,30 +73,30 @@ public abstract class AbstractRangeTest extends TestCase { } public void setUp() { - five = new Integer(5); - ten = new Integer(10); - twelve = new Integer(12); - fifteen = new Integer(15); - twenty = new Integer(20); - twentyFive = new Integer(25); - long8 = new Long(8); - long10 = new Long(10); - long12 = new Long(12); - long20 = new Long(20); - long21 = new Long(21); - double8 = new Double(8); - double10 = new Double(10); - double12 = new Double(12); - double20 = new Double(20); - double21 = new Double(21); - float8 = new Float(8); - float10 = new Float(10); - float12 = new Float(12); - float20 = new Float(20); - float21 = new Float(21); + five = new Integer(5); + ten = new Integer(10); + twelve = new Integer(12); + fifteen = new Integer(15); + twenty = new Integer(20); + twentyFive = new Integer(25); + long8 = new Long(8); + long10 = new Long(10); + long12 = new Long(12); + long20 = new Long(20); + long21 = new Long(21); + double8 = new Double(8); + double10 = new Double(10); + double12 = new Double(12); + double20 = new Double(20); + double21 = new Double(21); + float8 = new Float(8); + float10 = new Float(10); + float12 = new Float(12); + float20 = new Float(20); + float21 = new Float(21); } - //-------------------------------------------------------------------------- + // -------------------------------------------------------------------------- public void testGetMinimum() { assertEquals(10L, tenToTwenty.getMinimumLong()); @@ -117,7 +116,7 @@ public abstract class AbstractRangeTest extends TestCase { public void testContainsLong() { assertEquals(false, tenToTwenty.containsLong(null)); - assertEquals(true, tenToTwenty.containsLong(nonComparable)); + assertEquals(true, tenToTwenty.containsLong(nonComparableNumber)); assertEquals(false, tenToTwenty.containsLong(five)); assertEquals(true, tenToTwenty.containsLong(ten)); @@ -152,7 +151,7 @@ public abstract class AbstractRangeTest extends TestCase { public void testContainsInteger() { assertEquals(false, tenToTwenty.containsInteger(null)); - assertEquals(true, tenToTwenty.containsInteger(nonComparable)); + assertEquals(true, tenToTwenty.containsInteger(nonComparableNumber)); assertEquals(false, tenToTwenty.containsInteger(five)); assertEquals(true, tenToTwenty.containsInteger(ten)); @@ -187,7 +186,7 @@ public abstract class AbstractRangeTest extends TestCase { public void testContainsDouble() { assertEquals(false, tenToTwenty.containsDouble(null)); - assertEquals(true, tenToTwenty.containsDouble(nonComparable)); + assertEquals(true, tenToTwenty.containsDouble(nonComparableNumber)); assertEquals(false, tenToTwenty.containsDouble(five)); assertEquals(true, tenToTwenty.containsDouble(ten)); @@ -222,7 +221,7 @@ public abstract class AbstractRangeTest extends TestCase { public void testContainsFloat() { assertEquals(false, tenToTwenty.containsFloat(null)); - assertEquals(true, tenToTwenty.containsFloat(nonComparable)); + assertEquals(true, tenToTwenty.containsFloat(nonComparableNumber)); assertEquals(false, tenToTwenty.containsFloat(five)); assertEquals(true, tenToTwenty.containsFloat(ten)); diff --git a/src/test/org/apache/commons/lang/math/DoubleRangeTest.java b/src/test/org/apache/commons/lang/math/DoubleRangeTest.java index 9128c2374..be6ada173 100644 --- a/src/test/org/apache/commons/lang/math/DoubleRangeTest.java +++ b/src/test/org/apache/commons/lang/math/DoubleRangeTest.java @@ -67,7 +67,7 @@ public final class DoubleRangeTest extends AbstractRangeTest { assertSame(double8, nr.getMinimumNumber()); assertSame(double8, nr.getMaximumNumber()); - Range r = new DoubleRange(nonComparable); + Range r = new DoubleRange(nonComparableNumber); try { new DoubleRange(null); @@ -131,7 +131,7 @@ public final class DoubleRangeTest extends AbstractRangeTest { public void testContainsNumber() { assertEquals(false, tenToTwenty.containsNumber(null)); - assertEquals(true, tenToTwenty.containsNumber(nonComparable)); + assertEquals(true, tenToTwenty.containsNumber(nonComparableNumber)); assertEquals(false, tenToTwenty.containsNumber(five)); assertEquals(true, tenToTwenty.containsNumber(ten)); diff --git a/src/test/org/apache/commons/lang/math/FloatRangeTest.java b/src/test/org/apache/commons/lang/math/FloatRangeTest.java index 24c2d396a..43905b292 100644 --- a/src/test/org/apache/commons/lang/math/FloatRangeTest.java +++ b/src/test/org/apache/commons/lang/math/FloatRangeTest.java @@ -67,7 +67,7 @@ public final class FloatRangeTest extends AbstractRangeTest { assertSame(float8, nr.getMinimumNumber()); assertSame(float8, nr.getMaximumNumber()); - Range r = new FloatRange(nonComparable); + Range r = new FloatRange(nonComparableNumber); try { new FloatRange(null); @@ -131,7 +131,7 @@ public final class FloatRangeTest extends AbstractRangeTest { public void testContainsNumber() { assertEquals(false, tenToTwenty.containsNumber(null)); - assertEquals(true, tenToTwenty.containsNumber(nonComparable)); + assertEquals(true, tenToTwenty.containsNumber(nonComparableNumber)); assertEquals(false, tenToTwenty.containsNumber(five)); assertEquals(true, tenToTwenty.containsNumber(ten)); diff --git a/src/test/org/apache/commons/lang/math/IntRangeTest.java b/src/test/org/apache/commons/lang/math/IntRangeTest.java index feb2d50e6..30d9c7ce7 100644 --- a/src/test/org/apache/commons/lang/math/IntRangeTest.java +++ b/src/test/org/apache/commons/lang/math/IntRangeTest.java @@ -64,7 +64,7 @@ public final class IntRangeTest extends AbstractRangeTest { assertSame(five, nr.getMinimumNumber()); assertSame(five, nr.getMaximumNumber()); - Range r = new IntRange(nonComparable); + Range r = new IntRange(nonComparableNumber); try { new IntRange(null); @@ -124,7 +124,7 @@ public final class IntRangeTest extends AbstractRangeTest { public void testContainsNumber() { assertEquals(false, tenToTwenty.containsNumber(null)); - assertEquals(true, tenToTwenty.containsNumber(nonComparable)); + assertEquals(true, tenToTwenty.containsNumber(nonComparableNumber)); assertEquals(false, tenToTwenty.containsNumber(five)); assertEquals(true, tenToTwenty.containsNumber(ten)); diff --git a/src/test/org/apache/commons/lang/math/LongRangeTest.java b/src/test/org/apache/commons/lang/math/LongRangeTest.java index 408be5a85..b3d026168 100644 --- a/src/test/org/apache/commons/lang/math/LongRangeTest.java +++ b/src/test/org/apache/commons/lang/math/LongRangeTest.java @@ -62,7 +62,7 @@ public final class LongRangeTest extends AbstractRangeTest { assertSame(long8, nr.getMinimumNumber()); assertSame(long8, nr.getMaximumNumber()); - Range r = new LongRange(nonComparable); + Range r = new LongRange(nonComparableNumber); try { new LongRange(null); @@ -112,7 +112,7 @@ public final class LongRangeTest extends AbstractRangeTest { public void testContainsNumber() { assertEquals(false, tenToTwenty.containsNumber(null)); - assertEquals(true, tenToTwenty.containsNumber(nonComparable)); + assertEquals(true, tenToTwenty.containsNumber(nonComparableNumber)); assertEquals(false, tenToTwenty.containsNumber(five)); assertEquals(true, tenToTwenty.containsNumber(ten)); diff --git a/src/test/org/apache/commons/lang/math/NumberRangeTest.java b/src/test/org/apache/commons/lang/math/NumberRangeTest.java index ec767f5c0..0e633a88f 100644 --- a/src/test/org/apache/commons/lang/math/NumberRangeTest.java +++ b/src/test/org/apache/commons/lang/math/NumberRangeTest.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.commons.lang.math; import junit.framework.Test; @@ -20,7 +21,7 @@ import junit.framework.TestSuite; /** * Test cases for the {@link NumberRange} class. - * + * * @author Christopher Elkins * @author Ringo De Smet * @author Stephen Colebourne @@ -28,143 +29,119 @@ import junit.framework.TestSuite; */ public final class NumberRangeTest extends AbstractRangeTest { - public NumberRangeTest(String name) { - super(name); - } - public static Test suite() { TestSuite suite = new TestSuite(NumberRangeTest.class); suite.setName("NumberRange Tests"); return suite; } - + + public NumberRangeTest(String name) { + super(name); + } + + void checkConstructorException(Number num) { + try { + new NumberRange(num); + fail("Expected an IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // Expected. + } + } + + void checkConstructorException(Number num1, Number num2) { + try { + new NumberRange(num1, num2); + fail("Expected an IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // Expected. + } + } + + protected Range createRange(Integer integer) { + return new NumberRange(integer); + } + + // -------------------------------------------------------------------------- + + protected Range createRange(Integer integer1, Integer integer2) { + return new NumberRange(integer1, integer2); + } + public void setUp() { super.setUp(); tenToTwenty = new NumberRange(ten, twenty); otherRange = new IntRange(ten, twenty); } - protected Range createRange(Integer integer1, Integer integer2) { - return new NumberRange(integer1, integer2); - } - protected Range createRange(Integer integer) { - return new NumberRange(integer); - } - - //-------------------------------------------------------------------------- - - public void testConstructorExceptions() { - try { - new NumberRange (new Double (Double.NaN)); - fail("no illegal argument exception"); - } catch (IllegalArgumentException e) { - } - - try { - new NumberRange (new Double (Double.NaN), new Double (12.2)); - fail("no illegal argument exception"); - } catch (IllegalArgumentException e) { - } - - try { - new NumberRange (new Double (12.2), new Double (Double.NaN)); - fail("no illegal argument exception"); - } catch (IllegalArgumentException e) { - } - - try { - new NumberRange (new Float (Float.NaN)); - fail("no illegal argument exception"); - } catch (IllegalArgumentException e) { - } - - try { - new NumberRange (new Float (Float.NaN), new Float(12.2)); - fail("no illegal argument exception"); - } catch (IllegalArgumentException e) { - } - - try { - new NumberRange (new Float(12.2), new Float (Float.NaN)); - fail("no illegal argument exception"); - } catch (IllegalArgumentException e) { - } - } - + /** + * Tests non-exceptional conditions for the one argument constructor. + */ public void testConstructor1() { NumberRange nr = new NumberRange(five); assertSame(five, nr.getMinimumNumber()); assertSame(five, nr.getMaximumNumber()); - - try { - new NumberRange(null); - fail(); - } catch (IllegalArgumentException ex) {} - try { - new NumberRange(nonComparable); - fail(); - } catch (IllegalArgumentException ex) {} } - + + /** + * Tests exceptional conditions for the one argument constructor. + */ + public void testConstructor1Exceptions() { + this.checkConstructorException(null); + this.checkConstructorException(nonComparableNumber); + this.checkConstructorException(new Float(Float.NaN)); + this.checkConstructorException(new Double(Double.NaN)); + } + + /** + * Tests non-exceptional conditions for the two argument constructor. + */ public void testConstructor2() { NumberRange nr = new NumberRange(five, ten); assertSame(five, nr.getMinimumNumber()); assertSame(ten, nr.getMaximumNumber()); - + nr = new NumberRange(ten, five); assertSame(five, nr.getMinimumNumber()); assertSame(ten, nr.getMaximumNumber()); - - // not null - try { - new NumberRange(five, null); - fail(); - } catch (IllegalArgumentException ex) {} - try { - new NumberRange(null, five); - fail(); - } catch (IllegalArgumentException ex) {} - try { - new NumberRange(null, null); - fail(); - } catch (IllegalArgumentException ex) {} - - // no mixed types - try { - new NumberRange(five, long21); - fail(); - } catch (IllegalArgumentException ex) {} - - // must be comparable - try { - new NumberRange(nonComparable, nonComparable); - fail(); - } catch (IllegalArgumentException ex) {} - - // no double NaN - try { - new NumberRange(new Double(0), new Double(Double.NaN)); - fail(); - } catch (IllegalArgumentException ex) {} - - try { - new NumberRange(new Double(Double.NaN), new Double(0)); - fail(); - } catch (IllegalArgumentException ex) {} - - // no float NaN - try { - new NumberRange(new Float(0), new Float(Float.NaN)); - fail(); - } catch (IllegalArgumentException ex) {} - - try { - new NumberRange(new Float(Float.NaN), new Float(0)); - fail(); - } catch (IllegalArgumentException ex) {} } - //-------------------------------------------------------------------------- + /** + * Tests exceptional conditions for the two argument constructor. + */ + public void testConstructor2Exceptions() { + this.checkConstructorException(null, null); + + this.checkConstructorException(new Float(12.2f), new Double(12.2)); + this.checkConstructorException(new Float(Float.NaN), new Double(12.2)); + this.checkConstructorException(new Double(Double.NaN), new Double(12.2)); + this.checkConstructorException(new Double(12.2), new Double(Double.NaN)); + this.checkConstructorException(new Double(Double.NaN), new Double(Double.NaN)); + this.checkConstructorException(null, new Double(12.2)); + this.checkConstructorException(new Double(12.2), null); + + this.checkConstructorException(new Double(12.2f), new Float(12.2)); + this.checkConstructorException(new Double(Double.NaN), new Float(12.2)); + this.checkConstructorException(new Float(Float.NaN), new Float(12.2)); + this.checkConstructorException(new Float(12.2), new Float(Float.NaN)); + this.checkConstructorException(new Float(Float.NaN), new Float(Float.NaN)); + this.checkConstructorException(null, new Float(12.2)); + this.checkConstructorException(new Float(12.2), null); + + this.checkConstructorException(nonComparableNumber, nonComparableNumber); + this.checkConstructorException(null, nonComparableNumber); + this.checkConstructorException(nonComparableNumber, null); + this.checkConstructorException(new Float(12.2), nonComparableNumber); + this.checkConstructorException(nonComparableNumber, new Float(12.2)); + } + + // -------------------------------------------------------------------------- + + public void testContainsLongBig() { + // original NumberRange class failed this test + NumberRange big = new NumberRange(new Long(Long.MAX_VALUE), new Long(Long.MAX_VALUE - 2)); + assertEquals(true, big.containsLong(Long.MAX_VALUE - 1)); + assertEquals(false, big.containsLong(Long.MAX_VALUE - 3)); + } public void testContainsNumber() { assertEquals(false, tenToTwenty.containsNumber(null)); @@ -173,20 +150,14 @@ public final class NumberRangeTest extends AbstractRangeTest { assertEquals(true, tenToTwenty.containsNumber(fifteen)); assertEquals(true, tenToTwenty.containsNumber(twenty)); assertEquals(false, tenToTwenty.containsNumber(twentyFive)); - + try { tenToTwenty.containsNumber(long21); fail(); - } catch (IllegalArgumentException ex) {} + } catch (IllegalArgumentException ex) { + } } - public void testContainsLongBig() { - // original NumberRange class failed this test - NumberRange big = new NumberRange(new Long(Long.MAX_VALUE), new Long(Long.MAX_VALUE- 2)); - assertEquals(true, big.containsLong(Long.MAX_VALUE - 1)); - assertEquals(false, big.containsLong(Long.MAX_VALUE - 3)); - } - - //-------------------------------------------------------------------------- + // -------------------------------------------------------------------------- }