📄 uispecassert.java
字号:
package org.uispec4j.assertion;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.uispec4j.UISpec4J;
import org.uispec4j.UISpecTestCase;
import org.uispec4j.utils.Utils;
import javax.swing.*;
/**
* Checks the validity of Assertion objects.<p/>
* These methods are not meant to be used directly from within tests - you can use
* them through {@link UISpecTestCase} or your own
* {@link TestCase} implementation.
*/
public class UISpecAssert {
/**
* Checks that the given assertion succeeds (with a retry strategy).
* The {@link Assertion#check()} method is called until the timeout
* specified by {@link UISpec4J#setAssertionTimeLimit(long)} is reached.
*/
public static void assertTrue(Assertion assertion) {
assertTrue(null, assertion);
}
/**
* Checks that the given assertion succeeds (with a retry strategy).
* The {@link Assertion#check()} method is called until the timeout
* specified by {@link UISpec4J#setAssertionTimeLimit(long)} is reached.
* If it fails an AssertionFailedError is thrown with the given message.
*/
public static void assertTrue(String message, Assertion assertion) {
checkAssertion(message, assertion, UISpec4J.getAssertionTimeLimit());
}
/**
* Checks that the given assertion fails (with a retry strategy).
* The {@link Assertion#check()} method is called until the timeout
* specified by {@link UISpec4J#setAssertionTimeLimit(long)} is reached.
*/
public static void assertFalse(final Assertion assertion) {
assertTrue(not(assertion));
}
/**
* Checks the given assertion fails (with a retry strategy).
* The {@link Assertion#check()} method is called until the timeout
* specified by {@link UISpec4J#setAssertionTimeLimit(long)} is reached.
* If it succeeds an AssertionFailedError is thrown with the given message.
*/
public static void assertFalse(String message, final Assertion assertion) {
assertTrue(message, not(assertion));
}
/**
* Waits until the given assertion becomes true.
* The {@link Assertion#check()} method is called until the timeout
* specified as parameter is reached.
*/
public static void waitUntil(Assertion assertion, long waitTimeLimit) {
checkAssertion(null, assertion, waitTimeLimit);
}
/**
* Waits until the given assertion becomes true.
* The {@link Assertion#check()} method is called until the timeout
* specified as parameter is reached.
* If it fails an AssertionFailedError is thrown with the given message.
*/
public static void waitUntil(String message, Assertion assertion, long waitTimeLimit) {
checkAssertion(message, assertion, waitTimeLimit);
}
/**
* Checks the given assertion equals the expected parameter (with a retry strategy).
* The {@link Assertion#check()} method is called until the timeout
* specified by {@link UISpec4J#setAssertionTimeLimit(long)} is reached.
*/
public static void assertEquals(boolean expected, Assertion assertion) {
assertEquals(null, expected, assertion);
}
/**
* Checks the given assertion equals the expected parameter (with a retry strategy).
* The {@link Assertion#check()} method is called until the timeout
* specified by {@link UISpec4J#setAssertionTimeLimit(long)} is reached.
* If it fails an AssertionFailedError is thrown with the given message.
*/
public static void assertEquals(String message, boolean expected, Assertion assertion) {
if (expected) {
assertTrue(message, assertion);
}
else {
assertFalse(message, assertion);
}
}
/**
* Returns a negation of the given assertion.
*/
public static Assertion not(final Assertion assertion) {
return new Assertion() {
public void check() {
try {
assertion.check();
throw new FailureNotDetectedError();
}
catch (FailureNotDetectedError e) {
throw new AssertionFailedError();
}
catch (Throwable e) {
}
}
};
}
/**
* Returns the intersection of the {@link Assertion} parameters.
*/
public static Assertion and(final Assertion[] assertions) {
return new Assertion() {
public void check() throws Exception {
for (int i = 0; i < assertions.length; i++) {
assertions[i].check();
}
}
};
}
/**
* Returns the union of the {@link Assertion} parameters.
*/
public static Assertion or(final Assertion[] assertions) {
return new Assertion() {
public void check() {
for (int i = 0; i < assertions.length; i++) {
try {
assertions[i].check();
break;
}
catch (Throwable e) {
if (i == assertions.length - 1) {
throw new AssertionFailedError();
}
}
}
}
};
}
private static void checkAssertion(String message, Assertion assertion, long waitTimeLimit) {
try {
Utils.waitForPendingAwtEventsToBeProcessed();
assertion.check();
}
catch (Throwable e) {
retry(message, assertion, waitTimeLimit);
}
}
/**
* @noinspection ForLoopThatDoesntUseLoopVariable
*/
private static void retry(String message, Assertion assertion, long waitTimeLimit) {
long elapsedTime = 0;
for (int i = 0; elapsedTime < waitTimeLimit; i++) {
int waitTime = i < 10 ? 20 : 200;
Utils.sleep(waitTime);
elapsedTime += waitTime;
try {
assertion.check();
return;
}
catch (Throwable e) {
}
}
try {
assertion.check();
}
catch (AssertionFailedError e) {
if (message == null) {
throw e;
}
throw new AssertionFailedError(message);
}
catch (Exception e) {
if (message != null)
throw new RuntimeException(message, e);
else {
throw new RuntimeException(e.getMessage(), e);
}
}
}
private static class FailureNotDetectedError extends AssertionFailedError {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -