alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.test.db;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import junit.framework.Assert;
import junit.textui.TestRunner;
import org.netbeans.jellytools.modules.db.*;
import org.netbeans.jellytools.modules.db.nodes.ConnectionNode;
import org.netbeans.jellytools.modules.db.nodes.DatabasesNode;
import org.netbeans.jellytools.modules.db.nodes.DriverNode;
import org.netbeans.jellytools.modules.db.nodes.DriversNode;
import org.netbeans.jellytools.modules.db.util.BuildConnectionNodeName;
import org.netbeans.jemmy.JemmyProperties;
import org.netbeans.jemmy.TimeoutExpiredException;
import org.netbeans.jemmy.operators.DialogOperator;
import org.netbeans.junit.NbTestSuite;

/**
 * This test tests
 *
 * @author Patrik Knakal
 * @version 1.0
 */
public class DBExplorerConnectionTest extends DBJellyTestCase {
    
    /**
     * Necessary constructor.
     */
    public DBExplorerConnectionTest(String name) {
        super(name);
        //DEBUG = true;
    }
    
    /**
     *
     */
    public static NbTestSuite suite() {
        //NbTestSuite suite = new NbTestSuite(class);
        NbTestSuite suite = new NbTestSuite();
        suite.addTest(new DBExplorerConnectionTest("testCheckSelectedDriver")); //NOI18N
        suite.addTest(new DBExplorerConnectionTest("testConnectUsing")); //NOI18N
        suite.addTest(new DBExplorerConnectionTest("testDisconnect")); //NOI18N
        suite.addTest(new DBExplorerConnectionTest("testConnect_OwerDisconnectedConnection")); //NOI18N
        suite.addTest(new DBExplorerConnectionTest("testDeleteConnection")); //NOI18N
        suite.addTest(new DBExplorerConnectionTest("testConnectUsing_RememberPassword")); //NOI18N
        return suite;
    }
    
    /**
     * This method checks if the correct DB driver is selected in
     * New Database Connection dialog.
     */
    public void testCheckSelectedDriver() {
        try {
            DriversNode driversNode = DriversNode.getInstance();
            debug("DriversNode created"); //NOI18N
            if (!driversNode.containsChild(DB_DRIVER_NAME) ) {
                log("WARNING: Driver with specified name DOESN'T exist !!!"); //NOI18N
                log("\tcreating one"); //NOI18N
                driversNode.addDriver();
                debug("Add Driver action called"); //NOI18N
                
                AddJDBCDriverOperator addDriverOperator = new AddJDBCDriverOperator();
                debug("AddJDBCDriverOperator created"); //NOI18N
                addDriverOperator.typeName(DB_DRIVER_NAME);
                debug("Driver name typed"); //NOI18N
                addDriverOperator.typeDriver(DB_DRIVER_CLASS);
                debug("Driver class typed"); //NOI18N
                addDriverOperator.typeDatabaseURLPrefix(DB_URL_PREFIX);
                debug("Driver URL prefix typed"); //NOI18N
                addDriverOperator.oK();
                debug("OK button pressed"); //NOI18N
            } else {
                debug("Specified driver exists"); //NOI18N
            }
            
            DriverNode driverNode = driversNode.getDriver(DB_DRIVER_NAME);
            debug("DriverNode created"); //NOI18N
            driverNode.connectUsing();
            debug("'connectUsing' action called"); //NOI18N
            
            NewDatabaseConnection connectionOperator = new NewDatabaseConnection();
            debug("NewDatabaseConnection created"); //NOI18N
            //check corectness of the selected database driver name
            if (!connectionOperator.getSelectedName().equals(DB_DRIVER_NAME) ) {
                throw new Exception("Selected driver is not correct;\n\tselected: "
                + connectionOperator.getSelectedName() + "\n\tshould be selected: " + DB_DRIVER_NAME); //NOI18N
            } else {
                debug("Selected driver is correct"); //NOI18N
            }
            connectionOperator.cancel();
        } catch (Exception exc) {
            exc.printStackTrace();
            Assert.fail(exc.toString() );
        }
    }
    
