From 2b13955bb35729622e5648368a2e836284112dad Mon Sep 17 00:00:00 2001 From: Daniel Sarosi Date: Mon, 21 May 2018 21:00:58 +0800 Subject: [PATCH] * Upgraded to play v2.6.x --- README.md | 2 +- app/controllers/Application.scala | 6 ++--- build.sbt | 11 ++++---- conf/application.conf | 4 +-- conf/routes | 2 +- project/build.properties | 2 +- project/plugins.sbt | 2 +- test/ApplicationSpec.scala | 44 ++++++++++++++++++++++--------- 8 files changed, 46 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index a5ae35b..46c037c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -This is an example of Play 2.4 application that uses Scaldi for dependency injection. +This is an example of Play 2.6 application that uses Scaldi for dependency injection. It can be also found as an activator template here: diff --git a/app/controllers/Application.scala b/app/controllers/Application.scala index 138fd64..c7b95d8 100644 --- a/app/controllers/Application.scala +++ b/app/controllers/Application.scala @@ -4,10 +4,10 @@ import net.sf.ehcache.CacheManager import play.api.mvc._ import scaldi.{Injectable, Injector} import service.{ServerStatusService, MessageService} -import play.api.cache.CacheApi +import play.api.cache.AsyncCacheApi -class Application(implicit inj: Injector) extends Controller with Injectable { - val cache = inject [CacheApi] +class Application(implicit inj: Injector) extends InjectedController with Injectable { + val cache = inject [AsyncCacheApi] val messageService = inject [MessageService] diff --git a/build.sbt b/build.sbt index 3e282f9..1362367 100644 --- a/build.sbt +++ b/build.sbt @@ -2,13 +2,14 @@ name := "scaldi-play-example" version := "1.0-SNAPSHOT" -scalaVersion := "2.11.7" +scalaVersion := "2.12.4" libraryDependencies ++= Seq( - "com.typesafe.play" %% "play-cache" % "2.5.0", - "org.scaldi" %% "scaldi-play" % "0.5.14", - "org.specs2" %% "specs2-core" % "3.5" % "test", - "org.specs2" %% "specs2-junit" % "3.5" % "test" + guice, + ehcache, + "org.scaldi" %% "scaldi-play" % "0.5.17", + "org.specs2" %% "specs2-core" % "4.2.0" % "test", + "org.specs2" %% "specs2-junit" % "4.2.0" % "test" ) resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases" diff --git a/conf/application.conf b/conf/application.conf index 6671930..c5b9c74 100644 --- a/conf/application.conf +++ b/conf/application.conf @@ -5,11 +5,11 @@ # ~~~~~ # The secret key is used to secure cryptographics functions. # If you deploy your application to several instances be sure to use the same key! -application.secret="Onk7agCh??yfgRf/^8>2K34@meq_2[IsWxFP:7QaS=@KlYujZyTSt1hpV" +play.http.secret.key="Onk7agCh??yfgRf/^8>2K34@meq_2[IsWxFP:7QaS=@KlYujZyTSt1hpV" # The application languages # ~~~~~ -application.langs="en" +play.i18n.langs=["en"] # Global object class # ~~~~~ diff --git a/conf/routes b/conf/routes index e71a0bd..a2df47c 100644 --- a/conf/routes +++ b/conf/routes @@ -3,7 +3,7 @@ # ~~~~ # Home page -GET / controllers.Application.index +GET / @controllers.Application.index # Map static resources from the /public folder to the /assets URL path GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset) diff --git a/project/build.properties b/project/build.properties index a6e117b..7c81737 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=0.13.8 +sbt.version=1.1.5 diff --git a/project/plugins.sbt b/project/plugins.sbt index da3d514..ca81e95 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -5,4 +5,4 @@ logLevel := Level.Warn resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/" // Use the Play sbt plugin for Play projects -addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.0") \ No newline at end of file +addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.6.13") \ No newline at end of file diff --git a/test/ApplicationSpec.scala b/test/ApplicationSpec.scala index 9436d24..05bee25 100644 --- a/test/ApplicationSpec.scala +++ b/test/ApplicationSpec.scala @@ -1,30 +1,39 @@ package test import org.specs2.mutable._ -import play.api.cache.EhCacheModule import play.api.inject.BuiltinModule - import play.api.test._ import play.api.test.Helpers._ -import scaldi.play.{ScaldiApplicationBuilder, ControllerInjector} -import scaldi.Module +import scaldi.play.{ControllerInjector, ScaldiApplicationBuilder} +import scaldi.{Injectable, Module} import service.MessageService import modules.{ServerModule, UserModule} import scaldi.play.condition._ import ScaldiApplicationBuilder._ +import controllers.AssetsModule +import play.api.{Application, Configuration, Environment, Logger} +import play.api.cache.ehcache.EhCacheModule +import play.api.http.HeaderNames +import play.api.i18n.I18nModule +import play.api.mvc.{AnyContentAsEmpty, CookiesModule} +import play.filters.csrf.CSRFModule +import play.filters.hosts.{AllowedHostsConfig, AllowedHostsModule} /** * Add your spec here. * You can mock out a whole application including requests, plugins etc. * For more information, consult the wiki. */ -class ApplicationSpec extends Specification { - +class ApplicationSpec extends Specification with Injectable { + + private val logger = Logger(this.getClass) + "Application" should { "send 404 on a bad request" in { - withScaldiApp() { - status(route(FakeRequest(GET, "/boum")).get) must equalTo(NOT_FOUND) + withScaldiInj() { implicit inj => + val application = inject[Application] + status(route(application, FakeRequest(GET, "/boum")).get) must equalTo(NOT_FOUND) } } @@ -37,8 +46,14 @@ class ApplicationSpec extends Specification { "render the index page (with full control over the module composition)" in { val module = new TestModule :: new ServerModule :: new UserModule :: new ControllerInjector - withScaldiApp(modules = Seq(module, new EhCacheModule, new BuiltinModule), loadModules = (_, _) => Seq.empty) { - val home = route(FakeRequest(GET, "/")).get + withScaldiInj( + modules = Seq(module, new AllowedHostsModule, new CookiesModule, new AssetsModule, new CSRFModule, new I18nModule, new EhCacheModule, new BuiltinModule), + loadModules = (_, _) => Seq.empty + ) { implicit inj => + + val application = inject[Application] + + val home = route(application, FakeRequest(GET, "/")).get status(home) must equalTo(OK) contentType(home) must beSome.which(_ == "text/html") @@ -48,8 +63,11 @@ class ApplicationSpec extends Specification { } "render the index page (with just override module)" in { - withScaldiApp(modules = Seq(new TestModule)) { - val home = route(FakeRequest(GET, "/")).get + withScaldiInj(modules = Seq(new TestModule)) { implicit inj => + + val application = inject[Application] + + val home = route(application, FakeRequest(GET, "/")).get status(home) must equalTo(OK) contentType(home) must beSome.which(_ == "text/html") @@ -62,7 +80,7 @@ class ApplicationSpec extends Specification { val application = new ScaldiApplicationBuilder().prependModule(new TestModule).build() running(application) { - val home = route(FakeRequest(GET, "/")).get + val home = route(application, FakeRequest(GET, "/")).get status(home) must equalTo(OK) contentType(home) must beSome.which(_ == "text/html")