001: /*
002: * MainWindowTest.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui;
013:
014: import javax.swing.JMenuBar;
015: import javax.swing.JMenuItem;
016: import javax.swing.JTabbedPane;
017: import junit.framework.TestCase;
018: import org.netbeans.jemmy.QueueTool;
019: import org.netbeans.jemmy.operators.JButtonOperator;
020: import org.netbeans.jemmy.operators.JComboBoxOperator;
021: import org.netbeans.jemmy.operators.JComponentOperator;
022: import org.netbeans.jemmy.operators.JDialogOperator;
023: import org.netbeans.jemmy.operators.JFrameOperator;
024: import org.netbeans.jemmy.operators.JLabelOperator;
025: import org.netbeans.jemmy.operators.JListOperator;
026: import org.netbeans.jemmy.operators.JMenuBarOperator;
027: import org.netbeans.jemmy.operators.JMenuItemOperator;
028: import org.netbeans.jemmy.operators.JMenuOperator;
029: import org.netbeans.jemmy.operators.JTableOperator;
030: import org.netbeans.jemmy.operators.JTextFieldOperator;
031: import org.netbeans.jemmy.operators.Operator.DefaultStringComparator;
032: import org.netbeans.jemmy.operators.Operator.StringComparator;
033: import workbench.db.ConnectionMgr;
034: import workbench.gui.actions.AppendResultsAction;
035: import workbench.gui.sql.SqlPanel;
036: import workbench.util.StringUtil;
037: import workbench.util.WbFile;
038:
039: /**
040: * @author support@sql-workbench.net
041: */
042: public class MainWindowTest extends TestCase {
043: private GuiTestUtil testUtil;
044:
045: public MainWindowTest(String testName) {
046: super (testName);
047: this .testUtil = new GuiTestUtil("MainWindowTest");
048: }
049:
050: private void startApplication() {
051: try {
052: testUtil.startApplication();
053: } catch (Exception e) {
054: e.printStackTrace();
055: fail(e.getMessage());
056: }
057: }
058:
059: private void aboutTest() {
060: JFrameOperator mainWindow = new JFrameOperator("SQL Workbench");
061: new JMenuBarOperator(mainWindow).pushMenuNoBlock("Help|About",
062: "|");
063:
064: JDialogOperator dialog = new JDialogOperator(mainWindow,
065: "About SQL Workbench/J");
066: dialog.setVisible(false);
067:
068: QueueTool tool = new QueueTool();
069: tool.waitEmpty();
070: dialog.dispose();
071:
072: }
073:
074: private void whatsNewTest() {
075: JFrameOperator mainWindow = new JFrameOperator("SQL Workbench");
076: new JMenuBarOperator(mainWindow).pushMenuNoBlock(
077: "Help|What's new", "|");
078: QueueTool tool = new QueueTool();
079: tool.waitEmpty();
080:
081: JDialogOperator dialog = new JDialogOperator(mainWindow,
082: "What's new");
083: dialog.setVisible(false);
084: dialog.dispose();
085: }
086:
087: private void settingsTest() {
088: JFrameOperator mainWindow = new JFrameOperator("SQL Workbench");
089: new JMenuBarOperator(mainWindow).pushMenuNoBlock(
090: "Tools|Options", "|");
091: JDialogOperator dialog = new JDialogOperator(mainWindow,
092: "Settings");
093: final JListOperator pages = new JListOperator(dialog);
094:
095: int count = pages.getModel().getSize();
096: assertEquals(10, count);
097:
098: NamedComponentChooser chooser = new NamedComponentChooser();
099: chooser.setName("pagetitle");
100:
101: QueueTool tool = new QueueTool();
102: for (int i = 1; i < count; i++) {
103: final int index = i;
104: pages.selectItem(index);
105: tool.waitEmpty();
106:
107: String pg = pages.getSelectedValue().toString();
108: JLabelOperator title = new JLabelOperator(dialog, chooser);
109: assertEquals(pg, title.getText());
110: }
111:
112: new JButtonOperator(dialog, "Cancel").push();
113: }
114:
115: // private void createDriver()
116: // {
117: // JFrameOperator mainWindow = new JFrameOperator("SQL Workbench");
118: // new JMenuBarOperator(mainWindow).pushMenuNoBlock("File|Manage Drivers", "|");
119: // JDialogOperator dialog = new JDialogOperator(mainWindow, "Manage drivers");
120: // JListOperator list = new JListOperator(dialog);
121: // list.selectItem("H2 Database Engine");
122: // new JButtonOperator(dialog, "Cancel").push();
123: // }
124:
125: private void definePKTest() {
126: NamedComponentChooser chooser = new NamedComponentChooser();
127: chooser.setName("sqleditor1");
128: JFrameOperator mainWindow = new JFrameOperator("SQL Workbench");
129: JMenuBarOperator mainMenu = new JMenuBarOperator(mainWindow);
130:
131: QueueTool tool = new QueueTool();
132:
133: chooser.setName("sqlpanel1");
134: JComponentOperator panel = new JComponentOperator(mainWindow,
135: chooser);
136: final SqlPanel sqlPanel = (SqlPanel) panel.getSource();
137:
138: JMenuOperator dataMenu = new JMenuOperator(mainMenu.getMenu(3));
139: JMenuItem saveItem = (JMenuItem) dataMenu.getMenuComponent(1);
140: final JMenuItemOperator save = new JMenuItemOperator(saveItem);
141: // assertFalse(save.isEnabled());
142:
143: runSql(
144: sqlPanel,
145: "create table nopk (id1 integer, data varchar(20));\n"
146: + "commit;\n"
147: + "insert into nopk (id1, data) values (1,'Ford');\n"
148: + "insert into nopk (id1, data) values (2,'Zaphod');\n"
149: + "commit;\n");
150: runSql(sqlPanel, "select id1, data from nopk;");
151:
152: JTableOperator result = new JTableOperator(mainWindow);
153: int rows = result.getRowCount();
154: assertEquals(2, rows);
155: assertEquals(2, result.getColumnCount());
156: result.setValueAt("Arthur", 0, 1);
157:
158: tool.waitEmpty();
159: assertEquals(3, result.getColumnCount());
160: assertTrue(save.isEnabled());
161:
162: new JMenuBarOperator(mainWindow).pushMenuNoBlock(
163: "Data|Define key columns", "|");
164: JDialogOperator definePK = new JDialogOperator(
165: "Select Key Columns");
166:
167: JTableOperator cols = new JTableOperator(definePK);
168: cols.setValueAt(Boolean.TRUE, 0, 1);
169: JButtonOperator ok = new JButtonOperator(definePK, "OK");
170: ok.push();
171: tool.waitEmpty();
172:
173: saveChanges(sqlPanel);
174: new JMenuBarOperator(mainWindow).pushMenuNoBlock(
175: "Data|Save changes to database", "|");
176: testUtil.waitWhileBusy(sqlPanel);
177: tool.waitEmpty();
178:
179: assertEquals(2, result.getColumnCount());
180: runSql(sqlPanel,
181: "select id1, data from nopk where data = 'Arthur';");
182:
183: result = new JTableOperator(mainWindow);
184: rows = result.getRowCount();
185: assertEquals(1, rows);
186: }
187:
188: private void pkWarningsTest() {
189: NamedComponentChooser chooser = new NamedComponentChooser();
190: chooser.setName("sqleditor1");
191: JFrameOperator mainWindow = new JFrameOperator("SQL Workbench");
192: JMenuBarOperator mainMenu = new JMenuBarOperator(mainWindow);
193:
194: QueueTool tool = new QueueTool();
195:
196: chooser.setName("sqlpanel1");
197: JComponentOperator panel = new JComponentOperator(mainWindow,
198: chooser);
199: final SqlPanel sqlPanel = (SqlPanel) panel.getSource();
200:
201: JMenuOperator dataMenu = new JMenuOperator(mainMenu.getMenu(3));
202: JMenuItem saveItem = (JMenuItem) dataMenu.getMenuComponent(1);
203: final JMenuItemOperator save = new JMenuItemOperator(saveItem);
204: assertFalse(save.isEnabled());
205:
206: runSql(
207: sqlPanel,
208: "create table jtest (id1 integer, id2 integer, data varchar(20), primary key (id1, id2));\n"
209: + "commit;\n"
210: + "insert into jtest (id1, id2, data) values (1,1,'Ford');\n"
211: + "commit;\n");
212: runSql(sqlPanel, "select id1, data from jtest;");
213:
214: JTableOperator result = new JTableOperator(mainWindow);
215: int rows = result.getRowCount();
216: assertEquals(1, rows);
217: assertEquals(2, result.getColumnCount());
218: result.setValueAt("Arthur", 0, 1);
219:
220: tool.waitEmpty();
221: assertEquals(3, result.getColumnCount());
222: assertTrue(save.isEnabled());
223:
224: new JMenuBarOperator(mainWindow).pushMenuNoBlock(
225: "Data|Save changes to database", "|");
226: JDialogOperator warning = new JDialogOperator(
227: "Missing key columns");
228: JButtonOperator cancel = new JButtonOperator(warning, "Cancel");
229: cancel.push();
230: tool.waitEmpty();
231: assertEquals(3, result.getColumnCount());
232: }
233:
234: private void connect() {
235: // Make sure not profiles exist
236: ConnectionMgr.getInstance().clearProfiles();
237:
238: JFrameOperator mainWindow = new JFrameOperator("SQL Workbench");
239: JMenuBar bar = mainWindow.getJMenuBar();
240: // JMenuBarOperator mainMenu = new JMenuBarOperator(mainWindow);
241: JMenuBarOperator menu = new JMenuBarOperator(bar);
242: menu.pushMenuNoBlock("File|Connect", "|");
243:
244: JDialogOperator dialog = new JDialogOperator(mainWindow,
245: "Select Connection Profile");
246: JTextFieldOperator profileName = new JTextFieldOperator(dialog,
247: "New Profile");
248: profileName.setText("Test Connection");
249:
250: NamedComponentChooser chooser = new NamedComponentChooser();
251: chooser.setName("driverclass");
252:
253: JComboBoxOperator driver = new JComboBoxOperator(dialog,
254: chooser);
255: driver.setToolTipText("test");
256: StringComparator comp = new DefaultStringComparator(false,
257: false);
258:
259: int index = driver.findItemIndex("H2 Database Engine", comp);
260: if (index <= 0)
261: fail("H2 Driver not found");
262: driver.selectItem(index);
263:
264: chooser.setName("url");
265: JTextFieldOperator url = new JTextFieldOperator(dialog, chooser);
266: WbFile db = new WbFile(testUtil.getBaseDir(), "testdb");
267: url.setText("jdbc:h2:" + db.getFullPath());
268:
269: chooser.setName("username");
270: JTextFieldOperator username = new JTextFieldOperator(dialog,
271: chooser);
272: username.setText("sa");
273: new JButtonOperator(dialog, "OK").push();
274:
275: // Connecting can take some time...
276: QueueTool tool = new QueueTool();
277: tool.waitEmpty();
278: chooser.setName("sqlpanel1");
279: JComponentOperator panel = new JComponentOperator(mainWindow,
280: chooser);
281: SqlPanel sqlPanel = (SqlPanel) panel.getSource();
282: testUtil.waitUntilConnected(sqlPanel);
283: }
284:
285: private void runSql() {
286: NamedComponentChooser chooser = new NamedComponentChooser();
287: chooser.setName("sqleditor1");
288: JFrameOperator mainWindow = new JFrameOperator("SQL Workbench");
289: JMenuBarOperator mainMenu = new JMenuBarOperator(mainWindow);
290:
291: chooser.setName("sqlpanel1");
292: JComponentOperator panel = new JComponentOperator(mainWindow,
293: chooser);
294: final SqlPanel sqlPanel = (SqlPanel) panel.getSource();
295:
296: String msg = runSql(
297: sqlPanel,
298: "create table person (nr integer primary key, firstname varchar(20), lastname varchar(20));");
299: System.out.println("Create message: " + msg);
300: assertTrue(msg.indexOf("Object created") > -1);
301:
302: msg = runSql(
303: sqlPanel,
304: "insert into person (nr, firstname, lastname) values (42, 'Ford', 'Prefect');\ncommit;");
305: assertNotNull(msg);
306: assertTrue(msg.indexOf("1 row(s) affected") > -1);
307:
308: runSql(sqlPanel, "select nr, firstname, lastname from person");
309:
310: JTableOperator result = new JTableOperator(mainWindow);
311: int rows = result.getRowCount();
312: assertEquals(1, rows);
313: assertEquals(3, result.getColumnCount());
314:
315: Object nr = result.getValueAt(0, 0);
316: assertEquals(nr, new Integer(42));
317:
318: JMenuOperator dataMenu = new JMenuOperator(mainMenu.getMenu(3));
319: JMenuItem saveItem = (JMenuItem) dataMenu.getMenuComponent(1);
320: JMenuItemOperator save = new JMenuItemOperator(saveItem);
321: assertFalse(save.isEnabled());
322:
323: result.setValueAt("Arthur", 0, 1);
324:
325: // The first call to setValueAt() will make the result table display
326: // the status column
327: assertEquals(4, result.getColumnCount());
328:
329: // because of the status column the lastname column
330: // is the column with index 3 (not 2)
331: result.setValueAt("Dent", 0, 3);
332:
333: assertTrue(save.isEnabled());
334:
335: saveChanges(sqlPanel);
336:
337: // Make sure the status column is turned off after saving
338: assertEquals(3, result.getColumnCount());
339:
340: runSql(sqlPanel,
341: "select nr, firstname, lastname from person where lastname = 'Dent';");
342:
343: // Obtain a new referenct to the result table as the
344: // SQLPanel has created a new instance when running the select
345: result = new JTableOperator(mainWindow);
346: assertEquals(1, result.getRowCount());
347: assertEquals(3, result.getColumnCount());
348:
349: String firstname = (String) result.getValueAt(0, 1);
350: assertEquals("Arthur", firstname);
351:
352: msg = runSql(sqlPanel,
353: "update person set firstname = null where nr = 42;\ncommit;");
354: System.out.println("update message: " + msg);
355: assertTrue(msg.indexOf("1 row(s) affected") > -1);
356:
357: msg = runSql(sqlPanel,
358: "select nr, firstname, lastname from person where lastname = 'Dent';");
359: System.out.println("Message: " + msg);
360:
361: result = new JTableOperator(mainWindow);
362: firstname = (String) result.getValueAt(0, 1);
363: assertTrue(StringUtil.isWhitespaceOrEmpty(firstname));
364:
365: result.setValueAt("Arthur", 0, 1);
366: QueueTool tool = new QueueTool();
367: tool.waitEmpty();
368: msg = saveChanges(sqlPanel);
369: tool.waitEmpty();
370: }
371:
372: private void appendTest() {
373: JFrameOperator mainWindow = new JFrameOperator("SQL Workbench");
374: NamedComponentChooser chooser = new NamedComponentChooser();
375: chooser.setName("sqlpanel1");
376: JComponentOperator panel = new JComponentOperator(mainWindow,
377: chooser);
378: final SqlPanel sqlPanel = (SqlPanel) panel.getSource();
379:
380: JMenuBarOperator mainMenu = new JMenuBarOperator(mainWindow);
381: JMenuOperator sqlMenu = new JMenuOperator(mainMenu.getMenu(4));
382:
383: JMenuItem appendItem = sqlMenu.getItem(18);
384: JMenuItemOperator append = new JMenuItemOperator(appendItem);
385: AppendResultsAction action = (AppendResultsAction) appendItem
386: .getAction();
387: assertFalse(appendItem.isSelected());
388:
389: runSql(sqlPanel, "select * from person");
390:
391: chooser.setName("resultspane");
392: JComponentOperator comp = new JComponentOperator(mainWindow,
393: chooser);
394: JTabbedPane resultTab = (JTabbedPane) comp.getSource();
395:
396: assertEquals(2, resultTab.getTabCount());
397:
398: QueueTool tool = new QueueTool();
399: mainMenu.pushMenu("SQL|Append new results", "|");
400: tool.waitEmpty();
401: assertTrue(sqlPanel.getAppendResults());
402: assertTrue(appendItem.isSelected());
403: assertTrue(action.getButton().isSelected());
404:
405: runSql(sqlPanel, "select * from person");
406: assertEquals(3, resultTab.getTabCount());
407:
408: mainMenu.pushMenu("SQL|Append new results", "|");
409: tool.waitEmpty();
410: assertFalse(sqlPanel.getAppendResults());
411: assertFalse(appendItem.isSelected());
412: assertFalse(action.getButton().isSelected());
413:
414: runSql(sqlPanel, "select * from person");
415: tool.waitEmpty();
416: try {
417: Thread.sleep(500);
418: } catch (Throwable th) {
419: }
420: assertEquals(2, resultTab.getTabCount());
421: }
422:
423: private String saveChanges(final SqlPanel panel) {
424: Runnable r = new Runnable() {
425: public void run() {
426: panel.updateDb();
427: }
428: };
429: testUtil.execute(r);
430: testUtil.waitWhileBusy(panel);
431: return panel.getLogMessage();
432: }
433:
434: private String runSql(final SqlPanel panel, final String sql) {
435: Runnable r = new Runnable() {
436: public void run() {
437: panel.getEditor().setText(sql);
438: panel.runAll();
439: }
440: };
441: testUtil.execute(r);
442: testUtil.waitWhileBusy(panel);
443: return panel.getLogMessage();
444: }
445:
446: public void testWindow() {
447: try {
448: startApplication();
449: connect();
450: whatsNewTest();
451: aboutTest();
452: settingsTest();
453: runSql();
454: appendTest();
455: pkWarningsTest();
456: definePKTest();
457: testUtil.stopApplication();
458: } catch (Exception e) {
459: e.printStackTrace();
460: fail(e.getMessage());
461: }
462: }
463: }
|