Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,89 @@ public final class ClientUtils {

private ClientUtils() {}

/**
* Checks whether the given string is non-null and contains at least one non-whitespace character.
*
* @param str the string to check
* @return {@code true} if {@code str} is non-null and has at least one non-whitespace character
*/
public static boolean isNotBlank(String str) {
return str != null && !str.trim().isEmpty();
}

/**
* Checks whether the given string is null or contains only whitespace.
*
* @param str the string to check
* @return {@code true} if {@code str} is null or trims to an empty string
*/
public static boolean isBlank(String str) {
return str == null || str.trim().isEmpty();
}

/**
* Checks whether the given string is null or empty (without trimming).
*
* @param str the string to check
* @return {@code true} if {@code str} is null or has zero length
*/
public static boolean isNullOrEmpty(String str) {
return str == null || str.isEmpty();
}

/**
* Returns {@code value} if it is not blank, otherwise returns {@code defaultValue}.
*
* @param value the candidate value
* @param defaultValue the value to return when {@code value} is null or only whitespace
* @return {@code value} when non-blank, {@code defaultValue} otherwise
*/
public static String defaultIfBlank(String value, String defaultValue) {
return isBlank(value) ? defaultValue : value;
}

/**
* Validates that the given string is non-blank.
*
* @param value the value to validate
* @param name human-readable name used in the error message
* @return {@code value} unchanged
* @throws IllegalArgumentException if {@code value} is null or only whitespace
*/
public static String requireNonBlank(String value, String name) {
if (isBlank(value)) {
throw new IllegalArgumentException("\"" + name + "\" must be non-null and non-blank");
}
return value;
}

/**
* Truncates the given string to at most {@code maxLength} characters. A {@code maxLength} of
* {@code 0} returns an empty string; a negative value throws {@link IllegalArgumentException}.
*
* @param value the string to truncate (may be null)
* @param maxLength maximum number of characters to keep (must be non-negative)
* @return the original string when null or already short enough, or a truncated copy otherwise
* @throws IllegalArgumentException if {@code maxLength} is negative
*/
public static String truncate(String value, int maxLength) {
if (maxLength < 0) {
throw new IllegalArgumentException("maxLength must be non-negative, got " + maxLength);
}
if (value == null || value.length() <= maxLength) {
return value;
}
return value.substring(0, maxLength);
}

/**
* Null-safe, case-insensitive string comparison.
*
* @param a first string (may be null)
* @param b second string (may be null)
* @return {@code true} if both are null or if they are equal ignoring case
*/
public static boolean equalsIgnoreCase(String a, String b) {
return a == null ? b == null : a.equalsIgnoreCase(b);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.clickhouse.client.api.internal;

import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.expectThrows;

@Test(groups = {"unit"})
public class ClientUtilsTest {

// ---------- isBlank ----------

@Test
public void isBlank_returnsTrueForNull() {
assertTrue(ClientUtils.isBlank(null));
}

@Test
public void isBlank_returnsTrueForEmpty() {
assertTrue(ClientUtils.isBlank(""));
}

@Test
public void isBlank_returnsTrueForWhitespaceOnly() {
assertTrue(ClientUtils.isBlank(" "));
assertTrue(ClientUtils.isBlank("\t"));
assertTrue(ClientUtils.isBlank("\n"));
assertTrue(ClientUtils.isBlank(" \t \n "));
}

@Test
public void isBlank_returnsFalseForNonBlank() {
assertFalse(ClientUtils.isBlank("a"));
assertFalse(ClientUtils.isBlank(" a "));
assertFalse(ClientUtils.isBlank("hello world"));
}

// ---------- defaultIfBlank ----------

@Test
public void defaultIfBlank_returnsDefaultWhenNull() {
assertEquals(ClientUtils.defaultIfBlank(null, "fallback"), "fallback");
}

@Test
public void defaultIfBlank_returnsDefaultWhenEmpty() {
assertEquals(ClientUtils.defaultIfBlank("", "fallback"), "fallback");
}

@Test
public void defaultIfBlank_returnsDefaultWhenWhitespace() {
assertEquals(ClientUtils.defaultIfBlank(" ", "fallback"), "fallback");
assertEquals(ClientUtils.defaultIfBlank("\t\n", "fallback"), "fallback");
}

@Test
public void defaultIfBlank_returnsValueWhenNonBlank() {
String value = "hello";
assertSame(ClientUtils.defaultIfBlank(value, "fallback"), value);
}

@Test
public void defaultIfBlank_returnsValueWithSurroundingWhitespace() {
assertEquals(ClientUtils.defaultIfBlank(" x ", "fallback"), " x ");
}

@Test
public void defaultIfBlank_returnsNullDefaultWhenBothBlank() {
assertNull(ClientUtils.defaultIfBlank(null, null));
assertNull(ClientUtils.defaultIfBlank(" ", null));
}

// ---------- truncate ----------

@Test
public void truncate_returnsNullWhenValueIsNull() {
assertNull(ClientUtils.truncate(null, 10));
assertNull(ClientUtils.truncate(null, 0));
}

@Test
public void truncate_returnsSameInstanceWhenAlreadyShortEnough() {
String value = "abc";
assertSame(ClientUtils.truncate(value, 3), value);
assertSame(ClientUtils.truncate(value, 10), value);
}

@Test
public void truncate_shortensLongerString() {
assertEquals(ClientUtils.truncate("abcdef", 3), "abc");
assertEquals(ClientUtils.truncate("abcdef", 5), "abcde");
assertEquals(ClientUtils.truncate("abcdef", 0), "");
}

@Test
public void truncate_returnsEmptyStringForEmptyInput() {
assertEquals(ClientUtils.truncate("", 0), "");
assertEquals(ClientUtils.truncate("", 5), "");
}

@Test
public void truncate_throwsOnNegativeMaxLength() {
IllegalArgumentException ex = expectThrows(IllegalArgumentException.class,
() -> ClientUtils.truncate("abc", -1));
assertTrue(ex.getMessage().contains("-1"));
}
}
Loading
Loading