View Javadoc
1   package org.dbunit;
2   
3   import junit.framework.ComparisonFailure;
4   
5   import org.dbunit.database.IDatabaseConnection;
6   import org.dbunit.util.fileloader.DataFileLoader;
7   import org.dbunit.util.fileloader.FlatXmlDataFileLoader;
8   import org.slf4j.Logger;
9   import org.slf4j.LoggerFactory;
10  
11  /**
12   * Integration test of extends of the PrepAndExpected.
13   * 
14   * @author Jeff Jensen jeffjensen AT users.sourceforge.net
15   * @author Last changed by: $Author$
16   * @version $Revision$ $Date$
17   * @since 2.4.8
18   */
19  public class DefaultPrepAndExpectedTestCaseExtIT extends
20          DefaultPrepAndExpectedTestCase {
21      private static final String PREP_DATA_FILE_NAME = "/xml/flatXmlDataSetTest.xml";
22      private static final String EXP_DATA_FILE_NAME = "/xml/flatXmlDataSetTestChanged.xml";
23  
24      private static final VerifyTableDefinition TEST_TABLE =
25              new VerifyTableDefinition("TEST_TABLE", new String[] {});
26  
27      private static final VerifyTableDefinition SECOND_TABLE =
28              new VerifyTableDefinition("SECOND_TABLE", new String[] {});
29  
30      private final Logger LOG =
31              LoggerFactory.getLogger(DefaultPrepAndExpectedTestCaseExtIT.class);
32  
33      private final DataFileLoader dataFileLoader = new FlatXmlDataFileLoader();
34  
35      private DatabaseEnvironment dbEnv;
36      private IDatabaseConnection connection;
37      private IDatabaseTester databaseTester;
38  
39      protected void setUp() throws Exception {
40          dbEnv = DatabaseEnvironment.getInstance();
41          connection = dbEnv.getConnection();
42          databaseTester = new DefaultDatabaseTester(connection);
43  
44          setDataFileLoader(dataFileLoader);
45          setDatabaseTester(databaseTester);
46  
47          // don't call super.setUp() here as prep data is not loaded yet
48          // (getDataSet() is null)
49          // super.setUp();
50      }
51  
52      public void testSuccessRun() throws Exception {
53          String[] prepDataFiles = {PREP_DATA_FILE_NAME};
54          String[] expectedDataFiles = {PREP_DATA_FILE_NAME};
55          VerifyTableDefinition[] tables = {TEST_TABLE, SECOND_TABLE};
56  
57          configureTest(tables, prepDataFiles, expectedDataFiles);
58          preTest();
59  
60          // skip modifying data and just verify the insert
61  
62          // reopen connection as DefaultOperationListener closes it after inserts
63          // maybe we need a KeepConnectionOpenOperationListener class?!
64          connection = dbEnv.getConnection();
65          databaseTester = new DefaultDatabaseTester(connection);
66          setDatabaseTester(databaseTester);
67  
68          postTest();
69      }
70  
71      public void testFailRun() throws Exception {
72          String[] prepDataFiles = {PREP_DATA_FILE_NAME};
73          String[] expectedDataFiles = {EXP_DATA_FILE_NAME};
74          VerifyTableDefinition[] tables = {TEST_TABLE, SECOND_TABLE};
75  
76          configureTest(tables, prepDataFiles, expectedDataFiles);
77          preTest();
78  
79          // skip modifying data and just verify the insert
80  
81          // reopen connection as DefaultOperationListener closes it after inserts
82          // maybe we need a KeepConnectionOpenOperationListener class?!
83          connection = dbEnv.getConnection();
84          databaseTester = new DefaultDatabaseTester(connection);
85          setDatabaseTester(databaseTester);
86  
87          try {
88              postTest();
89              fail("Did not catch expected exception:"
90                      + " junit.framework.ComparisonFailure");
91          } catch (ComparisonFailure e) {
92              // test passes
93          }
94      }
95  }