how to perform unit test on this code
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   deepika_bagadiya
Posted On:   Tuesday, August 6, 2002 12:31 AM

package Gic; import java.sql.*; import java.awt.*; public class AddState extends javax.swing.JDialog { /*This constructor is called when parent is JFrame */ public AddState(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); addComponents(); addListeners(); setSize(200,120); b = true; } /*This constructor is called when parent is Dialog */ public AddState(java.awt.Dialog parent, boolean modal, int id_State ) { super(parent, modal); m_objEditState = (EditState) parent; initComponents(); addComponents(); addListeners(); setSize(200,120);    More>>
			

package Gic;

import java.sql.*;
import java.awt.*;


public class AddState extends javax.swing.JDialog {

/*This constructor is called when parent is JFrame */
public AddState(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
addComponents();
addListeners();
setSize(200,120);
b = true;
}

/*This constructor is called when parent is Dialog */
public AddState(java.awt.Dialog parent, boolean modal, int
id_State ) {
super(parent, modal);
m_objEditState = (EditState) parent;
initComponents();
addComponents();
addListeners();
setSize(200,120);
b = false;
id = id_State;

/*This block of code get the value of selected state in parent form from database and set the StateText Field by
that value*/
try{
Statement stment = conn.createStatement();
ResultSet rst;
/* this function is called from database class to
perform Select query on database*/
rst = stment.executeQuery("Select State from State
Where State_Id = '"+ id +"' ");
while(rst.next()){
m_txtStateName.setText((String)rst.getString("State"));
}
rst.close();
conn.close();
}
catch(SQLException e) {
System.out.println("An error 11 occured
" +
"The SQLException message is: " + e.toString());
}
};


/*This function initialize all components*/
private void initComponents() {

m_lblStateName = new javax.swing.JLabel();
m_lblStateName.setFont(new java.awt.Font("Dialog", 0,
2));
m_lblStateName.setText("State Name");

m_txtStateName = new javax.swing.JTextField();

m_btnOk = new javax.swing.JButton();
m_btnOk.setFont(new java.awt.Font("Dialog", 0, 12));
m_btnOk.setText("Ok");

m_btnCancel = new javax.swing.JButton();
m_btnCancel.setFont(new java.awt.Font("Dialog", 0,
12));
m_btnCancel.setText("Cancel");

setTitle("Add State");
setResizable(false);
pack();
}

/*This function add various component to content pane and
Sets null layout*/
private void addComponents() {
Container content = getContentPane();
content.setLayout( null );

content.add(m_btnCancel);
m_btnCancel.setBounds(100, 60, 75, 25);

content.add( m_btnOk);
m_btnOk.setBounds(20, 60, 75, 25);

content.add(m_lblStateName);
m_lblStateName.setBounds(10, 20, 100, 20);

content.add(m_txtStateName);
m_txtStateName.setBounds(90, 20, 100, 20);
}

/*This function add window listener and Action listener*/
private void addListeners() {

addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt)
{
closeDialog(evt);
}
});

m_btnOk.addActionListener(new
java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent
evt)
{
m_btnOkActionPerformed(evt);
}
});

m_btnCancel.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
m_btnCancelActionPerformed(evt);
}
});

}

/*On Clicking ok button this form accepts state name from user and checks the value of b.If b is true then it inserts the value of state in database and if b is false then it udate the state value in database*/
private void ActionPerformed(java.awt.event.ActionEvent
evt) {
String state = m_txtStateName.getText();

if(b == true){
try{
m_objdbConn = new DatabaseConnection();
String s = "Select StateId from Config";
ResultSet rst = m_objdbConn.GetResultSet(s);
if(rst.isBeforeFirst()) {
rst.next();
}
int id = rst.getInt("StateId");
rst.close();
s = "Update Config Set StateId = StateId + 1";
/* this function is called from database class to perform
update query on database*/
m_objdbConn.ExecuteQuery(s);
s = "Insert Into State (State_Id,State) values ('"+ id
+"','" + state + "')";
/* this function is called from database class to perform
insert query on database*/
m_objdbConn.ExecuteQuery(s);
}

catch (SQLException e) {
System.out.println("An error 11 occurbed
" +
"The SQLException message is: " + e.toString());
}
}

if(b == false){
m_objdbConn = new DatabaseConnection();
String s = " Update State Set State = '" + state + "'
Where State_Id = '" + id + "' " ;
/* this function is called from database class to perform
update query on database*/
m_objdbConn.ExecuteQuery(s);
/* this function is called from EditState class to update
table display in that class*/
m_objEditState.editTable(state);
}
/* then the form unloads*/
try{
this.dispose();
this.finalize();
}

catch(Throwable t)
{
String Str = "Error during form unload";
}

}

/*On clicking cancel button the form gets unloaded*/
private void m_btnCancelActionPerformed
(java.awt.event.ActionEvent evt) {

try{
this.dispose();
this.finalize();
}
catch(Throwable t) {
String str = "Error during form load";
}

}

/*This function unloads the add state form*/
private void closeDialog(java.awt.event.WindowEvent evt)
{
setVisible(false);
dispose();
}


private javax.swing.JButton m_btnCancel;
//variable for cancel button

private javax.swing.JTextField m_txtStateName;
//variable for State name text field

private javax.swing.JLabel m_lblStateName;
//variable for State lablel

private javax.swing.JButton m_btnOk;
//variable for ok button

boolean b;
//global variable of boolean

int id ;
//global variable of integer

private EditState m_objEditState;
//object of state class

private DatabaseConnection m_objdbConn;
/*object of database connection class
(I have created my own databse connection class
which opens connection to database and execute query)*/



}
   <<Less

Re: how to perform unit test on this code

Posted By:   Anonymous  
Posted On:   Wednesday, August 7, 2002 02:15 AM

Sorry to bring bad news, but you will probably face some problems unit testing the code above. The problem is that it mixes business logic and user interface logic, which heavily complicates testing.

So, while trying to write test, it would probably be best to refactor to a more decoupled solution (take a look at the Model-View-Controller pattern). For example, the dialog probably shouldn't know anything about a database.

Take a look at XPloration: GUI testing for an example.

Re: how to perform unit test on this code

Posted By:   maulin_gajjar  
Posted On:   Tuesday, August 6, 2002 12:55 AM



Before I begin do enlighten me on this one....
are you familiar with the JUnit test case framework?

About | Sitemap | Contact