Supereka’s Weblog

July 4, 2009

grails dbunit-operator plugin usage

Filed under: Grails — Tags: , , — supereka @ 8:38 am

1. install

grails install-plugin dbunit-operator

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

2. conf/DataSource.groovy

   1: dataSource {

   2:     pooled = true

   3:     driverClassName = "net.sourceforge.jtds.jdbc.Driver"

   4:     username = "trainingadmin"

   5:     password = "trainingpw"

   6:     dbunitXmlType = "flat"

   7:     

   8: }

   9:  

  10: hibernate {

  11:     cache.use_second_level_cache=true

  12:     cache.use_query_cache=true

  13:     cache.provider_class='com.opensymphony.oscache.hibernate.OSCacheProvider'

  14: }

  15: // environment specific settings

  16: environments {

  17:     development {

  18:         dataSource {

  19:             dbCreate = "create-drop" // one of 'create', 'create-drop','update'

  20:             url = "jdbc:jtds:sqlserver://localhost:1433/TRAINING"

  21:             initialData = "data/dev/training.xml" // dbunit-operator Flat-XML or XML data file

  22:             initialOperation = "CLEAN_INSERT" // dbunit-operator operation

  23:         }

  24:     }

  25:     test {

  26:         dataSource {

  27:             dbCreate = "update"

  28:             url = "jdbc:hsqldb:mem:testDb"

  29:         }

  30:     }

  31:     production {

  32:         dataSource {

  33:             dbCreate = "update"

  34:             url = "jdbc:hsqldb:file:prodDb;shutdown=true"

  35:         }

  36:     }

  37: }

line

6: must specify like this to use xml data file

21: you can specific initial data here. WARNING this point you can only specific 1 data file. If you have any data file, specified at BootStrap.groovy, I will show you later in this post.

22: initialOperation: ???

3. conf/BootStrap.groovy : specific DbUnit action here, in init{}

  • you can put more data file here
  • can create object here

ex:BootStrap.groovy

   1: import com.nic.*

   2:  

   3: class BootStrap {

   4: /*

   5:   def init = { servletContext ->

   6:   }

   7: */

   8:  

   9:   def init = {servletContext ->

  10:     DbUnitOperator.create()

  11:     DbUnitOperator.operate("data/dev/employee.xml","CLEAN_INSERT")

  12:  

  13:     def bu1 = new BusinessUnit(buNo:'002', name: 'Accounting', buType:BusinessUnitType.DEPARTMENT)

  14:     bu1.save()

  15:  

  16:   }

  17:   def destroy = {

  18:   }

line

10 need let bootStrap to Create DbUnit (Need)
11 opt DbUnitOperator.operate("data/dev/employee.xml","CLEAN_INSERT")

// this is the way you can add more data file. each file can edit (just edit, cannot change structure) via Excel 2007.

13, 14 opt Another way you can create and save object. You can call object that already created by xml data file.

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.