|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| EmployeeTestCase.java | 50% | 88% | 87.5% | 83.3% |
|
||||||||||||||
| 1 |
package modal;
|
|
| 2 |
|
|
| 3 |
import java.awt.Container;
|
|
| 4 |
import java.awt.event.KeyEvent;
|
|
| 5 |
import java.util.List;
|
|
| 6 |
|
|
| 7 |
import javax.swing.JButton;
|
|
| 8 |
import javax.swing.JTextField;
|
|
| 9 |
|
|
| 10 |
import junit.extensions.jfcunit.JFCTestCase;
|
|
| 11 |
import junit.extensions.jfcunit.JFCTestHelper;
|
|
| 12 |
import junit.extensions.jfcunit.eventdata.KeyEventData;
|
|
| 13 |
import junit.extensions.jfcunit.eventdata.MouseEventData;
|
|
| 14 |
import junit.extensions.jfcunit.eventdata.StringEventData;
|
|
| 15 |
import junit.extensions.jfcunit.TestHelper;
|
|
| 16 |
import junit.textui.TestRunner;
|
|
| 17 |
import junit.extensions.jfcunit.finder.ComponentFinder;
|
|
| 18 |
import junit.extensions.jfcunit.finder.Finder;
|
|
| 19 |
import junit.extensions.jfcunit.finder.DialogFinder;
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
/**
|
|
| 23 |
* class EmployeeTestCase.
|
|
| 24 |
*
|
|
| 25 |
* This class illustrates how to use JFCUnit with modal
|
|
| 26 |
* dialogs.
|
|
| 27 |
*
|
|
| 28 |
* @author Kevin Wilson
|
|
| 29 |
*/
|
|
| 30 |
public class EmployeeTestCase extends JFCTestCase { |
|
| 31 |
|
|
| 32 |
/** The thread the modal dialog will execute on. */
|
|
| 33 |
private Thread m_modalThread = null; |
|
| 34 |
/** Any exception thrown by the modal dialog. */
|
|
| 35 |
private Exception m_lastException = null; |
|
| 36 |
/** The employee object under test. */
|
|
| 37 |
private Employee m_employee = null; |
|
| 38 |
/** The Helper to be used. */
|
|
| 39 |
private TestHelper m_helper = null; |
|
| 40 |
/** True if the application exited normally. */
|
|
| 41 |
private boolean m_normalExit = false; |
|
| 42 |
/** Flag. True if the application has been started. */
|
|
| 43 |
private volatile boolean m_started = false; |
|
| 44 |
|
|
| 45 |
/**
|
|
| 46 |
* Main method to run this class from the command line.
|
|
| 47 |
*
|
|
| 48 |
* @param args Command line arguments.
|
|
| 49 |
*/
|
|
| 50 | 0 |
public static void main(final String[] args) { |
| 51 | 0 |
TestRunner.run(EmployeeTestCase.class);
|
| 52 |
} |
|
| 53 |
|
|
| 54 |
/**
|
|
| 55 |
* Constructor.
|
|
| 56 |
* @param name Name of the test case.
|
|
| 57 |
*/
|
|
| 58 | 2 |
public EmployeeTestCase(final String name) {
|
| 59 | 2 |
super(name);
|
| 60 |
} |
|
| 61 |
|
|
| 62 |
/**
|
|
| 63 |
* Much of the work to take care of the modal
|
|
| 64 |
* state is done in this setup. The execution
|
|
| 65 |
* thread which starts the modal dialog must NOT
|
|
| 66 |
* be the test thread. Therefor it is run on yet
|
|
| 67 |
* another thread. A simple synchronization of
|
|
| 68 |
* the threads is used here. A more complex method
|
|
| 69 |
* may be required in a real application.
|
|
| 70 |
*
|
|
| 71 |
* @throws Exception may be thrown.
|
|
| 72 |
*/
|
|
| 73 | 2 |
public void setUp() throws Exception { |
| 74 | 2 |
m_helper = new JFCTestHelper();
|
| 75 | 2 |
m_lastException = null;
|
| 76 | 2 |
m_modalThread = new Thread(new Runnable() { |
| 77 | 2 |
public void run() { |
| 78 | 2 |
try {
|
| 79 | 2 |
m_lastException = null;
|
| 80 | 2 |
m_started = true;
|
| 81 | 2 |
new Employee();
|
| 82 | 2 |
m_normalExit = true;
|
| 83 |
} catch (Exception e) {
|
|
| 84 | 0 |
m_lastException = e; |
| 85 |
} |
|
| 86 |
} |
|
| 87 |
}, "ModalThread");
|
|
| 88 | 2 |
m_modalThread.start(); |
| 89 | 2 |
Thread.currentThread().yield(); |
| 90 |
|
|
| 91 |
// Wait for the thread to start
|
|
| 92 | 2 |
while (!m_started) {
|
| 93 | 0 |
Thread.currentThread().sleep(50); |
| 94 |
} |
|
| 95 |
|
|
| 96 |
// Give a little extra time for the painting/construction
|
|
| 97 | 2 |
Thread.currentThread().sleep(500); |
| 98 | 2 |
flushAWT(); |
| 99 | 2 |
checkException(); |
| 100 |
} |
|
| 101 |
|
|
| 102 |
/**
|
|
| 103 |
* Since the modal was fired by another thread. The
|
|
| 104 |
* test thread is left free to drive the modal dialog.
|
|
| 105 |
*/
|
|
| 106 | 1 |
public void testEmployeeEnter() { |
| 107 |
// The setup should leave the modal dialog running on a
|
|
| 108 |
// different thread. Thus we can access the dialog
|
|
| 109 |
// throught the normal JFCUnit finders.
|
|
| 110 | 1 |
List dialogs = new DialogFinder("New Employee").findAll(); |
| 111 | 1 |
assertEquals("Dialog not found:", 1, dialogs.size());
|
| 112 |
|
|
| 113 | 1 |
Container namePane = (Container) dialogs.get(0); |
| 114 | 1 |
JTextField field = null;
|
| 115 | 1 |
Finder f = new ComponentFinder(JTextField.class); |
| 116 | 1 |
field = (JTextField) f.find(namePane, 0); |
| 117 | 1 |
assertNotNull("Could not find field", field);
|
| 118 | 1 |
m_helper.sendString(new StringEventData(this, field, "Harry Potter")); |
| 119 | 1 |
m_helper.sendKeyAction(new KeyEventData(this, field, KeyEvent.VK_ENTER)); |
| 120 | 1 |
assertTrue("Unsuccessful exit:", m_normalExit);
|
| 121 | 1 |
checkException(); |
| 122 |
} |
|
| 123 |
|
|
| 124 |
/**
|
|
| 125 |
* Another test just for the fun of it.
|
|
| 126 |
*/
|
|
| 127 | 1 |
public void testEmployeeOkClick() { |
| 128 |
// The setup should leave the modal dialog running on a
|
|
| 129 |
// different thread. Thus we can access the dialog
|
|
| 130 |
// throught the normal JFCUnit finders.
|
|
| 131 | 1 |
List dialogs = new DialogFinder("New Employee").findAll(); |
| 132 | 1 |
assertEquals("Dialog not found:", 1, dialogs.size());
|
| 133 |
|
|
| 134 | 1 |
Container namePane = (Container) dialogs.get(0); |
| 135 | 1 |
Finder f = new ComponentFinder(JTextField.class); |
| 136 | 1 |
JTextField field = (JTextField) f.find(namePane, 0); |
| 137 | 1 |
assertNotNull("Could not find field", field);
|
| 138 | 1 |
m_helper.sendString(new StringEventData(this, field, "Harry Potter")); |
| 139 |
|
|
| 140 | 1 |
f = new ComponentFinder(JButton.class); |
| 141 | 1 |
JButton button = (JButton) f.find(namePane, 0); |
| 142 | 1 |
m_helper.enterClickAndLeave(new MouseEventData(this, button)); |
| 143 | 1 |
assertTrue("Unsuccessful exit:", m_normalExit);
|
| 144 | 1 |
checkException(); |
| 145 |
} |
|
| 146 |
|
|
| 147 |
/**
|
|
| 148 |
* Interrupt the modalThread if it is still running.
|
|
| 149 |
* Then shutdown the fixtures used.
|
|
| 150 |
*
|
|
| 151 |
* @throws Exception may be thrown.
|
|
| 152 |
*/
|
|
| 153 | 2 |
public void tearDown() throws Exception { |
| 154 | 2 |
if (m_lastException != null) { |
| 155 | 0 |
throw m_lastException;
|
| 156 |
} |
|
| 157 | 2 |
if (m_modalThread.isAlive()) {
|
| 158 | 0 |
m_modalThread.interrupt(); |
| 159 |
} |
|
| 160 | 2 |
m_modalThread = null;
|
| 161 | 2 |
m_employee = null;
|
| 162 | 2 |
m_helper = null;
|
| 163 |
} |
|
| 164 |
|
|
| 165 |
/**
|
|
| 166 |
* Check for the occurance of a Exception on the
|
|
| 167 |
* modalThread. Rethrow the exception if one was generated.
|
|
| 168 |
*/
|
|
| 169 | 4 |
private void checkException() { |
| 170 | 4 |
if (m_lastException != null) { |
| 171 | 0 |
fail(m_lastException.toString()); |
| 172 |
} |
|
| 173 |
} |
|
| 174 |
} |
|
| 175 |
|
|
||||||||||