
com.trigonic.utils.test.junit.DelayedAssert Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of test-utils Show documentation
Show all versions of test-utils Show documentation
Provides utilities for testing
The newest version!
package com.trigonic.utils.test.junit;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.Callable;
public class DelayedAssert {
private static final long DEFAULT_POLL_INTERVAL = 250L;
/* true */
public static void assertTrueAfter(long timeout, Callable callable) throws Exception {
assertTrueAfter(timeout, DEFAULT_POLL_INTERVAL, callable);
}
public static void assertTrueAfter(long timeout, long pollInterval, Callable callable) throws Exception {
long stopAfter = System.currentTimeMillis() + timeout;
while (System.currentTimeMillis() < stopAfter && !callable.call()) {
Thread.sleep(pollInterval);
}
assertTrue(callable.call());
}
/* false */
public static void assertFalseAfter(long timeout, Callable callable) throws Exception {
assertFalseAfter(timeout, DEFAULT_POLL_INTERVAL, callable);
}
public static void assertFalseAfter(long timeout, long pollInterval, Callable callable) throws Exception {
assertTrueAfter(timeout, pollInterval, new Not(callable));
}
/* null */
public static void assertNullAfter(long timeout, Callable callable) throws Exception {
assertNullAfter(timeout, DEFAULT_POLL_INTERVAL, callable);
}
public static void assertNullAfter(long timeout, long pollInterval, Callable callable) throws Exception {
assertEqualsAfter(null, timeout, pollInterval, callable);
}
/* not null */
public static void assertNotNullAfter(long timeout, Callable callable) throws Exception {
assertNotNullAfter(timeout, DEFAULT_POLL_INTERVAL, callable);
}
public static void assertNotNullAfter(long timeout, long pollInterval, Callable callable) throws Exception {
assertNotEqualsAfter(null, timeout, pollInterval, callable);
}
/* equals */
public static void assertEqualsAfter(T expected, long timeout, Callable callable) throws Exception {
assertEqualsAfter(expected, timeout, DEFAULT_POLL_INTERVAL, callable);
}
public static void assertEqualsAfter(T expected, long timeout, long pollInterval, Callable callable) throws Exception {
assertTrueAfter(timeout, pollInterval, new CallableEquals(expected, callable));
}
/* not equals */
public static void assertNotEqualsAfter(T expected, long timeout, Callable callable) throws Exception {
assertNotEqualsAfter(expected, timeout, DEFAULT_POLL_INTERVAL, callable);
}
public static void assertNotEqualsAfter(T expected, long timeout, long pollInterval, Callable callable) throws Exception {
assertFalseAfter(timeout, pollInterval, new CallableEquals(expected, callable));
}
/* same */
public static void assertSameAfter(T expected, long timeout, Callable callable) throws Exception {
assertSameAfter(expected, timeout, DEFAULT_POLL_INTERVAL, callable);
}
public static void assertSameAfter(T expected, long timeout, long pollInterval, Callable callable) throws Exception {
assertTrueAfter(timeout, pollInterval, new CallableSame(expected, callable));
}
/* different */
public static void assertDifferentAfter(T expected, long timeout, Callable callable) throws Exception {
assertDifferentAfter(expected, timeout, DEFAULT_POLL_INTERVAL, callable);
}
public static void assertDifferentAfter(T expected, long timeout, long pollInterval, Callable callable) throws Exception {
assertFalseAfter(timeout, pollInterval, new CallableSame(expected, callable));
}
/* useful callables */
protected static class Not implements Callable {
private Callable callable;
public Not(Callable callable) {
this.callable = callable;
}
public Boolean call() throws Exception {
return !callable.call();
}
}
protected static class CallableSame implements Callable {
private T expected;
private Callable callable;
public CallableSame(T expected, Callable callable) {
this.expected = expected;
this.callable = callable;
}
public Boolean call() throws Exception {
T actual = callable.call();
return expected == actual;
}
}
protected static class CallableEquals implements Callable {
private T expected;
private Callable callable;
public CallableEquals(T expected, Callable callable) {
this.expected = expected;
this.callable = callable;
}
public Boolean call() throws Exception {
T actual = callable.call();
return expected == actual || (expected != null && actual != null && expected.equals(actual));
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy