@Test(groups = "requiresTempFile")
  public void testAliasedBindingBindsCorrectly() {
    final String prefix = "test";
    Map<String, String> properties =
        createDefaultConfigurationProperties(prefix, temporaryFile.getAbsolutePath());

    Injector injector =
        createInjector(
            properties,
            new H2EmbeddedDataSourceModule(prefix, MainBinding.class, AliasBinding.class),
            new Module() {
              @Override
              public void configure(Binder binder) {
                binder.bind(TwoObjectsHolder.class);
              }
            });

    ObjectHolder objectHolder = injector.getInstance(ObjectHolder.class);
    TwoObjectsHolder twoObjectsHolder = injector.getInstance(TwoObjectsHolder.class);

    // Held data source objects should all be of the correct type
    Assertions.assertInstanceOf(twoObjectsHolder.mainDataSource, H2EmbeddedDataSource.class);
    Assertions.assertInstanceOf(twoObjectsHolder.aliasedDataSource, H2EmbeddedDataSource.class);

    // And should all be references to the same object
    assertSame(objectHolder.dataSource, twoObjectsHolder.mainDataSource);
    assertSame(objectHolder.dataSource, twoObjectsHolder.aliasedDataSource);
  }
  @Test(groups = "requiresTempFile")
  public void testCorrectConfigurationPrefix() {
    final String expectedPrefix = "expected";
    final String otherPrefix = "additional";

    final String propertySuffixToTest = ".db.connections.max";
    final int expectedValue = 1234;

    // Required properties for construction
    Map<String, String> properties =
        createDefaultConfigurationProperties(expectedPrefix, temporaryFile.getAbsolutePath());

    // Optional property added with two different prefixes, two different values
    properties.put(otherPrefix + propertySuffixToTest, Integer.toString(expectedValue + 5678));
    properties.put(expectedPrefix + propertySuffixToTest, Integer.toString(expectedValue));

    Injector injector =
        createInjector(
            properties, new H2EmbeddedDataSourceModule(expectedPrefix, MainBinding.class));

    ObjectHolder objectHolder = injector.getInstance(ObjectHolder.class);

    // Make sure we picked up the value with the expected prefix
    Assertions.assertInstanceOf(objectHolder.dataSource, H2EmbeddedDataSource.class);

    H2EmbeddedDataSource created = (H2EmbeddedDataSource) objectHolder.dataSource;

    assertEquals(
        created.getMaxConnections(),
        expectedValue,
        "Property value not loaded from correct prefix");
  }
示例#3
0
 private static <T> void assertAttributesNotEqual(
     ConfigurationMetadata<T> metadata, T actual, T expected) {
   for (AttributeMetadata attribute : metadata.getAttributes().values()) {
     Method getter = attribute.getGetter();
     if (getter == null) {
       continue;
     }
     Object actualAttributeValue = invoke(actual, getter);
     Object expectedAttributeValue = invoke(expected, getter);
     Assertions.assertNotEquals(actualAttributeValue, expectedAttributeValue, attribute.getName());
   }
 }
  @Test(groups = "requiresTempFile")
  public void testObjectBindingFromInjector() {
    final String prefix = "test";
    Map<String, String> properties =
        createDefaultConfigurationProperties(prefix, temporaryFile.getAbsolutePath());

    Injector injector =
        createInjector(properties, new H2EmbeddedDataSourceModule(prefix, MainBinding.class));

    ObjectHolder objectHolder = injector.getInstance(ObjectHolder.class);

    Assertions.assertInstanceOf(objectHolder.dataSource, H2EmbeddedDataSource.class);
  }
示例#5
0
 private void testJsonEquals(String json, Map<String, Object> expectedMap) throws Exception {
   Map<String, Object> jsonMap =
       objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {});
   Assertions.assertEqualsIgnoreOrder(jsonMap.entrySet(), expectedMap.entrySet());
 }