|
|
|
|
@ -16,10 +16,15 @@
@@ -16,10 +16,15 @@
|
|
|
|
|
|
|
|
|
|
package org.springframework.util; |
|
|
|
|
|
|
|
|
|
import junit.framework.TestCase; |
|
|
|
|
import org.springframework.test.AssertThrows; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.Collection; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.HashSet; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.Set; |
|
|
|
|
|
|
|
|
|
import java.util.*; |
|
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Unit tests for the {@link Assert} class. |
|
|
|
|
@ -27,158 +32,124 @@ import java.util.*;
@@ -27,158 +32,124 @@ import java.util.*;
|
|
|
|
|
* @author Keith Donald |
|
|
|
|
* @author Erwin Vervaet |
|
|
|
|
* @author Rick Evans |
|
|
|
|
* @author Arjen Poutsma |
|
|
|
|
*/ |
|
|
|
|
public class AssertTests extends TestCase { |
|
|
|
|
|
|
|
|
|
public void testInstanceOf() { |
|
|
|
|
final Set set = new HashSet(); |
|
|
|
|
Assert.isInstanceOf(HashSet.class, set); |
|
|
|
|
new AssertThrows(IllegalArgumentException.class, "a hash map is not a set") { |
|
|
|
|
public void test() throws Exception { |
|
|
|
|
Assert.isInstanceOf(HashMap.class, set); |
|
|
|
|
} |
|
|
|
|
}.runTest(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testIsNullDoesNotThrowExceptionIfArgumentIsNullWithMessage() { |
|
|
|
|
Assert.isNull(null, "Bla"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testIsNullDoesNotThrowExceptionIfArgumentIsNull() { |
|
|
|
|
Assert.isNull(null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testIsNullThrowsExceptionIfArgumentIsNotNull() { |
|
|
|
|
new AssertThrows(IllegalArgumentException.class, "object is not null") { |
|
|
|
|
public void test() throws Exception { |
|
|
|
|
Assert.isNull(new Object()); |
|
|
|
|
} |
|
|
|
|
}.runTest(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testIsNullThrowsExceptionIfArgumentIsNotNullWithMessage() { |
|
|
|
|
new AssertThrows(IllegalArgumentException.class, "object is not null") { |
|
|
|
|
public void test() throws Exception { |
|
|
|
|
Assert.isNull(new Object(), "Bla"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected void checkExceptionExpectations(Exception actualException) { |
|
|
|
|
assertEquals("Bla", actualException.getMessage()); |
|
|
|
|
super.checkExceptionExpectations(actualException); |
|
|
|
|
} |
|
|
|
|
}.runTest(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testIsTrueWithFalseExpressionThrowsException() throws Exception { |
|
|
|
|
new AssertThrows(IllegalArgumentException.class) { |
|
|
|
|
public void test() throws Exception { |
|
|
|
|
Assert.isTrue(false); |
|
|
|
|
} |
|
|
|
|
}.runTest(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testIsTrueWithTrueExpressionSunnyDay() throws Exception { |
|
|
|
|
Assert.isTrue(true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testHasLengthWithNullStringThrowsException() throws Exception { |
|
|
|
|
new AssertThrows(IllegalArgumentException.class) { |
|
|
|
|
public void test() throws Exception { |
|
|
|
|
Assert.hasLength(null); |
|
|
|
|
} |
|
|
|
|
}.runTest(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testHasLengthWithEmptyStringThrowsException() throws Exception { |
|
|
|
|
new AssertThrows(IllegalArgumentException.class) { |
|
|
|
|
public void test() throws Exception { |
|
|
|
|
Assert.hasLength(""); |
|
|
|
|
} |
|
|
|
|
}.runTest(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testHasLengthWithWhitespaceOnlyStringDoesNotThrowException() throws Exception { |
|
|
|
|
Assert.hasLength("\t "); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testHasLengthSunnyDay() throws Exception { |
|
|
|
|
Assert.hasLength("I Heart ..."); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testDoesNotContainWithNullSearchStringDoesNotThrowException() throws Exception { |
|
|
|
|
Assert.doesNotContain(null, "rod"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testDoesNotContainWithNullSubstringDoesNotThrowException() throws Exception { |
|
|
|
|
Assert.doesNotContain("A cool chick's name is Brod. ", null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testDoesNotContainWithEmptySubstringDoesNotThrowException() throws Exception { |
|
|
|
|
Assert.doesNotContain("A cool chick's name is Brod. ", ""); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testAssertNotEmptyWithNullCollectionThrowsException() throws Exception { |
|
|
|
|
new AssertThrows(IllegalArgumentException.class) { |
|
|
|
|
public void test() throws Exception { |
|
|
|
|
Assert.notEmpty((Collection) null); |
|
|
|
|
} |
|
|
|
|
}.runTest(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testAssertNotEmptyWithEmptyCollectionThrowsException() throws Exception { |
|
|
|
|
new AssertThrows(IllegalArgumentException.class) { |
|
|
|
|
public void test() throws Exception { |
|
|
|
|
Assert.notEmpty(new ArrayList()); |
|
|
|
|
} |
|
|
|
|
}.runTest(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testAssertNotEmptyWithCollectionSunnyDay() throws Exception { |
|
|
|
|
List collection = new ArrayList(); |
|
|
|
|
collection.add(""); |
|
|
|
|
Assert.notEmpty(collection); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testAssertNotEmptyWithNullMapThrowsException() throws Exception { |
|
|
|
|
new AssertThrows(IllegalArgumentException.class) { |
|
|
|
|
public void test() throws Exception { |
|
|
|
|
Assert.notEmpty((Map) null); |
|
|
|
|
} |
|
|
|
|
}.runTest(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testAssertNotEmptyWithEmptyMapThrowsException() throws Exception { |
|
|
|
|
new AssertThrows(IllegalArgumentException.class) { |
|
|
|
|
public void test() throws Exception { |
|
|
|
|
Assert.notEmpty(new HashMap()); |
|
|
|
|
} |
|
|
|
|
}.runTest(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testAssertNotEmptyWithMapSunnyDay() throws Exception { |
|
|
|
|
Map map = new HashMap(); |
|
|
|
|
map.put("", ""); |
|
|
|
|
Assert.notEmpty(map); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testIsInstanceofClassWithNullInstanceThrowsException() throws Exception { |
|
|
|
|
new AssertThrows(IllegalArgumentException.class) { |
|
|
|
|
public void test() throws Exception { |
|
|
|
|
Assert.isInstanceOf(String.class, null); |
|
|
|
|
} |
|
|
|
|
}.runTest(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testStateWithFalseExpressionThrowsException() throws Exception { |
|
|
|
|
new AssertThrows(IllegalStateException.class) { |
|
|
|
|
public void test() throws Exception { |
|
|
|
|
Assert.state(false); |
|
|
|
|
} |
|
|
|
|
}.runTest(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testStateWithTrueExpressionSunnyDay() throws Exception { |
|
|
|
|
Assert.state(true); |
|
|
|
|
} |
|
|
|
|
public class AssertTests { |
|
|
|
|
|
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
|
public void instanceOf() { |
|
|
|
|
final Set set = new HashSet(); |
|
|
|
|
Assert.isInstanceOf(HashSet.class, set); |
|
|
|
|
Assert.isInstanceOf(HashMap.class, set); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void isNullDoesNotThrowExceptionIfArgumentIsNullWithMessage() { |
|
|
|
|
Assert.isNull(null, "Bla"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void isNullDoesNotThrowExceptionIfArgumentIsNull() { |
|
|
|
|
Assert.isNull(null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
|
public void isNullThrowsExceptionIfArgumentIsNotNull() { |
|
|
|
|
Assert.isNull(new Object()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
|
public void isTrueWithFalseExpressionThrowsException() throws Exception { |
|
|
|
|
Assert.isTrue(false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void isTrueWithTrueExpressionSunnyDay() throws Exception { |
|
|
|
|
Assert.isTrue(true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
|
public void testHasLengthWithNullStringThrowsException() throws Exception { |
|
|
|
|
Assert.hasLength(null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
|
public void hasLengthWithEmptyStringThrowsException() throws Exception { |
|
|
|
|
Assert.hasLength(""); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void hasLengthWithWhitespaceOnlyStringDoesNotThrowException() throws Exception { |
|
|
|
|
Assert.hasLength("\t "); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void hasLengthSunnyDay() throws Exception { |
|
|
|
|
Assert.hasLength("I Heart ..."); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void doesNotContainWithNullSearchStringDoesNotThrowException() throws Exception { |
|
|
|
|
Assert.doesNotContain(null, "rod"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void doesNotContainWithNullSubstringDoesNotThrowException() throws Exception { |
|
|
|
|
Assert.doesNotContain("A cool chick's name is Brod. ", null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void doesNotContainWithEmptySubstringDoesNotThrowException() throws Exception { |
|
|
|
|
Assert.doesNotContain("A cool chick's name is Brod. ", ""); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
|
public void assertNotEmptyWithNullCollectionThrowsException() throws Exception { |
|
|
|
|
Assert.notEmpty((Collection) null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
|
public void assertNotEmptyWithEmptyCollectionThrowsException() throws Exception { |
|
|
|
|
Assert.notEmpty(new ArrayList()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void assertNotEmptyWithCollectionSunnyDay() throws Exception { |
|
|
|
|
List<String> collection = new ArrayList<String>(); |
|
|
|
|
collection.add(""); |
|
|
|
|
Assert.notEmpty(collection); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
|
public void assertNotEmptyWithNullMapThrowsException() throws Exception { |
|
|
|
|
Assert.notEmpty((Map) null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
|
public void assertNotEmptyWithEmptyMapThrowsException() throws Exception { |
|
|
|
|
Assert.notEmpty(new HashMap()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void assertNotEmptyWithMapSunnyDay() throws Exception { |
|
|
|
|
Map<String, String> map = new HashMap<String, String>(); |
|
|
|
|
map.put("", ""); |
|
|
|
|
Assert.notEmpty(map); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
|
public void isInstanceofClassWithNullInstanceThrowsException() throws Exception { |
|
|
|
|
Assert.isInstanceOf(String.class, null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test(expected = IllegalStateException.class) |
|
|
|
|
public void stateWithFalseExpressionThrowsException() throws Exception { |
|
|
|
|
Assert.state(false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void stateWithTrueExpressionSunnyDay() throws Exception { |
|
|
|
|
Assert.state(true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|