Validating Grails Configurations

When externalizing grails app configurations for multiple environments I want to ensure values are provided for all the required/expected properties. So I wrote a plugin to help..

Object Partners

When externalizing grails app configurations for multiple environments I want to ensure values are provided for all the required/expected properties.  So I wrote a plugin to help.

Validating Expected and Required Properties

Simply add something like this to Config.groovy

validate  {
    required = [ "a", "b", "c" ]
    expected = [ "p":123, "d":"foobar", "q":"/dev/null" ]
}

Then (usually in BootStrap.groovy ) to set defaults for missing expected properties callgrailsApplication.config.validateExpectedProperties()To check for required properties callgrailsApplication.config.validateRequiredProperties()A ConfigurationException will be thrown when required properties are missing.

The validate.expected and validate.required data can be specified at lower levels too…

grails {
    mongo {
        validate {
            required = [
                "grails.mongo.host",
                "grails.mongo.databaseName"
            ]
            expected = [
                "grails.mongo.port": 27017,
                "grails.mongo.bucket": "project"
            ]
        }
    }
}

This way you can validate portions of the config by callinggrailsApplication.config.grails.mongo.validateExpectedProperties()and so on.

Validating Existence of External Files

The ConfigUtils.validateExternalFiles method will check that a list of files does exist.  Use it like this in Config.groovy.

grails.config.locations << "file:${userHome}/.emacs"
grails.config.locations < "file:${userHome}/.grails/${appName}-config.groovy"
ConfigUtils.validateExternalFiles(grails.config.locations)

A ConfigurationException will be thrown when any of the files does not exist.

Resources

Share this Post

Related Blog Posts

JVM

Testing Examples for the Facebook SDK Grails Plugin

March 28th, 2013

Testing Examples for the Facebook SDK Grails Plugin

Object Partners
JVM

Do you want to ROC or be Groovy?

March 26th, 2013

Compares Resource Oriented Computing to Groovy as development platforms for enterprise computing.

Object Partners
JVM

Testing tabular output in Spock

March 18th, 2013

This details a way to create tests similar to FItnesse query fixtures using the Spock framework.

Object Partners

About the author