PageRenderTime 91ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/playframework/playframework
Java | 126 lines | 106 code | 17 blank | 3 comment | 0 complexity | 8e47cdc07831fac1731d2f49913cdf75 MD5 | raw file
  1. /*
  2. * Copyright (C) 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.hamcrest.MatcherAssert.assertThat;
  19. public class NamedDatabaseTest {
  20. @Rule public ExpectedException exception = ExpectedException.none();
  21. @Test
  22. public void bindDatabasesByName() {
  23. Map<String, Object> config =
  24. 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. Injector injector = createInjector(config);
  30. assertThat(
  31. injector.getInstance(DefaultComponent.class).db.getUrl(), equalTo("jdbc:h2:mem:default"));
  32. assertThat(
  33. injector.getInstance(NamedDefaultComponent.class).db.getUrl(),
  34. equalTo("jdbc:h2:mem:default"));
  35. assertThat(
  36. injector.getInstance(NamedOtherComponent.class).db.getUrl(), equalTo("jdbc:h2:mem:other"));
  37. }
  38. @Test
  39. public void notBindDefaultDatabaseWithoutConfiguration() {
  40. Map<String, Object> config =
  41. ImmutableMap.of(
  42. "db.other.driver", "org.h2.Driver",
  43. "db.other.url", "jdbc:h2:mem:other");
  44. Injector injector = createInjector(config);
  45. assertThat(
  46. injector.getInstance(NamedOtherComponent.class).db.getUrl(), equalTo("jdbc:h2:mem:other"));
  47. exception.expect(com.google.inject.ConfigurationException.class);
  48. injector.getInstance(DefaultComponent.class);
  49. }
  50. @Test
  51. public void notBindNamedDefaultDatabaseWithoutConfiguration() {
  52. Map<String, Object> config =
  53. ImmutableMap.of(
  54. "db.other.driver", "org.h2.Driver",
  55. "db.other.url", "jdbc:h2:mem:other");
  56. Injector injector = createInjector(config);
  57. assertThat(
  58. injector.getInstance(NamedOtherComponent.class).db.getUrl(), equalTo("jdbc:h2:mem:other"));
  59. exception.expect(com.google.inject.ConfigurationException.class);
  60. injector.getInstance(NamedDefaultComponent.class);
  61. }
  62. @Test
  63. public void allowDefaultDatabaseNameToBeConfigured() {
  64. Map<String, Object> config =
  65. ImmutableMap.of(
  66. "play.db.default", "other",
  67. "db.other.driver", "org.h2.Driver",
  68. "db.other.url", "jdbc:h2:mem:other");
  69. Injector injector = createInjector(config);
  70. assertThat(
  71. injector.getInstance(DefaultComponent.class).db.getUrl(), equalTo("jdbc:h2:mem:other"));
  72. assertThat(
  73. injector.getInstance(NamedOtherComponent.class).db.getUrl(), equalTo("jdbc:h2:mem:other"));
  74. exception.expect(com.google.inject.ConfigurationException.class);
  75. injector.getInstance(NamedDefaultComponent.class);
  76. }
  77. @Test
  78. public void allowDbConfigKeyToBeConfigured() {
  79. Map<String, Object> config =
  80. ImmutableMap.of(
  81. "play.db.config", "databases",
  82. "databases.default.driver", "org.h2.Driver",
  83. "databases.default.url", "jdbc:h2:mem:default");
  84. Injector injector = createInjector(config);
  85. assertThat(
  86. injector.getInstance(DefaultComponent.class).db.getUrl(), equalTo("jdbc:h2:mem:default"));
  87. assertThat(
  88. injector.getInstance(NamedDefaultComponent.class).db.getUrl(),
  89. equalTo("jdbc:h2:mem:default"));
  90. }
  91. private Injector createInjector(Map<String, Object> config) {
  92. GuiceApplicationBuilder builder =
  93. new GuiceApplicationLoader().builder(new Context(Environment.simple(), config));
  94. return Guice.createInjector(builder.applicationModule());
  95. }
  96. public static class DefaultComponent {
  97. @Inject Database db;
  98. }
  99. public static class NamedDefaultComponent {
  100. @Inject
  101. @NamedDatabase("default")
  102. Database db;
  103. }
  104. public static class NamedOtherComponent {
  105. @Inject
  106. @NamedDatabase("other")
  107. Database db;
  108. }
  109. }