I CANNOT get this JUnit Test to pass for the life of me. Can somebody point out where this has gone wrong. I am doing a data migration(MSSQL SERVER 2005), but I have the sourceDBUrl and the targetDCUrl the same URL so to narrow it down to syntax errors. So that is what I have, a syntax error. I am comparing the results of a table for the query
SELECT programmeapproval, resourceapproval FROM tr_timesheet WHERE timesheetid = ?
and the test always fails, but passes for other junit tests I have developed. I created 3 diffemt resultSetsEqual methods and none work. Yet, some other JUnit tests I have developed have PASSED. THE QUERY:
SELECT timesheetid, programmeapproval, resourceapproval FROM tr_timesheet
Returns three columns
timesheetid (PK,int, not null) (populated with a range of
numbers 2240 - 2282)
programmeapproval (smallint,not null) (populated with the
number 1 in every field)
resourceapproval (smallint, not null) (populated with a number
1 in every field)
When I run the query that is embedded in the code it only returns one row with the programmeapproval and resourceapproval columns and both field populated with the number 1.
I have all jdbc drivers correctly installed and tested for connectivity. The JUnit Test is failing at this point according to the IDE.
assertTrue(helper.resultSetsEqual2(sourceVal,targetVal));
This is the code:
/*THIS IS A JUNIT CLASS****?
package a7.unittests.dao;
import static org.junit.Assert.assertTrue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;
import org.junit.Test;
import artemispm.tritonalerts.TimesheetAlert;
public class UnitTestTimesheetAlert {
@Test
public void testQUERY_CHECKALERT() throws Exception{
UnitTestHelper helper = new UnitTestHelper();
Connection con = helper.getConnection(helper.sourceDBUrl);
Connection conTarget = helper.getConnection(helper.targetDBUrl);
PreparedStatement stmt = con.prepareStatement("select programmeapproval, resourceapproval from tr_timesheet where timesheetid = ?");
stmt.setInt(1, 2240);
ResultSet sourceVal = stmt.executeQuery();
stmt = conTarget.prepareStatement("select programmeapproval, resourceapproval from tr_timesheet where timesheetid = ?");
stmt.setInt(1,2240);
ResultSet targetVal = stmt.executeQuery();
assertTrue(helper.resultSetsEqual2(sourceVal,targetVal));
}}
/*END**/
/*THIS IS A REGULAR CLASS**/
package a7.unittests.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class UnitTestHelper {
static String sourceDBUrl = "jdbc:sqlserver://127.0.0.1:1433;databaseName=a7itm;user=a7user;password=a7user";
static String targetDBUrl = "jdbc:sqlserver://127.0.0.1:1433;databaseName=a7itm;user=a7user;password=a7user";
public Connection getConnection(String url)throws Exception{
return DriverManager.getConnection(url);
}
public boolean resultSetsEqual3 (ResultSet rs1, ResultSet rs2) throws SQLException {
int col = 1;
//ResultSetMetaData metadata = rs1.getMetaData();
//int count = metadata.getColumnCount();
while (rs1.next() && rs2.next()) {
final Object res1 = rs1.getObject(col);
final Object res2 = rs2.getObject(col);
// Check values
if (!res1.equals(res2)) {
throw new RuntimeException(String.format("%s and %s aren't equal at common position %d",
res1, res2, col));
}
// rs1 and rs2 must reach last row in the same iteration
if ((rs1.isLast() != rs2.isLast())) {
throw new RuntimeException("The two ResultSets contains different number of columns!");
}
}
return true;
}
public boolean resultSetsEqual (ResultSet source, ResultSet target) throws SQLException{
while(source.next())
{
target.next();
ResultSetMetaData metadata = source.getMetaData();
int count = metadata.getColumnCount();
for (int i =1; i<=count; i++)
{
if(source.getObject(i) != target.getObject(i))
{
return false;
}
}
}
return true;
}
public boolean resultSetsEqual2 (ResultSet source, ResultSet target) throws SQLException{
while(source.next())
{
target.next();
ResultSetMetaData metadata = source.getMetaData();
int count = metadata.getColumnCount();
for (int i =1; i<=count; i++)
{
if(source.getObject(i).equals(target.getObject(i)))
{
return false;
}
}
}
return true;
}
}
/END***/
/*PASTED NEW CLASS - THIS IS A JUNIT TEST CLASS*/
package a7.unittests.dao;
import static org.junit.Assert.*;
import java.sql.Connection;
import java.sql.DriverManager;
import org.junit.Test;
public class TestDatabaseConnection {
@Test
public void testConnection() throws Exception{
UnitTestHelper helper = new UnitTestHelper();
Connection con = helper.getConnection(helper.sourceDBUrl);
Connection conTarget = helper.getConnection(helper.targetDBUrl);
assertTrue(con != null && conTarget != null);
}
}
/**END***/