Permalink
Browse files

Add the possibility for a cause exception to exception handling.

  • Loading branch information...
1 parent 7ce2914 commit 0aa836dde90883ed1e5d273f996cb3f515a969d5 @akiraly committed Oct 28, 2011
Showing with 372 additions and 77 deletions.
  1. +83 −25 src/main/java/com/github/ver4j/BatchVerifier.java
  2. +13 −11 src/main/java/com/github/ver4j/ExceptionFactory.java
  3. +13 −2 src/main/java/com/github/ver4j/ExceptionTypeInfo.java
  4. +10 −0 src/main/java/com/github/ver4j/GeneralVerificationException.java
  5. +17 −15 src/main/java/com/github/ver4j/ObjectVerifier.java
  6. +2 −2 src/main/java/com/github/ver4j/TextVerifier.java
  7. +7 −0 src/main/java/com/github/ver4j/arg/exception/ArgumentTypeVerificationException.java
  8. +7 −0 src/main/java/com/github/ver4j/arg/exception/ArgumentVerificationException.java
  9. +8 −2 src/main/java/com/github/ver4j/arg/exception/BatchArgumentVerificationException.java
  10. +7 −0 src/main/java/com/github/ver4j/arg/exception/NullPointerArgumentVerificationException.java
  11. +7 −0 src/main/java/com/github/ver4j/arg/exception/TextArgumentVerificationException.java
  12. +7 −0 src/main/java/com/github/ver4j/result/exception/BatchResultVerificationException.java
  13. +7 −0 src/main/java/com/github/ver4j/result/exception/NullPointerResultVerificationException.java
  14. +7 −0 src/main/java/com/github/ver4j/result/exception/ResultTypeVerificationException.java
  15. +7 −0 src/main/java/com/github/ver4j/result/exception/ResultVerificationException.java
  16. +7 −0 src/main/java/com/github/ver4j/result/exception/TextResultVerificationException.java
  17. +8 −2 src/main/java/com/github/ver4j/state/exception/BatchStateVerificationException.java
  18. +7 −0 src/main/java/com/github/ver4j/state/exception/NullPointerStateVerificationException.java
  19. +7 −0 src/main/java/com/github/ver4j/state/exception/StateTypeVerificationException.java
  20. +7 −0 src/main/java/com/github/ver4j/state/exception/StateVerificationException.java
  21. +7 −0 src/main/java/com/github/ver4j/state/exception/TextStateVerificationException.java
  22. +32 −0 src/test/java/com/github/ver4j/BatchVerifierTest.java
  23. +7 −0 src/test/java/com/github/ver4j/BrokenTestVerificationException.java
  24. +8 −0 src/test/java/com/github/ver4j/BrokenTestVerificationException2.java
  25. +10 −0 src/test/java/com/github/ver4j/BrokenTestVerificationException3.java
  26. +37 −16 src/test/java/com/github/ver4j/ExceptionFactoryTest.java
  27. +15 −2 src/test/java/com/github/ver4j/ExceptionTypeInfoTest.java
  28. +11 −0 src/test/java/com/github/ver4j/SmokeTest.java
  29. +7 −0 src/test/java/com/github/ver4j/TestVerificationException.java
