Re: unit testing Cayenne objects

From: Erik Hatcher (eri..hatchersolutions.com)
Date: Thu May 05 2005 - 11:31:49 EDT

  • Next message: Dhruti Ramani: "Re: about Ordering in Cayenne"

    On May 4, 2005, at 9:04 PM, Andrus Adamchik wrote:

    >
    > On May 4, 2005, at 5:03 PM, Andrus Adamchik wrote:
    >
    >
    >> I am too lazy to put it on Wiki, so I have to answer this question
    >> every
    >> once in a while...
    >>
    >
    > And so I decided to fix it and created a Wiki article:
    >
    > http://objectstyle.org/confluence/display/CAY/Setting+Database
    > +Connection
    >
    > I also like the flavor of this approach suggested by Cris. And yet
    > another level of abstraction can be added by implementing a custom
    > org.objectstyle.cayenne.conf.DataSourceFactory and setting it in
    > the Modeler. I was thinking of providing a DataSourceFactory that
    > pops up a DB login dialog. Should be very useful for Swing/SWT apps.

    Thanks again to both Cris and Andrus! My experiences with Cayenne
    have been outstanding thanks to the responsiveness and keenness of
    this list.

    Here is the base test class that I've settled on for now, borrowing
    bits from the <cdbgen> task, Cris' DBUnit snippet, and such:

    abstract public class DbTestCase extends TestCase {
       public void setUp() throws Exception {
         String baseDir = System.getProperty("cayenneConfigBaseDir");

         // liberally borrowed from Cayenne's DbGeneratorTask
         InputSource in = new InputSource(new File(baseDir,
    "CollexDomainMap.map.xml").getAbsolutePath());
         DataMap dataMap = new MapLoader().loadDataMap(in);

         DbGenerator generator = new DbGenerator(new HSQLDBAdapter(),
    dataMap);
         generator.setShouldCreateFKConstraints(true);
         generator.setShouldCreatePKSupport(true);
         generator.setShouldCreateTables(true);
         generator.setShouldDropPKSupport(true);
         generator.setShouldDropTables(true);

         DriverDataSource dataSource = new DriverDataSource(new jdbcDriver
    (), "jdbc:hsqldb:mem:db", "sa", "");

         generator.runGenerator(dataSource);

         // populate some data
         Connection jdbcConnection = DriverManager.getConnection
    ("jdbc:hsqldb:mem:db");
         DatabaseConnection conn = new DatabaseConnection(jdbcConnection);
         FlatXmlDataSet dataSet = new FlatXmlDataSet(getClass
    ().getResourceAsStream(getClass().getSimpleName() + ".xml"));
         DatabaseOperation.CLEAN_INSERT.execute(conn, dataSet);

         // Configure the Cayenne DataContext to use a test-only set up,
    for an in-memory database
         FileConfiguration conf = new FileConfiguration("cayenne.xml");
         conf.addFilesystemPath(new File(baseDir));
         Configuration.initializeSharedConfiguration(conf);
         DataDomain domain = conf.getDomain();
         DataNode node = domain.getNode("CollexDomainNode");
         node.setDataSource(dataSource);

         // Put the data context into a ThreadLocal just like the web
    tier listener does, giving
         // direct DB access to any code that needs it.
         DataContext context = DataContext.createDataContext();
         DataContext.bindThreadDataContext(context);
       }

       protected final DataContext getDataContext() {
         return DataContext.getThreadDataContext();
       }
    }

    I'll need to rearrange the creation of the DB to a place where it
    only occurs once rather than for every testXXX method, but as it
    stands it works great for my couple of simple tests thus far.

         Erik



    This archive was generated by hypermail 2.0.0 : Thu May 05 2005 - 11:34:09 EDT