Revisit Assert to avoid single-arg assert methods (with refined messages)
Issue: SPR-15196
This commit is contained in:
@@ -59,7 +59,7 @@ public class Constants {
|
||||
* @throws IllegalArgumentException if the supplied {@code clazz} is {@code null}
|
||||
*/
|
||||
public Constants(Class<?> clazz) {
|
||||
Assert.notNull(clazz);
|
||||
Assert.notNull(clazz, "Class must not be null");
|
||||
this.className = clazz.getName();
|
||||
Field[] fields = clazz.getFields();
|
||||
for (Field field : fields) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -47,9 +47,9 @@ public abstract class ParameterizedTypeReference<T> {
|
||||
protected ParameterizedTypeReference() {
|
||||
Class<?> parameterizedTypeReferenceSubclass = findParameterizedTypeReferenceSubclass(getClass());
|
||||
Type type = parameterizedTypeReferenceSubclass.getGenericSuperclass();
|
||||
Assert.isInstanceOf(ParameterizedType.class, type);
|
||||
Assert.isInstanceOf(ParameterizedType.class, type, "Type must be a parameterized type");
|
||||
ParameterizedType parameterizedType = (ParameterizedType) type;
|
||||
Assert.isTrue(parameterizedType.getActualTypeArguments().length == 1);
|
||||
Assert.isTrue(parameterizedType.getActualTypeArguments().length == 1, "Number of type arguments must be 1");
|
||||
this.type = parameterizedType.getActualTypeArguments()[0];
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -408,7 +408,10 @@ abstract class SerializableTypeWrapper {
|
||||
private void readObject(ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
|
||||
inputStream.defaultReadObject();
|
||||
this.method = ReflectionUtils.findMethod(this.declaringClass, this.methodName);
|
||||
Assert.state(Type.class == this.method.getReturnType() || Type[].class == this.method.getReturnType());
|
||||
if (this.method.getReturnType() != Type.class && this.method.getReturnType() != Type[].class) {
|
||||
throw new IllegalStateException(
|
||||
"Invalid return type on deserialized method - needs to be Type or Type[]: " + this.method);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -23,15 +23,15 @@ import java.util.List;
|
||||
import joptsimple.OptionSet;
|
||||
import joptsimple.OptionSpec;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link CommandLinePropertySource} implementation backed by a JOpt {@link OptionSet}.
|
||||
*
|
||||
* <h2>Typical usage</h2>
|
||||
*
|
||||
* Configure and execute an {@code OptionParser} against the {@code String[]} of arguments
|
||||
* supplied to the {@code main} method, and create a {@link JOptCommandLinePropertySource}
|
||||
* using the resulting {@code OptionSet} object:
|
||||
*
|
||||
* <pre class="code">
|
||||
* public static void main(String[] args) {
|
||||
* OptionParser parser = new OptionParser();
|
||||
@@ -44,7 +44,7 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* See {@link CommandLinePropertySource} for complete general usage examples.
|
||||
*
|
||||
* <p>Requires JOpt version 4.3 or higher. Tested against JOpt up until 4.6.
|
||||
* <p>Requires JOpt Simple version 4.3 or higher. Tested against JOpt up until 5.0.
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @author Juergen Hoeller
|
||||
@@ -98,7 +98,7 @@ public class JOptCommandLinePropertySource extends CommandLinePropertySource<Opt
|
||||
List<?> argValues = this.source.valuesOf(name);
|
||||
List<String> stringArgValues = new ArrayList<>();
|
||||
for (Object argValue : argValues) {
|
||||
stringArgValues.add(argValue instanceof String ? (String) argValue : argValue.toString());
|
||||
stringArgValues.add(argValue.toString());
|
||||
}
|
||||
if (stringArgValues.isEmpty()) {
|
||||
return (this.source.has(name) ? Collections.emptyList() : null);
|
||||
@@ -111,8 +111,7 @@ public class JOptCommandLinePropertySource extends CommandLinePropertySource<Opt
|
||||
List<?> argValues = this.source.nonOptionArguments();
|
||||
List<String> stringArgValues = new ArrayList<>();
|
||||
for (Object argValue : argValues) {
|
||||
Assert.isInstanceOf(String.class, argValue, "Argument values must be of type String");
|
||||
stringArgValues.add((String) argValue);
|
||||
stringArgValues.add(argValue.toString());
|
||||
}
|
||||
return (stringArgValues.isEmpty() ? Collections.emptyList() :
|
||||
Collections.unmodifiableList(stringArgValues));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -49,9 +49,9 @@ import java.util.function.Supplier;
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
* @author Colin Sampaleanu
|
||||
* @author Rob Harrop
|
||||
* @author Sam Brannen
|
||||
* @since 1.1.2
|
||||
*/
|
||||
public abstract class Assert {
|
||||
@@ -66,6 +66,7 @@ public abstract class Assert {
|
||||
* @param messageSupplier a supplier for the exception message to use if the
|
||||
* assertion fails
|
||||
* @throws IllegalArgumentException if {@code expression} is {@code false}
|
||||
* @since 5.0
|
||||
*/
|
||||
public static void isTrue(boolean expression, Supplier<String> messageSupplier) {
|
||||
if (!expression) {
|
||||
@@ -87,17 +88,6 @@ public abstract class Assert {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert a boolean expression, throwing an {@code IllegalArgumentException}
|
||||
* if the expression evaluates to {@code false}.
|
||||
* <pre class="code">Assert.isTrue(i > 0);</pre>
|
||||
* @param expression a boolean expression
|
||||
* @throws IllegalArgumentException if {@code expression} is {@code false}
|
||||
*/
|
||||
public static void isTrue(boolean expression) {
|
||||
isTrue(expression, "[Assertion failed] - this expression must be true");
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that an object is {@code null}.
|
||||
* <pre class="code">
|
||||
@@ -107,6 +97,7 @@ public abstract class Assert {
|
||||
* @param messageSupplier a supplier for the exception message to use if the
|
||||
* assertion fails
|
||||
* @throws IllegalArgumentException if the object is not {@code null}
|
||||
* @since 5.0
|
||||
*/
|
||||
public static void isNull(Object object, Supplier<String> messageSupplier) {
|
||||
if (object != null) {
|
||||
@@ -127,16 +118,6 @@ public abstract class Assert {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that an object is {@code null}.
|
||||
* <pre class="code">Assert.isNull(value);</pre>
|
||||
* @param object the object to check
|
||||
* @throws IllegalArgumentException if the object is not {@code null}
|
||||
*/
|
||||
public static void isNull(Object object) {
|
||||
isNull(object, "[Assertion failed] - the object argument must be null");
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that an object is not {@code null}.
|
||||
* <pre class="code">
|
||||
@@ -146,6 +127,7 @@ public abstract class Assert {
|
||||
* @param messageSupplier a supplier for the exception message to use if the
|
||||
* assertion fails
|
||||
* @throws IllegalArgumentException if the object is {@code null}
|
||||
* @since 5.0
|
||||
*/
|
||||
public static void notNull(Object object, Supplier<String> messageSupplier) {
|
||||
if (object == null) {
|
||||
@@ -166,16 +148,6 @@ public abstract class Assert {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that an object is not {@code null}.
|
||||
* <pre class="code">Assert.notNull(clazz);</pre>
|
||||
* @param object the object to check
|
||||
* @throws IllegalArgumentException if the object is {@code null}
|
||||
*/
|
||||
public static void notNull(Object object) {
|
||||
notNull(object, "[Assertion failed] - this argument is required; it must not be null");
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the given String is not empty; that is,
|
||||
* it must not be {@code null} and not the empty String.
|
||||
@@ -187,6 +159,7 @@ public abstract class Assert {
|
||||
* assertion fails
|
||||
* @see StringUtils#hasLength
|
||||
* @throws IllegalArgumentException if the text is empty
|
||||
* @since 5.0
|
||||
*/
|
||||
public static void hasLength(String text, Supplier<String> messageSupplier) {
|
||||
if (!StringUtils.hasLength(text)) {
|
||||
@@ -209,19 +182,6 @@ public abstract class Assert {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the given String is not empty; that is,
|
||||
* it must not be {@code null} and not the empty String.
|
||||
* <pre class="code">Assert.hasLength(name);</pre>
|
||||
* @param text the String to check
|
||||
* @see StringUtils#hasLength
|
||||
* @throws IllegalArgumentException if the text is empty
|
||||
*/
|
||||
public static void hasLength(String text) {
|
||||
hasLength(text,
|
||||
"[Assertion failed] - this String argument must have length; it must not be null or empty");
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the given String contains valid text content; that is, it must not
|
||||
* be {@code null} and must contain at least one non-whitespace character.
|
||||
@@ -233,6 +193,7 @@ public abstract class Assert {
|
||||
* assertion fails
|
||||
* @see StringUtils#hasText
|
||||
* @throws IllegalArgumentException if the text does not contain valid text content
|
||||
* @since 5.0
|
||||
*/
|
||||
public static void hasText(String text, Supplier<String> messageSupplier) {
|
||||
if (!StringUtils.hasText(text)) {
|
||||
@@ -255,19 +216,6 @@ public abstract class Assert {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the given String contains valid text content; that is, it must not
|
||||
* be {@code null} and must contain at least one non-whitespace character.
|
||||
* <pre class="code">Assert.hasText(name, "'name' must not be empty");</pre>
|
||||
* @param text the String to check
|
||||
* @see StringUtils#hasText
|
||||
* @throws IllegalArgumentException if the text does not contain valid text content
|
||||
*/
|
||||
public static void hasText(String text) {
|
||||
hasText(text,
|
||||
"[Assertion failed] - this String argument must have text; it must not be null, empty, or blank");
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the given text does not contain the given substring.
|
||||
* <pre class="code">
|
||||
@@ -278,6 +226,7 @@ public abstract class Assert {
|
||||
* @param messageSupplier a supplier for the exception message to use if the
|
||||
* assertion fails
|
||||
* @throws IllegalArgumentException if the text contains the substring
|
||||
* @since 5.0
|
||||
*/
|
||||
public static void doesNotContain(String textToSearch, String substring, Supplier<String> messageSupplier) {
|
||||
if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) &&
|
||||
@@ -301,18 +250,6 @@ public abstract class Assert {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the given text does not contain the given substring.
|
||||
* <pre class="code">Assert.doesNotContain(name, "rod");</pre>
|
||||
* @param textToSearch the text to search
|
||||
* @param substring the substring to find within the text
|
||||
* @throws IllegalArgumentException if the text contains the substring
|
||||
*/
|
||||
public static void doesNotContain(String textToSearch, String substring) {
|
||||
doesNotContain(textToSearch, substring,
|
||||
() -> "[Assertion failed] - this String argument must not contain the substring [" + substring + "]");
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that an array contains elements; that is, it must not be
|
||||
* {@code null} and must contain at least one element.
|
||||
@@ -323,6 +260,7 @@ public abstract class Assert {
|
||||
* @param messageSupplier a supplier for the exception message to use if the
|
||||
* assertion fails
|
||||
* @throws IllegalArgumentException if the object array is {@code null} or contains no elements
|
||||
* @since 5.0
|
||||
*/
|
||||
public static void notEmpty(Object[] array, Supplier<String> messageSupplier) {
|
||||
if (ObjectUtils.isEmpty(array)) {
|
||||
@@ -344,18 +282,6 @@ public abstract class Assert {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that an array contains elements; that is, it must not be
|
||||
* {@code null} and must contain at least one element.
|
||||
* <pre class="code">Assert.notEmpty(array);</pre>
|
||||
* @param array the array to check
|
||||
* @throws IllegalArgumentException if the object array is {@code null} or
|
||||
* contains no elements
|
||||
*/
|
||||
public static void notEmpty(Object[] array) {
|
||||
notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element");
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that an array contains no {@code null} elements.
|
||||
* <p>Note: Does not complain if the array is empty!
|
||||
@@ -366,6 +292,7 @@ public abstract class Assert {
|
||||
* @param messageSupplier a supplier for the exception message to use if the
|
||||
* assertion fails
|
||||
* @throws IllegalArgumentException if the object array contains a {@code null} element
|
||||
* @since 5.0
|
||||
*/
|
||||
public static void noNullElements(Object[] array, Supplier<String> messageSupplier) {
|
||||
if (array != null) {
|
||||
@@ -395,17 +322,6 @@ public abstract class Assert {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that an array contains no {@code null} elements.
|
||||
* <p>Note: Does not complain if the array is empty!
|
||||
* <pre class="code">Assert.noNullElements(array);</pre>
|
||||
* @param array the array to check
|
||||
* @throws IllegalArgumentException if the object array contains a {@code null} element
|
||||
*/
|
||||
public static void noNullElements(Object[] array) {
|
||||
noNullElements(array, "[Assertion failed] - this array must not contain any null elements");
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that a collection contains elements; that is, it must not be
|
||||
* {@code null} and must contain at least one element.
|
||||
@@ -417,6 +333,7 @@ public abstract class Assert {
|
||||
* assertion fails
|
||||
* @throws IllegalArgumentException if the collection is {@code null} or
|
||||
* contains no elements
|
||||
* @since 5.0
|
||||
*/
|
||||
public static void notEmpty(Collection<?> collection, Supplier<String> messageSupplier) {
|
||||
if (CollectionUtils.isEmpty(collection)) {
|
||||
@@ -439,19 +356,6 @@ public abstract class Assert {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that a collection contains elements; that is, it must not be
|
||||
* {@code null} and must contain at least one element.
|
||||
* <pre class="code">Assert.notEmpty(collection, "Collection must contain elements");</pre>
|
||||
* @param collection the collection to check
|
||||
* @throws IllegalArgumentException if the collection is {@code null} or
|
||||
* contains no elements
|
||||
*/
|
||||
public static void notEmpty(Collection<?> collection) {
|
||||
notEmpty(collection,
|
||||
"[Assertion failed] - this collection must not be empty: it must contain at least 1 element");
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that a Map contains entries; that is, it must not be {@code null}
|
||||
* and must contain at least one entry.
|
||||
@@ -462,6 +366,7 @@ public abstract class Assert {
|
||||
* @param messageSupplier a supplier for the exception message to use if the
|
||||
* assertion fails
|
||||
* @throws IllegalArgumentException if the map is {@code null} or contains no entries
|
||||
* @since 5.0
|
||||
*/
|
||||
public static void notEmpty(Map<?, ?> map, Supplier<String> messageSupplier) {
|
||||
if (CollectionUtils.isEmpty(map)) {
|
||||
@@ -483,29 +388,6 @@ public abstract class Assert {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that a Map contains entries; that is, it must not be {@code null}
|
||||
* and must contain at least one entry.
|
||||
* <pre class="code">Assert.notEmpty(map);</pre>
|
||||
* @param map the map to check
|
||||
* @throws IllegalArgumentException if the map is {@code null} or contains no entries
|
||||
*/
|
||||
public static void notEmpty(Map<?, ?> map) {
|
||||
notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry");
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the provided object is an instance of the provided class.
|
||||
* <pre class="code">Assert.instanceOf(Foo.class, foo);</pre>
|
||||
* @param type the type to check against
|
||||
* @param obj the object to check
|
||||
* @throws IllegalArgumentException if the object is not an instance of type
|
||||
* @see Class#isInstance
|
||||
*/
|
||||
public static void isInstanceOf(Class<?> type, Object obj) {
|
||||
isInstanceOf(type, obj, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the provided object is an instance of the provided class.
|
||||
* <pre class="code">
|
||||
@@ -519,11 +401,12 @@ public abstract class Assert {
|
||||
* in ":" or "." so that the generated message looks OK when appended to it.
|
||||
* @throws IllegalArgumentException if the object is not an instance of type
|
||||
* @see Class#isInstance
|
||||
* @since 5.0
|
||||
*/
|
||||
public static void isInstanceOf(Class<?> type, Object obj, Supplier<String> messageSupplier) {
|
||||
notNull(type, "Type to check against must not be null");
|
||||
if (!type.isInstance(obj)) {
|
||||
isInstanceCheckFailed(type, obj, nullSafeGet(messageSupplier));
|
||||
instanceCheckFailed(type, obj, nullSafeGet(messageSupplier));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -541,26 +424,26 @@ public abstract class Assert {
|
||||
public static void isInstanceOf(Class<?> type, Object obj, String message) {
|
||||
notNull(type, "Type to check against must not be null");
|
||||
if (!type.isInstance(obj)) {
|
||||
isInstanceCheckFailed(type, obj, message);
|
||||
instanceCheckFailed(type, obj, message);
|
||||
}
|
||||
}
|
||||
|
||||
private static void isInstanceCheckFailed(Class<?> type, Object obj, String message) {
|
||||
throw new IllegalArgumentException(
|
||||
(StringUtils.hasLength(message) ? message + " " : "") +
|
||||
"Object of class [" + (obj != null ? obj.getClass().getName() : "null") +
|
||||
"] must be an instance of " + type);
|
||||
/**
|
||||
* Assert that the provided object is an instance of the provided class.
|
||||
* <pre class="code">Assert.instanceOf(Foo.class, foo);</pre>
|
||||
* @param type the type to check against
|
||||
* @param obj the object to check
|
||||
* @throws IllegalArgumentException if the object is not an instance of type
|
||||
* @see Class#isInstance
|
||||
*/
|
||||
public static void isInstanceOf(Class<?> type, Object obj) {
|
||||
isInstanceOf(type, obj, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that {@code superType.isAssignableFrom(subType)} is {@code true}.
|
||||
* <pre class="code">Assert.isAssignable(Number.class, myClass);</pre>
|
||||
* @param superType the super type to check
|
||||
* @param subType the sub type to check
|
||||
* @throws IllegalArgumentException if the classes are not assignable
|
||||
*/
|
||||
public static void isAssignable(Class<?> superType, Class<?> subType) {
|
||||
isAssignable(superType, subType, "");
|
||||
private static void instanceCheckFailed(Class<?> type, Object obj, String message) {
|
||||
String className = (obj != null ? obj.getClass().getName() : "null");
|
||||
throw new IllegalArgumentException(StringUtils.hasLength(message) ? message + ": " + className :
|
||||
"Object of class [" + className + "] must be an instance of " + type);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -575,11 +458,12 @@ public abstract class Assert {
|
||||
* by this method in order to provide further context. It should normally end
|
||||
* in ":" or "." so that the generated message looks OK when appended to it.
|
||||
* @throws IllegalArgumentException if the classes are not assignable
|
||||
* @since 5.0
|
||||
*/
|
||||
public static void isAssignable(Class<?> superType, Class<?> subType, Supplier<String> messageSupplier) {
|
||||
notNull(superType, "Super type to check against must not be null");
|
||||
if (subType == null || !superType.isAssignableFrom(subType)) {
|
||||
isAssignableCheckFailed(superType, subType, nullSafeGet(messageSupplier));
|
||||
assignableCheckFailed(superType, subType, nullSafeGet(messageSupplier));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -596,12 +480,23 @@ public abstract class Assert {
|
||||
public static void isAssignable(Class<?> superType, Class<?> subType, String message) {
|
||||
notNull(superType, "Super type to check against must not be null");
|
||||
if (subType == null || !superType.isAssignableFrom(subType)) {
|
||||
isAssignableCheckFailed(superType, subType, message);
|
||||
assignableCheckFailed(superType, subType, message);
|
||||
}
|
||||
}
|
||||
|
||||
private static void isAssignableCheckFailed(Class<?> superType, Class<?> subType, String message) {
|
||||
throw new IllegalArgumentException((StringUtils.hasLength(message) ? message + " " : "") +
|
||||
/**
|
||||
* Assert that {@code superType.isAssignableFrom(subType)} is {@code true}.
|
||||
* <pre class="code">Assert.isAssignable(Number.class, myClass);</pre>
|
||||
* @param superType the super type to check
|
||||
* @param subType the sub type to check
|
||||
* @throws IllegalArgumentException if the classes are not assignable
|
||||
*/
|
||||
public static void isAssignable(Class<?> superType, Class<?> subType) {
|
||||
isAssignable(superType, subType, "");
|
||||
}
|
||||
|
||||
private static void assignableCheckFailed(Class<?> superType, Class<?> subType, String message) {
|
||||
throw new IllegalArgumentException(StringUtils.hasLength(message) ? message + ": " + subType :
|
||||
subType + " is not assignable to " + superType);
|
||||
}
|
||||
|
||||
@@ -618,6 +513,7 @@ public abstract class Assert {
|
||||
* @param messageSupplier a supplier for the exception message to use if the
|
||||
* assertion fails
|
||||
* @throws IllegalStateException if {@code expression} is {@code false}
|
||||
* @since 5.0
|
||||
*/
|
||||
public static void state(boolean expression, Supplier<String> messageSupplier) {
|
||||
if (!expression) {
|
||||
@@ -641,19 +537,6 @@ public abstract class Assert {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert a boolean expression, throwing an {@link IllegalStateException}
|
||||
* if the expression evaluates to {@code false}.
|
||||
* <p>Call {@link #isTrue} if you wish to throw an {@code IllegalArgumentException}
|
||||
* on an assertion failure.
|
||||
* <pre class="code">Assert.state(id == null);</pre>
|
||||
* @param expression a boolean expression
|
||||
* @throws IllegalStateException if {@code expression} is {@code false}
|
||||
*/
|
||||
public static void state(boolean expression) {
|
||||
state(expression, "[Assertion failed] - this state invariant must be true");
|
||||
}
|
||||
|
||||
private static String nullSafeGet(Supplier<String> messageSupplier) {
|
||||
return (messageSupplier != null ? messageSupplier.get() : null);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -903,7 +903,7 @@ public class ConcurrentReferenceHashMap<K, V> extends AbstractMap<K, V> implemen
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
Assert.state(this.last != null);
|
||||
Assert.state(this.last != null, "No element to remove");
|
||||
ConcurrentReferenceHashMap.this.remove(this.last.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -35,6 +35,7 @@ import static org.hamcrest.CoreMatchers.*;
|
||||
* @author Rick Evans
|
||||
* @author Arjen Poutsma
|
||||
* @author Sam Brannen
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class AssertTests {
|
||||
|
||||
@@ -43,14 +44,15 @@ public class AssertTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void isTrue() {
|
||||
Assert.isTrue(true);
|
||||
public void isTrueWithMessage() {
|
||||
Assert.isTrue(true, "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isTrueWithFalseExpression() {
|
||||
public void isTrueWithFalse() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
Assert.isTrue(false);
|
||||
thrown.expectMessage("enigma");
|
||||
Assert.isTrue(false, "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -72,22 +74,11 @@ public class AssertTests {
|
||||
Assert.isTrue(false, (Supplier<String>) null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNull() {
|
||||
Assert.isNull(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNullWithMessage() {
|
||||
Assert.isNull(null, "Bla");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNullWithNonNullObject() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
Assert.isNull(new Object());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNullWithMessageSupplier() {
|
||||
Assert.isNull(null, () -> "enigma");
|
||||
@@ -108,8 +99,8 @@ public class AssertTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notNull() {
|
||||
Assert.notNull("foo");
|
||||
public void notNullWithMessage() {
|
||||
Assert.notNull("foo", "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -133,24 +124,26 @@ public class AssertTests {
|
||||
|
||||
@Test
|
||||
public void hasLength() {
|
||||
Assert.hasLength("I Heart ...");
|
||||
Assert.hasLength("I Heart ...", "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hasLengthWithWhitespaceOnly() {
|
||||
Assert.hasLength("\t ");
|
||||
Assert.hasLength("\t ", "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hasLengthWithEmptyString() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
Assert.hasLength("");
|
||||
thrown.expectMessage("enigma");
|
||||
Assert.hasLength("", "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hasLengthWithNull() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
Assert.hasLength(null);
|
||||
thrown.expectMessage("enigma");
|
||||
Assert.hasLength(null, "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -186,7 +179,7 @@ public class AssertTests {
|
||||
|
||||
@Test
|
||||
public void hasText() {
|
||||
Assert.hasText("foo");
|
||||
Assert.hasText("foo", "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -204,7 +197,7 @@ public class AssertTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hasTextWithNullAndMessage() {
|
||||
public void hasTextWithNull() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("enigma");
|
||||
Assert.hasText(null, "enigma");
|
||||
@@ -245,21 +238,21 @@ public class AssertTests {
|
||||
|
||||
@Test
|
||||
public void doesNotContainWithNullSearchString() {
|
||||
Assert.doesNotContain(null, "rod");
|
||||
Assert.doesNotContain(null, "rod", "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doesNotContainWithNullSubstring() {
|
||||
Assert.doesNotContain("A cool chick's name is Brod.", null);
|
||||
Assert.doesNotContain("A cool chick's name is Brod.", null, "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doesNotContainWithEmptySubstring() {
|
||||
Assert.doesNotContain("A cool chick's name is Brod.", "");
|
||||
Assert.doesNotContain("A cool chick's name is Brod.", "", "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doesNotContainWithNullSearchStringAndNullSubstringAndMessage() {
|
||||
public void doesNotContainWithNullSearchStringAndNullSubstring() {
|
||||
Assert.doesNotContain(null, null, "enigma");
|
||||
}
|
||||
|
||||
@@ -299,12 +292,26 @@ public class AssertTests {
|
||||
|
||||
@Test
|
||||
public void notEmptyArray() {
|
||||
Assert.notEmpty(new String[] { "1234" });
|
||||
Assert.notEmpty(new String[] {"1234"}, "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notEmptyArrayWithEmptyArray() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("enigma");
|
||||
Assert.notEmpty(new String[] {}, "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notEmptyArrayWithNullArray() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("enigma");
|
||||
Assert.notEmpty((Object[]) null, "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notEmptyArrayWithMessageSupplier() {
|
||||
Assert.notEmpty(new String[] { "1234" }, () -> "enigma");
|
||||
Assert.notEmpty(new String[] {"1234"}, () -> "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -369,19 +376,21 @@ public class AssertTests {
|
||||
|
||||
@Test
|
||||
public void notEmptyCollection() {
|
||||
Assert.notEmpty(singletonList("foo"));
|
||||
Assert.notEmpty(singletonList("foo"), "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notEmptyCollectionWithEmptyCollection() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
Assert.notEmpty(emptyList());
|
||||
thrown.expectMessage("enigma");
|
||||
Assert.notEmpty(emptyList(), "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notEmptyCollectionWithNullCollection() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
Assert.notEmpty((Collection<?>) null);
|
||||
thrown.expectMessage("enigma");
|
||||
Assert.notEmpty((Collection<?>) null, "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -412,19 +421,21 @@ public class AssertTests {
|
||||
|
||||
@Test
|
||||
public void notEmptyMap() {
|
||||
Assert.notEmpty(singletonMap("foo", "bar"));
|
||||
Assert.notEmpty(singletonMap("foo", "bar"), "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notEmptyMapWithNullMap() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
Assert.notEmpty((Map<?, ?>) null);
|
||||
thrown.expectMessage("enigma");
|
||||
Assert.notEmpty((Map<?, ?>) null, "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notEmptyMapWithEmptyMap() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
Assert.notEmpty(emptyMap());
|
||||
thrown.expectMessage("enigma");
|
||||
Assert.notEmpty(emptyMap(), "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -455,21 +466,21 @@ public class AssertTests {
|
||||
|
||||
@Test
|
||||
public void isInstanceOf() {
|
||||
Assert.isInstanceOf(String.class, "foo");
|
||||
Assert.isInstanceOf(String.class, "foo", "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isInstanceOfWithNullType() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("Type to check against must not be null");
|
||||
Assert.isInstanceOf(null, "foo");
|
||||
Assert.isInstanceOf(null, "foo", "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isInstanceOfWithNullInstance() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("Object of class [null] must be an instance of class java.lang.String");
|
||||
Assert.isInstanceOf(String.class, null);
|
||||
thrown.expectMessage("enigma: null");
|
||||
Assert.isInstanceOf(String.class, null, "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -482,9 +493,8 @@ public class AssertTests {
|
||||
@Test
|
||||
public void isInstanceOfWithTypeMismatchAndCustomMessage() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage(
|
||||
"Custom message. Object of class [java.lang.Long] must be an instance of class java.lang.String");
|
||||
Assert.isInstanceOf(String.class, 42L, "Custom message.");
|
||||
thrown.expectMessage("Custom message: java.lang.Long");
|
||||
Assert.isInstanceOf(String.class, 42L, "Custom message");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -502,8 +512,8 @@ public class AssertTests {
|
||||
@Test
|
||||
public void isInstanceOfWithNullInstanceAndMessageSupplier() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("enigma: Object of class [null] must be an instance of class java.lang.String");
|
||||
Assert.isInstanceOf(String.class, null, () -> "enigma:");
|
||||
thrown.expectMessage("enigma: null");
|
||||
Assert.isInstanceOf(String.class, null, () -> "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -516,27 +526,27 @@ public class AssertTests {
|
||||
@Test
|
||||
public void isInstanceOfWithTypeMismatchAndMessageSupplier() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("enigma: Object of class [java.lang.Long] must be an instance of class java.lang.String");
|
||||
Assert.isInstanceOf(String.class, 42L, () -> "enigma:");
|
||||
thrown.expectMessage("enigma: java.lang.Long");
|
||||
Assert.isInstanceOf(String.class, 42L, () -> "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAssignable() {
|
||||
Assert.isAssignable(Number.class, Integer.class);
|
||||
Assert.isAssignable(Number.class, Integer.class, "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAssignableWithNullSupertype() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("Super type to check against must not be null");
|
||||
Assert.isAssignable(null, Integer.class);
|
||||
Assert.isAssignable(null, Integer.class, "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAssignableWithNullSubtype() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("null is not assignable to class java.lang.Integer");
|
||||
Assert.isAssignable(Integer.class, null);
|
||||
thrown.expectMessage("enigma: null");
|
||||
Assert.isAssignable(Integer.class, null, "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -549,8 +559,8 @@ public class AssertTests {
|
||||
@Test
|
||||
public void isAssignableWithTypeMismatchAndCustomMessage() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("enigma: class java.lang.Integer is not assignable to class java.lang.String");
|
||||
Assert.isAssignable(String.class, Integer.class, "enigma:");
|
||||
thrown.expectMessage("enigma: class java.lang.Integer");
|
||||
Assert.isAssignable(String.class, Integer.class, "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -568,8 +578,8 @@ public class AssertTests {
|
||||
@Test
|
||||
public void isAssignableWithNullSubtypeAndMessageSupplier() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("enigma: null is not assignable to class java.lang.Integer");
|
||||
Assert.isAssignable(Integer.class, null, () -> "enigma:");
|
||||
thrown.expectMessage("enigma: null");
|
||||
Assert.isAssignable(Integer.class, null, () -> "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -582,19 +592,20 @@ public class AssertTests {
|
||||
@Test
|
||||
public void isAssignableWithTypeMismatchAndMessageSupplier() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("enigma: class java.lang.Integer is not assignable to class java.lang.String");
|
||||
Assert.isAssignable(String.class, Integer.class, () -> "enigma:");
|
||||
thrown.expectMessage("enigma: class java.lang.Integer");
|
||||
Assert.isAssignable(String.class, Integer.class, () -> "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void state() {
|
||||
Assert.state(true);
|
||||
Assert.state(true, "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stateWithFalseExpression() {
|
||||
thrown.expect(IllegalStateException.class);
|
||||
Assert.state(false);
|
||||
thrown.expectMessage("enigma");
|
||||
Assert.state(false, "enigma");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user