001: package org.uispec4j.assertion;
002:
003: import junit.framework.AssertionFailedError;
004: import junit.framework.TestCase;
005: import org.uispec4j.UISpec4J;
006: import org.uispec4j.UISpecTestCase;
007: import org.uispec4j.utils.Utils;
008:
009: import javax.swing.*;
010:
011: /**
012: * Checks the validity of Assertion objects.<p/>
013: * These methods are not meant to be used directly from within tests - you can use
014: * them through {@link UISpecTestCase} or your own
015: * {@link TestCase} implementation.
016: */
017: public class UISpecAssert {
018:
019: /**
020: * Checks that the given assertion succeeds (with a retry strategy).
021: * The {@link Assertion#check()} method is called until the timeout
022: * specified by {@link UISpec4J#setAssertionTimeLimit(long)} is reached.
023: */
024: public static void assertTrue(Assertion assertion) {
025: assertTrue(null, assertion);
026: }
027:
028: /**
029: * Checks that the given assertion succeeds (with a retry strategy).
030: * The {@link Assertion#check()} method is called until the timeout
031: * specified by {@link UISpec4J#setAssertionTimeLimit(long)} is reached.
032: * If it fails an AssertionFailedError is thrown with the given message.
033: */
034: public static void assertTrue(String message, Assertion assertion) {
035: checkAssertion(message, assertion, UISpec4J
036: .getAssertionTimeLimit());
037: }
038:
039: /**
040: * Checks that the given assertion fails (with a retry strategy).
041: * The {@link Assertion#check()} method is called until the timeout
042: * specified by {@link UISpec4J#setAssertionTimeLimit(long)} is reached.
043: */
044: public static void assertFalse(final Assertion assertion) {
045: assertTrue(not(assertion));
046: }
047:
048: /**
049: * Checks the given assertion fails (with a retry strategy).
050: * The {@link Assertion#check()} method is called until the timeout
051: * specified by {@link UISpec4J#setAssertionTimeLimit(long)} is reached.
052: * If it succeeds an AssertionFailedError is thrown with the given message.
053: */
054: public static void assertFalse(String message,
055: final Assertion assertion) {
056: assertTrue(message, not(assertion));
057: }
058:
059: /**
060: * Waits until the given assertion becomes true.
061: * The {@link Assertion#check()} method is called until the timeout
062: * specified as parameter is reached.
063: */
064: public static void waitUntil(Assertion assertion, long waitTimeLimit) {
065: checkAssertion(null, assertion, waitTimeLimit);
066: }
067:
068: /**
069: * Waits until the given assertion becomes true.
070: * The {@link Assertion#check()} method is called until the timeout
071: * specified as parameter is reached.
072: * If it fails an AssertionFailedError is thrown with the given message.
073: */
074: public static void waitUntil(String message, Assertion assertion,
075: long waitTimeLimit) {
076: checkAssertion(message, assertion, waitTimeLimit);
077: }
078:
079: /**
080: * Checks the given assertion equals the expected parameter (with a retry strategy).
081: * The {@link Assertion#check()} method is called until the timeout
082: * specified by {@link UISpec4J#setAssertionTimeLimit(long)} is reached.
083: */
084: public static void assertEquals(boolean expected,
085: Assertion assertion) {
086: assertEquals(null, expected, assertion);
087: }
088:
089: /**
090: * Checks the given assertion equals the expected parameter (with a retry strategy).
091: * The {@link Assertion#check()} method is called until the timeout
092: * specified by {@link UISpec4J#setAssertionTimeLimit(long)} is reached.
093: * If it fails an AssertionFailedError is thrown with the given message.
094: */
095: public static void assertEquals(String message, boolean expected,
096: Assertion assertion) {
097: if (expected) {
098: assertTrue(message, assertion);
099: } else {
100: assertFalse(message, assertion);
101: }
102: }
103:
104: /**
105: * Returns a negation of the given assertion.
106: */
107: public static Assertion not(final Assertion assertion) {
108: return new Assertion() {
109: public void check() {
110: try {
111: assertion.check();
112: throw new FailureNotDetectedError();
113: } catch (FailureNotDetectedError e) {
114: throw new AssertionFailedError();
115: } catch (Throwable e) {
116: }
117: }
118: };
119: }
120:
121: /**
122: * Returns the intersection of the {@link Assertion} parameters.
123: */
124: public static Assertion and(final Assertion[] assertions) {
125: return new Assertion() {
126: public void check() throws Exception {
127: for (int i = 0; i < assertions.length; i++) {
128: assertions[i].check();
129: }
130: }
131: };
132: }
133:
134: /**
135: * Returns the union of the {@link Assertion} parameters.
136: */
137: public static Assertion or(final Assertion[] assertions) {
138: return new Assertion() {
139: public void check() {
140: for (int i = 0; i < assertions.length; i++) {
141: try {
142: assertions[i].check();
143: break;
144: } catch (Throwable e) {
145: if (i == assertions.length - 1) {
146: throw new AssertionFailedError();
147: }
148: }
149: }
150: }
151: };
152: }
153:
154: private static void checkAssertion(String message,
155: Assertion assertion, long waitTimeLimit) {
156: try {
157: Utils.waitForPendingAwtEventsToBeProcessed();
158: assertion.check();
159: } catch (Throwable e) {
160: retry(message, assertion, waitTimeLimit);
161: }
162: }
163:
164: /**
165: * @noinspection ForLoopThatDoesntUseLoopVariable
166: */
167: private static void retry(String message, Assertion assertion,
168: long waitTimeLimit) {
169: long elapsedTime = 0;
170: for (int i = 0; elapsedTime < waitTimeLimit; i++) {
171: int waitTime = i < 10 ? 20 : 200;
172: Utils.sleep(waitTime);
173: elapsedTime += waitTime;
174: try {
175: assertion.check();
176: return;
177: } catch (Throwable e) {
178: }
179: }
180: try {
181: assertion.check();
182: } catch (AssertionFailedError e) {
183: if (message == null) {
184: throw e;
185: }
186: throw new AssertionFailedError(message);
187: } catch (Exception e) {
188: if (message != null)
189: throw new RuntimeException(message, e);
190:
191: else {
192: throw new RuntimeException(e.getMessage(), e);
193: }
194: }
195: }
196:
197: private static class FailureNotDetectedError extends
198: AssertionFailedError {
199: }
200: }
|