PageRenderTime 32ms CodeModel.GetById 25ms app.highlight 5ms RepoModel.GetById 1ms 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
  5package play.db;
  6
  7import java.util.Map;
  8import javax.inject.Inject;
  9
 10import com.google.common.collect.ImmutableMap;
 11import com.google.inject.Guice;
 12import com.google.inject.Injector;
 13
 14import org.junit.Rule;
 15import org.junit.rules.ExpectedException;
 16import org.junit.Test;
 17
 18import play.ApplicationLoader.Context;
 19import play.Environment;
 20import play.inject.guice.GuiceApplicationBuilder;
 21import play.inject.guice.GuiceApplicationLoader;
 22
 23import static org.hamcrest.CoreMatchers.*;
 24import static org.hamcrest.MatcherAssert.assertThat;
 25
 26public class NamedDatabaseTest {
 27
 28  @Rule public ExpectedException exception = ExpectedException.none();
 29
 30  @Test
 31  public void bindDatabasesByName() {
 32    Map<String, Object> config =
 33        ImmutableMap.of(
 34            "db.default.driver", "org.h2.Driver",
 35            "db.default.url", "jdbc:h2:mem:default",
 36            "db.other.driver", "org.h2.Driver",
 37            "db.other.url", "jdbc:h2:mem:other");
 38    Injector injector = createInjector(config);
 39    assertThat(
 40        injector.getInstance(DefaultComponent.class).db.getUrl(), equalTo("jdbc:h2:mem:default"));
 41    assertThat(
 42        injector.getInstance(NamedDefaultComponent.class).db.getUrl(),
 43        equalTo("jdbc:h2:mem:default"));
 44    assertThat(
 45        injector.getInstance(NamedOtherComponent.class).db.getUrl(), equalTo("jdbc:h2:mem:other"));
 46  }
 47
 48  @Test
 49  public void notBindDefaultDatabaseWithoutConfiguration() {
 50    Map<String, Object> config =
 51        ImmutableMap.of(
 52            "db.other.driver", "org.h2.Driver",
 53            "db.other.url", "jdbc:h2:mem:other");
 54    Injector injector = createInjector(config);
 55    assertThat(
 56        injector.getInstance(NamedOtherComponent.class).db.getUrl(), equalTo("jdbc:h2:mem:other"));
 57    exception.expect(com.google.inject.ConfigurationException.class);
 58    injector.getInstance(DefaultComponent.class);
 59  }
 60
 61  @Test
 62  public void notBindNamedDefaultDatabaseWithoutConfiguration() {
 63    Map<String, Object> config =
 64        ImmutableMap.of(
 65            "db.other.driver", "org.h2.Driver",
 66            "db.other.url", "jdbc:h2:mem:other");
 67    Injector injector = createInjector(config);
 68    assertThat(
 69        injector.getInstance(NamedOtherComponent.class).db.getUrl(), equalTo("jdbc:h2:mem:other"));
 70    exception.expect(com.google.inject.ConfigurationException.class);
 71    injector.getInstance(NamedDefaultComponent.class);
 72  }
 73
 74  @Test
 75  public void allowDefaultDatabaseNameToBeConfigured() {
 76    Map<String, Object> config =
 77        ImmutableMap.of(
 78            "play.db.default", "other",
 79            "db.other.driver", "org.h2.Driver",
 80            "db.other.url", "jdbc:h2:mem:other");
 81    Injector injector = createInjector(config);
 82    assertThat(
 83        injector.getInstance(DefaultComponent.class).db.getUrl(), equalTo("jdbc:h2:mem:other"));
 84    assertThat(
 85        injector.getInstance(NamedOtherComponent.class).db.getUrl(), equalTo("jdbc:h2:mem:other"));
 86    exception.expect(com.google.inject.ConfigurationException.class);
 87    injector.getInstance(NamedDefaultComponent.class);
 88  }
 89
 90  @Test
 91  public void allowDbConfigKeyToBeConfigured() {
 92    Map<String, Object> config =
 93        ImmutableMap.of(
 94            "play.db.config", "databases",
 95            "databases.default.driver", "org.h2.Driver",
 96            "databases.default.url", "jdbc:h2:mem:default");
 97    Injector injector = createInjector(config);
 98    assertThat(
 99        injector.getInstance(DefaultComponent.class).db.getUrl(), equalTo("jdbc:h2:mem:default"));
100    assertThat(
101        injector.getInstance(NamedDefaultComponent.class).db.getUrl(),
102        equalTo("jdbc:h2:mem:default"));
103  }
104
105  private Injector createInjector(Map<String, Object> config) {
106    GuiceApplicationBuilder builder =
107        new GuiceApplicationLoader().builder(new Context(Environment.simple(), config));
108    return Guice.createInjector(builder.applicationModule());
109  }
110
111  public static class DefaultComponent {
112    @Inject Database db;
113  }
114
115  public static class NamedDefaultComponent {
116    @Inject
117    @NamedDatabase("default")
118    Database db;
119  }
120
121  public static class NamedOtherComponent {
122    @Inject
123    @NamedDatabase("other")
124    Database db;
125  }
126}