このQUERYをデータベースに送信すると、両方のデータベースに手動でクエリを入力すると正しいデータが返されますが、アサーションエラーが発生し続けます。
唯一の違いは、文字通り下に表示される列のタイトルです(左はMYSQL、右はMSSQLです)
_____________________________________________
count(*) | (No Column Name)
_____________________________________________
43 | 43
|
これが私のJAVAコードです
public class UnitTestDMUtility_Select {
@Test
public void testQUERY_CHECKBOTHPARTS() throws Exception{
UnitTestHelper helper = new UnitTestHelper();
Connection con = helper.getConnection(helper.sourceDBUrl);
Connection conTarget = helper.getConnection(helper.targetDBUrl);
PreparedStatement stmt = con.prepareStatement(DMUtility.QUERY_CHECKBOTHPARTS);
stmt.setInt(1, 101);
ResultSet sourceVal = stmt.executeQuery();
//Here is the QUERY
//select count(*) from tr_demand where demandtypeid=101
stmt = conTarget.prepareStatement(DMUtility.QUERY_CHECKBOTHPARTS);
stmt.setInt(1, 101);
ResultSet targetVal = stmt.executeQuery();
assertTrue(helper.resultSetsEqual2(sourceVal,targetVal));
}
}
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 {
public Connection getConnection(String url)throws Exception{
return DriverManager.getConnection(url);
}
/*
* Use for Insert and Update and Delete Statements
*/
public boolean resultSetsEqual4 (int source, int target){
System.out.println("The source is: "+source+" The target is: "+target);
if(((source>0)&&(target>0)&&(target==source)))
{
return true;
}
else
{
return false;
}
}
/*
* Used to test SELECT statements passing in objects
*/
public boolean resultSetsEqual2 (ResultSet source, ResultSet target) throws SQLException{
System.out.println("The source is: "+source+" The target is: "+target);
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;
}
public boolean resultSetsEqual (ResultSet source, ResultSet target) throws SQLException{
System.out.println("The source is: "+source+" The target is: "+target);
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 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;
}
}