Commit fd1b96a6 authored by Rajiv Prabhakar's avatar Rajiv Prabhakar
Browse files

v1.12.0 release:

- Validatec.notNull updated to return the object verified
- Validatec error messages standardized and updated to be prescriptive, not descriptive
parent 7dc795e4
Pipeline #14106697 passed with stage
in 1 minute and 14 seconds
......@@ -6,10 +6,10 @@
<groupId>com.rajivprab</groupId>
<artifactId>cava</artifactId>
<version>1.11.1</version>
<version>1.12.0</version>
<name>Cava: Clean Java</name>
<description>A library that enables users to write minimal, clean and simple Java code</description>
<description>A library that enables users to write minimal, clean and simple Java</description>
<url>https://gitlab.com/whacks/cava</url>
<scm>
<url>https://gitlab.com/whacks/cava</url>
......
......@@ -3,6 +3,7 @@ package org.rajivprab.cava;
import com.google.common.base.Strings;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.javatuples.Pair;
import java.util.Arrays;
import java.util.Collection;
......@@ -93,7 +94,7 @@ public class Validatec {
}
private static String getSizeMessage(Collection collection, int size) {
return "Size check failed. Expecting: " + size + ", found: " + collection;
return collection + " should have " + size + " entries";
}
// ---------- Array.length
......@@ -122,7 +123,7 @@ public class Validatec {
}
private static String getArrayLengthMessage(Object[] array, int length) {
return "Array length check failed. Expecting: " + length + ", found: " + Arrays.toString(array);
return Arrays.toString(array) + " should have " + length + " entries";
}
// ---------- String.length
......@@ -151,7 +152,7 @@ public class Validatec {
}
private static String getStringLengthMessage(String string, int length) {
return "String length check failed. Expecting: " + length + ", found: " + string;
return wrap(string) + " should be " + length + " characters long";
}
// ---------- String.matches
......@@ -180,7 +181,7 @@ public class Validatec {
}
private static String getStringMatchesMessage(String string, String regex) {
return "String matches check failed. Expecting: " + regex + ", found: " + string;
return wrap(string) + " should match " + wrap(regex);
}
// ---------- Less than
......@@ -249,10 +250,10 @@ public class Validatec {
}
private static <T> String getGreaterThanMessage(Comparable<T> a, T b) {
return a + " is not greater than " + b;
return a + " should be greater than " + b;
}
// ---------- Greater than --------
// ---------- Greater than or equal --------
public static <T> void greaterOrEqual(Comparable<T> a, T b) {
greaterOrEqual(a, b, DEFAULT_EXCEPTION);
......@@ -283,7 +284,7 @@ public class Validatec {
}
private static <T> String getGreaterOrEqualMessage(Comparable<T> a, T b) {
return a + " is not greater than " + b;
return a + " should be greater than or equal to " + b;
}
// ---------- Equals
......@@ -316,7 +317,7 @@ public class Validatec {
}
private static String equalsErrorMessage(Object a, Object b) {
return "Values are not equal. First arg: " + a + ", Second arg: " + b;
return a + " should equal " + b;
}
// ---------- Not Equals
......@@ -349,7 +350,7 @@ public class Validatec {
}
private static String notEqualsErrorMessage(Object a, Object b) {
return "Values are equal. First arg: " + a + ", Second arg: " + b;
return a + " should not equal " + b;
}
// ---------- Null
......@@ -378,48 +379,45 @@ public class Validatec {
}
private static String getNullMessage(Object object) {
return "Expecting null. Found: " + object;
return object + " should be null";
}
// ---------- Not Null. Deprecated, use NoNulls
// ---------- Not Null
@Deprecated
public static void notNull(Object object) {
notNull(object, "Found unexpected null object");
private static final String NOT_NULL_MESSAGE = "Argument should not be null";
public static <T> T notNull(T arg) {
return notNull(arg, NOT_NULL_MESSAGE);
}
@Deprecated
public static void notNull(Object object, RuntimeException e) {
notNull(object, () -> throwException(e));
public static <T> T notNull(T arg, RuntimeException e) {
return notNull(arg, () -> throwException(e));
}
@Deprecated
public static void notNull(Object object, String message) {
notNull(object, message, DEFAULT_EXCEPTION);
public static <T> T notNull(T arg, String message) {
return notNull(arg, message, DEFAULT_EXCEPTION);
}
@Deprecated
public static void notNull(Object object, Class<? extends RuntimeException> exceptionType) {
notNull(object, "Found unexpected null object", exceptionType);
public static <T> T notNull(T arg, Class<? extends RuntimeException> exceptionType) {
return notNull(arg, NOT_NULL_MESSAGE, exceptionType);
}
@Deprecated
public static void notNull(Object object, String message, Class<? extends RuntimeException> exceptionType) {
notNull(object, () -> throwException(message, exceptionType));
public static <T> T notNull(T arg, String message, Class<? extends RuntimeException> exceptionType) {
return notNull(arg, () -> throwException(message, exceptionType));
}
@Deprecated
public static void notNull(Object object, Runnable runnable) {
if (object == null) {
log.error("Not expecting object to be null");
public static <T> T notNull(T arg, Runnable runnable) {
if (arg == null) {
log.error(NOT_NULL_MESSAGE);
runnable.run();
}
return arg;
}
// ---------- No Nulls
public static void noNulls(Object... objects) {
noNulls(getNullObjectsString(objects), objects);
noNulls(getNoNullsString(objects), objects);
}
public static void noNulls(String message, Object... objects) {
......@@ -427,7 +425,7 @@ public class Validatec {
}
public static void noNulls(Class<? extends RuntimeException> exceptionType, Object... objects) {
noNulls(getNullObjectsString(objects), exceptionType, objects);
noNulls(getNoNullsString(objects), exceptionType, objects);
}
public static void noNulls(String message, Class<? extends RuntimeException> exceptionType, Object... objects) {
......@@ -440,19 +438,19 @@ public class Validatec {
public static void noNulls(Runnable runnable, Object... objects) {
if (objects == null) {
log.error(getNullObjectsString(objects));
log.error(getNoNullsString(objects));
runnable.run();
}
for (Object object : objects) {
if (object == null) {
log.error(getNullObjectsString(objects));
log.error(getNoNullsString(objects));
runnable.run();
}
}
}
private static String getNullObjectsString(Object... objects) {
return "Found null in: " + Arrays.toString(objects);
private static String getNoNullsString(Object... objects) {
return Arrays.toString(objects) + " should not contain nulls";
}
// ---------- String Contains
......@@ -474,7 +472,7 @@ public class Validatec {
}
private static String stringContainsErrorMessage(String string, String subString) {
return "Substring: '" + subString + "' does not exist in String: " + string;
return wrap(string) + " should contain " + wrap(subString);
}
// ---------- Collection contains entry
......@@ -507,8 +505,8 @@ public class Validatec {
}
}
private static String collectionContainsErrorMessage(Collection collection, Object value) {
return "Unable to find: " + value + ", in collection: " + collection;
private static <T> String collectionContainsErrorMessage(Collection<T> collection, T value) {
return collection + " should contain " + value;
}
// ---------- Collection does not contain entry
......@@ -521,13 +519,13 @@ public class Validatec {
doesNotContain(collection, value, message, DEFAULT_EXCEPTION);
}
public static <T> void doesNotContain(Collection<T> collection, T value, Class<? extends RuntimeException> exceptionType) {
doesNotContain(collection, value, collectionContainsErrorMessage(collection, value), exceptionType);
public static <T> void doesNotContain(Collection<T> collection, T value, Class<? extends RuntimeException> exception) {
doesNotContain(collection, value, collectionContainsErrorMessage(collection, value), exception);
}
public static <T> void doesNotContain(Collection<T> collection, T value, String message,
Class<? extends RuntimeException> exceptionType) {
doesNotContain(collection, value, () -> throwException(message, exceptionType));
Class<? extends RuntimeException> exception) {
doesNotContain(collection, value, () -> throwException(message, exception));
}
public static <T> void doesNotContain(Collection<T> collection, T value, RuntimeException e) {
......@@ -541,8 +539,8 @@ public class Validatec {
}
}
private static String collectionDoesNotContainsErrorMessage(Collection collection, Object value) {
return "Found: " + value + ", in collection: " + collection;
private static <T> String collectionDoesNotContainsErrorMessage(Collection<T> collection, T value) {
return collection + " should not contain " + value;
}
// ---------- Not Empty - Collection
......@@ -563,7 +561,7 @@ public class Validatec {
}
private static String getCollectionNotEmptyMessage(Collection collection) {
return "Collection is empty: " + collection;
return collection + " should not be empty";
}
// ---------- Not Empty - String
......@@ -577,7 +575,7 @@ public class Validatec {
}
public static void notEmpty(String message, Class<? extends RuntimeException> exceptionType, String... strings) {
if (strings == null) {
if (strings == null || strings.length == 0) {
log.error(getStringNotEmptyMessage(strings));
throwException(message, exceptionType);
}
......@@ -590,7 +588,7 @@ public class Validatec {
}
private static String getStringNotEmptyMessage(String... strings) {
return "One of the strings is null or empty: " + Arrays.toString(strings);
return Arrays.toString(strings) + " should not be empty, and should not contain null/empty strings";
}
// ---------- Map contains key, value
......@@ -616,10 +614,14 @@ public class Validatec {
}
private static <K, V> String getMapContainsMessage(Map<K, V> map, K key, V value) {
return "Unable to find: (" + key + ", " + value + ") in map: " + map;
return map.entrySet() + " should contain " + Pair.with(key, value);
}
// ----------
// ---------- Helpers
private static String wrap(String arg) {
return arg == null ? "null" : "'" + arg + "'";
}
// Enables lazy construction of exception, only if it will be thrown
private static void throwException(String message, Class<? extends RuntimeException> exceptionType) {
......
......@@ -102,8 +102,13 @@ public class ValidatecTest extends TestBase {
// ------ NotNull
@Test
public void notNull_allGood() {
Validatec.notNull(123);
public void notNull_allGood_primitive() {
Truth.assertThat(Validatec.notNull(123)).isEqualTo(123);
}
@Test
public void notNull_allGood_object() {
Truth.assertThat(Validatec.notNull("")).isEqualTo("");
}
@Test (expected = IllegalArgumentException.class)
......@@ -122,7 +127,7 @@ public class ValidatecTest extends TestBase {
Validatec.notNull(null, ArithmeticException.class);
Assert.fail();
} catch (ArithmeticException expected) {
Truth.assertThat(expected).hasMessageThat().contains("Found unexpected null object");
Truth.assertThat(expected).hasMessageThat().isEqualTo("Argument should not be null");
}
}
......@@ -154,7 +159,7 @@ public class ValidatecTest extends TestBase {
Validatec.noNulls(ArithmeticException.class, null, Optional.empty());
Assert.fail();
} catch (ArithmeticException e) {
Truth.assertThat(e).hasMessageThat().contains("Found null in: [null, Optional.empty]");
Truth.assertThat(e).hasMessageThat().isEqualTo("[null, Optional.empty] should not contain nulls");
}
}
......@@ -181,7 +186,7 @@ public class ValidatecTest extends TestBase {
Validatec.size(ImmutableList.of(1, 2), 3, ArithmeticException.class);
Assert.fail();
} catch (ArithmeticException e) {
Truth.assertThat(e).hasMessageThat().contains("Size check failed. Expecting: 3, found: [1, 2]");
Truth.assertThat(e).hasMessageThat().isEqualTo("[1, 2] should have 3 entries");
}
}
......@@ -191,7 +196,7 @@ public class ValidatecTest extends TestBase {
Validatec.size(null, 3, ArithmeticException.class);
Assert.fail();
} catch (ArithmeticException e) {
Truth.assertThat(e).hasMessageThat().contains("Size check failed. Expecting: 3, found: null");
Truth.assertThat(e).hasMessageThat().isEqualTo("null should have 3 entries");
}
}
......@@ -208,7 +213,7 @@ public class ValidatecTest extends TestBase {
Validatec.length(new Character[]{'a', 'b'}, 3, ArithmeticException.class);
Assert.fail();
} catch (ArithmeticException e) {
Truth.assertThat(e).hasMessageThat().contains("Array length check failed. Expecting: 3, found: [a, b]");
Truth.assertThat(e).hasMessageThat().isEqualTo("[a, b] should have 3 entries");
}
}
......@@ -218,7 +223,7 @@ public class ValidatecTest extends TestBase {
Validatec.length((Object[]) null, 3, ArithmeticException.class);
Assert.fail();
} catch (ArithmeticException e) {
Truth.assertThat(e).hasMessageThat().contains("Array length check failed. Expecting: 3, found: null");
Truth.assertThat(e).hasMessageThat().isEqualTo("null should have 3 entries");
}
}
......@@ -235,17 +240,17 @@ public class ValidatecTest extends TestBase {
Validatec.length("ab", 3, ArithmeticException.class);
Assert.fail();
} catch (ArithmeticException e) {
Truth.assertThat(e).hasMessageThat().contains("String length check failed. Expecting: 3, found: ab");
Truth.assertThat(e).hasMessageThat().isEqualTo("'ab' should be 3 characters long");
}
}
@Test
public void nullString_throwExceptionGiven() {
try {
Validatec.length((String) null, 3, ArithmeticException.class);
Validatec.length((String) null, 4, ArithmeticException.class);
Assert.fail();
} catch (ArithmeticException e) {
Truth.assertThat(e).hasMessageThat().contains("String length check failed. Expecting: 3, found: null");
Truth.assertThat(e).hasMessageThat().contains("null should be 4 characters long");
}
}
......@@ -262,7 +267,7 @@ public class ValidatecTest extends TestBase {
Validatec.matches("abc", "[ab]+", ArithmeticException.class);
Assert.fail();
} catch (ArithmeticException e) {
Truth.assertThat(e).hasMessageThat().contains("String matches check failed. Expecting: [ab]+, found: abc");
Truth.assertThat(e).hasMessageThat().isEqualTo("'abc' should match '[ab]+'");
}
}
......@@ -272,7 +277,7 @@ public class ValidatecTest extends TestBase {
Validatec.matches(null, "[abc]+", ArithmeticException.class);
Assert.fail();
} catch (ArithmeticException e) {
Truth.assertThat(e).hasMessageThat().contains("String matches check failed. Expecting: [abc]+, found: null");
Truth.assertThat(e).hasMessageThat().isEqualTo("null should match '[abc]+'");
}
}
......@@ -282,7 +287,7 @@ public class ValidatecTest extends TestBase {
Validatec.matches("abc", null, ArithmeticException.class);
Assert.fail();
} catch (ArithmeticException e) {
Truth.assertThat(e).hasMessageThat().contains("String matches check failed. Expecting: null, found: abc");
Truth.assertThat(e).hasMessageThat().isEqualTo("'abc' should match null");
}
}
......@@ -471,7 +476,7 @@ public class ValidatecTest extends TestBase {
Validatec.notEmpty(Lists.newArrayList());
Assert.fail();
} catch (IllegalArgumentException expected) {
Truth.assertThat(expected).hasMessageThat().contains("Collection is empty");
Truth.assertThat(expected).hasMessageThat().isEqualTo("[] should not be empty");
}
}
......@@ -488,7 +493,8 @@ public class ValidatecTest extends TestBase {
Validatec.notEmpty("123", "");
Assert.fail();
} catch (IllegalArgumentException expected) {
Truth.assertThat(expected).hasMessageThat().isEqualTo("One of the strings is null or empty: [123, ]");
Truth.assertThat(expected).hasMessageThat()
.isEqualTo("[123, ] should not be empty, and should not contain null/empty strings");
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment