View Javadoc
1   package org.dbunit;
2   
3   import junit.framework.ComparisonFailure;
4   import junit.framework.TestCase;
5   
6   import org.dbunit.database.IDatabaseConnection;
7   import org.dbunit.util.fileloader.DataFileLoader;
8   import org.dbunit.util.fileloader.FlatXmlDataFileLoader;
9   import org.slf4j.Logger;
10  import org.slf4j.LoggerFactory;
11  
12  /**
13   * Integration test of composition of the PrepAndExpected (simulated DI).
14   * 
15   * @author Jeff Jensen jeffjensen AT users.sourceforge.net
16   * @author Last changed by: $Author$
17   * @version $Revision$ $Date$
18   * @since 2.4.8
19   */
20  public class DefaultPrepAndExpectedTestCaseDiIT extends TestCase {
21      private final Logger LOG =
22              LoggerFactory.getLogger(DefaultPrepAndExpectedTestCaseDiIT.class);
23  
24      private static final String PREP_DATA_FILE_NAME = "/xml/flatXmlDataSetTest.xml";
25      private static final String EXP_DATA_FILE_NAME = "/xml/flatXmlDataSetTestChanged.xml";
26  
27      private static final VerifyTableDefinition TEST_TABLE =
28              new VerifyTableDefinition("TEST_TABLE", new String[] {});
29  
30      private static final VerifyTableDefinition SECOND_TABLE =
31              new VerifyTableDefinition("SECOND_TABLE", new String[] {});
32  
33      private final DataFileLoader dataFileLoader = new FlatXmlDataFileLoader();
34  
35      private DatabaseEnvironment dbEnv;
36      private IDatabaseConnection connection;
37      private IDatabaseTester databaseTester;
38  
39      private DefaultPrepAndExpectedTestCase tc;
40  
41      protected void setUp() throws Exception {
42          dbEnv = DatabaseEnvironment.getInstance();
43          connection = dbEnv.getConnection();
44          databaseTester = new DefaultDatabaseTester(connection);
45  
46          tc = new DefaultPrepAndExpectedTestCase(dataFileLoader, databaseTester);
47  
48          super.setUp();
49      }
50  
51      public void testSuccessRun() throws Exception {
52          // use same files to have no data comparison fails
53          String[] prepDataFiles = {PREP_DATA_FILE_NAME};
54          String[] expectedDataFiles = {PREP_DATA_FILE_NAME};
55          VerifyTableDefinition[] tables = {TEST_TABLE, SECOND_TABLE};
56  
57          tc.configureTest(tables, prepDataFiles, expectedDataFiles);
58          tc.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          tc.setDatabaseTester(databaseTester);
67  
68          tc.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          tc.configureTest(tables, prepDataFiles, expectedDataFiles);
77          tc.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          tc.setDatabaseTester(databaseTester);
86  
87          try {
88              tc.postTest();
89              fail("Did not catch expected exception:"
90                      + " junit.framework.ComparisonFailure");
91          } catch (ComparisonFailure e) {
92              // test passes
93          }
94      }
95  }