Skip to content

Commit

Permalink
Return original object in Preconditions
Browse files Browse the repository at this point in the history
The commit updates the Preconditions class so that its methods return
the original object in case of a successful check. In addition, various
clients of the Preconditions API have been updated to make use of this
new feature.

Issue: #95

------------------------------------------------------------------------
On behalf of the community, the JUnit Lambda Team thanks Klarna AB
(http://www.klarna.com) for supporting the JUnit crowdfunding campaign!
------------------------------------------------------------------------
  • Loading branch information
sbrannen committed Jan 10, 2016
1 parent 04fc374 commit 48ae2ab
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 44 deletions.
Expand Up @@ -38,86 +38,106 @@ private Preconditions() {
* Assert that the supplied {@link Object} is not {@code null}.
*
* @param object the object to check
* @param message precondition failure message
* @param message precondition violation message
* @return the supplied object as a convenience
* @throws IllegalArgumentException if the supplied object is {@code null}
* @see #notNull(Object, Supplier)
*/
public static void notNull(Object object, String message) throws IllegalArgumentException {
notNull(object, () -> message);
public static <T extends Object> T notNull(T object, String message) throws IllegalArgumentException {
return notNull(object, () -> message);
}

/**
* Assert that the supplied {@link Object} is not {@code null}.
*
* @param object the object to check
* @param messageSupplier precondition failure message supplier
* @param messageSupplier precondition violation message supplier
* @return the supplied object as a convenience
* @throws IllegalArgumentException if the supplied object is {@code null}
* @see #condition(boolean, Supplier)
*/
public static void notNull(Object object, Supplier<String> messageSupplier) throws IllegalArgumentException {
public static <T extends Object> T notNull(T object, Supplier<String> messageSupplier)
throws IllegalArgumentException {
condition(object != null, messageSupplier);
return object;
}

/**
* Assert that the supplied {@link String} is not {@code null} or empty.
*
* @param str the string to check
* @param message precondition failure message
* @param message precondition violation message
* @return the supplied string as a convenience
* @throws IllegalArgumentException if the supplied string is {@code null} or empty
* @see #notEmpty(String, Supplier)
*/
public static void notEmpty(String str, String message) throws IllegalArgumentException {
notEmpty(str, () -> message);
public static String notEmpty(String str, String message) throws IllegalArgumentException {
return notEmpty(str, () -> message);
}

/**
* Assert that the supplied {@link String} is not {@code null} or empty.
*
* @param str the string to check
* @param messageSupplier precondition failure message supplier
* @param messageSupplier precondition violation message supplier
* @return the supplied string as a convenience
* @throws IllegalArgumentException if the supplied string is {@code null} or empty
* @see StringUtils#isNotEmpty(CharSequence)
* @see #condition(boolean, Supplier)
*/
public static void notEmpty(String str, Supplier<String> messageSupplier) throws IllegalArgumentException {
public static String notEmpty(String str, Supplier<String> messageSupplier) throws IllegalArgumentException {
condition(StringUtils.isNotEmpty(str), messageSupplier);
return str;
}

/**
* Assert that the supplied {@link Collection} is not {@code null} or empty.
*
* @param collection the collection to check
* @param message precondition failure message
* @param message precondition violation message
* @return the supplied collection as a convenience
* @throws IllegalArgumentException if the supplied collection is {@code null} or empty
* @see #condition(boolean, Supplier)
*/
public static void notEmpty(Collection<?> collection, String message) throws IllegalArgumentException {
public static <T extends Collection<?>> T notEmpty(T collection, String message) throws IllegalArgumentException {
condition(collection != null && !collection.isEmpty(), () -> message);
return collection;
}

/**
* Assert that the supplied {@link String} is not {@code null} or blank.
*
* @param str the string to check
* @param message precondition failure message
* @param message precondition violation message
* @return the supplied string as a convenience
* @throws IllegalArgumentException if the supplied string is {@code null} or blank
* @see #notBlank(String, Supplier)
*/
public static void notBlank(String str, String message) throws IllegalArgumentException {
notBlank(str, () -> message);
public static String notBlank(String str, String message) throws IllegalArgumentException {
return notBlank(str, () -> message);
}

/**
* Assert that the supplied {@link String} is not {@code null} or blank.
*
* @param str the string to check
* @param messageSupplier precondition failure message supplier
* @param messageSupplier precondition violation message supplier
* @return the supplied string as a convenience
* @throws IllegalArgumentException if the supplied string is {@code null} or blank
* @see StringUtils#isNotBlank(String)
* @see #condition(boolean, Supplier)
*/
public static void notBlank(String str, Supplier<String> messageSupplier) throws IllegalArgumentException {
public static String notBlank(String str, Supplier<String> messageSupplier) throws IllegalArgumentException {
condition(StringUtils.isNotBlank(str), messageSupplier);
return str;
}

/**
* Assert that the supplied {@code predicate} is {@code true}.
*
* @param predicate the predicate to check
* @param message precondition failure message
* @param message precondition violation message
* @throws IllegalArgumentException if the predicate is {@code false}
* @see #condition(boolean, Supplier)
*/
public static void condition(boolean predicate, String message) throws IllegalArgumentException {
Expand All @@ -128,7 +148,7 @@ public static void condition(boolean predicate, String message) throws IllegalAr
* Assert that the supplied {@code predicate} is {@code true}.
*
* @param predicate the predicate to check
* @param messageSupplier precondition failure message supplier
* @param messageSupplier precondition violation message supplier
* @throws IllegalArgumentException if the predicate is {@code false}
*/
public static void condition(boolean predicate, Supplier<String> messageSupplier) throws IllegalArgumentException {
Expand Down
Expand Up @@ -36,13 +36,11 @@ public class ToStringBuilder {
private final List<String> values = new ArrayList<>();

public ToStringBuilder(Object obj) {
Preconditions.notNull(obj, "Object must not be null");
this.type = obj.getClass();
this.type = Preconditions.notNull(obj, "Object must not be null").getClass();
}

public ToStringBuilder(Class<?> type) {
Preconditions.notNull(type, "Class must not be null");
this.type = type;
this.type = Preconditions.notNull(type, "Class must not be null");
}

public ToStringBuilder append(String name, Object value) {
Expand Down
Expand Up @@ -34,8 +34,7 @@ public abstract class AbstractTestDescriptor implements TestDescriptor {
private final Set<TestDescriptor> children = new LinkedHashSet<>();

protected AbstractTestDescriptor(String uniqueId) {
Preconditions.notBlank(uniqueId, "uniqueId must not be null or empty");
this.uniqueId = uniqueId;
this.uniqueId = Preconditions.notBlank(uniqueId, "uniqueId must not be null or empty");
}

@Override
Expand Down Expand Up @@ -94,8 +93,7 @@ public Set<? extends TestDescriptor> getChildren() {
}

protected final void setSource(TestSource source) {
Preconditions.notNull(source, "test source must not be null");
this.source = source;
this.source = Preconditions.notNull(source, "TestSource must not be null");
}

@Override
Expand Down
Expand Up @@ -28,8 +28,8 @@ public FileSystemSource(File sourceFileOrDirectory) {
}

public FileSystemSource(File sourceFileOrDirectory, FilePosition positionInFile) {
Preconditions.notNull(sourceFileOrDirectory, "source file or directory must not be null");
this.sourceFileOrDirectory = sourceFileOrDirectory;
this.sourceFileOrDirectory = Preconditions.notNull(sourceFileOrDirectory,
"source file or directory must not be null");
this.positionInFile = positionInFile;
}

Expand Down
Expand Up @@ -44,8 +44,7 @@ public static TestExecutionResult failed(Throwable throwable) {
}

public TestExecutionResult(Status status, Throwable throwable) {
Preconditions.notNull(status, "status must not be null");
this.status = status;
this.status = Preconditions.notNull(status, "Status must not be null");
this.throwable = throwable;
}

Expand Down
Expand Up @@ -27,8 +27,7 @@ public final class TestId implements Serializable {
private final String uniqueId;

TestId(String uniqueId) {
Preconditions.notNull(uniqueId, "uniqueId must not be null");
this.uniqueId = uniqueId;
this.uniqueId = Preconditions.notBlank(uniqueId, "uniqueId must not be null or empty");
}

@Override
Expand All @@ -42,12 +41,12 @@ public boolean equals(Object obj) {

@Override
public int hashCode() {
return uniqueId.hashCode();
return this.uniqueId.hashCode();
}

@Override
public String toString() {
return uniqueId;
return this.uniqueId;
}

}
Expand Up @@ -64,9 +64,7 @@ public class ClassTestDescriptor extends JUnit5TestDescriptor implements Contain
ClassTestDescriptor(String uniqueId, Class<?> testClass) {
super(uniqueId);

Preconditions.notNull(testClass, "Class must not be null");

this.testClass = testClass;
this.testClass = Preconditions.notNull(testClass, "Class must not be null");
this.displayName = determineDisplayName(testClass, testClass.getName());

setSource(new JavaSource(testClass));
Expand Down
Expand Up @@ -45,7 +45,7 @@ JUnit5Testable fromUniqueId(String uniqueId, String engineId) {
}

JUnit5Testable fromClass(Class<?> clazz, String engineId) {
Preconditions.notNull(clazz, "clazz must not be null");
Preconditions.notNull(clazz, "Class must not be null");
Preconditions.notBlank(engineId, "Engine ID must not be null or empty");
if (isPotentialTestContainer.test(clazz)) {
String uniqueId = engineId + ":" + clazz.getName();
Expand Down
Expand Up @@ -49,11 +49,8 @@ public class MethodTestDescriptor extends JUnit5TestDescriptor implements Leaf<J
MethodTestDescriptor(String uniqueId, Class<?> testClass, Method testMethod) {
super(uniqueId);

Preconditions.notNull(testClass, "Class must not be null");
Preconditions.notNull(testMethod, "Method must not be null");

this.testClass = testClass;
this.testMethod = testMethod;
this.testClass = Preconditions.notNull(testClass, "Class must not be null");
this.testMethod = Preconditions.notNull(testMethod, "Method must not be null");
this.displayName = determineDisplayName(testMethod, testMethod.getName());

setSource(new JavaSource(testMethod));
Expand Down

0 comments on commit 48ae2ab

Please sign in to comment.