Saturday, March 1, 2008

Simple UI to Verify database in Java

Disclaimer: This post is related to IBM Rational Functional Tester With Java. It needs some slight modifications to work with other tools.

Sometimes, your automated test script needs to do some strange stuff. Like accessing the database, extracting some value and using this value in subsequent U.I steps in your script.

Suppose that you have hundreds of scripts that needs the following block to access the DB and read a certain value:

try{
Connection db_connection = DriverManager.getConnection("jdbc:odbc:DBNAME",
"username","password");


Statement stm2 = db_connection.createStatement();
stm2.executeQuery("select * from TableName");

}
catch( SQLException x ){

JOptionPane.showMessageDialog(null,
x.getLocalizedMessage().toString(),"Error",
JOptionPane.ERROR_MESSAGE);

}

What if the username and password is changed after a period of time for a certain reason. This means that you need to modify all your scripts and change the credentials. And this could be a pain and very time consuming.

A good practice is creating a class that will prompt the user to enter the credentials and validates them before running the script. This will save time and headache in subsequent releases.

The following is the code for the class with the proprietary information removed:


import javax.swing.JOptionPane;
import resources.SendValidUNandPWHelper;

import com.rational.test.ft.*;
import com.rational.test.ft.object.interfaces.*;
import com.rational.test.ft.object.interfaces.siebel.*;
import com.rational.test.ft.script.*;
import com.rational.test.ft.value.*;
import com.rational.test.ft.vp.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
* Description : Functional Test Script
* @author Ashehadeh
*/
class SendValidUNandPW extends SendValidUNandPWHelper
{
/**
* Script Name : CheckDBDialog
* Generated : Feb 19, 2008 8:31:06 AM
* Description : Functional Test Script
* Original Host : WinNT Version 5.1 Build 2600 (S)
*
* @since 2008/02/19
* @author Ashehadeh
*/
/**
* @param args
*/





public String[] sendValidCredentials(){

String[] result=null;
String[] k =validateInput();

while (k==null){
k= validateInput();
}

return k;


}


//check if the provided credential can access the DB
public static String[] validateInput(){

String[] result=null;
String[] k =isDBonline();

if (k==null){
result =null;
System.exit(0);

}

else {


try{
Connection db_connection = DriverManager.getConnection("jdbc:odbc:DBNAME",k[0],k[1]);

Statement stm2 = db_connection.createStatement();
stm2.executeQuery("select * from TABLEName");
result =k;

}
catch( SQLException x ){

JOptionPane.showMessageDialog(null,
"Please Enter correct Uasername/Password!","Error",
JOptionPane.ERROR_MESSAGE);
/* JOptionPane.showMessageDialog(null,
x.getLocalizedMessage().toString(),"Error",
JOptionPane.ERROR_MESSAGE);*/

result=null;

}

}
return result;
}


//check if the db is online from the first place

public static String[] isDBonline(){


String[] result=null;
String[] k =isOneEmpty();


if (k==null){
result =null;
System.exit(0);


}


//now check for DB connectivity
else {

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

result =k;

}



catch(ClassNotFoundException c){
JOptionPane.showMessageDialog(null,
"DB Unavailable!","Error",
JOptionPane.ERROR_MESSAGE);
result=null;
System.exit(0);

}

}
return result;
}


//is one of the fields empty? if so, exit
public static String[] isOneEmpty(){

String[] k=showDialog();
String[] result;

// if one of the username or passwords are empty, exit.
if (k[0]==null || k[1].equals("") ||
k[1].equals("") || k[1]==null){

JOptionPane.showMessageDialog(null,
"Error. Fill the two fields. Click ok to exit!","Error",
JOptionPane.ERROR_MESSAGE);

result=null;
System.exit(0);

}


//if both of them are not empty continue
else{
result=k;

}

return result;

}

//prompt the user for username and password
public static String[] showDialog() {

String DBUsername=JOptionPane.showInputDialog(null,
"Enter DB username:", "Input", JOptionPane.QUESTION_MESSAGE);
String DBpassword=JOptionPane.showInputDialog(null,
"Enter DB password:", "Input", JOptionPane.QUESTION_MESSAGE);
String[] UPArray={DBUsername,DBpassword};
return UPArray;

}



}


Original post: Click here

Category Cloud

Label Cloud