Commit 31016c8f authored by Rajiv Prabhakar's avatar Rajiv Prabhakar
Browse files

v1.13.0 release: Validatec.notEmpty(String) added

parent fd1b96a6
Pipeline #14122436 passed with stage
in 1 minute and 34 seconds
......@@ -6,7 +6,7 @@
<groupId>com.rajivprab</groupId>
<artifactId>cava</artifactId>
<version>1.12.0</version>
<version>1.13.0</version>
<name>Cava: Clean Java</name>
<description>A library that enables users to write minimal, clean and simple Java</description>
......
......@@ -566,28 +566,56 @@ public class Validatec {
// ---------- Not Empty - String
public static String notEmpty(String string) {
return notEmpty(string, DEFAULT_EXCEPTION);
}
public static String notEmpty(String string, Class<? extends RuntimeException> exceptionType) {
return notEmpty(string, getStringNotEmptyMessage(string), exceptionType);
}
public static String notEmpty(String string, String message, Class<? extends RuntimeException> exceptionType) {
if (Strings.isNullOrEmpty(string)) {
log.error(getStringNotEmptyMessage(string));
throwException(message, exceptionType);
}
return string;
}
private static String getStringNotEmptyMessage(String string) {
return "'" + string + "' should not be null or empty";
}
// ---------- Not Empty - Strings-array
@Deprecated
public static void notEmpty(String... strings) {
notEmpty(DEFAULT_EXCEPTION, strings);
}
@Deprecated
public static void notEmpty(Class<? extends RuntimeException> exceptionType, String... strings) {
notEmpty(getStringNotEmptyMessage(strings), exceptionType, strings);
notEmpty(getStringsNotEmptyMessage(strings), exceptionType, strings);
}
@Deprecated
public static void notEmpty(String message, Class<? extends RuntimeException> exceptionType, String... strings) {
if (strings == null || strings.length == 0) {
log.error(getStringNotEmptyMessage(strings));
log.error(getStringsNotEmptyMessage(strings));
throwException(message, exceptionType);
}
if (strings.length == 1) {
log.error("For single-string check, use other notEmpty method. Found: " + strings[0]);
}
for (String string : strings) {
if (Strings.isNullOrEmpty(string)) {
log.error(getStringNotEmptyMessage(strings));
log.error(getStringsNotEmptyMessage(strings));
throwException(message, exceptionType);
}
}
}
private static String getStringNotEmptyMessage(String... strings) {
private static String getStringsNotEmptyMessage(String... strings) {
return Arrays.toString(strings) + " should not be empty, and should not contain null/empty strings";
}
......
......@@ -480,7 +480,35 @@ public class ValidatecTest extends TestBase {
}
}
// -------- Strings notEmpty
// -------- String notEmpty
@Test
public void stringNotEmpty_allGood() {
Truth.assertThat(Validatec.notEmpty("a", null, ArithmeticException.class)).isEqualTo("a");
}
@Test
public void stringNotEmpty_failEmpty_constructDefaultException() {
try {
Validatec.notEmpty("");
Assert.fail();
} catch (IllegalArgumentException expected) {
Truth.assertThat(expected).hasMessageThat()
.isEqualTo("'' should not be null or empty");
}
}
@Test
public void stringNotEmpty_failNull_constructGivenException() {
try {
Validatec.notEmpty((String) null, "my custom message", ArithmeticException.class);
Assert.fail();
} catch (ArithmeticException expected) {
Truth.assertThat(expected).hasMessageThat().contains("my custom message");
}
}
// -------- Strings array notEmpty
@Test
public void stringsNotEmpty_allGood() {
......
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