View
108 src/main/java/com/github/ver4j/BatchVerifier.java
@@ -1,21 +1,22 @@
package com.github.ver4j;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Locale;
import java.util.Map;
import javax.annotation.Nonnull;
public class BatchVerifier extends AObjectVerifierAwareVerifier {
- private static final String FAILED_NOT_EMPTY_CAUSE = "it is a null or emtpy array/collection/map";
+ private static final String FAILED_NOT_EMPTY_CAUSE = "the array/collection/iterable/map is null or emtpy";
- private static final String FAILED_NO_NULL_ELEMENTS_CAUSE = "it is a null element containing array/iterable";
+ private static final String FAILED_NO_NULL_ELEMENTS_CAUSE = "the array/iterable contains null element";
- private static final String FAILED_NO_NULL_KEYS_CAUSE = "it is a null key containing map";
+ private static final String FAILED_NO_NULL_KEYS_CAUSE = "the map contains null key";
- private static final String FAILED_NO_NULL_VALUES_CAUSE = "it is a null value containing map";
+ private static final String FAILED_NO_NULL_VALUES_CAUSE = "the map contains null value";
- private static final String FAILED_NO_NULL_KEYS_VALUES_CAUSE = "it is a null key/value containing map";
+ private static final String FAILED_NO_NULL_KEYS_VALUES_CAUSE = "the map contains null key/value";
private final ExceptionFactory<?> notEmptyExceptionFactory;
@@ -103,9 +104,39 @@ public BatchVerifier(@Nonnull ObjectVerifier objectVerifier,
}
@Nonnull
+ public final <T, I extends Iterable<T>> I notEmpty(@Nonnull I iterable) {
+ object().notNull(iterable);
+ if (isDisabled() || iterable.iterator().hasNext())
+ return iterable;
+ throw notEmptyExceptionFactory.newException();
+ }
+
+ @Nonnull
+ public final <T, I extends Iterable<T>> I notEmpty(@Nonnull I iterable,
+ Object errorMessage, Object... errorMessageArgs) {
+ object().notNull(iterable, errorMessage, errorMessageArgs);
+ if (isDisabled() || iterable.iterator().hasNext())
+ return iterable;
+ throw notEmptyExceptionFactory.newException(errorMessage,
+ errorMessageArgs);
+ }
+
+ @Nonnull
+ public final <T, I extends Iterable<T>> I notEmptyCm(@Nonnull I iterable,
+ String errorMessageTemplate, Locale locale,
+ Object... errorMessageArgs) {
+ object().notNullCm(iterable, errorMessageTemplate, locale,
+ errorMessageArgs);
+ if (isDisabled() || iterable.iterator().hasNext())
+ return iterable;
+ throw notEmptyExceptionFactory.newExceptionCm(errorMessageTemplate,
+ locale, errorMessageArgs);
+ }
+
+ @Nonnull
public final <K, V, M extends Map<K, V>> M notEmpty(@Nonnull M map) {
object().notNull(map);
- if (isDisabled() || map.size() > 0)
+ if (isDisabled() || !map.isEmpty())
return map;
throw notEmptyExceptionFactory.newException();
}
@@ -114,7 +145,7 @@ public BatchVerifier(@Nonnull ObjectVerifier objectVerifier,
public final <K, V, M extends Map<K, V>> M notEmpty(@Nonnull M map,
Object errorMessage, Object... errorMessageArgs) {
object().notNull(map, errorMessage, errorMessageArgs);
- if (isDisabled() || map.size() > 0)
+ if (isDisabled() || !map.isEmpty())
return map;
throw notEmptyExceptionFactory.newException(errorMessage,
errorMessageArgs);
@@ -125,18 +156,39 @@ public BatchVerifier(@Nonnull ObjectVerifier objectVerifier,
String errorMessageTemplate, Locale locale,
Object... errorMessageArgs) {
object().notNullCm(map, errorMessageTemplate, locale, errorMessageArgs);
- if (isDisabled() || map.size() > 0)
+ if (isDisabled() || !map.isEmpty())
return map;
throw notEmptyExceptionFactory.newExceptionCm(errorMessageTemplate,
locale, errorMessageArgs);
}
@Nonnull
+ private final Map<Object, Object> newContext(@Nonnull Map<?, ?> map) {
+ Map<Object, Object> context = object().newContextMap();
+ context.put("map", map);
+ return context;
+ }
+
+ @Nonnull
+ private final Map<Object, Object> newContext(@Nonnull Iterable<?> iterable) {
+ Map<Object, Object> context = object().newContextMap();
+ context.put("iterable", iterable);
+ return context;
+ }
+
+ @Nonnull
+ private final Map<Object, Object> newContext(@Nonnull Object[] array) {
+ Map<Object, Object> context = object().newContextMap();
+ context.put("array", Arrays.toString(array));
+ return context;
+ }
+
+ @Nonnull
public final <K, V, M extends Map<K, V>> M noNullKeys(@Nonnull M map) {
object().notNull(map);
if (isDisabled() || !hasNull(map.keySet()))
return map;
- throw noNullKeysFactory.newException();
+ throw noNullKeysFactory.newException(newContext(map), (Throwable) null);
}
@Nonnull
@@ -145,7 +197,8 @@ public BatchVerifier(@Nonnull ObjectVerifier objectVerifier,
object().notNull(map, errorMessage, errorMessageArgs);
if (isDisabled() || !hasNull(map.keySet()))
return map;
- throw noNullKeysFactory.newException(errorMessage, errorMessageArgs);
+ throw noNullKeysFactory.newException(errorMessage, errorMessageArgs,
+ newContext(map), null);
}
@Nonnull
@@ -156,15 +209,16 @@ public BatchVerifier(@Nonnull ObjectVerifier objectVerifier,
if (isDisabled() || !hasNull(map.keySet()))
return map;
throw noNullKeysFactory.newExceptionCm(errorMessageTemplate, locale,
- errorMessageArgs);
+ errorMessageArgs, newContext(map), null);
}
@Nonnull
public final <K, V, M extends Map<K, V>> M noNullValues(@Nonnull M map) {
object().notNull(map);
if (isDisabled() || !hasNull(map.values()))
return map;
- throw noNullValuesFactory.newException();
+ throw noNullValuesFactory.newException(newContext(map),
+ (Throwable) null);
}
@Nonnull
@@ -173,7 +227,8 @@ public BatchVerifier(@Nonnull ObjectVerifier objectVerifier,
object().notNull(map, errorMessage, errorMessageArgs);
if (isDisabled() || !hasNull(map.values()))
return map;
- throw noNullValuesFactory.newException(errorMessage, errorMessageArgs);
+ throw noNullValuesFactory.newException(errorMessage, errorMessageArgs,
+ newContext(map), null);
}
@Nonnull
@@ -184,15 +239,16 @@ public BatchVerifier(@Nonnull ObjectVerifier objectVerifier,
if (isDisabled() || !hasNull(map.values()))
return map;
throw noNullValuesFactory.newExceptionCm(errorMessageTemplate, locale,
- errorMessageArgs);
+ errorMessageArgs, newContext(map), null);
}
@Nonnull
public final <K, V, M extends Map<K, V>> M noNullKeysValues(@Nonnull M map) {
object().notNull(map);
if (isDisabled() || !hasNullKeyOrValue(map.entrySet()))
return map;
- throw noNullKeysValuesFactory.newException();
+ throw noNullKeysValuesFactory.newException(newContext(map),
+ (Throwable) null);
}
@Nonnull
@@ -202,7 +258,7 @@ public BatchVerifier(@Nonnull ObjectVerifier objectVerifier,
if (isDisabled() || !hasNullKeyOrValue(map.entrySet()))
return map;
throw noNullKeysValuesFactory.newException(errorMessage,
- errorMessageArgs);
+ errorMessageArgs, newContext(map), null);
}
@Nonnull
@@ -213,7 +269,7 @@ public BatchVerifier(@Nonnull ObjectVerifier objectVerifier,
if (isDisabled() || !hasNullKeyOrValue(map.entrySet()))
return map;
throw noNullKeysValuesFactory.newExceptionCm(errorMessageTemplate,
- locale, errorMessageArgs);
+ locale, errorMessageArgs, newContext(map), null);
}
private <K, V> boolean hasNullKeyOrValue(Iterable<Map.Entry<K, V>> iterable) {
@@ -228,7 +284,8 @@ public BatchVerifier(@Nonnull ObjectVerifier objectVerifier,
object().notNull(iterable);
if (isDisabled() || !hasNull(iterable))
return iterable;
- throw noNullElementsFactory.newException();
+ throw noNullElementsFactory.newException(newContext(iterable),
+ (Throwable) null);
}
@Nonnull
@@ -238,8 +295,8 @@ public BatchVerifier(@Nonnull ObjectVerifier objectVerifier,
object().notNull(iterable, errorMessage, errorMessageArgs);
if (isDisabled() || !hasNull(iterable))
return iterable;
- throw noNullElementsFactory
- .newException(errorMessage, errorMessageArgs);
+ throw noNullElementsFactory.newException(errorMessage,
+ errorMessageArgs, newContext(iterable), null);
}
@Nonnull
@@ -251,7 +308,7 @@ public BatchVerifier(@Nonnull ObjectVerifier objectVerifier,
if (isDisabled() || !hasNull(iterable))
return iterable;
throw noNullElementsFactory.newExceptionCm(errorMessageTemplate,
- locale, errorMessageArgs);
+ locale, errorMessageArgs, newContext(iterable), null);
}
private boolean hasNull(Iterable<?> iterable) {
@@ -266,7 +323,8 @@ private boolean hasNull(Iterable<?> iterable) {
object().notNull(array);
if (isDisabled() || !hasNull(array))
return array;
- throw noNullElementsFactory.newException();
+ throw noNullElementsFactory.newException(newContext(array),
+ (Throwable) null);
}
@Nonnull
@@ -275,8 +333,8 @@ private boolean hasNull(Iterable<?> iterable) {
object().notNull(array, errorMessage, errorMessageArgs);
if (isDisabled() || !hasNull(array))
return array;
- throw noNullElementsFactory
- .newException(errorMessage, errorMessageArgs);
+ throw noNullElementsFactory.newException(errorMessage,
+ errorMessageArgs, newContext(array), null);
}
@Nonnull
@@ -288,7 +346,7 @@ private boolean hasNull(Iterable<?> iterable) {
if (isDisabled() || !hasNull(array))
return array;
throw noNullElementsFactory.newExceptionCm(errorMessageTemplate,
- locale, errorMessageArgs);
+ locale, errorMessageArgs, newContext(array), null);
}
private boolean hasNull(Object[] array) {
View
24 src/main/java/com/github/ver4j/ExceptionFactory.java
@@ -27,47 +27,49 @@ public ExceptionFactory(String category,
}
public final T newException() {
- return newException(null);
+ return newException(null, (Throwable) null);
}
- public final T newException(Map<?, ?> ctxtArgs) {
- return createException(null, null, new Object[] { null }, ctxtArgs);
+ public final T newException(Map<?, ?> ctxtArgs, Throwable cause) {
+ return createException(null, null, new Object[] { null }, ctxtArgs,
+ cause);
}
public final T newException(Object errorMessage, Object[] errorMessageArgs) {
- return newException(errorMessage, errorMessageArgs, null);
+ return newException(errorMessage, errorMessageArgs, null, null);
}
public final T newException(Object errorMessage, Object[] errorMessageArgs,
- Map<?, ?> ctxtArgs) {
+ Map<?, ?> ctxtArgs, Throwable cause) {
Object arg;
if (errorMessage != null && errorMessageArgs != null)
arg = messageInfo.createMessage(null, errorMessage.toString(),
errorMessageArgs);
else
arg = errorMessage;
- return createException(null, null, new Object[] { arg }, ctxtArgs);
+ return createException(null, null, new Object[] { arg }, ctxtArgs,
+ cause);
}
public final T newExceptionCm(String errorMessageTemplate, Locale locale,
Object[] errorMessageArgs) {
return newExceptionCm(errorMessageTemplate, locale, errorMessageArgs,
- null);
+ null, null);
}
public final T newExceptionCm(String errorMessageTemplate, Locale locale,
- Object[] errorMessageArgs, Map<?, ?> ctxtArgs) {
+ Object[] errorMessageArgs, Map<?, ?> ctxtArgs, Throwable cause) {
return createException(locale, errorMessageTemplate, errorMessageArgs,
- ctxtArgs);
+ ctxtArgs, cause);
}
protected T createException(Locale locale, String messageTemplate,
- Object[] messageTemplateArgs, Map<?, ?> ctxtArgs) {
+ Object[] messageTemplateArgs, Map<?, ?> ctxtArgs, Throwable cause) {
String message = messageInfo.createMessage(locale, messageTemplate,
messageTemplateArgs);
try {
- T result = typeInfo.create(message, category);
+ T result = typeInfo.create(message, category, cause);
if (ctxtArgs != null)
for (Map.Entry<?, ?> entry : ctxtArgs.entrySet())
View
15 src/main/java/com/github/ver4j/ExceptionTypeInfo.java
@@ -11,13 +11,18 @@
private final Constructor<T> exceptionConstructor;
+ private final Constructor<T> exceptionConstructorWithCause;
+
public ExceptionTypeInfo(Class<T> exceptionType) {
super();
this.type = exceptionType;
try {
this.exceptionConstructor = exceptionType.getConstructor(
String.class, String.class);
+
+ this.exceptionConstructorWithCause = exceptionType.getConstructor(
+ String.class, String.class, Throwable.class);
} catch (NoSuchMethodException e) {
throw new IllegalStateException(
"No constructor with correct signature found for exception type: "
@@ -30,10 +35,16 @@ public ExceptionTypeInfo(Class<T> exceptionType) {
return new ExceptionTypeInfo<T>(exceptionType);
}
- public T create(String message, String category)
+ public T create(String message, String category, Throwable cause)
throws InstantiationException, IllegalAccessException,
InvocationTargetException {
- return exceptionConstructor.newInstance(message, category);
+ T instance;
+ if (cause == null)
+ instance = exceptionConstructor.newInstance(message, category);
+ else
+ instance = exceptionConstructorWithCause.newInstance(message,
+ category, cause);
+ return instance;
}
public final Class<T> getType() {
View
10 src/main/java/com/github/ver4j/GeneralVerificationException.java
@@ -1,5 +1,7 @@
package com.github.ver4j;
+import javax.annotation.Nonnull;
+
import org.apache.commons.lang3.exception.ContextedRuntimeException;
public class GeneralVerificationException extends ContextedRuntimeException
@@ -15,6 +17,14 @@ public GeneralVerificationException(String message, String category) {
addContextValue("verification-category", category);
}
+ public GeneralVerificationException(String message, String category,
+ @Nonnull Throwable cause) {
+ super(message, cause);
+ this.category = category;
+ if (category != null)
+ addContextValue("verification-category", category);
+ }
+
@Override
public final String getCategory() {
return category;
View
32 src/main/java/com/github/ver4j/ObjectVerifier.java
@@ -7,15 +7,15 @@
import javax.annotation.Nonnull;
public class ObjectVerifier extends AVerifier {
- private static final String FAILED_IS_TRUE_CAUSE = "it is false";
+ private static final String FAILED_IS_TRUE_CAUSE = "the expression is false";
- private static final String FAILED_IS_FALSE_CAUSE = "it is true";
+ private static final String FAILED_IS_FALSE_CAUSE = "the expression is true";
- private static final String FAILED_IS_ASSIGNABLE_FROM_CAUSE = "the tested class can not be casted to the reference class";
+ private static final String FAILED_IS_ASSIGNABLE_FROM_CAUSE = "the class can not be casted to the reference class";
- private static final String FAILED_IS_INSTANCE_OF_CAUSE = "the tested object is not an instance of the reference class";
+ private static final String FAILED_IS_INSTANCE_OF_CAUSE = "the object is not an instance of the reference class";
- private static final String FAILED_NOT_NULL_CAUSE = "it is null";
+ private static final String FAILED_NOT_NULL_CAUSE = "the object is null";
private final String category;
@@ -106,8 +106,8 @@ public final void isFalseCm(boolean expression,
notNull(type);
if (isDisabled() || superType.isAssignableFrom(type))
return type;
- throw isAssignableFromExceptionFactory
- .newException(newAssignableFromContext(superType, type));
+ throw isAssignableFromExceptionFactory.newException(
+ newAssignableFromContext(superType, type), (Throwable) null);
}
@Nonnull
@@ -119,7 +119,8 @@ public final void isFalseCm(boolean expression,
if (isDisabled() || superType.isAssignableFrom(type))
return type;
throw isAssignableFromExceptionFactory.newException(errorMessage,
- errorMessageArgs, newAssignableFromContext(superType, type));
+ errorMessageArgs, newAssignableFromContext(superType, type),
+ null);
}
@Nonnull
@@ -132,7 +133,7 @@ public final void isFalseCm(boolean expression,
return type;
throw isAssignableFromExceptionFactory.newExceptionCm(
errorMessageTemplate, locale, errorMessageArgs,
- newAssignableFromContext(superType, type));
+ newAssignableFromContext(superType, type), null);
}
@Nonnull
@@ -150,8 +151,8 @@ public final void isFalseCm(boolean expression,
notNull(obj);
if (isDisabled() || type.isInstance(obj))
return type.cast(obj);
- throw isInstanceOfExceptionFactory.newException(newInstanceOfContext(
- obj, type));
+ throw isInstanceOfExceptionFactory.newException(
+ newInstanceOfContext(obj, type), (Throwable) null);
}
@Nonnull
@@ -162,7 +163,7 @@ public final void isFalseCm(boolean expression,
if (isDisabled() || type.isInstance(obj))
return type.cast(obj);
throw isInstanceOfExceptionFactory.newException(errorMessage,
- errorMessageArgs, newInstanceOfContext(obj, type));
+ errorMessageArgs, newInstanceOfContext(obj, type), null);
}
@Nonnull
@@ -173,15 +174,16 @@ public final void isFalseCm(boolean expression,
notNull(obj, errorMessageTemplate, locale, errorMessageArgs);
if (isDisabled() || type.isInstance(obj))
return type.cast(obj);
- throw isInstanceOfExceptionFactory.newExceptionCm(errorMessageTemplate,
- locale, errorMessageArgs, newInstanceOfContext(obj, type));
+ throw isInstanceOfExceptionFactory
+ .newExceptionCm(errorMessageTemplate, locale, errorMessageArgs,
+ newInstanceOfContext(obj, type), null);
}
@Nonnull
private final Map<Object, Object> newInstanceOfContext(@Nonnull Object obj,
@Nonnull Class<?> type) {
Map<Object, Object> context = newContextMap();
- context.put("tested class", obj);
+ context.put("tested object", obj);
context.put("reference class", type);
return context;
}
View
4 src/main/java/com/github/ver4j/TextVerifier.java
@@ -8,9 +8,9 @@
public class TextVerifier<S extends CharSequence> extends
AObjectVerifierAwareVerifier {
- private static final String FAILED_NOT_EMPTY_CAUSE = "it is an empty text";
+ private static final String FAILED_NOT_EMPTY_CAUSE = "the text is null or empty";
- private static final String FAILED_NOT_BLANK_CAUSE = "it is an empty text or it contains only whitespace characters";
+ private static final String FAILED_NOT_BLANK_CAUSE = "the text is null or empty or it contains only whitespace characters";
private final ExceptionFactory<?> notEmptyExceptionFactory;
View
7 src/main/java/com/github/ver4j/arg/exception/ArgumentTypeVerificationException.java
@@ -1,10 +1,17 @@
package com.github.ver4j.arg.exception;
+import javax.annotation.Nonnull;
+
public class ArgumentTypeVerificationException extends
ArgumentVerificationException {
private static final long serialVersionUID = 1L;
public ArgumentTypeVerificationException(String message, String category) {
super(message, category);
}
+
+ public ArgumentTypeVerificationException(String message, String category,
+ @Nonnull Throwable cause) {
+ super(message, category, cause);
+ }
}
View
7 src/main/java/com/github/ver4j/arg/exception/ArgumentVerificationException.java
@@ -1,5 +1,7 @@
package com.github.ver4j.arg.exception;
+import javax.annotation.Nonnull;
+
import com.github.ver4j.GeneralVerificationException;
public class ArgumentVerificationException extends GeneralVerificationException {
@@ -8,4 +10,9 @@
public ArgumentVerificationException(String message, String category) {
super(message, category);
}
+
+ public ArgumentVerificationException(String message, String category,
+ @Nonnull Throwable cause) {
+ super(message, category, cause);
+ }
}
View
10 src/main/java/com/github/ver4j/arg/exception/BatchArgumentVerificationException.java
@@ -1,11 +1,17 @@
package com.github.ver4j.arg.exception;
+import javax.annotation.Nonnull;
+
public class BatchArgumentVerificationException extends
ArgumentVerificationException {
private static final long serialVersionUID = 1L;
- public BatchArgumentVerificationException(String message,
- String category) {
+ public BatchArgumentVerificationException(String message, String category) {
super(message, category);
}
+
+ public BatchArgumentVerificationException(String message, String category,
+ @Nonnull Throwable cause) {
+ super(message, category, cause);
+ }
}
View
7 src/main/java/com/github/ver4j/arg/exception/NullPointerArgumentVerificationException.java
@@ -1,5 +1,7 @@
package com.github.ver4j.arg.exception;
+import javax.annotation.Nonnull;
+
public class NullPointerArgumentVerificationException extends
ArgumentVerificationException {
private static final long serialVersionUID = 1L;
@@ -8,4 +10,9 @@ public NullPointerArgumentVerificationException(String message,
String category) {
super(message, category);
}
+
+ public NullPointerArgumentVerificationException(String message,
+ String category, @Nonnull Throwable cause) {
+ super(message, category, cause);
+ }
}
View
7 src/main/java/com/github/ver4j/arg/exception/TextArgumentVerificationException.java
@@ -1,10 +1,17 @@
package com.github.ver4j.arg.exception;
+import javax.annotation.Nonnull;
+
public class TextArgumentVerificationException extends
ArgumentVerificationException {
private static final long serialVersionUID = 1L;
public TextArgumentVerificationException(String message, String category) {
super(message, category);
}
+
+ public TextArgumentVerificationException(String message, String category,
+ @Nonnull Throwable cause) {
+ super(message, category, cause);
+ }
}
View
7 src/main/java/com/github/ver4j/result/exception/BatchResultVerificationException.java
@@ -1,10 +1,17 @@
package com.github.ver4j.result.exception;
+import javax.annotation.Nonnull;
+
public class BatchResultVerificationException extends
ResultVerificationException {
private static final long serialVersionUID = 1L;
public BatchResultVerificationException(String message, String category) {
super(message, category);
}
+
+ public BatchResultVerificationException(String message, String category,
+ @Nonnull Throwable cause) {
+ super(message, category, cause);
+ }
}
View
7 src/main/java/com/github/ver4j/result/exception/NullPointerResultVerificationException.java
@@ -1,5 +1,7 @@
package com.github.ver4j.result.exception;
+import javax.annotation.Nonnull;
+
public class NullPointerResultVerificationException extends
ResultVerificationException {
private static final long serialVersionUID = 1L;
@@ -8,4 +10,9 @@ public NullPointerResultVerificationException(String message,
String category) {
super(message, category);
}
+
+ public NullPointerResultVerificationException(String message,
+ String category, @Nonnull Throwable cause) {
+ super(message, category, cause);
+ }
}
View
7 src/main/java/com/github/ver4j/result/exception/ResultTypeVerificationException.java
@@ -1,10 +1,17 @@
package com.github.ver4j.result.exception;
+import javax.annotation.Nonnull;
+
public class ResultTypeVerificationException extends
ResultVerificationException {
private static final long serialVersionUID = 1L;
public ResultTypeVerificationException(String message, String category) {
super(message, category);
}
+
+ public ResultTypeVerificationException(String message, String category,
+ @Nonnull Throwable cause) {
+ super(message, category, cause);
+ }
}
View
7 src/main/java/com/github/ver4j/result/exception/ResultVerificationException.java
@@ -1,5 +1,7 @@
package com.github.ver4j.result.exception;
+import javax.annotation.Nonnull;
+
import com.github.ver4j.GeneralVerificationException;
public class ResultVerificationException extends GeneralVerificationException {
@@ -8,4 +10,9 @@
public ResultVerificationException(String message, String category) {
super(message, category);
}
+
+ public ResultVerificationException(String message, String category,
+ @Nonnull Throwable cause) {
+ super(message, category, cause);
+ }
}
View
7 src/main/java/com/github/ver4j/result/exception/TextResultVerificationException.java
@@ -1,10 +1,17 @@
package com.github.ver4j.result.exception;
+import javax.annotation.Nonnull;
+
public class TextResultVerificationException extends
ResultVerificationException {
private static final long serialVersionUID = 1L;
public TextResultVerificationException(String message, String category) {
super(message, category);
}
+
+ public TextResultVerificationException(String message, String category,
+ @Nonnull Throwable cause) {
+ super(message, category, cause);
+ }
}
View
10 src/main/java/com/github/ver4j/state/exception/BatchStateVerificationException.java
@@ -1,10 +1,16 @@
package com.github.ver4j.state.exception;
-public class BatchStateVerificationException extends
- StateVerificationException {
+import javax.annotation.Nonnull;
+
+public class BatchStateVerificationException extends StateVerificationException {
private static final long serialVersionUID = 1L;
public BatchStateVerificationException(String message, String category) {
super(message, category);
}
+
+ public BatchStateVerificationException(String message, String category,
+ @Nonnull Throwable cause) {
+ super(message, category, cause);
+ }
}
View
7 src/main/java/com/github/ver4j/state/exception/NullPointerStateVerificationException.java
@@ -1,10 +1,17 @@
package com.github.ver4j.state.exception;
+import javax.annotation.Nonnull;
+
public class NullPointerStateVerificationException extends
StateVerificationException {
private static final long serialVersionUID = 1L;
public NullPointerStateVerificationException(String message, String category) {
super(message, category);
}
+
+ public NullPointerStateVerificationException(String message,
+ String category, @Nonnull Throwable cause) {
+ super(message, category, cause);
+ }
}
View
7 src/main/java/com/github/ver4j/state/exception/StateTypeVerificationException.java
@@ -1,9 +1,16 @@
package com.github.ver4j.state.exception;
+import javax.annotation.Nonnull;
+
public class StateTypeVerificationException extends StateVerificationException {
private static final long serialVersionUID = 1L;
public StateTypeVerificationException(String message, String category) {
super(message, category);
}
+
+ public StateTypeVerificationException(String message, String category,
+ @Nonnull Throwable cause) {
+ super(message, category, cause);
+ }
}
View
7 src/main/java/com/github/ver4j/state/exception/StateVerificationException.java
@@ -1,5 +1,7 @@
package com.github.ver4j.state.exception;
+import javax.annotation.Nonnull;
+
import com.github.ver4j.GeneralVerificationException;
public class StateVerificationException extends GeneralVerificationException {
@@ -8,4 +10,9 @@
public StateVerificationException(String message, String category) {
super(message, category);
}
+
+ public StateVerificationException(String message, String category,
+ @Nonnull Throwable cause) {
+ super(message, category, cause);
+ }
}
View
7 src/main/java/com/github/ver4j/state/exception/TextStateVerificationException.java
@@ -1,9 +1,16 @@
package com.github.ver4j.state.exception;
+import javax.annotation.Nonnull;
+
public class TextStateVerificationException extends StateVerificationException {
private static final long serialVersionUID = 1L;
public TextStateVerificationException(String message, String category) {
super(message, category);
}
+
+ public TextStateVerificationException(String message, String category,
+ @Nonnull Throwable cause) {
+ super(message, category, cause);
+ }
}
View
32 src/test/java/com/github/ver4j/BatchVerifierTest.java
@@ -40,6 +40,12 @@ public void testNotEmptyWithTrue() {
Assert.assertSame(list, verifier.notEmpty(list, "%s", "1"));
Assert.assertSame(list, verifier.notEmptyCm(list, "", null));
+ Iterable<String> iterable = list;
+ Assert.assertSame(iterable, verifier.notEmpty(iterable));
+ Assert.assertSame(iterable, verifier.notEmpty(iterable, ""));
+ Assert.assertSame(iterable, verifier.notEmpty(iterable, "%s", "1"));
+ Assert.assertSame(iterable, verifier.notEmptyCm(iterable, "", null));
+
Map<String, String> map = new HashMap<String, String>();
map.put("", "");
Assert.assertSame(map, verifier.notEmpty(map));
@@ -64,6 +70,12 @@ public void testNotEmptyWithFalseDisabled() {
Assert.assertSame(list, verifier.notEmpty(list, "%s", "1"));
Assert.assertSame(list, verifier.notEmptyCm(list, "", null));
+ Iterable<Object> iterable = list;
+ Assert.assertSame(iterable, verifier.notEmpty(iterable));
+ Assert.assertSame(iterable, verifier.notEmpty(iterable, ""));
+ Assert.assertSame(iterable, verifier.notEmpty(iterable, "%s", "1"));
+ Assert.assertSame(iterable, verifier.notEmptyCm(iterable, "", null));
+
Map<String, String> map = new HashMap<String, String>();
Assert.assertSame(map, verifier.notEmpty(map));
Assert.assertSame(map, verifier.notEmpty(map, ""));
@@ -112,6 +124,26 @@ public void testNotEmptyCmCollectionWithFalse() {
}
@Test(expected = BatchArgumentVerificationException.class)
+ public void testNotEmptyIterableWithFalse1() {
+ verifier.notEmpty((Iterable<?>) Arrays.asList());
+ }
+
+ @Test(expected = BatchArgumentVerificationException.class)
+ public void testNotEmptyIterableWithFalse2() {
+ verifier.notEmpty((Iterable<?>) Arrays.asList(), "");
+ }
+
+ @Test(expected = BatchArgumentVerificationException.class)
+ public void testNotEmptyIterableWithFalse3() {
+ verifier.notEmpty((Iterable<?>) Arrays.asList(), "%s", "1");
+ }
+
+ @Test(expected = BatchArgumentVerificationException.class)
+ public void testNotEmptyCmIterableWithFalse() {
+ verifier.notEmptyCm((Iterable<?>) Arrays.asList(), "", null);
+ }
+
+ @Test(expected = BatchArgumentVerificationException.class)
public void testNotEmptyMapWithFalse1() {
verifier.notEmpty(new HashMap<String, String>());
}
View
7 src/test/java/com/github/ver4j/BrokenTestVerificationException.java
@@ -1,9 +1,16 @@
package com.github.ver4j;
+import javax.annotation.Nonnull;
+
class BrokenTestVerificationException extends GeneralVerificationException {
private static final long serialVersionUID = 1L;
public BrokenTestVerificationException(String message) {
super(message, null);
}
+
+ public BrokenTestVerificationException(String message, String category,
+ @Nonnull Throwable cause) {
+ super(message, category, cause);
+ }
}
View
8 src/test/java/com/github/ver4j/BrokenTestVerificationException2.java
@@ -1,10 +1,18 @@
package com.github.ver4j;
+import javax.annotation.Nonnull;
+
class BrokenTestVerificationException2 extends GeneralVerificationException {
private static final long serialVersionUID = 1L;
public BrokenTestVerificationException2(String message, String category) {
super(message, category);
throw new UnsupportedOperationException("You shouldn't use me.");
}
+
+ public BrokenTestVerificationException2(String message, String category,
+ @Nonnull Throwable cause) {
+ super(message, category, cause);
+ throw new UnsupportedOperationException("You shouldn't use me.");
+ }
}
View
10 src/test/java/com/github/ver4j/BrokenTestVerificationException3.java
@@ -0,0 +1,10 @@
+package com.github.ver4j;
+
+
+class BrokenTestVerificationException3 extends GeneralVerificationException {
+ private static final long serialVersionUID = 1L;
+
+ public BrokenTestVerificationException3(String message, String category) {
+ super(message, category);
+ }
+}
View
53 src/test/java/com/github/ver4j/ExceptionFactoryTest.java
@@ -15,47 +15,68 @@ public void testWithCorrectExceptionType() {
ExceptionTypeInfo.of(TestVerificationException.class),
new ExceptionMessageInfo("Test"));
+ RuntimeException cause = null;
+
TestVerificationException exception = exceptionFactory.createException(
- null, null, new Object[] { 1 }, null);
- checkException(exception);
+ null, null, new Object[] { 1 }, null, null);
+ checkException(exception, cause);
Map<Object, Object> autoArgs = new LinkedHashMap<Object, Object>();
autoArgs.put("key", "value");
exception = exceptionFactory.newException();
- checkException(exception);
+ checkException(exception, cause);
- exception = exceptionFactory.newException(autoArgs);
- checkException(exception);
+ exception = exceptionFactory.newException(autoArgs, (Throwable) null);
+ checkException(exception, cause);
exception = exceptionFactory.newException(1, null);
- checkException(exception);
+ checkException(exception, cause);
- exception = exceptionFactory.newException(null, null);
- checkException(exception);
+ exception = exceptionFactory.newException(null, (Object[]) null);
+ checkException(exception, cause);
exception = exceptionFactory.newException(null, new Object[0]);
- checkException(exception);
+ checkException(exception, cause);
exception = exceptionFactory.newException("", new Object[0]);
- checkException(exception);
+ checkException(exception, cause);
exception = exceptionFactory.newException("%s", new Object[] { 1 });
- checkException(exception);
+ checkException(exception, cause);
exception = exceptionFactory.newException("%s", new Object[] { 1 },
- autoArgs);
- checkException(exception);
+ autoArgs, null);
+ checkException(exception, cause);
exception = exceptionFactory.newExceptionCm("%s", null,
new Object[] { 1 });
- checkException(exception);
+ checkException(exception, cause);
+
+ exception = exceptionFactory.newExceptionCm("%s", null,
+ new Object[] { 1 }, autoArgs, null);
+ checkException(exception, cause);
+
+ cause = new RuntimeException();
+
+ exception = exceptionFactory.newException(null, cause);
+ checkException(exception, cause);
+
+ exception = exceptionFactory.newException("%s", new Object[] { 1 },
+ autoArgs, cause);
+ checkException(exception, cause);
+
+ exception = exceptionFactory.newExceptionCm("%s", null,
+ new Object[] { 1 }, autoArgs, cause);
+ checkException(exception, cause);
}
- private void checkException(TestVerificationException exception) {
+ private void checkException(TestVerificationException exception,
+ Throwable cause) {
Assert.assertNotNull(exception);
Assert.assertNotNull(exception.getMessage());
Assert.assertNotNull(exception.getCategory());
+ Assert.assertSame(cause, exception.getCause());
}
@Test(expected = IllegalStateException.class)
@@ -71,7 +92,7 @@ public void testWithBrokenExceptionType2() {
ExceptionFactory<BrokenTestVerificationException2> exceptionFactory = ExceptionFactory
.of(null, exceptionInfo, new ExceptionMessageInfo("Test"));
- exceptionFactory.createException(null, null, null, null);
+ exceptionFactory.createException(null, null, null, null, null);
}
}
View
17 src/test/java/com/github/ver4j/ExceptionTypeInfoTest.java
@@ -15,11 +15,19 @@ public void testWithCorrectExceptionType() throws InstantiationException,
String category = "category";
TestVerificationException exception = exceptionTypeInfo.create(message,
- category);
+ category, null);
Assert.assertNotNull(exception);
Assert.assertEquals(message, exception.getRawMessage());
Assert.assertEquals(category, exception.getCategory());
+
+ RuntimeException cause = new RuntimeException();
+ exception = exceptionTypeInfo.create(message, category, cause);
+
+ Assert.assertNotNull(exception);
+ Assert.assertEquals(message, exception.getRawMessage());
+ Assert.assertEquals(category, exception.getCategory());
+ Assert.assertSame(cause, exception.getCause());
}
@Test(expected = IllegalStateException.class)
@@ -38,7 +46,12 @@ public void testWithBrokenExceptionType2() throws InstantiationException,
} catch (IllegalStateException e) {
throw new RuntimeException(e);
}
- exceptionTypeInfo.create("message", "category");
+ exceptionTypeInfo.create("message", "category", null);
}
+ @Test(expected = IllegalStateException.class)
+ public void testWithBrokenExceptionType3() {
+ new ExceptionTypeInfo<BrokenTestVerificationException3>(
+ BrokenTestVerificationException3.class);
+ }
}
View
11 src/test/java/com/github/ver4j/SmokeTest.java
@@ -0,0 +1,11 @@
+package com.github.ver4j;
+
+import org.junit.Test;
+
+public class SmokeTest {
+
+ @Test
+ public void testGlobalVerifier() {
+ Verify.arg.notNull(1);
+ }
+}
View
7 src/test/java/com/github/ver4j/TestVerificationException.java
@@ -1,9 +1,16 @@
package com.github.ver4j;
+import javax.annotation.Nonnull;
+
class TestVerificationException extends GeneralVerificationException {
private static final long serialVersionUID = 1L;
public TestVerificationException(String message, String category) {
super(message, category);
}
+
+ public TestVerificationException(String message, String category,
+ @Nonnull Throwable cause) {
+ super(message, category, cause);
+ }
}

0 comments on commit 0aa836d

Please sign in to comment.