    /**
     * This method creates a new database connection with parametters defined in
     * the database.properties
     */
    public void testConnectUsing() {
        try {
            DriverNode driverNode = DriversNode.getInstance().getDriver(DB_DRIVER_NAME);
            debug("DriverNode created"); //NOI18N
            driverNode.connectUsing();
            debug("'connectUsing' action called"); //NOI18N
            
            NewDatabaseConnection connectionOperator = new NewDatabaseConnection();
            debug("NewDatabaseConnection created"); //NOI18N
            // check corectness of the selected database driver name
            if (!connectionOperator.getSelectedName().equals(DB_DRIVER_NAME) ) {
                log("WARNING: Selected driver is not correct !!!"); //NOI18N
                log("\tselecting correct one"); //NOI18N
                connectionOperator.selectName(DB_DRIVER_NAME);
                log("\t... done"); //NOI18N
            }
            connectionOperator.setDatabaseURL(DB_URL);
            debug("DB URL set"); //NOI18N
            connectionOperator.typeUserName(DB_USER);
            debug("User name typed"); //NOI18N
            connectionOperator.typePassword(DB_PASSWD);
            debug("Password typed"); //NOI18N
            connectionOperator.selectPageAdvanced();
            debug("Advanced tab 'selected'"); //NOI18N
            // if the actual tab is not Advanced then there is something wrong
            if (connectionOperator.selectPageAdvanced() == null) { //NOI18N
                throw new Exception("The 'Advanced' tab is not selected correctly."); //NOI18N
            }
            // check for schema value
            if ((DB_SCHEMA != null) && !DB_SCHEMA.equals("") ) { //NOI18N
                connectionOperator.getSchemas();
                debug("Schema list obtained. Selecting: " + DB_SCHEMA); //NOI18N
                // try to create an Error dialog ... if exist, then there is an
                // error during connecting to database
                detectErrorDialog("There is not possible to connect to the database.");
                connectionOperator.selectSelectSchema(DB_SCHEMA);
                debug("Schema selected correctly."); //NOI18N
            } else {
                // this is necessary, because the SCHEMA in config file can be
                // empty, but DB can support schemas, so there is necessary to
                // take one because of creation of ConnectionNode name
                DB_SCHEMA = connectionOperator.getSelectedSelectSchema();
                debug("DB schema is updated to: + " + DB_SCHEMA); //NOI18N
            }
            connectionOperator.ok();
            debug("OK button pushed"); //NOI18N
            // try to create an Error dialog ... if exist, then there is an
            // error during connecting to database
            detectErrorDialog("There is not possible to connect to the database."); //NOI18N
            
            DatabasesNode databasesNode = DatabasesNode.getInstance();
            debug("DatabasesNode created."); //NOI18N
            String connectionNodeName = BuildConnectionNodeName.buildConnectionName(DB_URL, DB_USER, DB_SCHEMA);
            debug("ConnectionNode name == " + connectionNodeName);
            //test if the node with defined connection exists
            if (!databasesNode.containsChild(connectionNodeName) ) {
                throw new Exception("The connection node with name: '" + connectionNodeName + "' doesn't exist."); //NOI18N
            }
            
            ConnectionNode connectionNode = databasesNode.getConnection(connectionNodeName);
            debug("ConnectionNode creted.");
            // test if the node is in 'connected' mode by checking if 'Disconnect' is enabled
            if (!connectionNode.callPopup().showMenuItem("Disconnect", "|").isEnabled() ) {
                throw new Exception("The connection is not active.");
            }
            // close the popup
            connectionNode.select();
        } catch (Exception exc) {
            exc.printStackTrace();
            Assert.fail(exc.toString() );
        }
    }
    
