JAVA-SQL-数据迁移 - ResultSet比较Failing JUnit测试

时间:2012-12-04 22:38:25

标签: java sql jdbc data-migration

我不能让这个JUnit测试通过我的生命。有人可以指出出错的地方。我正在进行数据迁移(MSSQL SERVER 2005),但我有sourceDBUrl和targetDCUrl相同的URL,以便将其缩小到语法错误。这就是我所拥有的,语法错误。我正在比较查询表的结果

SELECT programmeapproval, resourceapproval FROM tr_timesheet WHERE timesheetid = ?

并且测试总是失败,但是我已经开发了其他的junit测试。我创建了3个diffemt resultSetsEqual方法,但没有一个工作。然而,我开发的其他一些JUnit测试已经通过了。查询:

SELECT timesheetid,programmeapproval,resourceapproval FROM tr_timesheet

返回三列

  • timesheetid (PK,int,not null)(填充范围为 数字2240 - 2282)
  • programmeapproval (smallint,not null)(填充了 每个领域都是第1名)
  • resourceapproval (smallint,not null)(填充数字 每个领域都有1个)

当我运行嵌入在代码中的查询时,它只返回一行包含programmeapproval和resourceapproval列,并且这两个字段都填充了数字1.

我正确安装了所有jdbc驱动程序并测试了连接性。根据IDE,JUnit测试此时失败。

assertTrue(helper.resultSetsEqual2(sourceVal,targetVal));

这是代码:

/ * 这是一个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 * * /

/ * 这是一个常规课程 ** /

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 ** * /

/ * 过去的新课程 - 这是一个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 *** /

1 个答案:

答案 0 :(得分:3)

当对象彼此false时,您返回equal。在这种方法中,我将条件更改为not equals

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))) //added !
             {
                  return false;
             }
        }
    }
    return true;
}