Junit之后备份DB数据

时间:2018-01-12 03:45:12

标签: java

执行Junit测试时,删除数据库的原始数据如何执行备份,以便在Junit测试后检索数据

@Test
public void testExecute_N001() throws Exception {
    this.importFileName_tsc = "input/input_tsc_" + "1" + EXTENSION_XML;
    this.importFileName_postgre = "input/input_eccube_" + "1"
            + EXTENSION_XML;
    this.resultFileName = "output/output_" + "1" + EXTENSION_XML;
    super.setResultBatchFilePrefix("N-001_");
    super.copyToImportFile("input/N-001" + EXTENSION_TXT);
    checkNoBatchTarget();
}

public void testExecute_N002() throws Exception {
    this.importFileName_tsc = "input/input_tsc_" + "1" + EXTENSION_XML;
    this.importFileName_postgre = "input/input_eccube_" + "1"
            + EXTENSION_XML;
    this.resultFileName = "output/output_" + "1" + EXTENSION_XML;

    super.setResultBatchFilePrefix("N-002_");
    super.copyToImportFile("input/N-002" + EXTENSION_TXT);
    checkBatchExecute();
}

public void testExecute_E001() throws Exception {
    this.importFileName_tsc = "input/input_tsc_" + "2" + EXTENSION_XML;
    this.importFileName_postgre = "input/input_eccube_" + "2"
            + EXTENSION_XML;
    this.resultFileName = "output/output_" + "2" + EXTENSION_XML;

    super.setResultBatchFilePrefix("E-001_");
    super.copyToImportFile("input/E-001" + EXTENSION_TXT);
    checkError();
}

private void checkNoBatchTarget() throws Exception {
    IDatabaseConnection ecCubeConn = null; // DBコネクション
    IDatabaseConnection connection = null; // DB POSTGRESQL

    try {
        connection = getConnection();

        IDataSet dataSet = getDataSetFromFile(this.importFileName_tsc);

        // 個別データ削除処理
        DatabaseOperation.DELETE_ALL.execute(connection, dataSet);

        // xmlからテストデータを取得し、テーブルにセットする
        DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
        ecCubeConn = getConnection();

        IDataSet dataSet_postgre = getDataSetFromFile(this.importFileName_postgre);
        DatabaseOperation.DELETE_ALL.execute(ecCubeConn, dataSet_postgre);
        DatabaseOperation.CLEAN_INSERT.execute(ecCubeConn, dataSet_postgre);

        int result = super.batchStart(args);

        // 戻り値チェック
        assertEquals("0", String.valueOf(result));

        // DBテーブルチェック
        // 期待されるデータを取得
        IDataSet expectedDataSet = getDataSetFromFile(this.resultFileName);

        QueryDataSet queryDataSet = new QueryDataSet(ecCubeConn);
        queryDataSet
                .addTable(
                        DESTINATION_TABLE_1,
                        "SELECT  store_cd, store_name, area_id, product_code, goods_code, maker_id, maker_product_code, size, color, stock, season FROM "
                                + DESTINATION_TABLE_1);
        IDataSet databaseDataSet = queryDataSet;

        ITable expectedTableOrd = sortTable(expectedDataSet);
        ITable actualTableOrd = sortTable(databaseDataSet);

        // REMOVE UNNECESSARY COLUMNS FOR TESTING
        expectedTableOrd = DefaultColumnFilter.includedColumnsTable(
                expectedTableOrd, new String[] { "store_cd", "store_name",
                        " area_id", "product_code", "goods_code",
                        "maker_id", "maker_product_code", "size", "color",
                        "stock", "season" });

        // 更新されていることを確認
        Assertion.assertEquals(expectedTableOrd, actualTableOrd);

    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    } finally {
        if (super.isBatchFile()) {
            super.moveToBackup();
        }

        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {

        }
    }
}