    /**
     * This method test if there is possible to disconnect connection. This test
     * is usable only after testConnectUsing method, because it create
     * active connection.
     */
    public void testDisconnect() {
        try {
            DatabasesNode databasesNode = DatabasesNode.getInstance();
            debug("DatabasesNode created.");
            String connectionNodeName = BuildConnectionNodeName.buildConnectionName(DB_URL, DB_USER, DB_SCHEMA);
            debug("ConnectionNode name == " + connectionNodeName);
            // test if the node with defined connection exists
            if (!databasesNode.containsChild(connectionNodeName) ) {
                throw new Exception("The connection node with name: '" + connectionNodeName + "' doesn't exist."); //NOI18N
            }
            
            ConnectionNode connectionNode = databasesNode.getConnection(connectionNodeName);
            debug("ConnectionNode creted.");
            // test if the node is in 'connected' mode by checking if
            // 'Disconnect' is enabled, if there have to be called 'Disconnect'
            // then it have to be enabled == connection is active
            if (!connectionNode.callPopup().showMenuItem("Disconnect", "|").isEnabled()) {
                throw new Exception("The connection is not active.");
            }
            connectionNode.disconnect();
            debug("'Disconnect' method called");
            // try to create an Error dialog ... if exist, then there is an
            // error during disconnecting
            detectErrorDialog("There is an error during disconnectiong from the database."); //NOI18N
            // test if the node is in 'disconnected' mode by checking if
            // 'Connect' is enabled
            if (!connectionNode.callPopup().showMenuItem("Connect", "|").isEnabled() ) {
                throw new Exception("The connection was not disconnected.");
            }
            // close the popup
            connectionNode.select();
        } catch (Exception exc) {
            exc.printStackTrace();
            Assert.fail(exc.toString());
        }
    }
    
    /**
     * This method test Connect method over existing connection node
     */
    public void testConnect_OwerDisconnectedConnection() {
        try {
            DatabasesNode databasesNode = DatabasesNode.getInstance();
            debug("DatabasesNode created."); //NOI18N
            String connectionNodeName = BuildConnectionNodeName.buildConnectionName(DB_URL, DB_USER, DB_SCHEMA);
            debug("ConnectionNode name == " + connectionNodeName);
            // test if the node with defined connection exists
            if (!databasesNode.containsChild(connectionNodeName) ) {
                throw new Exception("The connection node with name: '" + connectionNodeName + "' doesn't exist."); //NOI18N
            }
            
            ConnectionNode connectionNode = databasesNode.getConnection(connectionNodeName);
            debug("ConnectionNode creted");
            // test if the node is in 'disconnected' mode by checking if 'Connect' is enabled
            if (!connectionNode.callPopup().showMenuItem("Connect", "|").isEnabled() ) {
                throw new Exception("The connection is still active (expected state is inactive).");
            }
            connectionNode.connect();
            debug("'connect' called.");
            
            ConnectDialogOperator connectOperator = new ConnectDialogOperator();
            debug("ConnectDialogOperator created.");
            connectOperator.typePassword(DB_PASSWD);
            debug("Password typed");
            if ((DB_SCHEMA != null) && !DB_SCHEMA.equals("") ) { //NOI18N
                connectOperator.selectPageAdvanced(); //NOI18N
                debug("Advanced tab 'selected'"); //NOI18N
                // try to create an Error dialog ... if exist, then there is an
                // error during connecting to database
                connectOperator.getSchemas();
                debug("Schema list obtained. Selecting: " + DB_SCHEMA); //NOI18N
                detectErrorDialog("There is not possible to connect to the database."); //NOI18N
                // if the actual tab is not Advanced then there is something wrong
                if (!connectOperator.getSelectedTab().equals("Advanced") ) { //NOI18N
                    throw new Exception("The 'Advanced' tab is not selected correctly."); //NOI18N
                }
                connectOperator.selectSelectSchema(DB_SCHEMA);
                debug("Schema selected correctly."); //NOI18N
            } else {
                debug("DB schema is null or empty string"); //NOI18N
            }
            connectOperator.ok();
            debug("OK button pushed"); //NOI18N
            // try to create an Error dialog ... if exist, then there is an error
            // during connecting to database
            detectErrorDialog("There is not possible to connect to the database."); //NOI18N
            //test if the node is in 'connected' mode by checking if 'Disconnect' is enabled
            if (!connectionNode.callPopup().showMenuItem("Disconnect", "|").isEnabled() ) {
                throw new Exception("The connection is not active.");
            }
            connectionNode.disconnect();
            debug("Disconnected");
        } catch (Exception exc) {
            exc.printStackTrace();
            Assert.fail(exc.toString() );
        }
    }
    
