1
votes

I'm having some trouble with an environment configuration. For some tests, we need a real database for our integration tests. To have a clean database everytime we start the test, we thought we just change the dbCreate property in the configuration files that are already loaded:

config-integration.properties:

# connection
db.name=integration_test
db.host=127.0.0.1
db.port=3306
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://${db.host}:${db.port}/${db.name}?useUnicode=yes&characterEncoding=UTF-8&autoReconnect=true
dataSource.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

# configuration
dataSource.username=foo
dataSource.password=bar
dataSource.dbCreate=create-drop
dataSource.pooled=true
dataSource.properties.maxActive=50
dataSource.properties.maxIdle=25
dataSource.properties.minIdle=5
dataSource.properties.initialSize=5
dataSource.properties.minEvictableIdleTimeMillis=60000
dataSource.properties.timeBetweenEvictionRunsMillis=60000
dataSource.properties.maxWait=10000
dataSource.properties.validationQuery=/* ping */

Config.groovy:

environment {
....
integration {
    log4j = {
        root { debug 'stdout' }
        warn 'org.apache'
        warn 'grails.spring'
        info 'org.codehaus.groovy.grails'
        warn 'net.sf.ehcache'
        warn 'org.hibernate'
    }

    grails.serverURL = "http://www.foobar.com"

    if (!grails.config.locations || !(grails.config.locations instanceof List)) {
        grails.config.locations = []
    }
    // loading default configuration
    grails.config.locations << "classpath:app/config-integration.properties"

    grails.gorm.failOnError = true
}
....
}

The problem now is, that this simply doesn't work. The tables are not created and therefore the tests die with an SQL Exception. There's not even a message about a wrong username/password if I change it to something incorrect. So it looks like the configuration is not loaded at the moment when grails/hibernate should create the tables in the database.

Edit: What I forgot: If I define the dataSource in the Config.groovy itself, it works.

Does anyone have an idea what I'm doing wrong here?

Thanks a lot in advance!

2
Make sure that config-integration is in your classpath. You can also convert to a groovy and use println to see if the file is loaded. - user800014
It is in grails-app/conf/app, just as other different configs and their all loaded correctly no matter what environment - also custom ones. There seems to be a specific link with the create-drop part, b/c it also works if the database is already fully set up (which is not an option b/c of the tests). - Moritz

2 Answers

0
votes

This may be a lame clarification I am looking for, but are you using

environment{
  test{
   ....
  }
}

or

environment{
  integration{
   ....
  }
}

DSL in the config file?

If I am not mistaken, we only have development, test and production environment DSLs available.

0
votes

So, I couldn't figure out why, but it looks like the table dropping & creation stuff gets done by Hibernate before the grails app is actually started and the configs are loaded. Therefore I'm now using a different approach and load the configs "manually" and set the properties. Looks like this now and works fine:

integration {
    log4j = {
        root { debug 'stdout' }
        warn 'org.apache'
        warn 'grails.spring'
        info 'org.codehaus.groovy.grails'
        warn 'net.sf.ehcache'
        warn 'org.hibernate'
    }

    def props = new Properties()

    new File("grails-app/conf/app/appconfig-integration-tests.properties").withInputStream {
        stream -> props.load(stream)
    }

    def user_file = new File("grails-app/conf/app/appconfig-${System.getProperty("user.name")}.properties")
    if(user_file.exists()) {
        user_file.withInputStream {
            stream -> props.load(stream)
        }
    }

    dataSource {
        username = props.get("dataSource.username")
        password = props.get("dataSource.password")
        dbCreate = props.get("dataSource.dbCreate")
        driverClassName = props.get("dataSource.driverClassName")
        dialect = props.get("dataSource.dialect")
        url = props.get("dataSource.url")
    }

    grails.serverURL = "http://www.foobar.com"

    grails.gorm.failOnError = true
}