Java Reference
In-Depth Information
U SING A R EPLACEMENT D ATA S ET
We already used a ReplacementDataSet to replace the [ID] token; we could reuse the
same dataset to also replace NULL values. All we need is to define a new token (say,
[NULL] ) and add another replacement role:
replacementDataSet.addReplacementObject("[NULL]", null);
Listing 17.15 shows all relevant changes, with comments.
Listing 17.15 ReplacementDataSet approach to the missing column issue
[...]
public class AbstractDbUnitTestCase {
[...]
protected IDataSet getReplacedDataSet(IDataSet originalDataSet, long id)
throws Exception {
ReplacementDataSet replacementDataSet = new
ReplacementDataSet(originalDataSet);
replacementDataSet.addReplacementObject("[ID]", id);
replacementDataSet.addReplacementObject("[NULL]", null);
return replacementDataSet;
}
}
B
[...]
public class NULLTest extends AbstractDbUnitTestCase {
@Test
public void testNULLReplacementDataset() throws Exception {
IDataSet okDataSet = getDataSet("/user-ok.xml");
DatabaseOperation.CLEAN_INSERT.execute(dbunitConnection, okDataSet);
IDataSet actualDataSet = dbunitConnection.createDataSet();
Assertion.assertEquals(okDataSet, actualDataSet);
IDataSet revertedDataSet = getReplacedDataSet("/user-replacement.xml",
-1);
IDataSet sortedDataSet = new SortedDataSet(revertedDataSet);
Assertion.assertEquals(sortedDataSet, actualDataSet);
}
}
This is the same method we used before; we just added a new replacement role
here B .
In C , in order to simplify, we're using the method that expects an ID and passing a
bogus value ( -1 ), because it won't be replaced anyway (as the dataset doesn't have any
[ID] token). Ideally, though, getReplacedDataSet() should be overloaded to handle
the situation where the ID isn't necessary. Better yet, the tokens to be replaced
shouldn't be passed as parameters (we look at how to do that later, in section 17.7.3).
If in E we compared actualDataSet against revertedDataSet (which is the origi-
nal XML file with the proper [NULL] tokens replaced), the assertion would still fail.
Although this time the number of columns is correct, the order would be reverted;
the database query would return rows 1 and 2, whereas the dataset defines the order
as 2 and 1. In order to solve this issue without changing the order in the dataset
C
D
E
 
 
Search WWH ::




Custom Search