Skip to content

Commit

Permalink
Add an environment setting from either system property or init parame…
Browse files Browse the repository at this point in the history
…ter.
  • Loading branch information
rossabaker committed Sep 28, 2010
1 parent 08c768c commit 2e52ac9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/src/main/scala/org/scalatra/ScalatraKernel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ object ScalatraKernel
type Action = () => Any

val httpMethods = List("GET", "POST", "PUT", "DELETE")

val EnvironmentKey = "org.scalatra.environment"
}
import ScalatraKernel._

Expand Down Expand Up @@ -199,4 +201,7 @@ trait ScalatraKernel extends Handler with Initializable
case config: FilterConfig => Option(config.getInitParameter(name))
case _ => None
}

def environment: String = System.getProperty(EnvironmentKey, initParameter(EnvironmentKey).getOrElse("development"))
def isDevelopmentMode = environment.toLowerCase.startsWith("dev")
}
45 changes: 45 additions & 0 deletions core/src/test/scala/org/scalatra/EnvironmentTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.scalatra

import org.scalatest.matchers.ShouldMatchers
import test.scalatest.ScalatraSuite

class EnvironmentFilter extends ScalatraFilter {
get("/*/environment") {
environment
}

get("/*/is-development-mode") {
isDevelopmentMode
}
}

class EnvironmentFilterTest extends ScalatraSuite with ShouldMatchers {
val devFilterHolder = addFilter(classOf[EnvironmentFilter], "/dev/*")

val prodFilterHolder = addFilter(classOf[EnvironmentFilter], "/prod/*")
prodFilterHolder.setInitParameter("org.scalatra.environment", "production")

test("default environment is 'development'") {
get("/dev/environment") {
body should equal ("development")
}
}

test("is development mode if environment is 'development'") {
get("/dev/is-development-mode") {
body should equal ("true")
}
}

test("environment comes from org.scalatra.environment init parameter") {
get("/prod/environment") {
body should equal ("production")
}
}

test("is not development mode if environment is 'production'") {
get("/prod/is-development-mode") {
body should equal ("false")
}
}
}

0 comments on commit 2e52ac9

Please sign in to comment.