Extra tests inspired by Clover

bug 22098, from Phil Steitz


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137569 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-08-04 19:51:00 +00:00
parent 30426cf21e
commit cf7f46f654
1 changed files with 37 additions and 6 deletions

View File

@ -67,7 +67,7 @@ import junit.textui.TestRunner;
* Unit tests {@link org.apache.commons.lang.util.Validate}. * Unit tests {@link org.apache.commons.lang.util.Validate}.
* *
* @author Stephen Colebourne * @author Stephen Colebourne
* @version $Id: ValidateTest.java,v 1.1 2002/12/29 22:09:53 scolebourne Exp $ * @version $Id: ValidateTest.java,v 1.2 2003/08/04 19:51:00 scolebourne Exp $
*/ */
public class ValidateTest extends TestCase { public class ValidateTest extends TestCase {
@ -98,6 +98,7 @@ public class ValidateTest extends TestCase {
Validate.isTrue(true); Validate.isTrue(true);
try { try {
Validate.isTrue(false); Validate.isTrue(false);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("The validated expression is false", ex.getMessage()); assertEquals("The validated expression is false", ex.getMessage());
} }
@ -108,6 +109,7 @@ public class ValidateTest extends TestCase {
Validate.isTrue(true, "MSG"); Validate.isTrue(true, "MSG");
try { try {
Validate.isTrue(false, "MSG"); Validate.isTrue(false, "MSG");
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage()); assertEquals("MSG", ex.getMessage());
} }
@ -118,6 +120,7 @@ public class ValidateTest extends TestCase {
Validate.isTrue(true, "MSG", new Integer(6)); Validate.isTrue(true, "MSG", new Integer(6));
try { try {
Validate.isTrue(false, "MSG", new Integer(6)); Validate.isTrue(false, "MSG", new Integer(6));
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("MSG6", ex.getMessage()); assertEquals("MSG6", ex.getMessage());
} }
@ -128,6 +131,7 @@ public class ValidateTest extends TestCase {
Validate.isTrue(true, "MSG", 7); Validate.isTrue(true, "MSG", 7);
try { try {
Validate.isTrue(false, "MSG", 7); Validate.isTrue(false, "MSG", 7);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("MSG7", ex.getMessage()); assertEquals("MSG7", ex.getMessage());
} }
@ -138,6 +142,7 @@ public class ValidateTest extends TestCase {
Validate.isTrue(true, "MSG", 7.4d); Validate.isTrue(true, "MSG", 7.4d);
try { try {
Validate.isTrue(false, "MSG", 7.4d); Validate.isTrue(false, "MSG", 7.4d);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("MSG7.4", ex.getMessage()); assertEquals("MSG7.4", ex.getMessage());
} }
@ -148,6 +153,7 @@ public class ValidateTest extends TestCase {
Validate.notNull(new Object()); Validate.notNull(new Object());
try { try {
Validate.notNull(null); Validate.notNull(null);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("The validated object is null", ex.getMessage()); assertEquals("The validated object is null", ex.getMessage());
} }
@ -158,6 +164,7 @@ public class ValidateTest extends TestCase {
Validate.notNull(new Object(), "MSG"); Validate.notNull(new Object(), "MSG");
try { try {
Validate.notNull(null, "MSG"); Validate.notNull(null, "MSG");
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage()); assertEquals("MSG", ex.getMessage());
} }
@ -168,11 +175,13 @@ public class ValidateTest extends TestCase {
Validate.notEmpty(new Object[] {null}); Validate.notEmpty(new Object[] {null});
try { try {
Validate.notEmpty((Object[]) null); Validate.notEmpty((Object[]) null);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("The validated array is empty", ex.getMessage()); assertEquals("The validated array is empty", ex.getMessage());
} }
try { try {
Validate.notEmpty(new Object[0]); Validate.notEmpty(new Object[0]);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("The validated array is empty", ex.getMessage()); assertEquals("The validated array is empty", ex.getMessage());
} }
@ -183,11 +192,13 @@ public class ValidateTest extends TestCase {
Validate.notEmpty(new Object[] {null}, "MSG"); Validate.notEmpty(new Object[] {null}, "MSG");
try { try {
Validate.notEmpty((Object[]) null, "MSG"); Validate.notEmpty((Object[]) null, "MSG");
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage()); assertEquals("MSG", ex.getMessage());
} }
try { try {
Validate.notEmpty(new Object[0], "MSG"); Validate.notEmpty(new Object[0], "MSG");
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage()); assertEquals("MSG", ex.getMessage());
} }
@ -198,11 +209,13 @@ public class ValidateTest extends TestCase {
Collection coll = new ArrayList(); Collection coll = new ArrayList();
try { try {
Validate.notEmpty((Collection) null); Validate.notEmpty((Collection) null);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("The validated collection is empty", ex.getMessage()); assertEquals("The validated collection is empty", ex.getMessage());
} }
try { try {
Validate.notEmpty(coll); Validate.notEmpty(coll);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("The validated collection is empty", ex.getMessage()); assertEquals("The validated collection is empty", ex.getMessage());
} }
@ -215,11 +228,13 @@ public class ValidateTest extends TestCase {
Collection coll = new ArrayList(); Collection coll = new ArrayList();
try { try {
Validate.notEmpty((Collection) null, "MSG"); Validate.notEmpty((Collection) null, "MSG");
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage()); assertEquals("MSG", ex.getMessage());
} }
try { try {
Validate.notEmpty(coll, "MSG"); Validate.notEmpty(coll, "MSG");
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage()); assertEquals("MSG", ex.getMessage());
} }
@ -232,11 +247,13 @@ public class ValidateTest extends TestCase {
Map map = new HashMap(); Map map = new HashMap();
try { try {
Validate.notEmpty((Map) null); Validate.notEmpty((Map) null);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("The validated map is empty", ex.getMessage()); assertEquals("The validated map is empty", ex.getMessage());
} }
try { try {
Validate.notEmpty(map); Validate.notEmpty(map);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("The validated map is empty", ex.getMessage()); assertEquals("The validated map is empty", ex.getMessage());
} }
@ -249,11 +266,13 @@ public class ValidateTest extends TestCase {
Map map = new HashMap(); Map map = new HashMap();
try { try {
Validate.notEmpty((Map) null, "MSG"); Validate.notEmpty((Map) null, "MSG");
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage()); assertEquals("MSG", ex.getMessage());
} }
try { try {
Validate.notEmpty(map, "MSG"); Validate.notEmpty(map, "MSG");
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage()); assertEquals("MSG", ex.getMessage());
} }
@ -266,11 +285,13 @@ public class ValidateTest extends TestCase {
Validate.notEmpty("hjl"); Validate.notEmpty("hjl");
try { try {
Validate.notEmpty((String) null); Validate.notEmpty((String) null);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("The validated string is empty", ex.getMessage()); assertEquals("The validated string is empty", ex.getMessage());
} }
try { try {
Validate.notEmpty(""); Validate.notEmpty("");
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("The validated string is empty", ex.getMessage()); assertEquals("The validated string is empty", ex.getMessage());
} }
@ -278,14 +299,16 @@ public class ValidateTest extends TestCase {
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
public void testNotEmptyString2() { public void testNotEmptyString2() {
Validate.notEmpty(new Object[] {null}, "MSG"); Validate.notEmpty("a", "MSG");
try { try {
Validate.notEmpty((String) null, "MSG"); Validate.notEmpty((String) null, "MSG");
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage()); assertEquals("MSG", ex.getMessage());
} }
try { try {
Validate.notEmpty("", "MSG"); Validate.notEmpty("", "MSG");
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage()); assertEquals("MSG", ex.getMessage());
} }
@ -297,12 +320,14 @@ public class ValidateTest extends TestCase {
Validate.noNullElements(array); Validate.noNullElements(array);
try { try {
Validate.noNullElements((Object[]) null); Validate.noNullElements((Object[]) null);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("The validated object is null", ex.getMessage()); assertEquals("The validated object is null", ex.getMessage());
} }
array[1] = null; array[1] = null;
try { try {
Validate.notEmpty(array); Validate.noNullElements(array);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("The validated array contains null element at index: 1", ex.getMessage()); assertEquals("The validated array contains null element at index: 1", ex.getMessage());
} }
@ -314,12 +339,14 @@ public class ValidateTest extends TestCase {
Validate.noNullElements(array, "MSG"); Validate.noNullElements(array, "MSG");
try { try {
Validate.noNullElements((Object[]) null, "MSG"); Validate.noNullElements((Object[]) null, "MSG");
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("The validated object is null", ex.getMessage()); assertEquals("The validated object is null", ex.getMessage());
} }
array[1] = null; array[1] = null;
try { try {
Validate.notEmpty(array, "MSG"); Validate.noNullElements(array, "MSG");
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage()); assertEquals("MSG", ex.getMessage());
} }
@ -333,12 +360,14 @@ public class ValidateTest extends TestCase {
Validate.noNullElements(coll); Validate.noNullElements(coll);
try { try {
Validate.noNullElements((Collection) null); Validate.noNullElements((Collection) null);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("The validated object is null", ex.getMessage()); assertEquals("The validated object is null", ex.getMessage());
} }
coll.set(1, null); coll.set(1, null);
try { try {
Validate.notEmpty(coll); Validate.noNullElements(coll);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("The validated collection contains null element at index: 1", ex.getMessage()); assertEquals("The validated collection contains null element at index: 1", ex.getMessage());
} }
@ -352,12 +381,14 @@ public class ValidateTest extends TestCase {
Validate.noNullElements(coll, "MSG"); Validate.noNullElements(coll, "MSG");
try { try {
Validate.noNullElements((Collection) null, "MSG"); Validate.noNullElements((Collection) null, "MSG");
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("The validated object is null", ex.getMessage()); assertEquals("The validated object is null", ex.getMessage());
} }
coll.set(1, null); coll.set(1, null);
try { try {
Validate.notEmpty(coll, "MSG"); Validate.noNullElements(coll, "MSG");
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage()); assertEquals("MSG", ex.getMessage());
} }