blob: a47638fbdcbcf3ff6d7e34de5b292614332da1de [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.tamaya.builder;
import org.apache.tamaya.ConfigException;
import org.apache.tamaya.Configuration;
import org.apache.tamaya.builder.spi.*;
import org.hamcrest.CoreMatchers;
import org.hamcrest.Matchers;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
import java.io.IOException;
import java.net.URL;
import static java.util.Arrays.asList;
import static org.apache.tamaya.builder.util.mockito.NotMockedAnswer.NOT_MOCKED_ANSWER;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.CALLS_REAL_METHODS;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
public class PropertySourceBasedConfigurationBuilderTest {
@Test
public void buildCanBuildEmptyConfiguration() {
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
Configuration config = builder.build();
assertThat(config, notNullValue());
}
@Test(expected = IllegalStateException.class)
public void buildCanBeCalledOnlyOnce() {
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
builder.build();
builder.build();
}
/*********************************************************************
* Tests for adding P r o p e r t y S o u r c e s
*/
@Test(expected = NullPointerException.class)
public void addPropertySourcesDoesNotAcceptNullValue() {
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
builder.addPropertySources((PropertySource[])null);
}
@Test(expected = IllegalStateException.class)
public void propertySourceCanNotBeAddedAfterBuildingTheConfiguration() {
PropertySource first = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn("first").when(first).getName();
doReturn(100).when(first).getOrdinal();
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder().addPropertySources(first);
builder.build();
PropertySource second = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn("second").when(first).getName();
builder.addPropertySources(second);
}
@Test
public void singleAddedPropertySourceIsUsed() {
PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn("one").when(source).getName();
doReturn(PropertyValue.of("keyOfA","a", "test")).when(source).get("keyOfA");
doReturn(100).when(source).getOrdinal();
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder().addPropertySources(source);
Configuration config = builder.build();
String valueOfA = config.get("keyOfA");
assertThat(valueOfA, notNullValue());
assertThat(valueOfA, equalTo("a"));
}
@Test
public void twoAddedPropertySourcesAreUsed() {
PropertySource sourceOne = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn("one").when(sourceOne).getName();
doReturn(PropertyValue.of("keyOfA","b", "test")).when(sourceOne).get("keyOfA");
doReturn(10).when(sourceOne).getOrdinal();
PropertySource sourceTwo = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn("two").when(sourceTwo).getName();
doReturn(PropertyValue.of("keyOfA","a", "test")).when(sourceTwo).get("keyOfA");
doReturn(10).when(sourceTwo).getOrdinal();
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder().addPropertySources(sourceOne)
.addPropertySources(sourceTwo);
Configuration config = builder.build();
String valueOfA = config.get("keyOfA");
assertThat(valueOfA, notNullValue());
assertThat(valueOfA, equalTo("a"));
}
@Ignore
@Test(expected = ConfigException.class)
public void twoPropertySourcesSamePrioritySameKey() {
PropertySource sourceOne = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn("one").when(sourceOne).getName();
doReturn("b").when(sourceOne).get("keyOfA");
doReturn(20).when(sourceOne).getOrdinal();
PropertySource sourceTwo = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn("two").when(sourceTwo).getName();
doReturn("a").when(sourceTwo).get("keyOfA");
doReturn(20).when(sourceTwo).getOrdinal();
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder().addPropertySources(sourceOne)
.addPropertySources(sourceTwo);
Configuration config = builder.build();
config.get("keyOfA");
}
@Test
public void twoPropertySourcesDiffPrioritySameKeyLowerAddedFirst() {
PropertySource sourceOne = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn("one").when(sourceOne).getName();
doReturn(PropertyValue.of("keyOfA","b", "test")).when(sourceOne).get("keyOfA");
doReturn(10).when(sourceOne).getOrdinal();
PropertySource sourceTwo = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn("two").when(sourceTwo).getName();
doReturn(PropertyValue.of("keyOfA","a", "test")).when(sourceTwo).get("keyOfA");
doReturn(20).when(sourceTwo).getOrdinal();
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder().addPropertySources(sourceOne)
.addPropertySources(sourceTwo);
Configuration config = builder.build();
String valueOfA = config.get("keyOfA");
assertThat(valueOfA, notNullValue());
assertThat(valueOfA, equalTo("a"));
}
@Test
public void twoPropertySourcesDiffPrioritySameKeyHigherAddedFirst() {
PropertySource sourceOne = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn("one").when(sourceOne).getName();
doReturn(PropertyValue.of("keyOfA","b", "test")).when(sourceOne).get("keyOfA");
doReturn(30).when(sourceOne).getOrdinal();
PropertySource sourceTwo = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn("two").when(sourceTwo).getName();
doReturn(PropertyValue.of("keyOfA","a", "test")).when(sourceTwo).get("keyOfA");
doReturn(20).when(sourceTwo).getOrdinal();
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder().addPropertySources(sourceOne, sourceTwo);
Configuration config = builder.build();
String valueOfA = config.get("keyOfA");
assertThat(valueOfA, notNullValue());
assertThat(valueOfA, equalTo("b"));
}
@Test
public void consecutiveCallsToAddPropertySourceArePossible() {
PropertySource sourceOne = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn("one").when(sourceOne).getName();
doReturn(null).when(sourceOne).get(anyString());
doReturn(PropertyValue.of("b","b", "test")).when(sourceOne).get("b");
doReturn(30).when(sourceOne).getOrdinal();
PropertySource sourceTwo = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn("two").when(sourceTwo).getName();
doReturn(null).when(sourceTwo).get(anyString());
doReturn(PropertyValue.of("a","a", "test")).when(sourceTwo).get("a");
doReturn(30).when(sourceTwo).getOrdinal();
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder().addPropertySources(sourceOne)
.addPropertySources(sourceTwo);
Configuration config = builder.build();
assertThat(config.get("b"), equalTo("b"));
assertThat(config.get("a"), equalTo("a"));
}
@Test
public void addMultiplePropertySourcesWhereOneIsNull() {
PropertySource sourceOne = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn("one").when(sourceOne).getName();
doReturn(null).when(sourceOne).get(anyString());
doReturn(PropertyValue.of("b","b", "test")).when(sourceOne).get("b");
doReturn(30).when(sourceOne).getOrdinal();
PropertySource sourceTwo = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn("two").when(sourceTwo).getName();
doReturn(null).when(sourceTwo).get(anyString());
doReturn(PropertyValue.of("a","a", "test")).when(sourceTwo).get("a");
doReturn(30).when(sourceTwo).getOrdinal();
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder().addPropertySources(sourceOne, null, sourceTwo);
Configuration config = builder.build();
assertThat(config.get("b"), equalTo("b"));
assertThat(config.get("a"), equalTo("a"));
}
/**
* ******************************************************************
* Tests for adding P r o p e r t y C o n v e r t e r
*/
// @Test(expected = NullPointerException.class)
// public void canNotAddNullPropertyConverter() {
// PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
//
// builder.addPropertyConverter(TypeLiteral.of(CustomTypeA.class), null);
// }
//
// @Test(expected = NullPointerException.class)
// public void canNotAddNullTypeLiteralButPropertyConverter() {
// PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
// builder.addPropertyConverter((TypeLiteral)null,
// new PropertyConverter() {
// @Override
// public CustomTypeA convert(final String prop, ConversionContext context) {
// return new CustomTypeA(prop, prop);
// }
// });
// }
//
// @Test
// public void addedPropertyConverterWithTypeLiteralIsUsedByConfiguration() {
// PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER);
//
// doReturn("source").when(source).getName();
// doReturn(PropertyValue.of("key","A", "test")).when(source).get("key");
// doReturn(100).when(source).getOrdinal();
//
// PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
//
// builder.addPropertyConverter(TypeLiteral.of(CustomTypeA.class),
// new PropertyConverter() {
// @Override
// public CustomTypeA convert(final String prop, ConversionContext context) {
// return new CustomTypeA(prop, prop);
// }
// });
// builder.addPropertySources(source);
//
// Configuration config = builder.build();
//
// Object resultRaw = config.get("key", CustomTypeA.class);
//
// assertThat(resultRaw, CoreMatchers.notNullValue());
//
// CustomTypeA result = (CustomTypeA)resultRaw;
//
// assertThat(result.getName(), equalTo("AA"));
// }
//
// @Test
// public void addedPropertyConverterWithClassIsUsedByConfiguration() {
// PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER);
//
// doReturn("source").when(source).getName();
// doReturn(PropertyValue.of("key","A", "test")).when(source).get("key");
// doReturn(100).when(source).getOrdinal();
//
// PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
//
// builder.addPropertyConverter(TypeLiteral.of(CustomTypeA.class),
// new PropertyConverter() {
// @Override
// public CustomTypeA convert(final String prop, ConversionContext context) {
// return new CustomTypeA(prop, prop);
// }
// });
// builder.addPropertySources(source);
//
// Configuration config = builder.build();
//
// Object resultRaw = config.get("key", CustomTypeA.class);
//
// assertThat(resultRaw, CoreMatchers.notNullValue());
//
// CustomTypeA result = (CustomTypeA)resultRaw;
//
// assertThat(result.getName(), equalTo("AA"));
// }
//
// @Test
// public void canGetAndConvertPropertyViaOfMethod() {
// PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER);
//
// doReturn("source").when(source).getName();
// doReturn(PropertyValue.of("key","A", "test")).when(source).get("key");
// doReturn(100).when(source).getOrdinal();
//
// PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
//
// builder.addPropertySources(source);
//
// Configuration config = builder.build();
//
// Object resultRaw = config.get("key", CustomTypeB.class);
//
// assertThat(resultRaw, CoreMatchers.notNullValue());
//
// CustomTypeB result = (CustomTypeB)resultRaw;
//
// assertThat(result.getName(), equalTo("A"));
// }
/*********************************************************************
* Tests for adding P r o p e r t y F i l t e r
*/
@Test(expected = NullPointerException.class)
public void canNotAddNullAsPropertyFilter() {
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
builder.addPropertyFilters((PropertyFilter[])null);
}
@Test
public void canAddNonSPIPropertyFilter() {
PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn(PropertyValue.of("key","M", "test")).when(source).get("key");
doReturn("source").when(source).getName();
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
Configuration config = builder.addPropertySources(source)
.addPropertyFilters(new TestANonSPIPropertyFilter())
.build();
String property = config.get("key");
assertThat(property, CoreMatchers.notNullValue());
assertThat(property, CoreMatchers.containsString("ABC"));
}
@Test
public void canAddNonSPIPropertyFiltersViaConsecutiveCalls() {
PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn(PropertyValue.of("key","M", "test")).when(source).get("key");
doReturn("source").when(source).getName();
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
Configuration config = builder.addPropertySources(source)
.addPropertyFilters(new TestANonSPIPropertyFilter())
.addPropertyFilters(new TestBNonSPIPropertyFilter())
.build();
String property = config.get("key");
assertThat(property, CoreMatchers.notNullValue());
assertThat(property, CoreMatchers.containsString("ABC"));
assertThat(property, CoreMatchers.containsString("XYZ"));
}
@Test
public void canAddMultipleNonSPIPropertyFiltersWhileOneIsNull() {
PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn(PropertyValue.of("key","M", "test")).when(source).get("key");
doReturn("source").when(source).getName();
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
Configuration config = builder.addPropertySources(source)
.addPropertyFilters(new TestANonSPIPropertyFilter(),
null,
new TestBNonSPIPropertyFilter())
.build();
String property = config.get("key");
assertThat(property, CoreMatchers.notNullValue());
assertThat(property, CoreMatchers.containsString("ABC"));
assertThat(property, CoreMatchers.containsString("XYZ"));
}
@Test
public void overhandedNullPropertyFilterIsSafelyHandled() {
PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn(PropertyValue.of("key","M", "test")).when(source).get("key");
doReturn("source").when(source).getName();
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
Configuration config = builder.addPropertySources(source)
.addPropertyFilters((PropertyFilter)null) // The cast is needed!
.addPropertyFilters(new TestBNonSPIPropertyFilter())
.build();
String property = config.get("key");
assertThat(property, CoreMatchers.notNullValue());
assertThat(property, CoreMatchers.containsString("XYZ"));
}
@Test
public void canAddMultipleNonSPIPropertyFilter() {
PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn(PropertyValue.of("key","M", "test")).when(source).get("key");
doReturn("source").when(source).getName();
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
Configuration config = builder.addPropertySources(source)
.addPropertyFilters(new TestANonSPIPropertyFilter(),
new TestBNonSPIPropertyFilter())
.build();
String property = config.get("key");
assertThat(property, CoreMatchers.notNullValue());
assertThat(property, CoreMatchers.containsString("ABC"));
assertThat(property, CoreMatchers.containsString("XYZ"));
}
/*********************************************************************
* Tests for adding
* P r o p e r t y S o u r c e P r o v i d e r s
*/
@Test
public void handlesSafelyPropertyProviderReturningNullInsteadOfPropertySource() {
PropertySourceProvider nullReturning = mock(PropertySourceProvider.class, NOT_MOCKED_ANSWER);
doReturn(asList((PropertySource)null)).when(nullReturning).getPropertySources();
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
Configuration config = builder.addPropertySourceProviders(new TestPropertySourceProviderB(),
nullReturning,
new TestPropertySourceProvider())
.build();
assertThat(config.get("tpsp_a"), Matchers.equalTo("A"));
assertThat(config.get("tpsp_b"), Matchers.equalTo("B"));
assertThat(config.get("tpsp_x"), Matchers.equalTo("X"));
assertThat(config.get("tpsp_y"), Matchers.equalTo("Y"));
verify(nullReturning).getPropertySources();
}
@Test(expected = NullPointerException.class)
public void cannotAddNullAsPropertyProvider() {
new PropertySourceBasedConfigurationBuilder().addPropertySourceProviders((PropertySourceProvider[])null);
}
@Test
public void canAddMultipleNonSPIPropertySourceProviders() {
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
Configuration config = builder.addPropertySourceProviders(new TestPropertySourceProviderB(),
new TestPropertySourceProvider())
.build();
assertThat(config.get("tpsp_a"), Matchers.equalTo("A"));
assertThat(config.get("tpsp_b"), Matchers.equalTo("B"));
assertThat(config.get("tpsp_x"), Matchers.equalTo("X"));
assertThat(config.get("tpsp_y"), Matchers.equalTo("Y"));
}
@Test
public void canAddMultipleNonSPIPropertySourceProvidersWhileOfOfThemIsNull() {
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
Configuration config = builder.addPropertySourceProviders(new TestPropertySourceProviderB(), null,
new TestPropertySourceProvider())
.build();
assertThat(config.get("tpsp_a"), Matchers.equalTo("A"));
assertThat(config.get("tpsp_b"), Matchers.equalTo("B"));
assertThat(config.get("tpsp_x"), Matchers.equalTo("X"));
assertThat(config.get("tpsp_y"), Matchers.equalTo("Y"));
}
/*
* Tests for adding
* P r o p e r t y V a l u e C o m b i n a t i o n P o l i c y
*/
// @todo TAYAMA-60 Write more tests
/*********************************************************************
* Tests for enabling and disabling of automatic loading of
* P r o p e r t y S o u r c e s
*/
// @Test
// public void enablingOfProvidedPropertySourceServiceProvidersIsOk() {
// PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
//
// builder.disableProvidedPropertyConverters()
// .enableProvidedPropertyConverters();
//
// assertThat(builder.isPropertyConverterLoadingEnabled(), is(true));
// }
//
// @Test
// public void disablingOfProvidedPropertySourceServiceProvidersIsOk() {
// PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
//
// builder.enableProvidedPropertyConverters()
// .disableProvidedPropertyConverters();
//
// assertThat(builder.isPropertyConverterLoadingEnabled(), is(false));
// }
@Test(expected = ConfigException.class)
public void loadingOrPropertyConvertersCanBeDisabled() {
PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn("source").when(source).getName();
doReturn(PropertyValue.of("key","A", "test")).when(source).get("key");
doReturn(100).when(source).getOrdinal();
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder().addPropertySources(source);
Configuration config = builder.build();
config.get("key");
}
@Test
public void loadingOfPropertyConvertersCanBeEnabled() {
PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn("source").when(source).getName();
doReturn(PropertyValue.of("key","A", "test")).when(source).get("key");
doReturn(100).when(source).getOrdinal();
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder().addPropertySources(source);
Configuration config = builder.build();
String result = config.get("key");
assertThat(result, notNullValue());
assertThat(result, equalTo("A"));
}
/*********************************************************************
* Tests for enabling and disabling of automatic loading of
* P r o p e r t y S o u r c e s
*/
@Test
public void enablingOfPropertySourceLoadingIsOk() {
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
builder.disableProvidedPropertySources()
.enableProvidedPropertySources();
assertThat(builder.isPropertySourcesLoadingEnabled(), is(true));
}
@Test
public void disablingPropertySourceLoadingIsOk() {
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
builder.enableProvidedPropertySources()
.disableProvidedPropertySources();
assertThat(builder.isPropertySourcesLoadingEnabled(), is(false));
}
@Test
public void loadingOfPropertySourcesCanBeEnabled() {
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
Configuration config = builder.disableProvidedPropertySources()
.enableProvidedPropertySources()
.build();
assertThat(builder.isPropertySourcesLoadingEnabled(), is(true));
assertThat(config.get("tps_a"), Matchers.equalTo("A"));
}
@Test
public void loadingOfPropertySourcesCanBeDisabled() {
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
Configuration config = builder.enableProvidedPropertySources()
.disableProvidedPropertySources()
.build();
assertThat(builder.isPropertySourcesLoadingEnabled(), is(false));
assertThat(config.get("tps_c"), Matchers.nullValue());
}
/*********************************************************************
* Tests for enabling and disabling of automatic loading of
* P r o p e r t y F i l t e r s
*/
@Test
public void enablingOfPropertyFiltersLoadingIsOk() {
PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn("source").when(source).getName();
doReturn(PropertyValue.of("key","A", "test")).when(source).get("key");
doReturn(100).when(source).getOrdinal();
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
Configuration config = builder.disableProvidedPropertyFilters()
.enabledProvidedPropertyFilters()
.addPropertySources(source)
.build();
String property = config.get("key");
assertThat(property, CoreMatchers.notNullValue());
assertThat(property, Matchers.equalTo("AinBerlin"));
}
@Test
public void disablingOfPropertyFiltersLoadingIsOk() {
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
builder.enabledProvidedPropertyFilters()
.disableProvidedPropertyFilters();
assertThat(builder.isPropertyFilterLoadingEnabled(), is(false));
}
@Test
public void loadingOfPropertyFiltersCanBeDisabled() {
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
builder.disableProvidedPropertyFilters()
.enabledProvidedPropertyFilters();
assertThat(builder.isPropertyFilterLoadingEnabled(), is(true));
}
@Test
public void loadingOfPropertyFiltersCanBeEnabled() {
PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER);
doReturn("source").when(source).getName();
doReturn(PropertyValue.of("key","A", "test")).when(source).get("key");
doReturn(100).when(source).getOrdinal();
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
Configuration config = builder.enabledProvidedPropertyFilters()
.disableProvidedPropertyFilters()
.addPropertySources(source)
.build();
String property = config.get("key");
assertThat(property, CoreMatchers.notNullValue());
assertThat(property, Matchers.equalTo("A"));
}
/*********************************************************************
* Tests for enabling and disabling of automatic loading of
* P r o p e r t y S o u r c e P r o v i d e r s
*/
@Test
public void disablingOfPropertySourceProvidersIsOk() {
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
builder.enableProvidedPropertySourceProviders()
.disableProvidedPropertySourceProviders()
.build();
assertThat(builder.isPropertySourceProvidersLoadingEnabled(), is(false));
}
@Test
public void enablingOfPropertySourceProvidersIsOk() {
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
builder.disableProvidedPropertySourceProviders()
.enableProvidedPropertySourceProviders()
.build();
assertThat(builder.isPropertySourceProvidersLoadingEnabled(), is(true));
}
@Test
public void loadingOfPropertySourceProvidersCanBeEnabled() {
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
Configuration config = builder.disableProvidedPropertySourceProviders()
.enableProvidedPropertySourceProviders()
.build();
assertThat(builder.isPropertySourceProvidersLoadingEnabled(), is(true));
assertThat(config.get("tpsp_x"), Matchers.equalTo("X"));
assertThat(config.get("tpsp_y"), Matchers.equalTo("Y"));
}
@Test
public void loadingOfPropertySourceProvidersCanBeDisabled() {
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
Configuration config = builder.enableProvidedPropertySourceProviders()
.disableProvidedPropertySourceProviders()
.build();
assertThat(builder.isPropertySourceProvidersLoadingEnabled(), is(false));
assertThat(config.get("tpsp_x"), nullValue());
assertThat(config.get("tpsp_x"), nullValue());
}
@Test(expected = ConfigException.class)
public void ioExceptionIsTurnedInConfigExceptionWhenLoadingResourceViaURL() throws Exception {
URL resource = this.getClass().getResource("/configfiles/json/simple.json");
assertThat(resource, CoreMatchers.notNullValue());
PropertySourceBasedConfigurationBuilder builder = mock(PropertySourceBasedConfigurationBuilder.class, CALLS_REAL_METHODS);
doThrow(IOException.class).when(builder).getConfigurationDataFromURL(Mockito.eq(resource));
builder.addPropertySource(resource).build();
}
/*********************************************************************
* Tests for loading resources via URL (as String)
*/
@Test(expected = ConfigException.class)
public void tryToLoadOneUnsupportedPropertySourceViaStringURL() {
URL resource = this.getClass().getResource("/configfiles/other/simple.oml");
assertThat(resource, CoreMatchers.notNullValue());
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
builder.addPropertySourceURLs(resource.toString()).build();
}
@Test
public void loadOneJSONPropertySourceViaStringURL() {
URL resource = this.getClass().getResource("/configfiles/json/simple.json");
assertThat(resource, CoreMatchers.notNullValue());
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
Configuration config = builder.addPropertySourceURLs(resource.toString())
.build();
assertThat(config, CoreMatchers.notNullValue());
assertThat(config.get("a"), equalTo("A"));
assertThat(config.get("b"), equalTo("B"));
}
@Test
public void loadMultipleJSONPropertySourceViaStringURL() {
URL first = this.getClass().getResource("/configfiles/json/first.json");
URL second = this.getClass().getResource("/configfiles/json/second.json");
URL third = this.getClass().getResource("/configfiles/json/third.json");
assertThat(first, CoreMatchers.notNullValue());
assertThat(second, CoreMatchers.notNullValue());
assertThat(third, CoreMatchers.notNullValue());
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
PropertySourceBasedConfiguration config = builder.addPropertySourceURLs(first.toString(), second.toString(),
null, third.toString())
.build();
assertThat(config, CoreMatchers.notNullValue());
// from first.json
assertThat(config.get("d"), equalTo("D"));
assertThat(config.get("e"), equalTo("E"));
// from second.json
assertThat(config.get("m"), equalTo("M"));
assertThat(config.get("n"), equalTo("N"));
// from thrid.json
assertThat(config.get("p"), equalTo("P"));
assertThat(config.get("q"), equalTo("Q"));
}
/**
* ******************************************************************
* Tests for loading resources via URL (as URL object)
*/
@Test
public void loadOneJSONPropertySourceViaURL() {
URL resource = this.getClass().getResource("/configfiles/json/simple.json");
assertThat(resource, CoreMatchers.notNullValue());
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
Configuration config = builder.addPropertySource(resource)
.build();
assertThat(config, CoreMatchers.notNullValue());
assertThat(config.get("a"), equalTo("A"));
assertThat(config.get("b"), equalTo("B"));
}
@Test
public void loadMultipleJSONPropertySourceViaURL() {
URL first = this.getClass().getResource("/configfiles/json/first.json");
URL second = this.getClass().getResource("/configfiles/json/second.json");
URL third = this.getClass().getResource("/configfiles/json/third.json");
assertThat(first, CoreMatchers.notNullValue());
assertThat(second, CoreMatchers.notNullValue());
assertThat(third, CoreMatchers.notNullValue());
PropertySourceBasedConfigurationBuilder builder = new PropertySourceBasedConfigurationBuilder();
Configuration config = builder.addPropertySourceURLs(first, second,
null, third)
.build();
assertThat(config, CoreMatchers.notNullValue());
// from first.json
assertThat(config.get("d"), equalTo("D"));
assertThat(config.get("e"), equalTo("E"));
// from second.json
assertThat(config.get("m"), equalTo("M"));
assertThat(config.get("n"), equalTo("N"));
// from thrid.json
assertThat(config.get("p"), equalTo("P"));
assertThat(config.get("q"), equalTo("Q"));
}
}