/*
* Copyright (c) 2012, Harald Kuhr
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.twelvemonkeys.lang;
import org.junit.Test;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import static org.junit.Assert.*;
/**
* ValidateTest
*
* @author Harald Kuhr
* @author last modified by $Author: haraldk$
* @version $Id: ValidateTest.java,v 1.0 11.04.12 09:06 haraldk Exp$
*/
public class ValidateTest {
// Not null
@Test
public void testNotNull() {
assertEquals("foo", Validate.notNull("foo"));
}
@Test(expected = IllegalArgumentException.class)
public void testNotNullNull() {
Validate.notNull(null);
}
@Test
public void testNotNullWithParameter() {
assertEquals("foo", Validate.notNull("foo", "bar"));
}
@Test(expected = IllegalArgumentException.class)
public void testNotNullWithParameterNull() {
try {
Validate.notNull(null, "xyzzy");
}
catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("xyzzy"));
throw e;
}
}
@Test
public void testNotNullWithNullParameter() {
Validate.notNull("foo", null);
}
@Test(expected = IllegalArgumentException.class)
public void testNotNullWithNullParameterNull() {
Validate.notNull(null, null);
}
// Not empty (CharSequence)
@Test
public void testNotEmptyCharSequence() {
assertEquals("foo", Validate.notEmpty("foo"));
}
@Test(expected = IllegalArgumentException.class)
public void testNotEmptyCharSequenceNull() {
Validate.notEmpty((CharSequence) null);
}
@Test(expected = IllegalArgumentException.class)
public void testNotEmptyCharSequenceEmpty() {
Validate.notEmpty("");
}
@Test(expected = IllegalArgumentException.class)
public void testNotEmptyCharSequenceOnlyWS() {
Validate.notEmpty(" \t\r");
}
@Test(expected = IllegalArgumentException.class)
public void testNotEmptyCharSequenceNullWithParameter() {
try {
Validate.notEmpty((CharSequence) null, "xyzzy");
}
catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("xyzzy"));
throw e;
}
}
@Test(expected = IllegalArgumentException.class)
public void testNotEmptyCharSequenceEmptyWithParameter() {
try {
Validate.notEmpty("", "xyzzy");
}
catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("xyzzy"));
throw e;
}
}
@Test(expected = IllegalArgumentException.class)
public void testNotEmptyCharSequenceOnlyWSWithParameter() {
try {
Validate.notEmpty(" \t", "xyzzy");
}
catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("xyzzy"));
throw e;
}
}
@Test
public void testNotEmptyCharSequenceWithParameter() {
assertEquals("foo", Validate.notEmpty("foo", "bar"));
}
@Test
public void testNotEmptyCharSequenceWithParameterNull() {
assertEquals("foo", Validate.notEmpty("foo", null));
}
@Test(expected = IllegalArgumentException.class)
public void testNotEmptyCharSequenceNullWithParameterNull() {
try {
Validate.notEmpty((CharSequence) null, null);
}
catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("parameter"));
throw e;
}
}
@Test(expected = IllegalArgumentException.class)
public void testNotEmptyCharSequenceEmptyWithParameterNull() {
try {
Validate.notEmpty("", null);
}
catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("parameter"));
throw e;
}
}
@Test(expected = IllegalArgumentException.class)
public void testNotEmptyCharSequenceOnlyWSWithParameterNull() {
try {
Validate.notEmpty(" \t\t \n", null);
}
catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("parameter"));
throw e;
}
}
// Not empty (array)
@Test
public void testNotEmptyArray() {
Integer[] array = new Integer[2];
assertSame(array, Validate.notEmpty(array));
}
@Test(expected = IllegalArgumentException.class)
public void testNotEmptyArrayNull() {
Validate.notEmpty((Object[]) null);
}
@Test(expected = IllegalArgumentException.class)
public void testNotEmptyArrayEmpty() {
Validate.notEmpty(new String[0]);
}
@Test
public void testNotEmptyArrayParameter() {
Integer[] array = new Integer[2];
assertSame(array, Validate.notEmpty(array, "bar"));
}
@Test(expected = IllegalArgumentException.class)
public void testNotEmptyArrayNullParameter() {
try {
Validate.notEmpty((Object[]) null, "xyzzy");
}
catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("xyzzy"));
throw e;
}
}
@Test(expected = IllegalArgumentException.class)
public void testNotEmptyArrayEmptyParameter() {
try {
Validate.notEmpty(new Float[0], "xyzzy");
}
catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("xyzzy"));
throw e;
}
}
@Test
public void testNotEmptyArrayWithParameterNull() {
Byte[] array = new Byte[1];
assertSame(array, Validate.notEmpty(array, null));
}
@Test(expected = IllegalArgumentException.class)
public void testNotEmptyArrayNullWithParameterNull() {
try {
Validate.notEmpty((Object[]) null, null);
}
catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("parameter"));
throw e;
}
}
@Test(expected = IllegalArgumentException.class)
public void testNotEmptyArrayEmptyWithParameterNull() {
try {
Validate.notEmpty(new Object[0], null);
}
catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("parameter"));
throw e;
}
}
// Not empty (Collection)
@Test
public void testNotEmptyCollection() {
Collection collection = Arrays.asList(new Integer[2]);
assertSame(collection, Validate.notEmpty(collection));
}
@Test(expected = IllegalArgumentException.class)
public void testNotEmptyCollectionNull() {
Validate.notEmpty((Collection>) null);
}
@Test(expected = IllegalArgumentException.class)
public void testNotEmptyCollectionEmpty() {
Validate.notEmpty(Collections.emptySet());
}
@Test
public void testNotEmptyCollectionParameter() {
List collection = Collections.singletonList(1);
assertSame(collection, Validate.notEmpty(collection, "bar"));
}
@Test(expected = IllegalArgumentException.class)
public void testNotEmptyCollectionNullParameter() {
try {
Validate.notEmpty((Collection>) null, "xyzzy");
}
catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("xyzzy"));
throw e;
}
}
@Test(expected = IllegalArgumentException.class)
public void testNotEmptyCollectionEmptyParameter() {
try {
Validate.notEmpty(new ArrayList