Re: Programatically generating the database schema

From: Mike Kienenberger (mkienen..laska.net)
Date: Thu Oct 09 2003 - 21:01:42 EDT

  • Next message: Mike Block: "Missing part of FK on a toOne toOne"

    Barry Walker <barrywalke..omcast.net> wrote:
    > I'm sure it's possible since the modeler does it, but is there a simple
    way to set up and generate the database schema from existing cayenne.xml and
    associated files? I'm writing JUnit tests and I'd like it to wipe the schema
    clean at the start of the tests.

    You might want to take a look at DBUnit.

    I just wrote a bunch of Junit tests over the last two days using Junit,
    DBUnit, StrutsTestCase, and JunitDoclet.

    DBUnit doesn't wipe the schema, but is your schema really going to change?

    It does have many options for wiping all or parts of your data.

    I used it to generate xml images from the shared company Oracle development
    database, then had my tests update a local OpenBase database on my machine
    with the data snapshots while running the tests.

    Here's probably the most complicated piece of test framework code I ended up
    writing.
    It uses an existing DataContext/DataObject to locate the DataNode driver
    info from Cayenne (There may be an easier way to do this, but this worked
    for me).

    This goes in my CayenneTestDatabaseFramework class which defines specific
    test behavior for Cayenne (startup info, generic objects (normal user
    record, administrative user record, etc)).

            public static PoolManager driverPoolManager(DataContext aDataContext)
            {
                    ObjEntity anObjEntity =
    aDataContext.getEntityResolver().lookupObjEntity(getNormalUser(aDataContext).getObjectId().getObjClass());
                    DataNode dataNode = aDataContext.dataNodeForObjEntity(anObjEntity);
                    PoolManager dataSource = (PoolManager) dataNode.getDataSource();
                    
                    return dataSource;
            }

    And then it uses that data node information to set up a DBUnit connection.
    Thus, your test has no hard-coded database driver info -- It's all pulled
    from Cayenne.
    This works good for me since I run tests locally against OpenBase, but will
    test remotely against Oracle. All I'll have to do is change the DataNode
    for my DataMap when I deploy remotely.

    This goes in my TestCase superclass as directed by the DBUnit docs:

            protected IDatabaseConnection getConnection() throws Exception
            {
                    if (false == CayenneTestDatabaseFramework.isInitialized())
                            throw new RuntimeException("CayenneTestDatabaseFramework not
    initialized");
                            
                    PoolManager aPoolManager =
    CayenneTestDatabaseFramework.driverPoolManager(getDataContext());

                    Class driverClass = Class.forName(aPoolManager.getJdbcDriver());
                    Connection jdbcConnection = DriverManager.getConnection(
                                    aPoolManager.getDataSourceUrl(), aPoolManager.getUserName(),
    aPoolManager.getPassword());
                    return new DatabaseConnection(jdbcConnection);
            }

    Unfortunately, I'm leaving tomorrow morning for a long weekend, so while I
    might be able to answer any additional questions tomorrow morning, most
    likely I won't respond again until Monday or Tuesday.

    -Mike



    This archive was generated by hypermail 2.0.0 : Thu Oct 09 2003 - 21:01:11 EDT