001: /*
002: * DataPumperTest.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.tools;
013:
014: import java.sql.ResultSet;
015: import java.sql.Statement;
016: import javax.swing.tree.TreeModel;
017: import junit.framework.TestCase;
018: import org.netbeans.jemmy.ClassReference;
019: import org.netbeans.jemmy.JemmyProperties;
020: import org.netbeans.jemmy.QueueTool;
021: import org.netbeans.jemmy.TestOut;
022: import org.netbeans.jemmy.operators.JButtonOperator;
023: import org.netbeans.jemmy.operators.JCheckBoxOperator;
024: import org.netbeans.jemmy.operators.JComboBoxOperator;
025: import org.netbeans.jemmy.operators.JDialogOperator;
026: import org.netbeans.jemmy.operators.JFrameOperator;
027: import org.netbeans.jemmy.operators.JTreeOperator;
028: import workbench.db.ConnectionMgr;
029: import workbench.db.WbConnection;
030: import workbench.gui.GuiTestUtil;
031: import workbench.gui.NamedComponentChooser;
032: import workbench.util.SqlUtil;
033: import workbench.util.WbFile;
034:
035: /**
036: *
037: * @author support@sql-workbench.net
038: */
039: public class DataPumperTest extends TestCase {
040: private GuiTestUtil util;
041: private WbFile targetDb;
042: private WbFile sourceDb;
043:
044: public DataPumperTest(String testName) {
045: super (testName);
046: util = new GuiTestUtil(testName);
047: targetDb = new WbFile(util.getBaseDir(), "targetdb");
048: sourceDb = new WbFile(util.getBaseDir(), "sourcedb");
049: try {
050: util.createProfiles(sourceDb, targetDb);
051: } catch (Exception e) {
052: e.printStackTrace();
053: fail(e.getMessage());
054: }
055: }
056:
057: public void startDataPumper() throws Exception {
058: String[] args = { "-datapumper -nosettings -configdir="
059: + util.getBaseDir() };
060: new ClassReference("workbench.WbManager")
061: .startApplication(args);
062: System.setProperty("workbench.system.doexit", "false");
063:
064: // JemmyProperties.getCurrentTimeouts().loadDebugTimeouts();
065: TestOut out = JemmyProperties.getProperties().getOutput()
066: .createErrorOutput();
067: JemmyProperties.getProperties().setOutput(out);
068: }
069:
070: public void connect(String buttonName, int profileIndex) {
071: JFrameOperator mainWindow = new JFrameOperator("Data Pumper");
072:
073: NamedComponentChooser chooser = new NamedComponentChooser();
074: chooser.setName(buttonName);
075: JButtonOperator select = new JButtonOperator(mainWindow,
076: chooser);
077: select.push();
078:
079: JDialogOperator dialog = new JDialogOperator(mainWindow,
080: "Select Connection Profile");
081:
082: chooser.setName("profileTree");
083:
084: JTreeOperator tree = new JTreeOperator(dialog, chooser);
085: TreeModel model = tree.getModel();
086: tree.selectRow(profileIndex);
087: new JButtonOperator(dialog, "OK").push();
088:
089: DataPumper pumper = (DataPumper) mainWindow.getContentPane()
090: .getComponent(0);
091: waitUntilConnected(pumper);
092: QueueTool tool = new QueueTool();
093: tool.waitEmpty();
094: }
095:
096: public void copyData() {
097: QueueTool tool = new QueueTool();
098: JFrameOperator mainWindow = new JFrameOperator("Data Pumper");
099: NamedComponentChooser chooser = new NamedComponentChooser();
100: chooser.setName("sourceTable");
101:
102: final JComboBoxOperator srctbl = new JComboBoxOperator(
103: mainWindow, chooser);
104:
105: tool.invokeAndWait(new Runnable() {
106: public void run() {
107: srctbl.setSelectedIndex(0);
108: }
109: });
110:
111: tool.waitEmpty();
112: try {
113: // wait a bit to make sure the target dropdown is populated
114: Thread.sleep(250);
115: } catch (Exception e) {
116: // ignore
117: }
118:
119: chooser.setName("targetTable");
120: final JComboBoxOperator targettbl = new JComboBoxOperator(
121: mainWindow, chooser);
122: assertEquals(2, targettbl.getItemCount());
123:
124: tool.invokeAndWait(new Runnable() {
125: public void run() {
126: targettbl.setSelectedIndex(1);
127: }
128: });
129:
130: tool.waitEmpty();
131:
132: chooser.setName("modeSelector");
133: final JComboBoxOperator mode = new JComboBoxOperator(
134: mainWindow, chooser);
135:
136: chooser.setName("deleteTargetCbx");
137: final JCheckBoxOperator deleteTarget = new JCheckBoxOperator(
138: mainWindow, chooser);
139: assertTrue(deleteTarget.isEnabled());
140:
141: tool.invokeAndWait(new Runnable() {
142: public void run() {
143: mode.setSelectedIndex(1); // Update mode
144: assertFalse(deleteTarget.isEnabled());
145:
146: mode.setSelectedItem("update,insert");
147: assertFalse(deleteTarget.isEnabled());
148:
149: mode.setSelectedItem("insert,update");
150: assertFalse(deleteTarget.isEnabled());
151:
152: mode.setSelectedItem("insert");
153: assertTrue(deleteTarget.isEnabled());
154: }
155: });
156:
157: DataPumper pumper = (DataPumper) mainWindow.getContentPane()
158: .getComponent(0);
159:
160: chooser.setName("startButton");
161: JButtonOperator start = new JButtonOperator(mainWindow, chooser);
162: start.push();
163: tool.waitEmpty();
164:
165: // Wait until copy has finished
166: int count = 0;
167: int sleepTime = 50;
168: while (pumper.isRunning()) {
169: Thread.yield();
170: try {
171: Thread.sleep(sleepTime);
172: } catch (Throwable th) {
173: }
174: count++;
175: if (count * sleepTime > 5000) {
176: System.out.println("*** Cancelling wait !!!!!");
177: break;
178: }
179: }
180: tool.waitEmpty();
181: }
182:
183: public void checkData() {
184: WbConnection target = ConnectionMgr.getInstance()
185: .findConnection("Dp-Target");
186: assertNotNull(target);
187:
188: Statement stmt = null;
189: ResultSet rs = null;
190: try {
191: stmt = target.createStatement();
192: rs = stmt.executeQuery("select count(*) from person");
193: if (rs.next()) {
194: int numrows = rs.getInt(1);
195: assertEquals(4, numrows);
196: } else {
197: fail("No data copied");
198: }
199: } catch (Exception e) {
200: e.printStackTrace();
201: fail(e.getMessage());
202: } finally {
203: SqlUtil.closeAll(rs, stmt);
204: }
205: }
206:
207: public void waitUntilConnected(DataPumper pumper) {
208: int count = 0;
209: int sleepTime = 50;
210: while (pumper.isConnecting) {
211: Thread.yield();
212: try {
213: Thread.sleep(sleepTime);
214: } catch (Throwable th) {
215: }
216: count++;
217: if (count * sleepTime > 5000)
218: break;
219: }
220: }
221:
222: public void closeWindow() {
223: JFrameOperator mainWindow = new JFrameOperator("Data Pumper");
224: NamedComponentChooser chooser = new NamedComponentChooser();
225: chooser.setName("closeButton");
226: JButtonOperator close = new JButtonOperator(mainWindow, chooser);
227: close.push();
228: }
229:
230: public void testDataPumper() {
231: try {
232: util.prepareSource(sourceDb);
233: util.prepareTarget(targetDb);
234: startDataPumper();
235: connect("selectSource", 1);
236: connect("selectTarget", 2);
237: copyData();
238: checkData();
239: closeWindow();
240: Thread.sleep(500);
241: util.stopApplication();
242: util.emptyBaseDirectory();
243: } catch (Exception e) {
244: e.printStackTrace();
245: fail(e.getMessage());
246: }
247: }
248: }
|