JdbcToCsvDemoTest

History Key

  • New content
  • Removed content

Recent Versions

Choose two versions to compare, or click the link to view it.

  1. 58. almost 3 years by dbastin
  2. 57. about 3 years by dbastin
  3. 56. about 3 years by dbastin
  4. 55. about 3 years by dbastin
  5. 54. about 3 years by dbastin
  6. 53. about 3 years by bensullivan
  7. 52. about 3 years by bensullivan
  8. 51. about 3 years by bensullivan
  9. 50. about 3 years by bensullivan
  10. 49. about 3 years by bensullivan
  11. 48. about 3 years by bensullivan
  12. 47. about 3 years by bensullivan
  13. 46. about 3 years by bensullivan
  14. 45. about 3 years by bensullivan
  15. 44. about 3 years by bensullivan
  16. 43. about 3 years by bensullivan
  17. 42. about 3 years by bensullivan
  18. 41. about 3 years by bensullivan
  19. 40. about 3 years by bensullivan
  20. 39. about 3 years by bensullivan
  21. 38. about 3 years by bensullivan
  22. 37. about 3 years by bensullivan
  23. 36. about 3 years by bensullivan
  24. 35. about 3 years by bensullivan
  25. 34. about 3 years by bensullivan
  26. 33. about 3 years by bensullivan
  27. 32. about 3 years by bensullivan
  28. 31. about 3 years by bensullivan
  29. 30. about 3 years by bensullivan
  30. 29. about 3 years by bensullivan
  31. 28. about 3 years by bensullivan
  32. 27. about 3 years by bensullivan
  33. 26. about 3 years by bensullivan
  34. 25. about 3 years by bensullivan
  35. 24. about 3 years by bensullivan
  36. 23. about 3 years by bensullivan
  37. 22. about 3 years by bensullivan
  38. 21. about 3 years by bensullivan
  39. 20. about 3 years by bensullivan
  40. 19. about 3 years by bensullivan
  41. 18. about 3 years by bensullivan
  42. 17. about 3 years by bensullivan
  43. 16. about 3 years by bensullivan
  44. 15. about 3 years by bensullivan
  45. 14. about 3 years by bensullivan
  46. 13. about 3 years by bensullivan
  47. 12. about 3 years by bensullivan
  48. 11. about 3 years by bensullivan
  49. 10. about 3 years by bensullivan
  50. 9. about 3 years by bensullivan
  51. 8. about 3 years by bensullivan
  52. 7. about 3 years by bensullivan
  53. 6. about 3 years by bensullivan
  54. 5. about 3 years by bensullivan
  55. 4. about 3 years by bensullivan
  56. 3. about 3 years by bensullivan
  57. 2. about 3 years by bensullivan
  58. 1. about 3 years by bensullivan
 

In this demonstration, we are going to take a database table, EMPLOYEE, in a HSQLDB database and transform each record in the table into a line in an output CSV file called employee.csv.

The demonstration test consists of the following parts:

  • Input data: EMPLOYEE table
  • Expected output: employee.csv
  • The test itself: JdbcToCsvDemoTest.java
  • A specification class: JdbcToCsvSpecification.java
  • A wiring class: JdbcToCsvWirer.java
  • A properties file: jdbc2csv.properties

Let's go through each part in turn. Basically, we want to turn this database table...

EMPLOYEE

ID NAME SALARY START
1 Fred 1000000 2001-01-01
2 Wilma 2000000 2002-01-01
3 Barney 3000000 2003-01-01
4 Betty 4000000 null
5 Dino 0 null

...into this...

employee.csv

The JdbcToCsvDemoTest contains code that illustrates how this is achieved. The test launches the Donkey, and checks that the CSV file output matches the expected results. Pretty simple really.

JdbcToCsvDemoTest.java

You will perhaps notice the TestCase implements some additional behaviour through the HasFixtures interface. This is a feature of the testing framework that the Donkey uses, called Sniper. The fixtures() method is called by Sniper for each test in a Sniper LifecycleTestCase. In this implementation, we are creating the test EMPLOYEE table we need to slurp from.

The Donkey is launched via a Trebuchet. A Trebuchet takes a Specification. Further details regarding a Specification are here.

Here is the Specification class for this demonstration test...

JdbcToCsvSpecification.java

The Wirer contains dependency injection configuration. This wiring is quite similar to the wiring in the CsvToSqlDemoTest. You can find out a bit about it here. Burper is the name we give to transformation error handlers.

JdbcToCsvWirer.java

The configuration required by each of these implementations is contained in the jdbc2csv.properties

jdbc2csv.properties

The db and sql are required by the DatabaseSlurper.

The properties:// prefix is interpreted by the donkey and loads the key value pairs contained in the prefixed properties filename which then become available in the Donkey session through the Config instance.

The contents:// prefix is interpreted by the donkey and assigns the contents of the prefixed filename as the value to the key; so in this case the SQL contained in config/employee-select.sql is assigned to the sql key and made available to the Donkey session through the Config instance.

Transform

A Slurper reads data from the inbound end-point into a Cake. The slurped Cake is then marshaled into a Gargler where the Cake is decomposed into individual Slices. The Gargler delegates to a Transform which performs the transformations and/or mappings. The Transform effects changes on each Slice in the Cake and returns a modified copy of the slice back to the Gargler. The transformed slices are collected in a new Cake which is then marshaled on to a Spitter.

Note that we have also set up the Wirer to use a Burper that logs and then swallows any transformation errors, in order to continue processing other slices of the Cake. The default behaviour is to halt on transformation error.

This concludes the walk-through of the JDBC to CSV Demonstration Test. Check out the code and run the JdbcToCsvDemoTest.java for yourself. Rock.