Thursday, February 17, 2011

Grails Environment Checking: Better know a framework

 I took this from http://www.make-go-now.com/2009/03/19/grails-environment-checking-better-know-a-framework/

Grails Environment Checking: Better know a framework

Checking the environment that your Grails application is running.

Lets say you have some code that is dependent on the environment that it is running.

Here is one way to check it:

import org.codehaus.groovy.grails.commons.GrailsApplication
import grails.util.GrailsUtil

//check if production
if ( GrailsUtil.getEnvironment().equals(GrailsApplication.ENV_PRODUCTION)) ){...}

//check if development
if ( GrailsUtil.getEnvironment().equals(GrailsApplication.ENV_DEVELOPMENT)) ){...}

//check if testing
if ( GrailsUtil.getEnvironment().equals(GrailsApplication.ENV_TEST) ){...}


And here is how you can set the environment in your tests:

import org.codehaus.groovy.grails.commons.GrailsApplication
import grails.util.GrailsUtil

//set envionment to production
System.setProperty(GrailsApplication.ENVIRONMENT, GrailsApplication.ENV_PRODUCTION)

//set envionment to development
System.setProperty(GrailsApplication.ENVIRONMENT, GrailsApplication.ENV_DEVELOPMENT)

//set envionment to test
System.setProperty(GrailsApplication.ENVIRONMENT, GrailsApplication.ENV_TEST)

 

No comments: