001: package org.uispec4j.assertion;
002:
003: import junit.framework.AssertionFailedError;
004: import org.uispec4j.UISpec4J;
005: import org.uispec4j.utils.Chrono;
006: import org.uispec4j.utils.Functor;
007: import org.uispec4j.utils.UnitTestCase;
008: import org.uispec4j.utils.Utils;
009:
010: public class UISpecAssertTest extends UnitTestCase {
011:
012: protected void setUp() throws Exception {
013: UISpec4J
014: .setAssertionTimeLimit(UISpec4J.DEFAULT_ASSERTION_TIME_LIMIT);
015: }
016:
017: public void testAssertTrue() throws Exception {
018: UISpecAssert.assertTrue(DummyAssertion.TRUE);
019: checkAssertionFailedError(new Functor() {
020: public void run() throws Exception {
021: UISpecAssert.assertTrue(DummyAssertion.FALSE);
022: }
023: });
024: }
025:
026: public void testAssertTrueRetriesUntilTheAssertionSucceeds()
027: throws Exception {
028: Chrono chrono = Chrono.start();
029: runThreadAndCheckAssertion(40, true);
030: chrono.assertElapsedTimeLessThan(150);
031: }
032:
033: public void testWaitForAssertionDoesNotTakeIntoAccountGlobalWaitTimeLimit()
034: throws Exception {
035: UISpec4J.setAssertionTimeLimit(0);
036: Chrono chrono = Chrono.start();
037: runThreadAndWaitForAssertion(50, 100);
038: chrono.assertElapsedTimeLessThan(200);
039:
040: UISpec4J.setAssertionTimeLimit(500);
041: checkAssertionFailedError(new Functor() {
042: public void run() throws Exception {
043: runThreadAndWaitForAssertion(300, 100);
044: }
045: }, "error!");
046:
047: final DummyAssertion assertion = new DummyAssertion(
048: "custom message");
049:
050: checkAssertionFailedError(new Functor() {
051: public void run() throws Exception {
052: UISpecAssert.waitUntil(assertion, 0);
053: }
054: }, "custom message");
055:
056: checkAssertionFailedError(new Functor() {
057: public void run() throws Exception {
058: UISpecAssert.waitUntil("other message", assertion, 0);
059: }
060: }, "other message");
061: }
062:
063: public void testAssertTrueRetriesUpToATimeLimit() throws Exception {
064: checkAssertionFailedError(new Functor() {
065: public void run() throws Exception {
066: runThreadAndCheckAssertion(900, true);
067: }
068: }, "error!");
069: }
070:
071: public void testAssertTrueAssertionFailedErrorMessage()
072: throws Exception {
073: UISpec4J.setAssertionTimeLimit(0);
074: final DummyAssertion assertion = new DummyAssertion(
075: "custom message");
076:
077: checkAssertionFails(assertion, "custom message");
078:
079: checkAssertionFailedError(new Functor() {
080: public void run() throws Exception {
081: UISpecAssert.assertTrue("other message", assertion);
082: }
083: }, "other message");
084:
085: checkAssertionFailedError(new Functor() {
086: public void run() throws Exception {
087: assertion.setError(null);
088: assertion.setException(new Exception(
089: "exception message"));
090: UISpecAssert
091: .assertTrue("assertTrue message", assertion);
092: }
093: }, "assertTrue message");
094: }
095:
096: public void testAssertFalseAssertionFailedErrorMessage()
097: throws Exception {
098: UISpec4J.setAssertionTimeLimit(0);
099: final DummyAssertion assertion = new DummyAssertion(
100: "custom message");
101:
102: checkAssertionFailedError(new Functor() {
103: public void run() throws Exception {
104: UISpecAssert.assertFalse(not(assertion));
105: }
106: }, null);
107:
108: checkAssertionFailedError(new Functor() {
109: public void run() throws Exception {
110: UISpecAssert.assertFalse("other message",
111: not(assertion));
112: }
113: }, "other message");
114: }
115:
116: public void testAssertFalse() throws Exception {
117: UISpecAssert.assertFalse(DummyAssertion.FALSE);
118: checkAssertionFailedError(new Functor() {
119: public void run() throws Exception {
120: UISpecAssert.assertFalse(DummyAssertion.TRUE);
121: }
122: });
123: }
124:
125: public void testAssertFalseRetriesUntilTheAssertionFails()
126: throws Exception {
127: Chrono chrono = Chrono.start();
128: runThreadAndCheckAssertion(80, false);
129: chrono.assertElapsedTimeLessThan(200);
130: }
131:
132: public void testAssertFalseRetriesUpToATimeLimit() throws Exception {
133: checkAssertionFailedError(new Functor() {
134: public void run() throws Exception {
135: runThreadAndCheckAssertion(900, false);
136: }
137: });
138: }
139:
140: public void testAssertEquals() throws Exception {
141: UISpecAssert.assertEquals(false, DummyAssertion.FALSE);
142: UISpecAssert.assertEquals(true, DummyAssertion.TRUE);
143: checkAssertionFailedError(new Functor() {
144: public void run() throws Exception {
145: UISpecAssert.assertEquals(true, DummyAssertion.FALSE);
146: }
147: }, DummyAssertion.DEFAULT_ERROR_MSG);
148: checkAssertionFailedError(new Functor() {
149: public void run() throws Exception {
150: UISpecAssert.assertEquals(false, DummyAssertion.TRUE);
151: }
152: });
153: }
154:
155: public void testAssertEqualsWithMessage() throws Exception {
156: final String message = "my custom message";
157: UISpecAssert.assertEquals(message, false, DummyAssertion.FALSE);
158: UISpecAssert.assertEquals(message, true, DummyAssertion.TRUE);
159: checkAssertionFailedError(new Functor() {
160: public void run() throws Exception {
161: UISpecAssert.assertEquals(message, true,
162: DummyAssertion.FALSE);
163: }
164: }, message);
165: checkAssertionFailedError(new Functor() {
166: public void run() throws Exception {
167: UISpecAssert.assertEquals(message, false,
168: DummyAssertion.TRUE);
169: }
170: }, message);
171: }
172:
173: public void testAssertionNegationOperator() throws Exception {
174: UISpecAssert.assertTrue(DummyAssertion.TRUE);
175: UISpecAssert.assertFalse(UISpecAssert.not(DummyAssertion.TRUE));
176: UISpecAssert.assertTrue(UISpecAssert.not(UISpecAssert
177: .not(DummyAssertion.TRUE)));
178: }
179:
180: public void testAssertionIntersectionOperator() throws Exception {
181: DummyAssertion assertion = new DummyAssertion(true);
182: UISpecAssert.assertTrue(UISpecAssert.and(new Assertion[] {
183: assertion, DummyAssertion.TRUE }));
184: UISpecAssert.assertFalse(UISpecAssert.and(new Assertion[] {
185: assertion, DummyAssertion.FALSE }));
186:
187: assertion.setError("");
188: UISpecAssert.assertFalse(UISpecAssert.and(new Assertion[] {
189: assertion, DummyAssertion.TRUE }));
190: UISpecAssert.assertFalse(UISpecAssert.and(new Assertion[] {
191: assertion, DummyAssertion.FALSE }));
192: }
193:
194: public void testAssertionUnionOperator() throws Exception {
195: DummyAssertion assertion = new DummyAssertion(true);
196: UISpecAssert.assertTrue(UISpecAssert.or(new Assertion[] {
197: assertion, DummyAssertion.TRUE }));
198: UISpecAssert.assertTrue(UISpecAssert.or(new Assertion[] {
199: assertion, DummyAssertion.FALSE }));
200:
201: assertion.setError("");
202: UISpecAssert.assertTrue(UISpecAssert.or(new Assertion[] {
203: assertion, DummyAssertion.TRUE }));
204: UISpecAssert
205: .assertTrue(UISpecAssert.or(new Assertion[] {
206: assertion, DummyAssertion.TRUE,
207: DummyAssertion.FALSE }));
208: UISpecAssert
209: .assertTrue(UISpecAssert.or(new Assertion[] {
210: assertion, DummyAssertion.FALSE,
211: DummyAssertion.TRUE }));
212: UISpecAssert.assertFalse(UISpecAssert.or(new Assertion[] {
213: assertion, DummyAssertion.FALSE }));
214: }
215:
216: private void runThreadAndCheckAssertion(int threadSleepTime,
217: final boolean useAssertTrue) throws Exception {
218: final DummyThread thread = new DummyThread(threadSleepTime);
219: thread.start();
220: Assertion assertion = new Assertion() {
221: public void check() {
222: if ((useAssertTrue) ? !thread.timeoutExpired
223: : thread.timeoutExpired) {
224: throw new AssertionFailedError("error!");
225: }
226: }
227: };
228: if (useAssertTrue) {
229: UISpecAssert.assertTrue(assertion);
230: } else {
231: UISpecAssert.assertFalse(assertion);
232: }
233: thread.join();
234: }
235:
236: private void runThreadAndWaitForAssertion(int threadSleepTime,
237: long waitTimeLimit) throws Exception {
238: final DummyThread thread = new DummyThread(threadSleepTime);
239: thread.start();
240: Assertion assertion = new Assertion() {
241: public void check() {
242: if (!thread.timeoutExpired) {
243: throw new AssertionFailedError("error!");
244: }
245: }
246: };
247: UISpecAssert.waitUntil(assertion, waitTimeLimit);
248: thread.join();
249: }
250:
251: private static class DummyThread extends Thread {
252: boolean timeoutExpired;
253: private int sleepTime;
254:
255: public DummyThread(int sleepTime) {
256: this .sleepTime = sleepTime;
257: }
258:
259: public void run() {
260: Utils.sleep(sleepTime);
261: timeoutExpired = true;
262: }
263: }
264: }
|