/framework/src/play/src/test/scala/play/utils/ReflectSpec.scala

https://github.com/erwan/Play20 · Scala · 96 lines · 67 code · 26 blank · 3 comment · 0 complexity · fb431d0f66b16b172ecadf980f4292cb MD5 · raw file

  1. /*
  2. * Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
  3. */
  4. package play.utils
  5. import javax.inject.Inject
  6. import com.google.inject.Guice
  7. import org.specs2.mutable.Specification
  8. import play.api.{ PlayException, Configuration, Environment }
  9. import play.api.inject.Binding
  10. import play.api.inject.guice.GuiceApplicationLoader
  11. import scala.reflect.ClassTag
  12. object ReflectSpec extends Specification {
  13. "Reflect" should {
  14. "load bindings from configuration" in {
  15. "return no bindings for provided configuration" in {
  16. bindings("provided", "none") must beEmpty
  17. }
  18. "return the default implementation when none configured or default class doesn't exist" in {
  19. doQuack(bindings("", "NoDuck")) must_== "quack"
  20. }
  21. "return a default Scala implementation" in {
  22. doQuack(bindings[CustomDuck]("")) must_== "custom quack"
  23. }
  24. "return a default Java implementation" in {
  25. doQuack(bindings[CustomJavaDuck]("")) must_== "java quack"
  26. }
  27. "return a configured Scala implementation" in {
  28. doQuack(bindings(classOf[CustomDuck].getName, "NoDuck")) must_== "custom quack"
  29. }
  30. "return a configured Java implementation" in {
  31. doQuack(bindings(classOf[CustomJavaDuck].getName, "NoDuck")) must_== "java quack"
  32. }
  33. "throw an exception if a configured class doesn't exist" in {
  34. doQuack(bindings[CustomDuck]("NoDuck")) must throwA[PlayException]
  35. }
  36. "throw an exception if a configured class doesn't implement either of the interfaces" in {
  37. doQuack(bindings[CustomDuck](classOf[NotADuck].getName)) must throwA[PlayException]
  38. }
  39. }
  40. }
  41. def bindings(configured: String, defaultClassName: String): Seq[Binding[_]] = {
  42. Reflect.bindingsFromConfiguration[Duck, JavaDuck, JavaDuckAdapter, DefaultDuck](
  43. Environment.simple(), Configuration.from(Map("duck" -> configured)), "duck", defaultClassName)
  44. }
  45. def bindings[Default: ClassTag](configured: String): Seq[Binding[_]] = {
  46. bindings(configured, implicitly[ClassTag[Default]].runtimeClass.getName)
  47. }
  48. trait Duck {
  49. def quack: String
  50. }
  51. trait JavaDuck {
  52. def getQuack: String
  53. }
  54. class JavaDuckAdapter @Inject() (underlying: JavaDuck) extends Duck {
  55. def quack = underlying.getQuack
  56. }
  57. class DefaultDuck extends Duck {
  58. def quack = "quack"
  59. }
  60. class CustomDuck extends Duck {
  61. def quack = "custom quack"
  62. }
  63. class CustomJavaDuck extends JavaDuck {
  64. def getQuack = "java quack"
  65. }
  66. class NotADuck
  67. def doQuack(bindings: Seq[Binding[_]]): String = {
  68. Guice.createInjector(GuiceApplicationLoader.guiced(bindings)).getInstance(classOf[Duck]).quack
  69. }
  70. }