001: /*
002: DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003:
004: Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005:
006:
007: The contents of this file are subject to the terms of either the GNU
008: General Public License Version 2 only ("GPL") or the Common
009: Development and Distribution License("CDDL") (collectively, the
010: "License"). You may not use this file except in compliance with the
011: License. You can obtain a copy of the License at
012: http://www.netbeans.org/cddl-gplv2.html
013: or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
014: specific language governing permissions and limitations under the
015: License. When distributing the software, include this License Header
016: Notice in each file and include the License file at
017: nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
018: particular file as subject to the "Classpath" exception as provided
019: by Sun in the GPL Version 2 section of the License file that
020: accompanied this code. If applicable, add the following below the
021: License Header, with the fields enclosed by brackets [] replaced by
022: your own identifying information:
023: "Portions Copyrighted [year] [name of copyright owner]"
024:
025: Contributor(s):
026:
027: The Original Software is NetBeans. The Initial Developer of the Original
028: Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
029: Microsystems, Inc. All Rights Reserved.
030:
031: If you wish your version of this file to be governed by only the CDDL
032: or only the GPL Version 2, indicate your decision by adding
033: "[Contributor] elects to include this software in this distribution
034: under the [CDDL or GPL Version 2] license." If you do not indicate a
035: single choice of license, a recipient has the option to distribute
036: your version of this file under either the CDDL, the GPL Version 2 or
037: to extend the choice of license to its licensees as provided above.
038: However, if you add GPL Version 2 code and therefore, elected the GPL
039: Version 2 license, then the option applies only if the new code is
040: made subject to such option by the copyright holder.
041: */
042: package org.netbeans.test.dataprovider.common;
043:
044: import java.awt.event.*;
045: import java.util.*;
046: import java.awt.event.*;
047: import java.awt.*;
048: import javax.swing.*;
049: import javax.swing.tree.*;
050: import org.netbeans.jemmy.*;
051: import org.netbeans.jemmy.operators.*;
052: import org.netbeans.jellytools.actions.*;
053: import org.netbeans.modules.visualweb.gravy.*;
054: import org.netbeans.modules.visualweb.gravy.plugins.*;
055: import org.netbeans.modules.visualweb.gravy.designer.*;
056: import org.netbeans.modules.visualweb.gravy.dataconnectivity.*;
057: import org.netbeans.modules.visualweb.gravy.model.deployment.*;
058:
059: public class DatabaseTests implements Constants {
060: public String checkDBConnection() {
061: String errMsg = null, dbURL = TestPropertiesHandler
062: .getDatabaseProperty("DB_URL");
063:
064: Utils.putFocusOnWindowServices();
065: Util.wait(1000);
066: new QueueTool().waitEmpty();
067:
068: TreePath dbTreePath = Utils.findServicesTreeNode(
069: SERVICES_TREE_NODE_DATABASES, true);
070: Util.wait(1000);
071: new QueueTool().waitEmpty();
072:
073: JTreeOperator treeOp = new ServerNavigatorOperator().getTree();
074: treeOp.expandPath(dbTreePath);
075: Util.wait(1000);
076: new QueueTool().waitEmpty();
077:
078: dbTreePath = Utils.findDBConnectionTreeNode();
079: if (dbTreePath == null) {
080: addNewDBDriver();
081: addNewDBConnection();
082: dbTreePath = Utils.findDBConnectionTreeNode();
083: if (dbTreePath == null)
084: return "The tree subnode [" + dbURL
085: + "] isn't found under the tree node ["
086: + SERVICES_TREE_NODE_DATABASES + "]";
087: }
088: treeOp = new ServerNavigatorOperator().getTree();
089: //JPopupMenuOperator popupMenuOp = new JPopupMenuOperator(treeOp.callPopupOnPath(dbTreePath));
090: JPopupMenuOperator popupMenuOp = Utils.callPopupOnPath(treeOp,
091: dbTreePath);
092: JMenuItem menuItem = TestUtils.findPopupMenuItemByLabel(
093: popupMenuOp, DB_POPUP_MENU_ITEM_LABEL_CONNECT, false,
094: false);
095: if (menuItem.isEnabled()) { // no DB connection
096: new JMenuItemOperator(menuItem).pushNoBlock();
097: Util.wait(500);
098: new QueueTool().waitEmpty();
099: connectToDB();
100: } else { // required DB is already connected
101: popupMenuOp.pressKey(KeyEvent.VK_ESCAPE);
102: }
103: Util.wait(500);
104: new QueueTool().waitEmpty();
105: if (errMsg == null) {
106: Utils
107: .logMsg("+++ DB connection to [" + dbURL
108: + "] is done");
109: }
110: return errMsg;
111: }
112:
113: private void connectToDB() {
114: JDialogOperator dialogOp = new JDialogOperator(
115: DB_DIALOG_CONNECT_TITLE);
116:
117: // for DB Derby: now the dialog "Connect" doesn't contain any
118: // text fields and user name and password are not required
119: if (Utils.isUsedDBDerby()) {
120: dialogOp.waitClosed();
121: Util.wait(500);
122: new QueueTool().waitEmpty();
123: return;
124: }
125:
126: new JTextFieldOperator(dialogOp, 0)
127: .setText(TestPropertiesHandler
128: .getDatabaseProperty("DB_Password"));
129: new JTextFieldOperator(dialogOp, 1)
130: .setText(TestPropertiesHandler
131: .getDatabaseProperty("DB_User"));
132: Util.wait(500);
133: new JButtonOperator(dialogOp, BUTTON_LABEL_OK).pushNoBlock();
134: Util.wait(500);
135: new QueueTool().waitEmpty();
136: }
137:
138: private void addNewDBDriver() {
139: String jdbcDriverName = getJDBCDriverName();
140: TreePath driverPath = Utils.findServicesTreeNode(
141: SERVICES_TREE_NODE_DATABASES + "|"
142: + DB_TREE_NODE_DRIVERS + "|" + jdbcDriverName,
143: false);
144: if (driverPath == null) {
145: if (Utils.isUsedDBDerby()) {
146: defineDBDerbySettings();
147: return;
148: }
149:
150: Utils.callPopupMenuOnServicesTreeNode(
151: SERVICES_TREE_NODE_DATABASES + "|"
152: + DB_TREE_NODE_DRIVERS,
153: DB_POPUP_MENU_ITEM_LABEL_NEW_DRIVER);
154:
155: JDialogOperator dialogOp = new JDialogOperator(
156: DB_DIALOG_NEW_DRIVER_TITLE);
157:
158: new JTextFieldOperator(dialogOp, 1).setText(jdbcDriverName);
159: Util.wait(500);
160:
161: new JButtonOperator(dialogOp, BUTTON_LABEL_ADD)
162: .pushNoBlock();
163: Util.wait(500);
164:
165: JFileChooserOperator fileChooserOp = new JFileChooserOperator();
166: String jdbcDriverPath = TestPropertiesHandler
167: .getDatabaseProperty("JDBC_Driver_AbsPath")
168: + "/"
169: + TestPropertiesHandler
170: .getDatabaseProperty("JDBC_Driver_File");
171: new JTextFieldOperator(fileChooserOp, 0)
172: .setText(jdbcDriverPath);
173: Util.wait(500);
174: new JButtonOperator(fileChooserOp, BUTTON_LABEL_OPEN)
175: .pushNoBlock();
176: Util.wait(1000);
177:
178: new JButtonOperator(dialogOp, BUTTON_LABEL_OK)
179: .pushNoBlock();
180: Util.wait(500);
181: new QueueTool().waitEmpty();
182:
183: Utils.logMsg("+++ New JDBC driver [" + jdbcDriverName
184: + "] has been added");
185: } else {
186: Utils.logMsg("+++ JDBC driver [" + jdbcDriverName
187: + "] exists alredy");
188: }
189: }
190:
191: private void defineDBDerbySettings() {
192: Util.getMainMenu().pushMenuNoBlock(
193: MAIN_MENU_ITEM_TOOLS_JAVA_DB_DATABASE_SETTINGS);
194: Util.wait(1000);
195: new QueueTool().waitEmpty();
196:
197: JDialogOperator dialogOp = new JDialogOperator(
198: DB_DIALOG_JAV_DB_SETTINGS_TITLE);
199:
200: String dbInstallation = TestPropertiesHandler
201: .getDatabaseProperty("DB_Installation");
202: new JTextFieldOperator(dialogOp, 1).setText(dbInstallation);
203: Util.wait(500);
204:
205: String dbLocation = TestPropertiesHandler
206: .getDatabaseProperty("DB_Location");
207: new JTextFieldOperator(dialogOp, 0).setText(dbLocation);
208: Util.wait(500);
209:
210: new JButtonOperator(dialogOp, BUTTON_LABEL_OK).pushNoBlock();
211: Util.wait(500);
212:
213: Utils.logMsg("+++ Java DB Settings are defined properly.");
214: }
215:
216: private String getJDBCDriverName() {
217: String dbType = TestPropertiesHandler
218: .getDatabaseProperty("Database"), jdbcDriverName = null;
219: if (dbType.toLowerCase().contains("oracle")) {
220: jdbcDriverName = DB_TREE_NODE_ORACLE_JDBC_DRIVER_NAME;
221: } else if (dbType.toLowerCase().contains("derby")) {
222: jdbcDriverName = DB_TREE_NODE_DERBY_JDBC_DRIVER_NAME;
223: } else {
224: jdbcDriverName = "Unknown JDBC Driver";
225: }
226: return jdbcDriverName;
227: }
228:
229: private void addNewDBConnection() {
230: TreePath dbTreePath = Utils.findDBConnectionTreeNode();
231: if (dbTreePath != null)
232: return;
233:
234: String jdbcDriverName = getJDBCDriverName();
235: Utils.callPopupMenuOnServicesTreeNode(
236: SERVICES_TREE_NODE_DATABASES,
237: DB_POPUP_MENU_ITEM_LABEL_NEW_CONNECTION);
238: JDialogOperator dialogOp = new JDialogOperator(
239: DB_DIALOG_NEW_CONNECTION_TITLE);
240:
241: JComboBoxOperator driverComboBoxOp = new JComboBoxOperator(
242: dialogOp);
243: driverComboBoxOp.selectItem(jdbcDriverName);
244: /* - workaround if the previous line has no effect
245: int itemIndex = -1, itemCount = driverComboBoxOp.getItemCount();
246: for (int i = 0; i < itemCount; ++i) {
247: String itemText = driverComboBoxOp.getItemAt(i).toString();
248: if (itemText.contains(jdbcDriverName)) {
249: itemIndex = i;
250: //Utils.logMsg("itemIndex = " + itemIndex);
251: break;
252: }
253: }
254: driverComboBoxOp.setSelectedIndex(itemIndex);
255: */
256:
257: Util.wait(500);
258:
259: String dbURL = TestPropertiesHandler
260: .getDatabaseProperty("DB_URL"), dbUser = TestPropertiesHandler
261: .getDatabaseProperty("DB_User"), dbPassword = TestPropertiesHandler
262: .getDatabaseProperty("DB_Password");
263:
264: new JTextFieldOperator(dialogOp, 1).setText(dbURL);
265: Util.wait(500);
266:
267: new JTextFieldOperator(dialogOp, 2).setText(dbUser);
268: Util.wait(500);
269:
270: new JTextFieldOperator(dialogOp, 3).setText(dbPassword);
271: Util.wait(500);
272:
273: new JButtonOperator(dialogOp, BUTTON_LABEL_OK).pushNoBlock();
274: Util.wait(500);
275: new QueueTool().waitEmpty();
276:
277: // wait until DB is connected and dialog is closed
278: waitDialogClosed(dialogOp);
279: }
280:
281: private void waitDialogClosed(JDialogOperator dialogOp) {
282: ((JDialog) dialogOp.getSource())
283: .addWindowListener(new WindowAdapter() {
284: @Override
285: public void windowClosed(WindowEvent e) {
286: synchronized (DatabaseTests.this ) {
287: DatabaseTests.this .notifyAll();
288: }
289: }
290: });
291: synchronized (this ) {
292: try {
293: wait(60000);
294: } catch (InterruptedException e) {
295: e.printStackTrace(Utils.logStream);
296: }
297: }
298: }
299: }
|