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 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              makeVerifyTableDefinition("TEST_TABLE");
26      private static final VerifyTableDefinition SECOND_TABLE =
27              makeVerifyTableDefinition("SECOND_TABLE");
28      private static final VerifyTableDefinition EMPTY_TABLE =
29              makeVerifyTableDefinition("EMPTY_TABLE");
30      private static final VerifyTableDefinition PK_TABLE =
31              makeVerifyTableDefinition("PK_TABLE");
32      private static final VerifyTableDefinition ONLY_PK_TABLE =
33              makeVerifyTableDefinition("ONLY_PK_TABLE");
34      private static final VerifyTableDefinition EMPTY_MULTITYPE_TABLE =
35              makeVerifyTableDefinition("EMPTY_MULTITYPE_TABLE");
36  
37      private final DataFileLoader dataFileLoader = new FlatXmlDataFileLoader();
38  
39      private DatabaseEnvironment dbEnv;
40      private IDatabaseConnection connection;
41      private IDatabaseTester databaseTester;
42  
43      private DefaultPrepAndExpectedTestCase tc;
44  
45      private static VerifyTableDefinition makeVerifyTableDefinition(
46              String tableName)
47      {
48          return new VerifyTableDefinition(tableName, new String[] {});
49      }
50  
51      protected void setUp() throws Exception {
52          dbEnv = DatabaseEnvironment.getInstance();
53          connection = dbEnv.getConnection();
54          databaseTester = new DefaultDatabaseTester(connection);
55  
56          tc = new DefaultPrepAndExpectedTestCase(dataFileLoader, databaseTester);
57  
58          super.setUp();
59      }
60  
61      public void testSuccessRun() throws Exception {
62          // use same files to have no data comparison fails
63          String[] prepDataFiles = {PREP_DATA_FILE_NAME};
64          String[] expectedDataFiles = {PREP_DATA_FILE_NAME};
65          VerifyTableDefinition[] tables = {TEST_TABLE, SECOND_TABLE, EMPTY_TABLE,
66                  PK_TABLE, ONLY_PK_TABLE, EMPTY_MULTITYPE_TABLE};
67  
68          tc.configureTest(tables, prepDataFiles, expectedDataFiles);
69          tc.preTest();
70  
71          // skip modifying data and just verify the insert
72  
73          // reopen connection as DefaultOperationListener closes it after inserts
74          // maybe we need a KeepConnectionOpenOperationListener class?!
75          connection = dbEnv.getConnection();
76          databaseTester = new DefaultDatabaseTester(connection);
77          tc.setDatabaseTester(databaseTester);
78  
79          tc.postTest();
80      }
81  
82      public void testFailRun() throws Exception {
83          String[] prepDataFiles = {PREP_DATA_FILE_NAME};
84          String[] expectedDataFiles = {EXP_DATA_FILE_NAME};
85          VerifyTableDefinition[] tables = {TEST_TABLE, SECOND_TABLE, EMPTY_TABLE,
86                  PK_TABLE, ONLY_PK_TABLE, EMPTY_MULTITYPE_TABLE};
87  
88          tc.configureTest(tables, prepDataFiles, expectedDataFiles);
89          tc.preTest();
90  
91          // skip modifying data and just verify the insert
92  
93          // reopen connection as DefaultOperationListener closes it after inserts
94          // maybe we need a KeepConnectionOpenOperationListener class?!
95          connection = dbEnv.getConnection();
96          databaseTester = new DefaultDatabaseTester(connection);
97          tc.setDatabaseTester(databaseTester);
98  
99          try {
100             tc.postTest();
101             fail("Did not catch expected exception:"
102                     + " junit.framework.ComparisonFailure");
103         } catch (ComparisonFailure e) {
104             // test passes
105         }
106     }
107 }