/framework/src/play-java-jdbc/src/test/java/play/db/NamedDatabaseTest.java

http://github.com/playframework/Play20 · Java · 112 lines · 92 code · 17 blank · 3 comment · 0 complexity · 8eb9f9977b190fd38ac8035bd0595f99 MD5 · raw file

  1. /*
  2. * Copyright (C) 2009-2016 Lightbend Inc. <https://www.lightbend.com>
  3. */
  4. package play.db;
  5. import java.util.Map;
  6. import javax.inject.Inject;
  7. import com.google.common.collect.ImmutableMap;
  8. import com.google.inject.Guice;
  9. import com.google.inject.Injector;
  10. import org.junit.Rule;
  11. import org.junit.rules.ExpectedException;
  12. import org.junit.Test;
  13. import play.ApplicationLoader.Context;
  14. import play.Environment;
  15. import play.inject.guice.GuiceApplicationBuilder;
  16. import play.inject.guice.GuiceApplicationLoader;
  17. import static org.hamcrest.CoreMatchers.*;
  18. import static org.junit.Assert.*;
  19. public class NamedDatabaseTest {
  20. @Rule
  21. public ExpectedException exception = ExpectedException.none();
  22. @Test
  23. public void bindDatabasesByName() {
  24. Map<String, Object> config = ImmutableMap.of(
  25. "db.default.driver", "org.h2.Driver",
  26. "db.default.url", "jdbc:h2:mem:default",
  27. "db.other.driver", "org.h2.Driver",
  28. "db.other.url", "jdbc:h2:mem:other"
  29. );
  30. Injector injector = createInjector(config);
  31. assertThat(injector.getInstance(DefaultComponent.class).db.getUrl(), equalTo("jdbc:h2:mem:default"));
  32. assertThat(injector.getInstance(NamedDefaultComponent.class).db.getUrl(), equalTo("jdbc:h2:mem:default"));
  33. assertThat(injector.getInstance(NamedOtherComponent.class).db.getUrl(), equalTo("jdbc:h2:mem:other"));
  34. }
  35. @Test
  36. public void notBindDefaultDatabaseWithoutConfiguration() {
  37. Map<String, Object> config = ImmutableMap.of(
  38. "db.other.driver", "org.h2.Driver",
  39. "db.other.url", "jdbc:h2:mem:other"
  40. );
  41. Injector injector = createInjector(config);
  42. assertThat(injector.getInstance(NamedOtherComponent.class).db.getUrl(), equalTo("jdbc:h2:mem:other"));
  43. exception.expect(com.google.inject.ConfigurationException.class);
  44. injector.getInstance(DefaultComponent.class);
  45. }
  46. @Test
  47. public void notBindNamedDefaultDatabaseWithoutConfiguration() {
  48. Map<String, Object> config = ImmutableMap.of(
  49. "db.other.driver", "org.h2.Driver",
  50. "db.other.url", "jdbc:h2:mem:other"
  51. );
  52. Injector injector = createInjector(config);
  53. assertThat(injector.getInstance(NamedOtherComponent.class).db.getUrl(), equalTo("jdbc:h2:mem:other"));
  54. exception.expect(com.google.inject.ConfigurationException.class);
  55. injector.getInstance(NamedDefaultComponent.class);
  56. }
  57. @Test
  58. public void allowDefaultDatabaseNameToBeConfigured() {
  59. Map<String, Object> config = ImmutableMap.of(
  60. "play.db.default", "other",
  61. "db.other.driver", "org.h2.Driver",
  62. "db.other.url", "jdbc:h2:mem:other"
  63. );
  64. Injector injector = createInjector(config);
  65. assertThat(injector.getInstance(DefaultComponent.class).db.getUrl(), equalTo("jdbc:h2:mem:other"));
  66. assertThat(injector.getInstance(NamedOtherComponent.class).db.getUrl(), equalTo("jdbc:h2:mem:other"));
  67. exception.expect(com.google.inject.ConfigurationException.class);
  68. injector.getInstance(NamedDefaultComponent.class);
  69. }
  70. @Test
  71. public void allowDbConfigKeyToBeConfigured() {
  72. Map<String, Object> config = ImmutableMap.of(
  73. "play.db.config", "databases",
  74. "databases.default.driver", "org.h2.Driver",
  75. "databases.default.url", "jdbc:h2:mem:default"
  76. );
  77. Injector injector = createInjector(config);
  78. assertThat(injector.getInstance(DefaultComponent.class).db.getUrl(), equalTo("jdbc:h2:mem:default"));
  79. assertThat(injector.getInstance(NamedDefaultComponent.class).db.getUrl(), equalTo("jdbc:h2:mem:default"));
  80. }
  81. private Injector createInjector(Map<String, Object> config) {
  82. GuiceApplicationBuilder builder = new GuiceApplicationLoader()
  83. .builder(new Context(Environment.simple(), config));
  84. return Guice.createInjector(builder.applicationModule());
  85. }
  86. public static class DefaultComponent {
  87. @Inject Database db;
  88. }
  89. public static class NamedDefaultComponent {
  90. @Inject @NamedDatabase("default") Database db;
  91. }
  92. public static class NamedOtherComponent {
  93. @Inject @NamedDatabase("other") Database db;
  94. }
  95. }