001: package org.uispec4j;
002:
003: import org.uispec4j.interception.WindowInterceptor;
004: import org.uispec4j.utils.UnitTestCase;
005: import org.uispec4j.utils.Utils;
006: import org.uispec4j.xml.EventLogger;
007:
008: import javax.swing.*;
009: import java.awt.event.ActionEvent;
010:
011: public class UISpecTestCaseTest extends UnitTestCase {
012:
013: public class MyTestCase extends UISpecTestCase {
014: public MyTestCase() {
015: }
016:
017: public void test() throws Exception {
018: }
019: }
020:
021: public static class MyAdapter extends EventLogger implements
022: UISpecAdapter {
023: private JFrame frame;
024:
025: public MyAdapter(JFrame frame) {
026: this .frame = frame;
027: }
028:
029: public MyAdapter() {
030: this (new JFrame("title"));
031: }
032:
033: public Window getMainWindow() {
034: log("getMainWindow");
035: return new Window(frame);
036: }
037: }
038:
039: public void testPropertyDefined() throws Exception {
040: System.setProperty(UISpecTestCase.ADAPTER_CLASS_PROPERTY,
041: MyAdapter.class.getName());
042: MyTestCase testCase = new MyTestCase();
043: testCase.setUp();
044: assertTrue(testCase.getMainWindow().titleEquals("title"));
045: }
046:
047: public void testGetMainWindowFailsIfThePropertyWasNotDefined()
048: throws Exception {
049: System.getProperties().remove(
050: UISpecTestCase.ADAPTER_CLASS_PROPERTY);
051: MyTestCase testCase = new MyTestCase();
052: testCase.setUp();
053: try {
054: testCase.getMainWindow();
055: fail();
056: } catch (UISpecTestCase.AdapterNotFoundException e) {
057: assertEquals(UISpecTestCase.PROPERTY_NOT_DEFINED, e
058: .getMessage());
059: }
060: }
061:
062: public void testGetMainWindowFailsIfThePropertyWasInitializedWithAWrongValue()
063: throws Exception {
064: System.setProperty(UISpecTestCase.ADAPTER_CLASS_PROPERTY,
065: "unknown");
066: MyTestCase testCase = new MyTestCase();
067: testCase.setUp();
068: try {
069: testCase.getMainWindow();
070: fail();
071: } catch (UISpecTestCase.AdapterNotFoundException e) {
072: assertEquals("Adapter class 'unknown' not found", e
073: .getMessage());
074: }
075: }
076:
077: public void testSettingTheAdapter() throws Exception {
078: MyAdapter adapter1 = new MyAdapter();
079: MyAdapter adapter2 = new MyAdapter();
080:
081: MyTestCase testCase = new MyTestCase();
082: testCase.setAdapter(adapter1);
083: testCase.setUp();
084: assertTrue(testCase.getMainWindow().titleEquals("title"));
085: adapter1
086: .assertEquals("<log>" + " <getMainWindow/>" + "</log>");
087: adapter2.assertEmpty();
088:
089: testCase.getMainWindow();
090: adapter1
091: .assertEquals("<log>" + " <getMainWindow/>" + "</log>");
092: adapter2.assertEmpty();
093:
094: testCase.setAdapter(adapter2);
095: testCase.getMainWindow();
096: adapter1.assertEmpty();
097: adapter2
098: .assertEquals("<log>" + " <getMainWindow/>" + "</log>");
099: }
100:
101: public void testTriggerExceptionsAreStoredAndRethrownInTearDownWhenNotCaughtImmediately()
102: throws Exception {
103: UISpec4J.setWindowInterceptionTimeLimit(100);
104: final JFrame frame = new JFrame("my frame");
105: final Exception exception = new RuntimeException(
106: "triggerException");
107:
108: MyExceptionTestCase exceptionTestCase = new MyExceptionTestCase(
109: frame, exception);
110: exceptionTestCase.setAdapter(new MyAdapter(frame));
111: exceptionTestCase.setUp();
112: exceptionTestCase.test();
113: try {
114: exceptionTestCase.tearDown();
115: fail();
116: } catch (Exception e) {
117: assertSame(exception, e);
118: }
119: }
120:
121: private JDialog createModalDialog(JFrame frame) {
122: final JDialog firstDialog = new JDialog(frame, "dlg", true);
123: firstDialog.getContentPane().add(
124: new JButton(new AbstractAction("Close") {
125: public void actionPerformed(ActionEvent e) {
126: firstDialog.dispose();
127: }
128: }));
129: return firstDialog;
130: }
131:
132: private class MyExceptionTestCase extends UISpecTestCase {
133: final JDialog firstDialog;
134: private final Exception exception;
135:
136: public MyExceptionTestCase(JFrame frame, Exception exception) {
137: this .exception = exception;
138: this .firstDialog = createModalDialog(frame);
139: }
140:
141: public void test() throws Exception {
142: Window window = WindowInterceptor
143: .getModalDialog(new Trigger() {
144: public void run() throws Exception {
145: firstDialog.setVisible(true);
146: throw exception;
147: }
148: });
149: window.getButton("Close").click();
150: Utils.sleep(1);
151: }
152: }
153: }
|