    /**
     * Delete connection node.
     */
    public void testDeleteConnection() {
        try {
            String connectionNodeName = BuildConnectionNodeName.buildConnectionName(DB_URL, DB_USER, DB_SCHEMA);
            debug("ConnectionNode name == " + connectionNodeName);
            DatabasesNode databasesNode = DatabasesNode.getInstance();
            debug("DatabasesNode created."); //NOI18N
            //test if the node with defined connection exists
            if (!databasesNode.containsChild(connectionNodeName) ) {
                throw new Exception("The connection node with name: '" + connectionNodeName + "' doesn't exist."); //NOI18N
            }
            
            ConnectionNode connectionNode = databasesNode.getConnection(connectionNodeName);
            debug("ConnectionNode creted.");
            //test if the node is in 'disconnected' mode by checking if 'Delete' is enabled
            if (!connectionNode.callPopup().showMenuItem("Delete", "|").isEnabled() ) {
                throw new Exception("The connection is still active (expected state is inactive).");
            }
            connectionNode.delete();
            debug("'Delete' action called");
            ConfirmObjectDeletionOperator deleteOperator = new ConfirmObjectDeletionOperator();
            debug("ConfirmObjectDeletionOperator created"); //NOI18N
            deleteOperator.yes();
            debug("Yes button pressed"); //NOI18N
            //if the connection node exists, it wasn't deleted correctly
            if (databasesNode.containsChild(connectionNodeName) ) {
                throw new Exception("Driver wasn't deleted."); //NOI18N
            }
        } catch (Exception exc) {
            System.out.println("Exception in 'testDeleteConnection' method:\n\t" + exc); //NOI18N
            Assert.fail(exc.toString() );
        }
    }
    
    /**
     * This method creates a new database connection with parametters defined in
     * the database.properties. This method check Remember pawsord.
     */
    public void testConnectUsing_RememberPassword() {
        try {
            // for test if the connection with remembering passwd is correct ...
            // ha to be null after test
            //??? ConnectDialogOperator connectOperator = null;
            
            DriverNode driverNode = DriversNode.getInstance().getDriver(DB_DRIVER_NAME);
            debug("DriverNode created"); //NOI18N
            driverNode.connectUsing();
            debug("'connectUsing' action called"); //NOI18N
            
            NewDatabaseConnection connectionOperator = new NewDatabaseConnection();
            debug("NewDatabaseConnection created"); //NOI18N
            //check corectness of the selected database driver name
            if (!connectionOperator.getSelectedName().equals(DB_DRIVER_NAME) ) {
                log("WARNING: Selected driver is not correct !!!"); //NOI18N
                log("\tselecting correct one"); //NOI18N
                connectionOperator.selectName(DB_DRIVER_NAME);
                log("\t... done"); //NOI18N
            }
            connectionOperator.setDatabaseURL(DB_URL);
            debug("DB URL set"); //NOI18N
            connectionOperator.typeUserName(DB_USER);
            debug("User name typed"); //NOI18N
            connectionOperator.typePassword(DB_PASSWD);
            debug("Password typed"); //NOI18N
            connectionOperator.checkRememberPasswordDuringThisSession(true);
            debug("'Remember password' check box checked");
            
            connectionOperator.selectPageAdvanced(); //NOI18N
            debug("Advanced tab 'selected'"); //NOI18N
            // try to create an Error dialog ... if exist, then there is an error
            // during connecting to database
            detectErrorDialog("There is not possible to connect to the database."); //NOI18N
            //if the actual tab is not Advanced then there is something wrong
            if (connectionOperator.selectPageAdvanced() == null) { //NOI18N
                throw new Exception("The 'Advanced' tab is not selected correctly."); //NOI18N
            }
            //check for schema value
            if ((DB_SCHEMA != null) && !DB_SCHEMA.equals("") ) { //NOI18N
                connectionOperator.getSchemas();
                debug("Schema list obtained. Selecting: " + DB_SCHEMA); //NOI18N
                connectionOperator.selectSelectSchema(DB_SCHEMA);
                debug("Schema selected correctly."); //NOI18N
            } else {
                // this is necessary, because the SCHEMA in config file can be
                // empty, but DB can support schemas, so there is necessary to
                // take one because of creation of ConnectionNode name
                DB_SCHEMA = connectionOperator.getSelectedSelectSchema();
                debug("DB schema was updated to: " + DB_SCHEMA); //NOI18N
            }
            connectionOperator.ok();
            debug("OK button pushed"); //NOI18N
            // try to create an Error dialog ... if exist, then there is an error
            // during connecting to database
            detectErrorDialog("There is not possible to connect to the database."); //NOI18N
            
            DatabasesNode databasesNode = DatabasesNode.getInstance();
            debug("DatabasesNode created."); //NOI18N
            String connectionNodeName = BuildConnectionNodeName.buildConnectionName(DB_URL, DB_USER, DB_SCHEMA);
            debug("ConnectionNode name == " + connectionNodeName);
            //test if the node with defined connection exists
            if (!databasesNode.containsChild(connectionNodeName) ) {
                throw new Exception("The connection node with name: '" + connectionNodeName + "' doesn't exist."); //NOI18N
            }
            ConnectionNode connectionNode = databasesNode.getConnection(connectionNodeName);
            debug("ConnectionNode creted.");
            // test if the node is in 'connected' mode by checking if
            // 'Disconnect' is enabled
            if (!connectionNode.callPopup().showMenuItem("Disconnect", "|").isEnabled() ) {
                throw new Exception("The connection is not active.");
            }
            connectionNode.disconnect();
            debug("'disconnect' called");
            // test if the node is in 'disconnected' mode by checking if
            // 'Connect' is enabled
            if (!connectionNode.callPopup().showMenuItem("Connect", "|").isEnabled() ) {
                throw new Exception("The connection was not disconnected.");
            }
            connectionNode.connect();
            debug("'connect' called");
            // try to create an Connect dialog ... if exist, then the
            // functionality is not correct
            //            try {
            //                connectOperator = new ConnectDialogOperator();
            //            } catch (TimeoutExpiredException exc) {
            //                connectOperator = null;
            //            }
            //            // check if the error dialog exists
            //            if (connectOperator != null) {
            //                throw new Exception("The password wasn't remembered."); //NOI18N
            //            }
            detectErrorDialog("The password wasn't remembered.", ConnectDialogOperator.class);
            // test if the node is in 'connected' mode by checking if
            // 'Disconnect' is enabled
            if (!connectionNode.callPopup().showMenuItem("Disconnect", "|").isEnabled() ) {
                throw new Exception("The connection is not active.");
            }
        } catch (Exception exc) {
            fail(exc);
        }
    }
    
