Skip to content

Commit

Permalink
Introduce StringUtils and Preconditions.notBlank(..)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Oct 30, 2015
1 parent 5bb449c commit 88ee91e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 14 deletions.
Expand Up @@ -23,10 +23,6 @@ private ObjectUtils() {
/* no-op */ /* no-op */
} }


public static boolean isEmpty(CharSequence charSequence) {
return (charSequence == null || charSequence.length() == 0);
}

public static String nullSafeToString(Class<?>... classes) { public static String nullSafeToString(Class<?>... classes) {
if (classes == null || classes.length == 0) { if (classes == null || classes.length == 0) {
return ""; return "";
Expand Down
Expand Up @@ -31,17 +31,31 @@ public static void notNull(Object object, Supplier<String> messageSupplier) {
} }


/** /**
* @see ObjectUtils#isEmpty(CharSequence) * @see StringUtils#isEmpty(CharSequence)
*/ */
public static void notEmpty(String str, String message) { public static void notEmpty(String str, String message) {
condition(!ObjectUtils.isEmpty(str), message); condition(!StringUtils.isEmpty(str), message);
} }


/** /**
* @see ObjectUtils#isEmpty(CharSequence) * @see StringUtils#isEmpty(CharSequence)
*/ */
public static void notEmpty(String str, Supplier<String> messageSupplier) { public static void notEmpty(String str, Supplier<String> messageSupplier) {
condition(!ObjectUtils.isEmpty(str), messageSupplier); condition(!StringUtils.isEmpty(str), messageSupplier);
}

/**
* @see StringUtils#isBlank(String)
*/
public static void notBlank(String str, String message) {
condition(!StringUtils.isBlank(str), message);
}

/**
* @see StringUtils#isBlank(String)
*/
public static void notBlank(String str, Supplier<String> messageSupplier) {
condition(!StringUtils.isBlank(str), messageSupplier);
} }


public static void condition(boolean predicate, String message) { public static void condition(boolean predicate, String message) {
Expand Down
@@ -0,0 +1,31 @@
/*
* Copyright 2015 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.junit.gen5.commons.util;

/**
* @author Sam Brannen
* @since 5.0
*/
public final class StringUtils {

private StringUtils() {
/* no-op */
}

public static boolean isEmpty(CharSequence charSequence) {
return (charSequence == null || charSequence.length() == 0);
}

public static boolean isBlank(String str) {
return (str == null || str.trim().length() == 0);
}

}
6 changes: 3 additions & 3 deletions junit5-api/src/main/java/org/junit/gen5/api/Assertions.java
Expand Up @@ -14,7 +14,7 @@
import java.util.function.BooleanSupplier; import java.util.function.BooleanSupplier;
import java.util.function.Supplier; import java.util.function.Supplier;


import org.junit.gen5.commons.util.ObjectUtils; import org.junit.gen5.commons.util.StringUtils;
import org.opentestalliance.AssertionFailedError; import org.opentestalliance.AssertionFailedError;
import org.opentestalliance.MultipleFailuresException; import org.opentestalliance.MultipleFailuresException;


Expand Down Expand Up @@ -250,7 +250,7 @@ public static <T extends Throwable> T expectThrows(Class<T> expected, Executable


private static void failEqual(Object actual, String message) { private static void failEqual(Object actual, String message) {
String prefix = "Values should be different. "; String prefix = "Values should be different. ";
if (!ObjectUtils.isEmpty(message)) { if (!StringUtils.isEmpty(message)) {
prefix = message + ". "; prefix = message + ". ";
} }
fail(prefix + "Actual: " + actual); fail(prefix + "Actual: " + actual);
Expand Down Expand Up @@ -295,7 +295,7 @@ private static String formatClassAndValue(Object value, String valueString) {
} }


private static String buildPrefix(String message) { private static String buildPrefix(String message) {
return (!ObjectUtils.isEmpty(message) ? message + " ==> " : ""); return (!StringUtils.isEmpty(message) ? message + " ==> " : "");
} }


private static String nullSafeGet(Supplier<String> messageSupplier) { private static String nullSafeGet(Supplier<String> messageSupplier) {
Expand Down
Expand Up @@ -10,16 +10,16 @@


package org.junit.gen5.engine.junit5.descriptor; package org.junit.gen5.engine.junit5.descriptor;


import static org.junit.gen5.commons.util.ObjectUtils.nullSafeToString; import static org.junit.gen5.commons.util.ObjectUtils.*;


import java.lang.reflect.Method; import java.lang.reflect.Method;


import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;


import org.junit.gen5.api.Test; import org.junit.gen5.api.Test;
import org.junit.gen5.commons.util.ObjectUtils;
import org.junit.gen5.commons.util.Preconditions; import org.junit.gen5.commons.util.Preconditions;
import org.junit.gen5.commons.util.StringUtils;
import org.junit.gen5.engine.TestDescriptor; import org.junit.gen5.engine.TestDescriptor;


/** /**
Expand Down Expand Up @@ -51,7 +51,7 @@ private String determineDisplayName() {
Test test = testMethod.getAnnotation(Test.class); Test test = testMethod.getAnnotation(Test.class);
if (test != null) { if (test != null) {
String customName = test.name(); String customName = test.name();
if (!ObjectUtils.isEmpty(customName)) { if (!StringUtils.isBlank(customName)) {
return customName; return customName;
} }
} }
Expand Down

0 comments on commit 88ee91e

Please sign in to comment.