Commit 71581a14 authored by Rajiv Prabhakar's avatar Rajiv Prabhakar
Browse files

v1.6.0 release:

- Added Validatec array-length-check, string-length-check and string-matches-check
parent 47d97c54
Pipeline #9294738 failed with stage
in 1 minute and 11 seconds
......@@ -6,7 +6,7 @@
<groupId>com.rajivprab</groupId>
<artifactId>cava</artifactId>
<version>1.5.0</version>
<version>1.6.0</version>
<name>Cava: Clean Java</name>
<description>A library that enables users to write minimal, clean and simple Java code</description>
......
......@@ -4,6 +4,7 @@ import com.google.common.base.Strings;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
......@@ -71,6 +72,93 @@ public class Validatec {
return "Size check failed. Expecting: " + size + ", found: " + collection;
}
// ---------- Array.length
public static void length(Object[] array, int length) {
length(array, length, getArrayLengthMessage(array, length));
}
public static void length(Object[] array, int length, String message) {
length(array, length, message, DEFAULT_EXCEPTION);
}
public static void length(Object[] array, int length, Class<? extends RuntimeException> exceptionType) {
length(array, length, getArrayLengthMessage(array, length), exceptionType);
}
public static void length(Object[] array, int length, String message, Class<? extends RuntimeException> type) {
length(array, length, () -> throwException(message, type));
}
public static void length(Object[] array, int length, Runnable runnable) {
if (array == null || array.length != length) {
log.error(getArrayLengthMessage(array, length));
runnable.run();
}
}
private static String getArrayLengthMessage(Object[] array, int length) {
return "Array length check failed. Expecting: " + length + ", found: " + Arrays.toString(array);
}
// ---------- String.length
public static void length(String string, int length) {
length(string, length, getStringLengthMessage(string, length));
}
public static void length(String string, int length, String message) {
length(string, length, message, DEFAULT_EXCEPTION);
}
public static void length(String string, int length, Class<? extends RuntimeException> exceptionType) {
length(string, length, getStringLengthMessage(string, length), exceptionType);
}
public static void length(String string, int length, String message, Class<? extends RuntimeException> exceptionType) {
length(string, length, () -> throwException(message, exceptionType));
}
public static void length(String string, int length, Runnable runnable) {
if (string == null || string.length() != length) {
log.error(getStringLengthMessage(string, length));
runnable.run();
}
}
private static String getStringLengthMessage(String string, int length) {
return "String length check failed. Expecting: " + length + ", found: " + string;
}
// ---------- String.matches
public static void matches(String string, String regex) {
matches(string, regex, getStringMatchesMessage(string, regex));
}
public static void matches(String string, String regex, String message) {
matches(string, regex, message, DEFAULT_EXCEPTION);
}
public static void matches(String string, String regex, Class<? extends RuntimeException> exceptionType) {
matches(string, regex, getStringMatchesMessage(string, regex), exceptionType);
}
public static void matches(String string, String regex, String message, Class<? extends RuntimeException> exceptionType) {
matches(string, regex, () -> throwException(message, exceptionType));
}
public static void matches(String string, String regex, Runnable runnable) {
if (regex == null || string == null || !string.matches(regex)) {
log.error(getStringMatchesMessage(string, regex));
runnable.run();
}
}
private static String getStringMatchesMessage(String string, String regex) {
return "String matches check failed. Expecting: " + regex + ", found: " + string;
}
// ---------- Less than - Numbers
public static <T> void lessThan(Comparable<T> one, T two) {
......
......@@ -108,7 +108,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");
Truth.assertThat(e).hasMessageThat().contains("Size check failed. Expecting: 3, found: [1, 2]");
}
}
......@@ -118,7 +118,98 @@ 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");
Truth.assertThat(e).hasMessageThat().contains("Size check failed. Expecting: 3, found: null");
}
}
// ----- Array.length
@Test
public void matchesArrayLength_allGood() {
Validatec.length(new Integer[]{1, 2}, 2);
}
@Test
public void doesNotMatchArrayLength_throwDefaultException() {
try {
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]");
}
}
@Test
public void nullArray_throwExceptionGiven() {
try {
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");
}
}
// ----- String.length
@Test
public void matchesStringLength_allGood() {
Validatec.length("12", 2);
}
@Test
public void doesNotMatchStringLength_throwDefaultException() {
try {
Validatec.length("ab", 3, ArithmeticException.class);
Assert.fail();
} catch (ArithmeticException e) {
Truth.assertThat(e).hasMessageThat().contains("String length check failed. Expecting: 3, found: ab");
}
}
@Test
public void nullString_throwExceptionGiven() {
try {
Validatec.length((String) null, 3, ArithmeticException.class);
Assert.fail();
} catch (ArithmeticException e) {
Truth.assertThat(e).hasMessageThat().contains("String length check failed. Expecting: 3, found: null");
}
}
// ----- String.matches
@Test
public void matchesString_allGood() {
Validatec.matches("abc", "[cab]+");
}
@Test
public void doesNotMatchString_throwDefaultException() {
try {
Validatec.matches("abc", "[ab]+", ArithmeticException.class);
Assert.fail();
} catch (ArithmeticException e) {
Truth.assertThat(e).hasMessageThat().contains("String matches check failed. Expecting: [ab]+, found: abc");
}
}
@Test
public void nullString_failMatch_throwExceptionGiven() {
try {
Validatec.matches(null, "[abc]+", ArithmeticException.class);
Assert.fail();
} catch (ArithmeticException e) {
Truth.assertThat(e).hasMessageThat().contains("String matches check failed. Expecting: [abc]+, found: null");
}
}
@Test
public void nullRegex_failMatch_throwExceptionGiven() {
try {
Validatec.matches("abc", null, ArithmeticException.class);
Assert.fail();
} catch (ArithmeticException e) {
Truth.assertThat(e).hasMessageThat().contains("String matches check failed. Expecting: null, found: abc");
}
}
......
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