    // LIB ////////////////////////////////////////////////////////////////////
    
    private void detectErrorDialog(String message) {
        detectErrorDialog(message, ErrorOperator.class);
    }
    
    private void detectErrorDialog(String message, Class clazz) {
        DialogOperator dialogOperator = null;
        
        if (!DialogOperator.class.isAssignableFrom(clazz)) {
            throw new IllegalStateException(clazz.getName() + "is not DialogOperator");
        }
        JemmyProperties.push();
        JemmyProperties.getCurrentTimeouts().setTimeout("DialogWaiter.WaitDialogTimeout", WAIT_ERROR_TIMEOUT);
        Constructor constructor = null;
        try {
            constructor = clazz.getConstructor(null);
        } catch (Exception ex) {
            throw new IllegalStateException(ex.getMessage());
        }
        try {
            dialogOperator = (DialogOperator) constructor.newInstance(null);
        } catch (InvocationTargetException ex) {
            if (ex.getCause() instanceof TimeoutExpiredException) {
                dialogOperator = null;
            } else {
                ex.printStackTrace();
                throw new IllegalStateException(ex.getMessage());
            }
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
            throw new IllegalStateException(ex.getMessage());
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
            throw new IllegalStateException(ex.getMessage());
        } catch (InstantiationException ex) {
            ex.printStackTrace();
            throw new IllegalStateException(ex.getMessage());
        }
        
        // check if the error dialog exists
        if (dialogOperator != null) {
            throw new IllegalStateException(message); //NOI18N
        }
        JemmyProperties.pop();
    }
    /**
     * Use for execution inside IDE
     */
    public static void main(java.lang.String[] args) throws Exception {
        DEBUG = true;
        System.setProperty("xmltest.dbgTimeouts", "true");
        JemmyProperties.getCurrentTimeouts().loadDebugTimeouts();
        
        TestRunner.run(suite());
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.