差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| study:java:dbunit:testdata [2020/05/20 06:13] – Create all page banana | study:java:dbunit:testdata [2020/05/20 06:43] (現在) – [Restore table data] banana | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| ====== Backup table data ====== | ====== Backup table data ====== | ||
| - | あるテーブルに対して、transactionを行うクラスをテストする際、テーブルデータをバックアップする方法を紹介する。\ | + | あるテーブルに対して、transactionを行うクラスをテストする際、テーブルデータをバックアップする方法を紹介する。\\ |
| ここで、紹介するテストクラスは、[[study: | ここで、紹介するテストクラスは、[[study: | ||
| 行 6: | 行 6: | ||
| <code java> | <code java> | ||
| public class TestSomeTableTransactionService extends AbstractCustomTestCase { | public class TestSomeTableTransactionService extends AbstractCustomTestCase { | ||
| - | //table backup file | + | |
| - | private File backupFile; | + | private File backupFile; |
| + | |||
| + | /* (non-Javadoc) | ||
| + | * @see parent.AbstractCustomTestCase# | ||
| + | */ | ||
| + | @Override | ||
| + | protected void prepareSpecific() throws Exception { | ||
| + | IDatabaseConnection _connection = null; | ||
| + | try { | ||
| + | _connection = databaseTester.getConnection(); | ||
| + | //prepare backup file | ||
| + | QueryDataSet _partialDataSet = new QueryDataSet(_connection); | ||
| + | _partialDataSet.addTable(" | ||
| + | backupFile = File.createTempFile(" | ||
| + | //write table data to file | ||
| + | FlatXmlDataSet.write(_partialDataSet, | ||
| + | |||
| + | //prepare test data | ||
| + | FlatXmlDataSetBuilder _builder = new FlatXmlDataSetBuilder(); | ||
| + | InputStream _is = getClass().getResourceAsStream(" | ||
| + | FlatXmlDataSet _dataSet = _builder.build(_is); | ||
| + | //clean insert | ||
| + | DatabaseOperation.CLEAN_INSERT.execute(_connection, | ||
| + | } finally { | ||
| + | if (_connection != null) _connection.close(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ここで、テストデータとしてtestdata.xmlを使用しているが、サンプルは次節で紹介する。 | ||
| + | |||
| + | ====== Prepare test data ====== | ||
| + | testdata.xmlを中身を下に示す。 | ||
| + | |||
| + | <code xml> | ||
| + | <?xml version=" | ||
| + | < | ||
| + | < | ||
| + | id=" | ||
| + | first_name=" | ||
| + | last_name=" | ||
| + | birth=" | ||
| + | dept_id=" | ||
| + | company=" | ||
| + | reg_date=" | ||
| + | reg_uid=" | ||
| + | mod_date=" | ||
| + | mod_uid=" | ||
| + | delete_flag=" | ||
| + | /> | ||
| + | </ | ||
| + | |||
| + | </ | ||
| + | |||
| + | ====== Restore table data ====== | ||
| + | テスト実施後、テーブルデータを元に復元するソースコードを以下に示す。 | ||
| + | |||
| + | <code java> | ||
| + | public class TestSomeTableTransactionService extends AbstractCustomTestCase { | ||
| + | //table backup file | ||
| + | private File backupFile; | ||
| + | |||
| + | /* (non-Javadoc) | ||
| + | * @see parent.AbstractCustomTestCase# | ||
| + | */ | ||
| + | @Override | ||
| + | protected void tearDownHook() throws Exception { | ||
| + | IDatabaseConnection _connection = null; | ||
| + | try { | ||
| + | _connection = databaseTester.getConnection(); | ||
| + | //restore backup file | ||
| + | FlatXmlDataSetBuilder _builder = new FlatXmlDataSetBuilder(); | ||
| + | IDataSet _dataSet = _builder.build(backupFile); | ||
| + | DatabaseOperation.CLEAN_INSERT.execute(_connection, | ||
| + | } finally { | ||
| + | if (_connection != null) _connection.close(); | ||
| + | } | ||
| + | } | ||
| - | /* (non-Javadoc) | ||
| - | * @see parent.AbstractCustomTestCase# | ||
| - | */ | ||
| - | @Override | ||
| - | protected void prepareSpecific() throws Exception { | ||
| - | IDatabaseConnection _connection = null; | ||
| - | } | ||
| } | } | ||
| </ | </ | ||