private void checkError() throws Exception {
    IDatabaseConnection connection = null; // DBコネクション
    IDatabaseConnection ecCubeConn = null; // DB POSTGRESQL

    try {
        connection = getConnection();
        IDataSet dataSet = getDataSetFromFile(this.importFileName_tsc);
        DatabaseOperation.DELETE_ALL.execute(connection, dataSet);
        DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
        ecCubeConn = getConnection();
        IDataSet dataSet_postgre = getDataSetFromFile(this.importFileName_postgre);
        DatabaseOperation.DELETE_ALL.execute(ecCubeConn, dataSet_postgre);
        DatabaseOperation.CLEAN_INSERT.execute(ecCubeConn, dataSet_postgre);

        int result = super.batchStart(args);

        assertEquals("-1", String.valueOf(result));

        IDataSet expectedDataSet = getDataSetFromFile(this.resultFileName);

        QueryDataSet queryDataSet = new QueryDataSet(ecCubeConn);
        queryDataSet
                .addTable(
                        DESTINATION_TABLE_1,
                        "SELECT  store_cd,, store_name, area_id, product_code, goods_code, maker_id, maker_product_code, size, color, stock, season  FROM "
                                + DESTINATION_TABLE_1);
        IDataSet databaseDataSet = queryDataSet;

        ITable expectedTableOrd = sortTable(expectedDataSet);
        ITable actualTableOrd = sortTable(databaseDataSet);

        expectedTableOrd = DefaultColumnFilter.includedColumnsTable(
                expectedTableOrd, new String[] { "store_cd", "store_name",
                        " area_id", "product_code", "goods_code",
                        "maker_id", "maker_product_code", "size", "color",
                        "stock", "season" });

        Assertion.assertEquals(expectedTableOrd, actualTableOrd);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    } finally {
        if (super.isBatchFile()) {
            super.moveToBackup();
        }

        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {

        }
    }
}

private void checkBatchExecute() throws Exception {
    IDatabaseConnection connection = null; // DBコネクション
    IDatabaseConnection ecCubeConn = null; // DB POSTGRESQL

    try {
        connection = getConnection();
        IDataSet dataSet = getDataSetFromFile(this.importFileName_tsc);
        DatabaseOperation.DELETE_ALL.execute(connection, dataSet);
        DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
        ecCubeConn = getConnection();
        IDataSet dataSet_postgre = getDataSetFromFile(this.importFileName_postgre);
        DatabaseOperation.DELETE_ALL.execute(ecCubeConn, dataSet_postgre);
        DatabaseOperation.CLEAN_INSERT.execute(ecCubeConn, dataSet_postgre);
        int result = super.batchStart(args);
        assertEquals("0", String.valueOf(result));

        IDataSet expectedDataSet = getDataSetFromFile(this.resultFileName);
        QueryDataSet queryDataSet = new QueryDataSet(ecCubeConn);
        queryDataSet
                .addTable(
                        DESTINATION_TABLE_1,
                        "SELECT store_cd,, store_name, area_id, product_code, goods_code, maker_id, maker_product_code, size, color, stock, season  FROM "
                                + DESTINATION_TABLE_1);
        IDataSet databaseDataSet = queryDataSet;

        ITable expectedTableOrd = sortTable(expectedDataSet);
        ITable actualTableOrd = sortTable(databaseDataSet);

        expectedTableOrd = DefaultColumnFilter.includedColumnsTable(
                expectedTableOrd, new String[] { "store_cd", "store_name",
                        " area_id", "product_code", "goods_code",
                        "maker_id", "maker_product_code", "size", "color",
                        "stock", "season" });
        Assertion.assertEquals(expectedTableOrd, actualTableOrd);

    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    } finally {
        if (super.isBatchFile()) {
            super.moveToBackup();
        }

        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {

        }
    }
}

private ITable sortTable(IDataSet dataSet) throws DataSetException {
    return new SortedTable(dataSet.getTable(DESTINATION_TABLE_1),
            new String[] { "store_cd" });
}

有没有其他简单的方法来检查数据库中的插入更新和删除..... 这个方法好吗...... 如何改变这段代码...... 有没有其他方法来实现它,以便它不会影响数据库数据..

1 个答案:

答案 0 :(得分:0)

  1. 如果可能,请设置一个可以运行测试的独立环境。我希望您没有在生产环境中运行测试。
  2. 切勿使用实际数据来运行测试。始终设置数据并对其进行测试。如果您使用实际数据,则无法保证它始终存在,因此您的测试将来可能会失败。
  3. 在事务中执行数据库插入/更新,并在执行测试后最后回滚。