001: package org.uispec4j;
002:
003: import junit.framework.TestCase;
004: import org.uispec4j.assertion.Assertion;
005: import org.uispec4j.assertion.UISpecAssert;
006: import org.uispec4j.interception.toolkit.UISpecDisplay;
007:
008: /**
009: * UISpec4J-enabled TestCase class.<p>
010: * Test cases derived from this class automatically set up the instanciation mechanism. They can
011: * also access the application main window by defining an adapter, i.e. a class implementing
012: * the {@link UISpecAdapter} class.<br>
013: * The adapter can be set from the test {@link #setUp()} or any test method using the
014: * {@link #setAdapter(UISpecAdapter)} method.
015: * The adapter can also be registered by setting the <code>uispec4j.adapter</code> property as follows:
016: * <pre><code>
017: * uispec4j.adapter=samples.addressbook.test.Adapter
018: * </code></pre>
019: */
020: public abstract class UISpecTestCase extends TestCase {
021:
022: static final String ADAPTER_CLASS_PROPERTY = "uispec4j.adapter";
023: static final String PROPERTY_NOT_DEFINED;
024:
025: private UISpecAdapter adapter;
026:
027: static {
028: PROPERTY_NOT_DEFINED = "Adapter class not defined - the '"
029: + ADAPTER_CLASS_PROPERTY
030: + "' property must refer to a class implementing the UISpecAdapter interface";
031: UISpec4J.init();
032: }
033:
034: protected UISpecTestCase() {
035: }
036:
037: protected UISpecTestCase(String testName) {
038: super (testName);
039: }
040:
041: public void setAdapter(UISpecAdapter adapter) {
042: this .adapter = adapter;
043: }
044:
045: /**
046: * Initializes the resources needed by the test case.<br>
047: * NB: If you provide your own implementation, do not forget to call this one first.
048: */
049: protected void setUp() throws Exception {
050: super .setUp();
051: UISpecDisplay.instance().reset();
052: }
053:
054: /**
055: * Checks whether an unexpected exception had occurred, and releases the test resources.
056: */
057: protected void tearDown() throws Exception {
058: UISpecDisplay.instance().rethrowIfNeeded();
059: UISpecDisplay.instance().reset();
060: super .tearDown();
061: }
062:
063: private void retrieveAdapter() throws AdapterNotFoundException {
064: String adapterClassName = System
065: .getProperty(ADAPTER_CLASS_PROPERTY);
066: if (adapterClassName == null) {
067: throw new AdapterNotFoundException();
068: }
069: try {
070: adapter = (UISpecAdapter) Class.forName(adapterClassName)
071: .newInstance();
072: } catch (Exception e) {
073: throw new AdapterNotFoundException(adapterClassName, e);
074: }
075: }
076:
077: /**
078: * Returns the Window created by the adapter.
079: *
080: * @throws AdapterNotFoundException if the <code>uispec4j.adapter</code> property does not refer
081: * to a valid adapter
082: */
083: public Window getMainWindow() throws AdapterNotFoundException {
084: return getAdapter().getMainWindow();
085: }
086:
087: /**
088: * Checks the given assertion.
089: *
090: * @see UISpecAssert#assertTrue(Assertion)
091: */
092: public void assertTrue(Assertion assertion) {
093: UISpecAssert.assertTrue(assertion);
094: }
095:
096: /**
097: * Waits for at most 'waitTimeLimit' ms until the assertion is true.
098: *
099: * @see UISpecAssert#waitUntil(Assertion, long)
100: */
101: public void waitUntil(Assertion assertion, long waitTimeLimit) {
102: UISpecAssert.waitUntil(assertion, waitTimeLimit);
103: }
104:
105: /**
106: * Checks that the given assertion fails.
107: *
108: * @see UISpecAssert#assertFalse(Assertion)
109: */
110: public void assertFalse(Assertion assertion) {
111: UISpecAssert.assertFalse(assertion);
112: }
113:
114: /**
115: * Checks the given assertion.
116: * If it fails an AssertionFailedError is thrown with the given message.
117: *
118: * @see UISpecAssert#assertTrue(String, Assertion)
119: */
120: public void assertTrue(String message, Assertion assertion) {
121: UISpecAssert.assertTrue(message, assertion);
122: }
123:
124: /**
125: * Waits for at most 'waitTimeLimit' ms until the assertion is true.
126: * If it fails an AssertionFailedError is thrown with the given message.
127: *
128: * @see UISpecAssert#waitUntil(String, Assertion, long)
129: */
130: public void waitUntil(String message, Assertion assertion,
131: long waitTimeLimit) {
132: UISpecAssert.waitUntil(message, assertion, waitTimeLimit);
133: }
134:
135: /**
136: * Checks that the given assertion fails.
137: * If it succeeds an AssertionFailedError is thrown with the given message.
138: *
139: * @see UISpecAssert#assertFalse(String, Assertion)
140: */
141: public void assertFalse(String message, Assertion assertion) {
142: UISpecAssert.assertFalse(message, assertion);
143: }
144:
145: /**
146: * Returns a negation of the given assertion.
147: *
148: * @see UISpecAssert#not(Assertion)
149: */
150: public Assertion not(Assertion assertion) {
151: return UISpecAssert.not(assertion);
152: }
153:
154: /**
155: * Returns the intersection of two assertions.
156: *
157: * @see UISpecAssert#and(Assertion[])
158: */
159: public Assertion and(Assertion assertion1, Assertion assertion2) {
160: return UISpecAssert.and(new Assertion[] { assertion1,
161: assertion2 });
162: }
163:
164: /**
165: * Returns the intersection of two assertions.
166: *
167: * @see UISpecAssert#and(Assertion[])
168: */
169: public Assertion and(Assertion[] assertions) {
170: return UISpecAssert.and(assertions);
171: }
172:
173: /**
174: * Returns the union of two assertions.
175: */
176: public Assertion or(Assertion assertion1, Assertion assertion2) {
177: return UISpecAssert
178: .or(new Assertion[] { assertion1, assertion2 });
179: }
180:
181: /**
182: * Returns the union of two assertions.
183: *
184: * @see UISpecAssert#or(Assertion[])
185: */
186: public Assertion or(Assertion[] assertions) {
187: return UISpecAssert.or(assertions);
188: }
189:
190: /**
191: * Checks that the given assertion equals the expected parameter.
192: *
193: * @see UISpecAssert#assertEquals(boolean, Assertion)
194: */
195: public void assertEquals(boolean expected, Assertion assertion) {
196: UISpecAssert.assertEquals(expected, assertion);
197: }
198:
199: /**
200: * Checks that the given assertion equals the expected parameter.
201: * If it fails an AssertionFailedError is thrown with the given message.
202: *
203: * @see UISpecAssert#assertEquals(String, boolean, Assertion)
204: */
205: public void assertEquals(String message, boolean expected,
206: Assertion assertion) {
207: UISpecAssert.assertEquals(message, expected, assertion);
208: }
209:
210: private UISpecAdapter getAdapter() throws AdapterNotFoundException {
211: if (adapter == null) {
212: retrieveAdapter();
213: }
214: return adapter;
215: }
216:
217: static class AdapterNotFoundException extends RuntimeException {
218: public AdapterNotFoundException() {
219: super (PROPERTY_NOT_DEFINED);
220: }
221:
222: public AdapterNotFoundException(String adapterClassName,
223: Exception e) {
224: super ("Adapter class '" + adapterClassName + "' not found",
225: e);
226: }
227: }
228: }
|