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
This commit is contained in:
parent
1e3ecfff20
commit
e47b5d8e75
|
@ -63,10 +63,9 @@ public abstract class AbstractRangeTest extends TestCase {
|
||||||
public long longValue() {
|
public long longValue() {
|
||||||
return 12L;
|
return 12L;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected InnerNumber nonComparable = new InnerNumber();
|
protected InnerNumber nonComparableNumber = new InnerNumber();
|
||||||
|
|
||||||
|
|
||||||
public AbstractRangeTest(String name) {
|
public AbstractRangeTest(String name) {
|
||||||
|
@ -97,7 +96,7 @@ public abstract class AbstractRangeTest extends TestCase {
|
||||||
float21 = new Float(21);
|
float21 = new Float(21);
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
|
|
||||||
public void testGetMinimum() {
|
public void testGetMinimum() {
|
||||||
assertEquals(10L, tenToTwenty.getMinimumLong());
|
assertEquals(10L, tenToTwenty.getMinimumLong());
|
||||||
|
@ -117,7 +116,7 @@ public abstract class AbstractRangeTest extends TestCase {
|
||||||
|
|
||||||
public void testContainsLong() {
|
public void testContainsLong() {
|
||||||
assertEquals(false, tenToTwenty.containsLong(null));
|
assertEquals(false, tenToTwenty.containsLong(null));
|
||||||
assertEquals(true, tenToTwenty.containsLong(nonComparable));
|
assertEquals(true, tenToTwenty.containsLong(nonComparableNumber));
|
||||||
|
|
||||||
assertEquals(false, tenToTwenty.containsLong(five));
|
assertEquals(false, tenToTwenty.containsLong(five));
|
||||||
assertEquals(true, tenToTwenty.containsLong(ten));
|
assertEquals(true, tenToTwenty.containsLong(ten));
|
||||||
|
@ -152,7 +151,7 @@ public abstract class AbstractRangeTest extends TestCase {
|
||||||
|
|
||||||
public void testContainsInteger() {
|
public void testContainsInteger() {
|
||||||
assertEquals(false, tenToTwenty.containsInteger(null));
|
assertEquals(false, tenToTwenty.containsInteger(null));
|
||||||
assertEquals(true, tenToTwenty.containsInteger(nonComparable));
|
assertEquals(true, tenToTwenty.containsInteger(nonComparableNumber));
|
||||||
|
|
||||||
assertEquals(false, tenToTwenty.containsInteger(five));
|
assertEquals(false, tenToTwenty.containsInteger(five));
|
||||||
assertEquals(true, tenToTwenty.containsInteger(ten));
|
assertEquals(true, tenToTwenty.containsInteger(ten));
|
||||||
|
@ -187,7 +186,7 @@ public abstract class AbstractRangeTest extends TestCase {
|
||||||
|
|
||||||
public void testContainsDouble() {
|
public void testContainsDouble() {
|
||||||
assertEquals(false, tenToTwenty.containsDouble(null));
|
assertEquals(false, tenToTwenty.containsDouble(null));
|
||||||
assertEquals(true, tenToTwenty.containsDouble(nonComparable));
|
assertEquals(true, tenToTwenty.containsDouble(nonComparableNumber));
|
||||||
|
|
||||||
assertEquals(false, tenToTwenty.containsDouble(five));
|
assertEquals(false, tenToTwenty.containsDouble(five));
|
||||||
assertEquals(true, tenToTwenty.containsDouble(ten));
|
assertEquals(true, tenToTwenty.containsDouble(ten));
|
||||||
|
@ -222,7 +221,7 @@ public abstract class AbstractRangeTest extends TestCase {
|
||||||
|
|
||||||
public void testContainsFloat() {
|
public void testContainsFloat() {
|
||||||
assertEquals(false, tenToTwenty.containsFloat(null));
|
assertEquals(false, tenToTwenty.containsFloat(null));
|
||||||
assertEquals(true, tenToTwenty.containsFloat(nonComparable));
|
assertEquals(true, tenToTwenty.containsFloat(nonComparableNumber));
|
||||||
|
|
||||||
assertEquals(false, tenToTwenty.containsFloat(five));
|
assertEquals(false, tenToTwenty.containsFloat(five));
|
||||||
assertEquals(true, tenToTwenty.containsFloat(ten));
|
assertEquals(true, tenToTwenty.containsFloat(ten));
|
||||||
|
|
|
@ -67,7 +67,7 @@ public final class DoubleRangeTest extends AbstractRangeTest {
|
||||||
assertSame(double8, nr.getMinimumNumber());
|
assertSame(double8, nr.getMinimumNumber());
|
||||||
assertSame(double8, nr.getMaximumNumber());
|
assertSame(double8, nr.getMaximumNumber());
|
||||||
|
|
||||||
Range r = new DoubleRange(nonComparable);
|
Range r = new DoubleRange(nonComparableNumber);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
new DoubleRange(null);
|
new DoubleRange(null);
|
||||||
|
@ -131,7 +131,7 @@ public final class DoubleRangeTest extends AbstractRangeTest {
|
||||||
|
|
||||||
public void testContainsNumber() {
|
public void testContainsNumber() {
|
||||||
assertEquals(false, tenToTwenty.containsNumber(null));
|
assertEquals(false, tenToTwenty.containsNumber(null));
|
||||||
assertEquals(true, tenToTwenty.containsNumber(nonComparable));
|
assertEquals(true, tenToTwenty.containsNumber(nonComparableNumber));
|
||||||
|
|
||||||
assertEquals(false, tenToTwenty.containsNumber(five));
|
assertEquals(false, tenToTwenty.containsNumber(five));
|
||||||
assertEquals(true, tenToTwenty.containsNumber(ten));
|
assertEquals(true, tenToTwenty.containsNumber(ten));
|
||||||
|
|
|
@ -67,7 +67,7 @@ public final class FloatRangeTest extends AbstractRangeTest {
|
||||||
assertSame(float8, nr.getMinimumNumber());
|
assertSame(float8, nr.getMinimumNumber());
|
||||||
assertSame(float8, nr.getMaximumNumber());
|
assertSame(float8, nr.getMaximumNumber());
|
||||||
|
|
||||||
Range r = new FloatRange(nonComparable);
|
Range r = new FloatRange(nonComparableNumber);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
new FloatRange(null);
|
new FloatRange(null);
|
||||||
|
@ -131,7 +131,7 @@ public final class FloatRangeTest extends AbstractRangeTest {
|
||||||
|
|
||||||
public void testContainsNumber() {
|
public void testContainsNumber() {
|
||||||
assertEquals(false, tenToTwenty.containsNumber(null));
|
assertEquals(false, tenToTwenty.containsNumber(null));
|
||||||
assertEquals(true, tenToTwenty.containsNumber(nonComparable));
|
assertEquals(true, tenToTwenty.containsNumber(nonComparableNumber));
|
||||||
|
|
||||||
assertEquals(false, tenToTwenty.containsNumber(five));
|
assertEquals(false, tenToTwenty.containsNumber(five));
|
||||||
assertEquals(true, tenToTwenty.containsNumber(ten));
|
assertEquals(true, tenToTwenty.containsNumber(ten));
|
||||||
|
|
|
@ -64,7 +64,7 @@ public final class IntRangeTest extends AbstractRangeTest {
|
||||||
assertSame(five, nr.getMinimumNumber());
|
assertSame(five, nr.getMinimumNumber());
|
||||||
assertSame(five, nr.getMaximumNumber());
|
assertSame(five, nr.getMaximumNumber());
|
||||||
|
|
||||||
Range r = new IntRange(nonComparable);
|
Range r = new IntRange(nonComparableNumber);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
new IntRange(null);
|
new IntRange(null);
|
||||||
|
@ -124,7 +124,7 @@ public final class IntRangeTest extends AbstractRangeTest {
|
||||||
|
|
||||||
public void testContainsNumber() {
|
public void testContainsNumber() {
|
||||||
assertEquals(false, tenToTwenty.containsNumber(null));
|
assertEquals(false, tenToTwenty.containsNumber(null));
|
||||||
assertEquals(true, tenToTwenty.containsNumber(nonComparable));
|
assertEquals(true, tenToTwenty.containsNumber(nonComparableNumber));
|
||||||
|
|
||||||
assertEquals(false, tenToTwenty.containsNumber(five));
|
assertEquals(false, tenToTwenty.containsNumber(five));
|
||||||
assertEquals(true, tenToTwenty.containsNumber(ten));
|
assertEquals(true, tenToTwenty.containsNumber(ten));
|
||||||
|
|
|
@ -62,7 +62,7 @@ public final class LongRangeTest extends AbstractRangeTest {
|
||||||
assertSame(long8, nr.getMinimumNumber());
|
assertSame(long8, nr.getMinimumNumber());
|
||||||
assertSame(long8, nr.getMaximumNumber());
|
assertSame(long8, nr.getMaximumNumber());
|
||||||
|
|
||||||
Range r = new LongRange(nonComparable);
|
Range r = new LongRange(nonComparableNumber);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
new LongRange(null);
|
new LongRange(null);
|
||||||
|
@ -112,7 +112,7 @@ public final class LongRangeTest extends AbstractRangeTest {
|
||||||
|
|
||||||
public void testContainsNumber() {
|
public void testContainsNumber() {
|
||||||
assertEquals(false, tenToTwenty.containsNumber(null));
|
assertEquals(false, tenToTwenty.containsNumber(null));
|
||||||
assertEquals(true, tenToTwenty.containsNumber(nonComparable));
|
assertEquals(true, tenToTwenty.containsNumber(nonComparableNumber));
|
||||||
|
|
||||||
assertEquals(false, tenToTwenty.containsNumber(five));
|
assertEquals(false, tenToTwenty.containsNumber(five));
|
||||||
assertEquals(true, tenToTwenty.containsNumber(ten));
|
assertEquals(true, tenToTwenty.containsNumber(ten));
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.apache.commons.lang.math;
|
package org.apache.commons.lang.math;
|
||||||
|
|
||||||
import junit.framework.Test;
|
import junit.framework.Test;
|
||||||
|
@ -28,84 +29,72 @@ import junit.framework.TestSuite;
|
||||||
*/
|
*/
|
||||||
public final class NumberRangeTest extends AbstractRangeTest {
|
public final class NumberRangeTest extends AbstractRangeTest {
|
||||||
|
|
||||||
public NumberRangeTest(String name) {
|
|
||||||
super(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Test suite() {
|
public static Test suite() {
|
||||||
TestSuite suite = new TestSuite(NumberRangeTest.class);
|
TestSuite suite = new TestSuite(NumberRangeTest.class);
|
||||||
suite.setName("NumberRange Tests");
|
suite.setName("NumberRange Tests");
|
||||||
return suite;
|
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() {
|
public void setUp() {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
tenToTwenty = new NumberRange(ten, twenty);
|
tenToTwenty = new NumberRange(ten, twenty);
|
||||||
otherRange = new IntRange(ten, twenty);
|
otherRange = new IntRange(ten, twenty);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Range createRange(Integer integer1, Integer integer2) {
|
/**
|
||||||
return new NumberRange(integer1, integer2);
|
* Tests non-exceptional conditions for the one argument constructor.
|
||||||
}
|
*/
|
||||||
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) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testConstructor1() {
|
public void testConstructor1() {
|
||||||
NumberRange nr = new NumberRange(five);
|
NumberRange nr = new NumberRange(five);
|
||||||
assertSame(five, nr.getMinimumNumber());
|
assertSame(five, nr.getMinimumNumber());
|
||||||
assertSame(five, nr.getMaximumNumber());
|
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() {
|
public void testConstructor2() {
|
||||||
NumberRange nr = new NumberRange(five, ten);
|
NumberRange nr = new NumberRange(five, ten);
|
||||||
assertSame(five, nr.getMinimumNumber());
|
assertSame(five, nr.getMinimumNumber());
|
||||||
|
@ -114,57 +103,45 @@ public final class NumberRangeTest extends AbstractRangeTest {
|
||||||
nr = new NumberRange(ten, five);
|
nr = new NumberRange(ten, five);
|
||||||
assertSame(five, nr.getMinimumNumber());
|
assertSame(five, nr.getMinimumNumber());
|
||||||
assertSame(ten, nr.getMaximumNumber());
|
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() {
|
public void testContainsNumber() {
|
||||||
assertEquals(false, tenToTwenty.containsNumber(null));
|
assertEquals(false, tenToTwenty.containsNumber(null));
|
||||||
|
@ -177,16 +154,10 @@ public final class NumberRangeTest extends AbstractRangeTest {
|
||||||
try {
|
try {
|
||||||
tenToTwenty.containsNumber(long21);
|
tenToTwenty.containsNumber(long21);
|
||||||
fail();
|
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------------
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue