|
From: Ashwin K. <AKu...@pd...> - 2011-01-19 16:26:41
|
Roberto: I have added the following extensions to DBUnit 2.4.8 in-house as we are using JUNIT3 and JDK 1.5 (Annotations). I understand that DBUnit 3+ will support JDK 1.5 and annotation. I checked out https://svn.codehaus.org/dbunit/ but I would need further support in understand how to go about adding my code here as I don't see all the classes from 2.4.8 here. Here is the summary of my extensions that I would like to contribute. EnhancedDBUnitTestCase (extends DBTestCase) (Supports newer annotations around test methods) Base class for the unit tests that need to test stored procedures. For Junit4, will have to add these to custom test runner. IDBUnitDatabaseProperties Constants defined that provide schema connectivity parameters. DBUnitConnectionManager (uses DBCP) This class provides connection pooling so tests can share the expensive connection object(s). DbUnitOperationListener DBUnit closes connection after setup, and thus requires connections to be opened/closed for test and again for tear down. This class ensures the same connection instance is used for setUp(), execution of test..() and tearDown() as well. SQLDatabaseOperation Helps extend functionality to use SQL scripts for input/clean up data sets. Useful for deleting when there is seed data in the table, circular references. PropertyReplacerInputStream This class is used by DBUnit to evaluate property place holders when reading the file containing the input dataset. Useful when your input dataset has to refer seed data that is already part of the schema, or you need to substitute dynamically calculated values in the data set (like dates) This is much faster as it extends BufferedInputStream DBUnitPropertyEnum (Will need to reconsider this) This class helps evaluate a given property value. Developers will add newer enums as and when necessary in the input data sets. PropertyEvaluator Interface that will be implemented by the custom property evaluator. DBUnitPropertyEvaluator Custom property evaluator ${TOMORROW_DATE_IN_MM_DD_YYYY} or ${SQL:select ID from EMPLOYEE where degree_type='MS'} DBUnitCustomDataSet Custom dataset that aggregates multiple datasets for a given unit test so we can set up circular referential data if so needed. DBUnitOrderedTableNameMap Uses MultiValueMap to handle multiple datasets having input data for the same table. DBUnitSetUpDatabaseOperation, DBUnitTearDownDatabaseOperation Database operations to be used in order to set up/clean up data for a test. getDataSet() gets invoked for both setup and teardown. Using the above, we can differentiate and retrieve the appropriate data set from set up and clean up. DBUnitTestConfiguration Annotation to specify set up, clean up data set(s) and methods to do the same. Example: @DBUnitTestConfiguration(setUpScripts={"employee_dept.xml","supervisor.xml"}, setUpScriptsOperation= DBUnitSetUpDatabaseOperation.REFRESH, tearDownScripts = {"delele_emp_dept.sql"}, tearDownScriptsOperation = DBUnitTearDownDatabaseOperation.USE_SQL) The above REFRESH will help create circular reference as it will update and not insert if the record is already present. Besides this, I have tried to address an issue that developers have in extracting data sets from RDBMS. Most developers can exercise the application and create test data however, extracting that seems challenging in order to create a test data set. So, I have created a DataExporter utility class that can allow developer to establish connection to a schema and specify which record in a table they want to extract. From there on, the utility will go over all the tables this record references (foreign key) to query if the developer is interested in extracting that. If so, this will go recursively. Set up connection properties Connection jdbcConnection; jdbcConnection = DriverManager.getConnection( "jdbc:oracle:thin:@machine-ip:1525:db", "user", "password"); connection = new DatabaseConnection(jdbcConnection, "company"); // for Oracle 10 DatabaseConfig config = connection.getConfig(); config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new Oracle10DataTypeFactory()); // try to extract desired data set extractDesiredDataSet(0, "EMPLOYEE", new Long(1233422)); Do you want to fetch : EMPLOYEE[ADDRESS_ID]->ADDRESS? (y/n) : y Do you want to fetch : ADDRESS[HOME_PHONE_ID]->PHONE? (y/n) : y Do you want to fetch : ADDRESS[MOBILE_PHONE_ID]->PHONE? (y/n) : y Do you want to fetch : EMPLOYEE[DEPARTMENT_ID]->DEPARTMENT? (y/n) : y Do you want to fetch : DEPARTMENT[SUPERVISOR_ID]->EMPLOYEE? (y/n) : y Once we opt out or walk through recursively, we have a neat data set that we can now import and massage to create test data sets. If someone has already contributed similar stuff, please let me know. If you think this can potentially add value to DBUnit, I would like to contribute to DBUnit3. It will be really helpful if you can mail or help me understand how to go about contributing this source to the DBUnit 3 on codehaus. Sincerely, ______________________________________________________________ Ashwini Kuntamukkala | PDX, Inc. EPS Architecture Team |