You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by pl...@apache.org on 2015/03/14 00:57:22 UTC
[1/3] incubator-tamaya git commit: TAMAYA-60 Removed the modules
package for the package hierarchy.
Repository: incubator-tamaya
Updated Branches:
refs/heads/master f7480b250 -> 812b11eac
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/modules/builder/ConfigurationBuilderTest.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/ConfigurationBuilderTest.java b/modules/builder/src/test/java/org/apache/tamaya/modules/builder/ConfigurationBuilderTest.java
deleted file mode 100644
index a5062bd..0000000
--- a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/ConfigurationBuilderTest.java
+++ /dev/null
@@ -1,896 +0,0 @@
-/*
- * 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.modules.builder;
-
-import org.apache.tamaya.ConfigException;
-import org.apache.tamaya.Configuration;
-import org.apache.tamaya.TypeLiteral;
-import org.apache.tamaya.modules.builder.util.types.CustomTypeA;
-import org.apache.tamaya.modules.builder.util.types.CustomTypeB;
-import org.apache.tamaya.modules.builder.util.types.CustomTypeC;
-import org.apache.tamaya.spi.PropertyFilter;
-import org.apache.tamaya.spi.PropertySource;
-import org.apache.tamaya.spi.PropertySourceProvider;
-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.modules.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 ConfigurationBuilderTest {
-
- @Test
- public void buildCanBuildEmptyConfiguration() {
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- Configuration config = builder.build();
-
- assertThat(config, notNullValue());
- }
-
- @Test(expected = IllegalStateException.class)
- public void buildCanBeCalledOnlyOnce() {
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- 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() {
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- 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();
-
- ConfigurationBuilder builder = new ConfigurationBuilder().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("a").when(source).get("keyOfA");
- doReturn(100).when(source).getOrdinal();
-
- ConfigurationBuilder builder = new ConfigurationBuilder().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("b").when(sourceOne).get("keyOfA");
- doReturn(10).when(sourceOne).getOrdinal();
-
- PropertySource sourceTwo = mock(PropertySource.class, NOT_MOCKED_ANSWER);
- doReturn("two").when(sourceTwo).getName();
- doReturn("a").when(sourceTwo).get("keyOfA");
- doReturn(10).when(sourceTwo).getOrdinal();
-
- ConfigurationBuilder builder = new ConfigurationBuilder().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();
-
- ConfigurationBuilder builder = new ConfigurationBuilder().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("b").when(sourceOne).get("keyOfA");
- doReturn(10).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();
-
- ConfigurationBuilder builder = new ConfigurationBuilder().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("b").when(sourceOne).get("keyOfA");
- doReturn(30).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();
-
- ConfigurationBuilder builder = new ConfigurationBuilder().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("b").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("a").when(sourceTwo).get("a");
- doReturn(30).when(sourceTwo).getOrdinal();
-
- ConfigurationBuilder builder = new ConfigurationBuilder().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("b").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("a").when(sourceTwo).get("a");
- doReturn(30).when(sourceTwo).getOrdinal();
-
- ConfigurationBuilder builder = new ConfigurationBuilder().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() {
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- builder.addPropertyConverter(TypeLiteral.of(CustomTypeA.class), null);
- }
-
- @Test(expected = NullPointerException.class)
- public void canNotAddNullTypeLiteralButPropertyConverter() {
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- builder.addPropertyConverter((TypeLiteral<CustomTypeA>)null,
- prop -> new CustomTypeA(prop, prop));
- }
-
- @Test
- public void addedPropertyConverterWithTypeLiteralIsUsedByConfiguration() {
- PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER);
-
- doReturn("source").when(source).getName();
- doReturn("A").when(source).get("key");
- doReturn(100).when(source).getOrdinal();
-
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- builder.addPropertyConverter(TypeLiteral.of(CustomTypeA.class),
- prop -> new CustomTypeA(prop, prop))
- .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("A").when(source).get("key");
- doReturn(100).when(source).getOrdinal();
-
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- builder.addPropertyConverter(CustomTypeA.class,
- prop -> new CustomTypeA(prop, prop))
- .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("A").when(source).get("key");
- doReturn(100).when(source).getOrdinal();
-
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- 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() {
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- builder.addPropertyFilters(null);
- }
-
- @Test
- public void canAddNonSPIPropertyFilter() {
- PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER);
-
- doReturn("M").when(source).get("key");
- doReturn("source").when(source).getName();
-
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- Configuration config = builder.addPropertySources(source)
- .addPropertyFilters(new TestNonSPIPropertyFilterA())
- .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("M").when(source).get("key");
- doReturn("source").when(source).getName();
-
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- Configuration config = builder.addPropertySources(source)
- .addPropertyFilters(new TestNonSPIPropertyFilterA())
- .addPropertyFilters(new TestNonSPIPropertyFilterB())
- .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("M").when(source).get("key");
- doReturn("source").when(source).getName();
-
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- Configuration config = builder.addPropertySources(source)
- .addPropertyFilters(new TestNonSPIPropertyFilterA(),
- null,
- new TestNonSPIPropertyFilterB())
- .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("M").when(source).get("key");
- doReturn("source").when(source).getName();
-
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- Configuration config = builder.addPropertySources(source)
- .addPropertyFilters((PropertyFilter)null) // The cast is needed!
- .addPropertyFilters(new TestNonSPIPropertyFilterB())
- .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("M").when(source).get("key");
- doReturn("source").when(source).getName();
-
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- Configuration config = builder.addPropertySources(source)
- .addPropertyFilters(new TestNonSPIPropertyFilterA(),
- new TestNonSPIPropertyFilterB())
- .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();
-
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- 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 ConfigurationBuilder().addPropertySourceProviders(null);
- }
-
- @Test
- public void canAddMultipleNonSPIPropertySourceProviders() {
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- 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() {
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- 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() {
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- builder.disableProvidedPropertyConverters()
- .enableProvidedPropertyConverters();
-
- assertThat(builder.isPropertyConverterLoadingEnabled(), is(true));
- }
-
- @Test
- public void disablingOfProvidedPropertySourceServiceProvidersIsOk() {
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- 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("A").when(source).get("key");
- doReturn(100).when(source).getOrdinal();
-
- ConfigurationBuilder builder = new ConfigurationBuilder().addPropertySources(source)
- .enableProvidedPropertyConverters()
- .disableProvidedPropertyConverters();
-
- Configuration config = builder.build();
-
- config.get("key", CustomTypeC.class);
- }
-
- @Test
- public void loadingOfPropertyConvertersCanBeEnabled() {
- PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER);
-
- doReturn("source").when(source).getName();
- doReturn("A").when(source).get("key");
- doReturn(100).when(source).getOrdinal();
-
- ConfigurationBuilder builder = new ConfigurationBuilder().addPropertySources(source)
- .disableProvidedPropertyConverters()
- .enableProvidedPropertyConverters();
-
- Configuration config = builder.build();
-
- CustomTypeC result = config.get("key", CustomTypeC.class);
-
- assertThat(result, notNullValue());
- assertThat(result.getValue(), 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() {
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- builder.disableProvidedPropertySources()
- .enableProvidedPropertySources();
-
- assertThat(builder.isPropertySourcesLoadingEnabled(), is(true));
- }
-
- @Test
- public void disablingPropertySourceLoadingIsOk() {
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- builder.enableProvidedPropertySources()
- .disableProvidedPropertySources();
-
- assertThat(builder.isPropertySourcesLoadingEnabled(), is(false));
- }
-
- @Test
- public void loadingOfPropertySourcesCanBeEnabled() {
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- Configuration config = builder.disableProvidedPropertySources()
- .enableProvidedPropertySources()
- .build();
-
-
- assertThat(builder.isPropertySourcesLoadingEnabled(), is(true));
- assertThat(config.get("tps_a"), Matchers.equalTo("A"));
- }
-
- @Test
- public void loadingOfPropertySourcesCanBeDisabled() {
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- 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("A").when(source).get("key");
- doReturn(100).when(source).getOrdinal();
-
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- 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() {
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- builder.enabledProvidedPropertyFilters()
- .disableProvidedPropertyFilters();
-
- assertThat(builder.isPropertyFilterLoadingEnabled(), is(false));
- }
-
- @Test
- public void loadingOfPropertyFiltersCanBeDisabled() {
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- 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("A").when(source).get("key");
- doReturn(100).when(source).getOrdinal();
-
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- 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() {
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- builder.enableProvidedPropertySourceProviders()
- .disableProvidedPropertySourceProviders()
- .build();
-
- assertThat(builder.isPropertySourceProvidersLoadingEnabled(), is(false));
- }
-
- @Test
- public void enablingOfPropertySourceProvidersIsOk() {
-
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- builder.disableProvidedPropertySourceProviders()
- .enableProvidedPropertySourceProviders()
- .build();
-
- assertThat(builder.isPropertySourceProvidersLoadingEnabled(), is(true));
- }
-
- @Test
- public void loadingOfPropertySourceProvidersCanBeEnabled() {
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- 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() {
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- 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());
-
- ConfigurationBuilder builder = mock(ConfigurationBuilder.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());
-
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- builder.addPropertySource(resource.toString()).build();
- }
-
- @Test
- public void loadOneJSONPropertySourceViaStringURL() {
- URL resource = this.getClass().getResource("/configfiles/json/simple.json");
-
- assertThat(resource, CoreMatchers.notNullValue());
-
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- Configuration config = builder.addPropertySource(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());
-
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- Configuration config = builder.addPropertySource(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());
-
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- 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());
-
- ConfigurationBuilder builder = new ConfigurationBuilder();
-
- Configuration config = builder.addPropertySource(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"));
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestNonSPIPropertyFilterA.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestNonSPIPropertyFilterA.java b/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestNonSPIPropertyFilterA.java
deleted file mode 100644
index 7d899dc..0000000
--- a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestNonSPIPropertyFilterA.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * 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.modules.builder;
-
-import org.apache.tamaya.spi.PropertyFilter;
-
-public class TestNonSPIPropertyFilterA implements PropertyFilter {
- @Override
- public String filterProperty(String key, String value) {
- String result = value;
-
- if (!result.contains(("ABC"))) {
- result = value + "ABC";
- }
-
- return result;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestNonSPIPropertyFilterB.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestNonSPIPropertyFilterB.java b/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestNonSPIPropertyFilterB.java
deleted file mode 100644
index d7f7a40..0000000
--- a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestNonSPIPropertyFilterB.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * 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.modules.builder;
-
-import org.apache.tamaya.spi.PropertyFilter;
-
-public class TestNonSPIPropertyFilterB implements PropertyFilter {
- @Override
- public String filterProperty(String key, String value) {
- String result = value;
-
- if (!result.contains(("XYZ"))) {
- result = value + "XYZ";
- }
-
- return result;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestPropertyFilter.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestPropertyFilter.java b/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestPropertyFilter.java
deleted file mode 100644
index 6e9f5ae..0000000
--- a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestPropertyFilter.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * 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.modules.builder;
-
-import org.apache.tamaya.spi.PropertyFilter;
-
-public class TestPropertyFilter implements PropertyFilter {
- @Override
- public String filterProperty(String key, String value) {
- String result = value;
-
- if (!result.contains(("inBerlin"))) {
- result = value + "inBerlin";
- }
-
- return result;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestPropertySource.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestPropertySource.java b/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestPropertySource.java
deleted file mode 100644
index 8c9fc00..0000000
--- a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestPropertySource.java
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * 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.modules.builder;
-
-import org.apache.tamaya.spi.PropertySource;
-
-import java.util.Collections;
-import java.util.Hashtable;
-import java.util.Map;
-
-public class TestPropertySource
- implements PropertySource
-{
- private Map<String, String> properties;
-
- {
- properties = new Hashtable<>(3);
- properties.put("tps_a", "A");
- properties.put("tps_b", "B");
- properties.put("tps_c", "C");
- }
-
- @Override
- public int getOrdinal() {
- return 456;
- }
-
- @Override
- public String getName() {
- return "TestPropertySource";
- }
-
- @Override
- public String get(String key) {
- return getProperties().get(key);
- }
-
- @Override
- public Map<String, String> getProperties() {
- return Collections.unmodifiableMap(properties);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestPropertySourceProvider.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestPropertySourceProvider.java b/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestPropertySourceProvider.java
deleted file mode 100644
index a753c25..0000000
--- a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestPropertySourceProvider.java
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * 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.modules.builder;
-
-import org.apache.tamaya.spi.PropertySource;
-import org.apache.tamaya.spi.PropertySourceProvider;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Map;
-
-public class TestPropertySourceProvider
- implements PropertySourceProvider
-{
- @Override
- public Collection<PropertySource> getPropertySources() {
- ArrayList<PropertySource> sources = new ArrayList<>(2);
-
- sources.add(new XProvidingPropertySource());
- sources.add(new YProvidingPropertySource());
-
- return sources;
- }
-
- private class YProvidingPropertySource implements PropertySource {
- private Map<String, String> props = Collections.singletonMap("tpsp_x", "X");
-
- @Override
- public int getOrdinal() {
- return 100;
- }
-
- @Override
- public String getName() {
- return "YProvidingPropertySource";
- }
-
- @Override
- public String get(String key) {
- return getProperties().get(key);
- }
-
- @Override
- public Map<String, String> getProperties() {
- return props;
- }
- }
-
- private class XProvidingPropertySource implements PropertySource {
- private Map<String, String> props = Collections.singletonMap("tpsp_y", "Y");
-
- @Override
- public Map<String, String> getProperties() {
- return props;
- }
-
- @Override
- public String get(String key) {
- return getProperties().get(key);
- }
-
- @Override
- public int getOrdinal() {
- return 100;
- }
-
- @Override
- public String getName() {
- return "XProvidingPropertySource";
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestPropertySourceProviderB.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestPropertySourceProviderB.java b/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestPropertySourceProviderB.java
deleted file mode 100644
index 890b166..0000000
--- a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/TestPropertySourceProviderB.java
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * 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.modules.builder;
-
-import org.apache.tamaya.spi.PropertySource;
-import org.apache.tamaya.spi.PropertySourceProvider;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Map;
-
-public class TestPropertySourceProviderB
- implements PropertySourceProvider
-{
- @Override
- public Collection<PropertySource> getPropertySources() {
- ArrayList<PropertySource> sources = new ArrayList<>(2);
-
- sources.add(new AProvidingPropertySource());
- sources.add(new BProvidingPropertySource());
-
- return sources;
- }
-
- private class BProvidingPropertySource implements PropertySource {
- private Map<String, String> props = Collections.singletonMap("tpsp_b", "B");
-
- @Override
- public int getOrdinal() {
- return 100;
- }
-
- @Override
- public String getName() {
- return "BProvidingPropertySource";
- }
-
- @Override
- public String get(String key) {
- return getProperties().get(key);
- }
-
- @Override
- public Map<String, String> getProperties() {
- return props;
- }
- }
-
- private class AProvidingPropertySource implements PropertySource {
- private Map<String, String> props = Collections.singletonMap("tpsp_a", "A");
-
- @Override
- public Map<String, String> getProperties() {
- return props;
- }
-
- @Override
- public String get(String key) {
- return getProperties().get(key);
- }
-
- @Override
- public int getOrdinal() {
- return 100;
- }
-
- @Override
- public String getName() {
- return "AProvidingPropertySource";
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/modules/builder/util/mockito/NotMockedAnswer.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/util/mockito/NotMockedAnswer.java b/modules/builder/src/test/java/org/apache/tamaya/modules/builder/util/mockito/NotMockedAnswer.java
deleted file mode 100644
index d754305..0000000
--- a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/util/mockito/NotMockedAnswer.java
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * 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.modules.builder.util.mockito;
-
-import org.mockito.exceptions.base.MockitoException;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
-
-import java.io.Serializable;
-
-// @todo This is a duplicated class
-public class NotMockedAnswer implements Answer<Object>, Serializable {
- public final static NotMockedAnswer NOT_MOCKED_ANSWER = new NotMockedAnswer();
-
- private NotMockedAnswer() {
- }
-
- @Override
- public Object answer(InvocationOnMock invocation) throws Throwable {
- StringBuilder msgBuilder = new StringBuilder();
-
- msgBuilder.append("Invocation of method not mocked: ")
- .append(invocation.getMethod().toGenericString());
-
- if (invocation.getArguments().length > 0) {
- msgBuilder.append(" Supplied arguments: ");
-
- for (int i = 0; i < invocation.getArguments().length; i++) {
- msgBuilder.append(invocation.getArguments()[i]);
-
- if (i - 1 < invocation.getArguments().length) {
- msgBuilder.append(", ");
- }
- }
- }
-
- throw new MockitoException(msgBuilder.toString());
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/modules/builder/util/types/CustomTypeA.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/util/types/CustomTypeA.java b/modules/builder/src/test/java/org/apache/tamaya/modules/builder/util/types/CustomTypeA.java
deleted file mode 100644
index 378a131..0000000
--- a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/util/types/CustomTypeA.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * 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.modules.builder.util.types;
-
-/**
- * Custom type with two argument constructor.
- */
-public class CustomTypeA {
- private String name;
-
- public CustomTypeA(String name, String other) {
- this.name = name + other;
- }
-
- public String getName() {
- return name;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/modules/builder/util/types/CustomTypeB.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/util/types/CustomTypeB.java b/modules/builder/src/test/java/org/apache/tamaya/modules/builder/util/types/CustomTypeB.java
deleted file mode 100644
index a00abda..0000000
--- a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/util/types/CustomTypeB.java
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * 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.modules.builder.util.types;
-
-/**
- * Custom type with factory method
- * {@link org.apache.tamaya.builder.util.types.CustomTypeB#of(String)}
- */
-public class CustomTypeB {
- private String name;
-
- private CustomTypeB(String value) {
- this.name = value;
- }
-
- public String getName() {
- return name;
- }
-
- public static CustomTypeB of(String source) {
- return new CustomTypeB(source);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/modules/builder/util/types/CustomTypeC.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/util/types/CustomTypeC.java b/modules/builder/src/test/java/org/apache/tamaya/modules/builder/util/types/CustomTypeC.java
deleted file mode 100644
index a75af2b..0000000
--- a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/util/types/CustomTypeC.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * 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.modules.builder.util.types;
-
-public class CustomTypeC {
- private String value;
-
-
- public CustomTypeC(String in, @SuppressWarnings("unused") int iHideThisConstructorForTamaya) {
- value = in;
- }
-
- public String getValue() {
- return value;
- }
-
- public static CustomTypeC produceFrom(String in) {
- return new CustomTypeC(in, -1);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/modules/builder/util/types/CustomTypeCPropertyConverter.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/util/types/CustomTypeCPropertyConverter.java b/modules/builder/src/test/java/org/apache/tamaya/modules/builder/util/types/CustomTypeCPropertyConverter.java
deleted file mode 100644
index ede9fda..0000000
--- a/modules/builder/src/test/java/org/apache/tamaya/modules/builder/util/types/CustomTypeCPropertyConverter.java
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * 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.modules.builder.util.types;
-
-import org.apache.tamaya.PropertyConverter;
-
-public class CustomTypeCPropertyConverter implements PropertyConverter<org.apache.tamaya.modules.builder.util.types.CustomTypeC> {
- @Override
- public org.apache.tamaya.modules.builder.util.types.CustomTypeC convert(String value) {
- return org.apache.tamaya.modules.builder.util.types.CustomTypeC.produceFrom(value);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.PropertyConverter
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.PropertyConverter b/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.PropertyConverter
index 9510492..b9e0d44 100644
--- a/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.PropertyConverter
+++ b/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.PropertyConverter
@@ -16,4 +16,4 @@
# specific language governing permissions and limitations
# under the License.
#
-org.apache.tamaya.modules.builder.util.types.CustomTypeCPropertyConverter
\ No newline at end of file
+org.apache.tamaya.builder.util.types.CustomTypeCPropertyConverter
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyFilter
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyFilter b/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyFilter
index d69635f..f35e9c5 100644
--- a/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyFilter
+++ b/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyFilter
@@ -16,4 +16,4 @@
# specific language governing permissions and limitations
# under the License.
#
-org.apache.tamaya.modules.builder.TestPropertyFilter
+org.apache.tamaya.builder.TestPropertyFilter
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource b/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource
index c0cc7f7..64d1b0d 100644
--- a/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource
+++ b/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource
@@ -16,4 +16,4 @@
# specific language governing permissions and limitations
# under the License.
#
-org.apache.tamaya.modules.builder.TestPropertySource
+org.apache.tamaya.builder.TestPropertySource
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider b/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider
index 12aa505..9a19ea0 100644
--- a/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider
+++ b/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider
@@ -16,4 +16,4 @@
# specific language governing permissions and limitations
# under the License.
#
-org.apache.tamaya.modules.builder.TestPropertySourceProvider
+org.apache.tamaya.builder.TestPropertySourceProvider
[2/3] incubator-tamaya git commit: TAMAYA-60 Removed the modules
package for the package hierarchy.
Posted by pl...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/builder/ConfigurationBuilderTest.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/builder/ConfigurationBuilderTest.java b/modules/builder/src/test/java/org/apache/tamaya/builder/ConfigurationBuilderTest.java
new file mode 100644
index 0000000..5f33c24
--- /dev/null
+++ b/modules/builder/src/test/java/org/apache/tamaya/builder/ConfigurationBuilderTest.java
@@ -0,0 +1,896 @@
+/*
+ * 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.TypeLiteral;
+import org.apache.tamaya.builder.util.types.CustomTypeA;
+import org.apache.tamaya.builder.util.types.CustomTypeB;
+import org.apache.tamaya.builder.util.types.CustomTypeC;
+import org.apache.tamaya.spi.PropertyFilter;
+import org.apache.tamaya.spi.PropertySource;
+import org.apache.tamaya.spi.PropertySourceProvider;
+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 ConfigurationBuilderTest {
+
+ @Test
+ public void buildCanBuildEmptyConfiguration() {
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ Configuration config = builder.build();
+
+ assertThat(config, notNullValue());
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void buildCanBeCalledOnlyOnce() {
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ 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() {
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ 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();
+
+ ConfigurationBuilder builder = new ConfigurationBuilder().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("a").when(source).get("keyOfA");
+ doReturn(100).when(source).getOrdinal();
+
+ ConfigurationBuilder builder = new ConfigurationBuilder().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("b").when(sourceOne).get("keyOfA");
+ doReturn(10).when(sourceOne).getOrdinal();
+
+ PropertySource sourceTwo = mock(PropertySource.class, NOT_MOCKED_ANSWER);
+ doReturn("two").when(sourceTwo).getName();
+ doReturn("a").when(sourceTwo).get("keyOfA");
+ doReturn(10).when(sourceTwo).getOrdinal();
+
+ ConfigurationBuilder builder = new ConfigurationBuilder().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();
+
+ ConfigurationBuilder builder = new ConfigurationBuilder().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("b").when(sourceOne).get("keyOfA");
+ doReturn(10).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();
+
+ ConfigurationBuilder builder = new ConfigurationBuilder().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("b").when(sourceOne).get("keyOfA");
+ doReturn(30).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();
+
+ ConfigurationBuilder builder = new ConfigurationBuilder().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("b").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("a").when(sourceTwo).get("a");
+ doReturn(30).when(sourceTwo).getOrdinal();
+
+ ConfigurationBuilder builder = new ConfigurationBuilder().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("b").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("a").when(sourceTwo).get("a");
+ doReturn(30).when(sourceTwo).getOrdinal();
+
+ ConfigurationBuilder builder = new ConfigurationBuilder().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() {
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ builder.addPropertyConverter(TypeLiteral.of(CustomTypeA.class), null);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void canNotAddNullTypeLiteralButPropertyConverter() {
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ builder.addPropertyConverter((TypeLiteral<CustomTypeA>)null,
+ prop -> new CustomTypeA(prop, prop));
+ }
+
+ @Test
+ public void addedPropertyConverterWithTypeLiteralIsUsedByConfiguration() {
+ PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER);
+
+ doReturn("source").when(source).getName();
+ doReturn("A").when(source).get("key");
+ doReturn(100).when(source).getOrdinal();
+
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ builder.addPropertyConverter(TypeLiteral.of(CustomTypeA.class),
+ prop -> new CustomTypeA(prop, prop))
+ .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("A").when(source).get("key");
+ doReturn(100).when(source).getOrdinal();
+
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ builder.addPropertyConverter(CustomTypeA.class,
+ prop -> new CustomTypeA(prop, prop))
+ .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("A").when(source).get("key");
+ doReturn(100).when(source).getOrdinal();
+
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ 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() {
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ builder.addPropertyFilters(null);
+ }
+
+ @Test
+ public void canAddNonSPIPropertyFilter() {
+ PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER);
+
+ doReturn("M").when(source).get("key");
+ doReturn("source").when(source).getName();
+
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ Configuration config = builder.addPropertySources(source)
+ .addPropertyFilters(new TestNonSPIPropertyFilterA())
+ .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("M").when(source).get("key");
+ doReturn("source").when(source).getName();
+
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ Configuration config = builder.addPropertySources(source)
+ .addPropertyFilters(new TestNonSPIPropertyFilterA())
+ .addPropertyFilters(new TestNonSPIPropertyFilterB())
+ .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("M").when(source).get("key");
+ doReturn("source").when(source).getName();
+
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ Configuration config = builder.addPropertySources(source)
+ .addPropertyFilters(new TestNonSPIPropertyFilterA(),
+ null,
+ new TestNonSPIPropertyFilterB())
+ .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("M").when(source).get("key");
+ doReturn("source").when(source).getName();
+
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ Configuration config = builder.addPropertySources(source)
+ .addPropertyFilters((PropertyFilter)null) // The cast is needed!
+ .addPropertyFilters(new TestNonSPIPropertyFilterB())
+ .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("M").when(source).get("key");
+ doReturn("source").when(source).getName();
+
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ Configuration config = builder.addPropertySources(source)
+ .addPropertyFilters(new TestNonSPIPropertyFilterA(),
+ new TestNonSPIPropertyFilterB())
+ .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();
+
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ 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 ConfigurationBuilder().addPropertySourceProviders(null);
+ }
+
+ @Test
+ public void canAddMultipleNonSPIPropertySourceProviders() {
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ 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() {
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ 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() {
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ builder.disableProvidedPropertyConverters()
+ .enableProvidedPropertyConverters();
+
+ assertThat(builder.isPropertyConverterLoadingEnabled(), is(true));
+ }
+
+ @Test
+ public void disablingOfProvidedPropertySourceServiceProvidersIsOk() {
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ 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("A").when(source).get("key");
+ doReturn(100).when(source).getOrdinal();
+
+ ConfigurationBuilder builder = new ConfigurationBuilder().addPropertySources(source)
+ .enableProvidedPropertyConverters()
+ .disableProvidedPropertyConverters();
+
+ Configuration config = builder.build();
+
+ config.get("key", CustomTypeC.class);
+ }
+
+ @Test
+ public void loadingOfPropertyConvertersCanBeEnabled() {
+ PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER);
+
+ doReturn("source").when(source).getName();
+ doReturn("A").when(source).get("key");
+ doReturn(100).when(source).getOrdinal();
+
+ ConfigurationBuilder builder = new ConfigurationBuilder().addPropertySources(source)
+ .disableProvidedPropertyConverters()
+ .enableProvidedPropertyConverters();
+
+ Configuration config = builder.build();
+
+ CustomTypeC result = config.get("key", CustomTypeC.class);
+
+ assertThat(result, notNullValue());
+ assertThat(result.getValue(), 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() {
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ builder.disableProvidedPropertySources()
+ .enableProvidedPropertySources();
+
+ assertThat(builder.isPropertySourcesLoadingEnabled(), is(true));
+ }
+
+ @Test
+ public void disablingPropertySourceLoadingIsOk() {
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ builder.enableProvidedPropertySources()
+ .disableProvidedPropertySources();
+
+ assertThat(builder.isPropertySourcesLoadingEnabled(), is(false));
+ }
+
+ @Test
+ public void loadingOfPropertySourcesCanBeEnabled() {
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ Configuration config = builder.disableProvidedPropertySources()
+ .enableProvidedPropertySources()
+ .build();
+
+
+ assertThat(builder.isPropertySourcesLoadingEnabled(), is(true));
+ assertThat(config.get("tps_a"), Matchers.equalTo("A"));
+ }
+
+ @Test
+ public void loadingOfPropertySourcesCanBeDisabled() {
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ 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("A").when(source).get("key");
+ doReturn(100).when(source).getOrdinal();
+
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ 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() {
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ builder.enabledProvidedPropertyFilters()
+ .disableProvidedPropertyFilters();
+
+ assertThat(builder.isPropertyFilterLoadingEnabled(), is(false));
+ }
+
+ @Test
+ public void loadingOfPropertyFiltersCanBeDisabled() {
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ 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("A").when(source).get("key");
+ doReturn(100).when(source).getOrdinal();
+
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ 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() {
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ builder.enableProvidedPropertySourceProviders()
+ .disableProvidedPropertySourceProviders()
+ .build();
+
+ assertThat(builder.isPropertySourceProvidersLoadingEnabled(), is(false));
+ }
+
+ @Test
+ public void enablingOfPropertySourceProvidersIsOk() {
+
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ builder.disableProvidedPropertySourceProviders()
+ .enableProvidedPropertySourceProviders()
+ .build();
+
+ assertThat(builder.isPropertySourceProvidersLoadingEnabled(), is(true));
+ }
+
+ @Test
+ public void loadingOfPropertySourceProvidersCanBeEnabled() {
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ 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() {
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ 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());
+
+ ConfigurationBuilder builder = mock(ConfigurationBuilder.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());
+
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ builder.addPropertySource(resource.toString()).build();
+ }
+
+ @Test
+ public void loadOneJSONPropertySourceViaStringURL() {
+ URL resource = this.getClass().getResource("/configfiles/json/simple.json");
+
+ assertThat(resource, CoreMatchers.notNullValue());
+
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ Configuration config = builder.addPropertySource(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());
+
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ Configuration config = builder.addPropertySource(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());
+
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ 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());
+
+ ConfigurationBuilder builder = new ConfigurationBuilder();
+
+ Configuration config = builder.addPropertySource(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"));
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/builder/TestNonSPIPropertyFilterA.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/builder/TestNonSPIPropertyFilterA.java b/modules/builder/src/test/java/org/apache/tamaya/builder/TestNonSPIPropertyFilterA.java
new file mode 100644
index 0000000..4b9ae72
--- /dev/null
+++ b/modules/builder/src/test/java/org/apache/tamaya/builder/TestNonSPIPropertyFilterA.java
@@ -0,0 +1,34 @@
+/*
+ * 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.spi.PropertyFilter;
+
+public class TestNonSPIPropertyFilterA implements PropertyFilter {
+ @Override
+ public String filterProperty(String key, String value) {
+ String result = value;
+
+ if (!result.contains(("ABC"))) {
+ result = value + "ABC";
+ }
+
+ return result;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/builder/TestNonSPIPropertyFilterB.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/builder/TestNonSPIPropertyFilterB.java b/modules/builder/src/test/java/org/apache/tamaya/builder/TestNonSPIPropertyFilterB.java
new file mode 100644
index 0000000..95112b0
--- /dev/null
+++ b/modules/builder/src/test/java/org/apache/tamaya/builder/TestNonSPIPropertyFilterB.java
@@ -0,0 +1,34 @@
+/*
+ * 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.spi.PropertyFilter;
+
+public class TestNonSPIPropertyFilterB implements PropertyFilter {
+ @Override
+ public String filterProperty(String key, String value) {
+ String result = value;
+
+ if (!result.contains(("XYZ"))) {
+ result = value + "XYZ";
+ }
+
+ return result;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertyFilter.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertyFilter.java b/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertyFilter.java
new file mode 100644
index 0000000..80c4cb3
--- /dev/null
+++ b/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertyFilter.java
@@ -0,0 +1,34 @@
+/*
+ * 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.spi.PropertyFilter;
+
+public class TestPropertyFilter implements PropertyFilter {
+ @Override
+ public String filterProperty(String key, String value) {
+ String result = value;
+
+ if (!result.contains(("inBerlin"))) {
+ result = value + "inBerlin";
+ }
+
+ return result;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertySource.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertySource.java b/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertySource.java
new file mode 100644
index 0000000..54deca3
--- /dev/null
+++ b/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertySource.java
@@ -0,0 +1,58 @@
+/*
+ * 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.spi.PropertySource;
+
+import java.util.Collections;
+import java.util.Hashtable;
+import java.util.Map;
+
+public class TestPropertySource
+ implements PropertySource
+{
+ private Map<String, String> properties;
+
+ {
+ properties = new Hashtable<>(3);
+ properties.put("tps_a", "A");
+ properties.put("tps_b", "B");
+ properties.put("tps_c", "C");
+ }
+
+ @Override
+ public int getOrdinal() {
+ return 456;
+ }
+
+ @Override
+ public String getName() {
+ return "TestPropertySource";
+ }
+
+ @Override
+ public String get(String key) {
+ return getProperties().get(key);
+ }
+
+ @Override
+ public Map<String, String> getProperties() {
+ return Collections.unmodifiableMap(properties);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertySourceProvider.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertySourceProvider.java b/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertySourceProvider.java
new file mode 100644
index 0000000..4e7db79
--- /dev/null
+++ b/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertySourceProvider.java
@@ -0,0 +1,89 @@
+/*
+ * 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.spi.PropertySource;
+import org.apache.tamaya.spi.PropertySourceProvider;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+
+public class TestPropertySourceProvider
+ implements PropertySourceProvider
+{
+ @Override
+ public Collection<PropertySource> getPropertySources() {
+ ArrayList<PropertySource> sources = new ArrayList<>(2);
+
+ sources.add(new XProvidingPropertySource());
+ sources.add(new YProvidingPropertySource());
+
+ return sources;
+ }
+
+ private class YProvidingPropertySource implements PropertySource {
+ private Map<String, String> props = Collections.singletonMap("tpsp_x", "X");
+
+ @Override
+ public int getOrdinal() {
+ return 100;
+ }
+
+ @Override
+ public String getName() {
+ return "YProvidingPropertySource";
+ }
+
+ @Override
+ public String get(String key) {
+ return getProperties().get(key);
+ }
+
+ @Override
+ public Map<String, String> getProperties() {
+ return props;
+ }
+ }
+
+ private class XProvidingPropertySource implements PropertySource {
+ private Map<String, String> props = Collections.singletonMap("tpsp_y", "Y");
+
+ @Override
+ public Map<String, String> getProperties() {
+ return props;
+ }
+
+ @Override
+ public String get(String key) {
+ return getProperties().get(key);
+ }
+
+ @Override
+ public int getOrdinal() {
+ return 100;
+ }
+
+ @Override
+ public String getName() {
+ return "XProvidingPropertySource";
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertySourceProviderB.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertySourceProviderB.java b/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertySourceProviderB.java
new file mode 100644
index 0000000..ddfb132
--- /dev/null
+++ b/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertySourceProviderB.java
@@ -0,0 +1,89 @@
+/*
+ * 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.spi.PropertySource;
+import org.apache.tamaya.spi.PropertySourceProvider;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+
+public class TestPropertySourceProviderB
+ implements PropertySourceProvider
+{
+ @Override
+ public Collection<PropertySource> getPropertySources() {
+ ArrayList<PropertySource> sources = new ArrayList<>(2);
+
+ sources.add(new AProvidingPropertySource());
+ sources.add(new BProvidingPropertySource());
+
+ return sources;
+ }
+
+ private class BProvidingPropertySource implements PropertySource {
+ private Map<String, String> props = Collections.singletonMap("tpsp_b", "B");
+
+ @Override
+ public int getOrdinal() {
+ return 100;
+ }
+
+ @Override
+ public String getName() {
+ return "BProvidingPropertySource";
+ }
+
+ @Override
+ public String get(String key) {
+ return getProperties().get(key);
+ }
+
+ @Override
+ public Map<String, String> getProperties() {
+ return props;
+ }
+ }
+
+ private class AProvidingPropertySource implements PropertySource {
+ private Map<String, String> props = Collections.singletonMap("tpsp_a", "A");
+
+ @Override
+ public Map<String, String> getProperties() {
+ return props;
+ }
+
+ @Override
+ public String get(String key) {
+ return getProperties().get(key);
+ }
+
+ @Override
+ public int getOrdinal() {
+ return 100;
+ }
+
+ @Override
+ public String getName() {
+ return "AProvidingPropertySource";
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/builder/util/mockito/NotMockedAnswer.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/builder/util/mockito/NotMockedAnswer.java b/modules/builder/src/test/java/org/apache/tamaya/builder/util/mockito/NotMockedAnswer.java
new file mode 100644
index 0000000..10b3734
--- /dev/null
+++ b/modules/builder/src/test/java/org/apache/tamaya/builder/util/mockito/NotMockedAnswer.java
@@ -0,0 +1,55 @@
+/*
+ * 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.util.mockito;
+
+import org.mockito.exceptions.base.MockitoException;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+import java.io.Serializable;
+
+// @todo This is a duplicated class
+public class NotMockedAnswer implements Answer<Object>, Serializable {
+ public final static NotMockedAnswer NOT_MOCKED_ANSWER = new NotMockedAnswer();
+
+ private NotMockedAnswer() {
+ }
+
+ @Override
+ public Object answer(InvocationOnMock invocation) throws Throwable {
+ StringBuilder msgBuilder = new StringBuilder();
+
+ msgBuilder.append("Invocation of method not mocked: ")
+ .append(invocation.getMethod().toGenericString());
+
+ if (invocation.getArguments().length > 0) {
+ msgBuilder.append(" Supplied arguments: ");
+
+ for (int i = 0; i < invocation.getArguments().length; i++) {
+ msgBuilder.append(invocation.getArguments()[i]);
+
+ if (i - 1 < invocation.getArguments().length) {
+ msgBuilder.append(", ");
+ }
+ }
+ }
+
+ throw new MockitoException(msgBuilder.toString());
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeA.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeA.java b/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeA.java
new file mode 100644
index 0000000..89b2f5b
--- /dev/null
+++ b/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeA.java
@@ -0,0 +1,34 @@
+/*
+ * 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.util.types;
+
+/**
+ * Custom type with two argument constructor.
+ */
+public class CustomTypeA {
+ private String name;
+
+ public CustomTypeA(String name, String other) {
+ this.name = name + other;
+ }
+
+ public String getName() {
+ return name;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeB.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeB.java b/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeB.java
new file mode 100644
index 0000000..f7f4d99
--- /dev/null
+++ b/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeB.java
@@ -0,0 +1,39 @@
+/*
+ * 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.util.types;
+
+/**
+ * Custom type with factory method
+ * {@link org.apache.tamaya.builder.util.types.CustomTypeB#of(String)}
+ */
+public class CustomTypeB {
+ private String name;
+
+ private CustomTypeB(String value) {
+ this.name = value;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public static CustomTypeB of(String source) {
+ return new CustomTypeB(source);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeC.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeC.java b/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeC.java
new file mode 100644
index 0000000..da9ce56
--- /dev/null
+++ b/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeC.java
@@ -0,0 +1,36 @@
+/*
+ * 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.util.types;
+
+public class CustomTypeC {
+ private String value;
+
+
+ public CustomTypeC(String in, @SuppressWarnings("unused") int iHideThisConstructorForTamaya) {
+ value = in;
+ }
+
+ public String getValue() {
+ return value;
+ }
+
+ public static CustomTypeC produceFrom(String in) {
+ return new CustomTypeC(in, -1);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeCPropertyConverter.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeCPropertyConverter.java b/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeCPropertyConverter.java
new file mode 100644
index 0000000..1754b30
--- /dev/null
+++ b/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeCPropertyConverter.java
@@ -0,0 +1,28 @@
+/*
+ * 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.util.types;
+
+import org.apache.tamaya.PropertyConverter;
+
+public class CustomTypeCPropertyConverter implements PropertyConverter<org.apache.tamaya.builder.util.types.CustomTypeC> {
+ @Override
+ public org.apache.tamaya.builder.util.types.CustomTypeC convert(String value) {
+ return org.apache.tamaya.builder.util.types.CustomTypeC.produceFrom(value);
+ }
+}
[3/3] incubator-tamaya git commit: TAMAYA-60 Removed the modules
package for the package hierarchy.
Posted by pl...@apache.org.
TAMAYA-60 Removed the modules package for the package hierarchy.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/812b11ea
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/812b11ea
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/812b11ea
Branch: refs/heads/master
Commit: 812b11eac3a9bd58e2fa3e0bc829bfc0369aae52
Parents: f7480b2
Author: Oliver B. Fischer <pl...@apache.org>
Authored: Sat Mar 14 00:56:48 2015 +0100
Committer: Oliver B. Fischer <pl...@apache.org>
Committed: Sat Mar 14 00:56:48 2015 +0100
----------------------------------------------------------------------
.../tamaya/builder/ConfigurationBuilder.java | 580 ++++++++++++
.../ProgrammaticConfigurationContext.java | 406 +++++++++
.../org/apache/tamaya/builder/package-info.java | 28 +
.../modules/builder/ConfigurationBuilder.java | 580 ------------
.../ProgrammaticConfigurationContext.java | 406 ---------
.../tamaya/modules/builder/package-info.java | 28 -
.../builder/ConfigurationBuilderTest.java | 896 +++++++++++++++++++
.../builder/TestNonSPIPropertyFilterA.java | 34 +
.../builder/TestNonSPIPropertyFilterB.java | 34 +
.../tamaya/builder/TestPropertyFilter.java | 34 +
.../tamaya/builder/TestPropertySource.java | 58 ++
.../builder/TestPropertySourceProvider.java | 89 ++
.../builder/TestPropertySourceProviderB.java | 89 ++
.../builder/util/mockito/NotMockedAnswer.java | 55 ++
.../tamaya/builder/util/types/CustomTypeA.java | 34 +
.../tamaya/builder/util/types/CustomTypeB.java | 39 +
.../tamaya/builder/util/types/CustomTypeC.java | 36 +
.../types/CustomTypeCPropertyConverter.java | 28 +
.../builder/ConfigurationBuilderTest.java | 896 -------------------
.../builder/TestNonSPIPropertyFilterA.java | 34 -
.../builder/TestNonSPIPropertyFilterB.java | 34 -
.../modules/builder/TestPropertyFilter.java | 34 -
.../modules/builder/TestPropertySource.java | 58 --
.../builder/TestPropertySourceProvider.java | 89 --
.../builder/TestPropertySourceProviderB.java | 89 --
.../builder/util/mockito/NotMockedAnswer.java | 55 --
.../modules/builder/util/types/CustomTypeA.java | 34 -
.../modules/builder/util/types/CustomTypeB.java | 39 -
.../modules/builder/util/types/CustomTypeC.java | 36 -
.../types/CustomTypeCPropertyConverter.java | 28 -
.../org.apache.tamaya.PropertyConverter | 2 +-
.../org.apache.tamaya.spi.PropertyFilter | 2 +-
.../org.apache.tamaya.spi.PropertySource | 2 +-
...org.apache.tamaya.spi.PropertySourceProvider | 2 +-
34 files changed, 2444 insertions(+), 2444 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/main/java/org/apache/tamaya/builder/ConfigurationBuilder.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/main/java/org/apache/tamaya/builder/ConfigurationBuilder.java b/modules/builder/src/main/java/org/apache/tamaya/builder/ConfigurationBuilder.java
new file mode 100644
index 0000000..51abbcf
--- /dev/null
+++ b/modules/builder/src/main/java/org/apache/tamaya/builder/ConfigurationBuilder.java
@@ -0,0 +1,580 @@
+/*
+ * 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.PropertyConverter;
+import org.apache.tamaya.TypeLiteral;
+import org.apache.tamaya.core.internal.DefaultConfiguration;
+import org.apache.tamaya.format.ConfigurationData;
+import org.apache.tamaya.format.ConfigurationFormats;
+import org.apache.tamaya.format.FlattenedDefaultPropertySource;
+import org.apache.tamaya.spi.ConfigurationContext;
+import org.apache.tamaya.spi.PropertyFilter;
+import org.apache.tamaya.spi.PropertySource;
+import org.apache.tamaya.spi.PropertySourceProvider;
+import org.apache.tamaya.spi.PropertyValueCombinationPolicy;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Objects;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static java.lang.String.format;
+
+/**
+ * <p>Builder class used for building a configuration manually without relying
+ * only on the Service Provider Interface API.</p>
+ *
+ * <p><strong>Features of the builder</strong></p>
+ *
+ * <ol>
+ * <li>Adding of property converters manually</li>
+ * <li>Adding of property sources directly</li>
+ * <li>Adding of property sources via URL</li>
+ * <li>Adding of property source providers directly</li>
+ * <li>Enabling and disabling of via SPI mechanism provided resources as converters,
+ * property sources, etc.</li>
+ * </ol>
+ *
+ * <p><strong>Example</strong></p>
+ *
+ * <pre>{@code ConfigurationBuilder builder = new ConfigurationBuilder();
+ * builder.disableProvidedPropertySources() // Do not load provided property
+ * .disableProvidedPropertySourceProviders() // sources and providers automatically
+ * .addPropertySource("file:/etc/conf.properties"); // Load properties from conf.properties
+ *
+ * Configuration config = builder.build();
+ * }</pre>
+ *
+ * <p><strong>Support for configuration formats</strong></p>
+ *
+ * The configuration builder allows you to add property resources
+ * via a URL, as shown in the code example above, without implementing
+ * a {@link org.apache.tamaya.spi.PropertySource PropertySource} or providing an
+ * instance of a {@link org.apache.tamaya.spi.PropertySource PropertySource}.
+ * If a property resource in
+ * a specific format can be added to configuration builder or not depends
+ * on the available implementations of
+ * {@link org.apache.tamaya.format.ConfigurationFormat} in the classpath.
+ * Which formats are available can be checked via
+ * {@link org.apache.tamaya.format.ConfigurationFormats#getFormats()}.
+ */
+public class ConfigurationBuilder {
+ /** Builder used to create new ConfigurationContext instances. */
+ private ProgrammaticConfigurationContext.Builder contextBuilder = new ProgrammaticConfigurationContext.Builder();
+
+ /**
+ * Flag if the config has already been built.
+ * Configuration can be built only once
+ */
+ private boolean built;
+
+ /**
+ * Flag if all existing property converter service providers
+ * should be loaded if the configuration is build.
+ */
+ private boolean loadProvidedPropertyConverters = true;
+
+ /**
+ * Flag if all existing property source service providers
+ * will be loaded if the configuration is build.
+ */
+ private boolean loadProvidedPropertySources = false;
+ private boolean loadProvidedPropertySourceProviders = false;
+
+ private boolean isLoadProvidedPropertyFilters = false;
+
+ /**
+ * Creates a new builder instance.
+ */
+ public ConfigurationBuilder() {
+ }
+
+ /**
+ * Allows to set configuration context during unit tests.
+ */
+ ConfigurationBuilder setConfigurationContext(ConfigurationContext configurationContext) {
+ contextBuilder.setConfigurationContext(configurationContext);
+ return this;
+ }
+
+ /**
+ * Adds one resources with properties in an arbitrary format
+ * to the configuration to be build.
+ *
+ * <p>If a specific format is supported depends on the available
+ * {@link org.apache.tamaya.format.ConfigurationFormat} implementations.</p>
+ *
+ * <pre>{@code URL resource = new URL("file:/etc/service/config.json");
+ *
+ * builder.addPropertySources(resource);}
+ * </pre>
+ *
+ * @param url resource with properties for the the configuration to be build.
+ *
+ * @return the builder instance currently used
+ *
+ * @see org.apache.tamaya.format.ConfigurationFormat
+ * @see org.apache.tamaya.format.ConfigurationFormats#getFormats()
+ */
+ public ConfigurationBuilder addPropertySource(URL url) {
+ try {
+ ConfigurationData data = getConfigurationDataFromURL(url);
+
+ FlattenedDefaultPropertySource propertySource = new FlattenedDefaultPropertySource(data);
+ addPropertySources(propertySource);
+ } catch (IOException e) {
+ throw new ConfigException("Failed to read " + url.toString(), e);
+ }
+
+ return this;
+ }
+
+ protected ConfigurationData getConfigurationDataFromURL(URL url) throws IOException {
+ ConfigurationData data = ConfigurationFormats.readConfigurationData(url);
+
+ if (null == data) {
+ String mesg = format("No configuration format found which is able " +
+ "to read properties from %s.", url.toString());
+
+ throw new ConfigException(mesg);
+ }
+
+ return data;
+ }
+
+ /**
+ * Adds one or more resources with properties in an arbitrary format
+ * to the configuration to be build.
+ *
+ * <p>If a specific format is supported depends on the available
+ * {@link org.apache.tamaya.format.ConfigurationFormat} implementations.</p>
+ *
+ *<pre>{@code URL first = new URL("file:/etc/service/config.json");
+ * URL second = new URL("file:/etc/defaults/values.properties");
+ *
+ * builder.addPropertySources(first, second);}
+ *</pre>
+ *
+ * @param url first resource with properties for the the configuration to be build.
+ * @param urls list additional of resources with properties for the configuration to be
+ * build.
+ *
+ * @return the builder instance currently used
+ *
+ * @see org.apache.tamaya.format.ConfigurationFormat
+ * @see org.apache.tamaya.format.ConfigurationFormats#getFormats()
+ */
+ public ConfigurationBuilder addPropertySource(URL url, URL... urls) {
+ Stream.of(Collections.singletonList(url), Arrays.asList(urls))
+ .flatMap(Collection::stream)
+ .filter(entry -> entry != null)
+ .collect(Collectors.toList())
+ .forEach(this::addPropertySource);
+
+ return this;
+ }
+
+
+ /**
+ * Adds one or more resources with properties in an arbitrary format
+ * to the configuration to be build.
+ *
+ * <p>If a specific format is supported depends on the available
+ * {@link org.apache.tamaya.format.ConfigurationFormat} implementations.</p>
+ *
+ *<pre>{@code builder.addPropertySources("file:/etc/service/config.json",
+ * "file:/etc/defaults/values.properties");}
+ *</pre>
+ *
+ * @param url first resource with properties for the the configuration to be build.
+ * @param urls list additional of resources with properties for the configuration to be
+ * build.
+ *
+ * @return the builder instance currently used
+ *
+ * @see org.apache.tamaya.format.ConfigurationFormat
+ * @see org.apache.tamaya.format.ConfigurationFormats#getFormats()
+ */
+ public ConfigurationBuilder addPropertySource(String url, String... urls) {
+ Stream.of(Collections.singletonList(url), Arrays.asList(urls))
+ .flatMap(Collection::stream)
+ .filter(entry -> entry != null)
+ .map(new StringToURLMapper())
+ .collect(Collectors.toList())
+ .forEach(this::addPropertySource);
+
+ return this;
+ }
+
+ /**
+ * Adds one or more property source instances to the configuration to be build.
+ *
+ *<pre>{@code PropertySource first = new CustomPropertySource();
+ * PropertySource second = new YetAnotherPropertySource();
+ *
+ * builder.addPropertySources(first, second)};
+ *</pre>
+ *
+ * @param sources list of property source instances with properties for the
+ * configuration to be build.
+ *
+ * @return the builder instance currently used
+ *
+ * @see org.apache.tamaya.spi.PropertySource
+ */
+ public ConfigurationBuilder addPropertySources(PropertySource... sources){
+ checkBuilderState();
+
+ contextBuilder.addPropertySources(Objects.requireNonNull(sources));
+ return this;
+ }
+
+ private void checkBuilderState() {
+ if (built) {
+ throw new IllegalStateException("Configuration has already been build.");
+ }
+ }
+
+ /**
+ * Adds one or more property source provider instances to the configuration to be build.
+ *
+ * <pre>{@code PropertySourceProvider jc = new JavaConfigurationProvider();
+ *
+ * builder.addPropertySources(jc)};
+ * </pre>
+ *
+ * @param providers list of property source provider instances each providing a set
+ * of property source instances for the configuration to be build.
+ *
+ * @return the builder instance currently used
+ *
+ * @see org.apache.tamaya.spi.PropertySourceProvider
+ */
+ public ConfigurationBuilder addPropertySourceProviders(PropertySourceProvider... providers){
+ contextBuilder.addPropertySourceProviders(providers);
+ return this;
+ }
+
+ /**
+ * Adds one or more property filter instances to the configuration to be build.
+ *
+ * <pre>{@code PropertyFilter quoteReplacingFilter = new QuoteFilter();
+ * PropertyFilter commaRemovingFilter = new CommaFilter();
+ *
+ * builder.addPropertyFilters(commaRemovingFilter, quoteReplacingFilter)};
+ * </pre>
+ *
+ * @param filters list of property filter instances which should be applied
+ * to the properties of the configuration to be build.
+ *
+ * @return the builder instance currently used
+ *
+ * @see org.apache.tamaya.spi.PropertyFilter
+ * @see #disableProvidedPropertyFilters()
+ * @see #enabledProvidedPropertyFilters()
+ */
+ public ConfigurationBuilder addPropertyFilters(PropertyFilter... filters){
+ Objects.requireNonNull(filters);
+
+ contextBuilder.addPropertyFilters(filters);
+ return this;
+ }
+
+
+ /**
+ * @return the builder instance currently used
+ */
+ public ConfigurationBuilder setPropertyValueCombinationPolicy(PropertyValueCombinationPolicy propertyValueCombinationPolicy){
+ contextBuilder.setPropertyValueCombinationPolicy(propertyValueCombinationPolicy);
+ return this;
+ }
+
+ /**
+ * Adds a property converter for the a given type to the configuration to
+ * be build.
+ *
+ * <pre>{@code PropertyConverter<MyType> converter = value -> new MyType(value, 42);
+ *
+ * builder.addPropertyConverter(MyType.class, converter}
+ * </pre>
+ *
+ * @param type the required target type the converter should be applied to
+ * @param converter the converter to be used to convert the string property
+ * to the given target type.
+ *
+ * @return the builder instance currently used
+ *
+ * @see org.apache.tamaya.PropertyConverter
+ * @see #enableProvidedPropertyConverters()
+ * @see #disableProvidedPropertyConverters()
+ */
+ public <T> ConfigurationBuilder addPropertyConverter(Class<T> type, PropertyConverter<T> converter) {
+ Objects.requireNonNull(type);
+ Objects.requireNonNull(converter);
+
+ return addPropertyConverter(TypeLiteral.of(type), converter);
+ }
+
+ /**
+ * @return the builder instance currently used
+ */
+ public <T> ConfigurationBuilder addPropertyConverter(TypeLiteral<T> type, PropertyConverter<T> propertyConverter){
+ Objects.requireNonNull(type);
+ Objects.requireNonNull(propertyConverter);
+
+ contextBuilder.addPropertyConverter(type, propertyConverter);
+ return this;
+ }
+
+ /**
+ * Checks if the automatic loading of all {@link org.apache.tamaya.PropertyConverter
+ * PropertyConverter} service providers is enabled or disabled.
+ *
+ * @return {@code true} if the automatic loading is enabled,
+ * otherwise {@code false}.
+ *
+ * @see #enableProvidedPropertyConverters()
+ * @see #disableProvidedPropertyConverters()
+ * @see #addPropertyConverter(Class, org.apache.tamaya.PropertyConverter)
+ * @see #addPropertyConverter(org.apache.tamaya.TypeLiteral, org.apache.tamaya.PropertyConverter)
+ */
+ public boolean isPropertyConverterLoadingEnabled() {
+ return loadProvidedPropertyConverters;
+ }
+
+ /**
+ * Enables the loading of all {@link org.apache.tamaya.PropertyConverter}
+ * service providers.
+ *
+ * @return the builder instance currently used
+ *
+ * @see org.apache.tamaya.PropertyConverter
+ * @see #disableProvidedPropertyConverters()
+ * @see #enableProvidedPropertyConverters()
+ */
+ public ConfigurationBuilder enableProvidedPropertyConverters() {
+ checkBuilderState();
+
+ loadProvidedPropertyConverters = true;
+
+ return this;
+ }
+
+ /**
+ * Disables the automatic loading of all {@link org.apache.tamaya.PropertyConverter}
+ * service providers.
+ *
+ * @return the builder instance currently used
+ *
+ * @see org.apache.tamaya.PropertyConverter
+ * @see #enableProvidedPropertyConverters()
+ * @see #addPropertyConverter(Class, org.apache.tamaya.PropertyConverter)
+ */
+ public ConfigurationBuilder disableProvidedPropertyConverters() {
+ checkBuilderState();
+
+ loadProvidedPropertyConverters = false;
+
+ return this;
+ }
+
+
+ /**
+ * Enables the automatic loading of all {@link org.apache.tamaya.spi.PropertySource}
+ * service providers.
+ *
+ * @return the builder instance currently used
+ *
+ * @see org.apache.tamaya.spi.PropertySource
+ * @see #disableProvidedPropertySources()
+ */
+ public ConfigurationBuilder enableProvidedPropertySources() {
+ checkBuilderState();
+
+ loadProvidedPropertySources = true;
+
+ return this;
+ }
+
+ /**
+ * Checks if the automatic loading of all {@link org.apache.tamaya.spi.PropertySource
+ * PropertySource} service providers is enabled or disabled.
+ *
+ * @return {@code true} if the automatic loading is enabled,
+ * otherwise {@code false}.
+ */
+ public boolean isPropertySourcesLoadingEnabled() {
+ return loadProvidedPropertySources;
+ }
+
+
+ /**
+ * Checks if the automatic loading of all {@link org.apache.tamaya.spi.PropertyFilter
+ * PropertyFilter} service providers is enabled or disabled.
+ *
+ * @return {@code true} if the automatic loading is enabled,
+ * otherwise {@code false}.
+ */
+ public boolean isPropertyFilterLoadingEnabled() {
+ return isLoadProvidedPropertyFilters;
+ }
+
+ /**
+ * Enables the automatic loading of all {@link org.apache.tamaya.spi.PropertyFilter}
+ * service providers.
+ *
+ * @return the builder instance currently used
+ *
+ * @see org.apache.tamaya.spi.PropertyFilter
+ * @see #disableProvidedPropertyFilters()
+ * @see #addPropertyFilters(org.apache.tamaya.spi.PropertyFilter...)
+ */
+ public ConfigurationBuilder enabledProvidedPropertyFilters() {
+ checkBuilderState();
+
+ isLoadProvidedPropertyFilters = true;
+
+ return this;
+ }
+
+ /**
+ * Disables the automatic loading of all {@link org.apache.tamaya.spi.PropertyFilter}
+ * service providers.
+ *
+ * @return the builder instance currently used
+ *
+ * @see org.apache.tamaya.spi.PropertyFilter
+ * @see #enabledProvidedPropertyFilters()
+ * @see #addPropertyFilters(org.apache.tamaya.spi.PropertyFilter...)
+ *
+ * @return the builder instance currently used
+ */
+ public ConfigurationBuilder disableProvidedPropertyFilters() {
+ checkBuilderState();
+
+ isLoadProvidedPropertyFilters = false;
+
+ return this;
+ }
+
+ /**
+ * Disables the automatic loading of all {@link org.apache.tamaya.spi.PropertySource}
+ * service providers.
+ *
+ * @return the builder instance currently used
+ *
+ * @see org.apache.tamaya.spi.PropertySource
+ * @see #enableProvidedPropertySources()
+ */
+ public ConfigurationBuilder disableProvidedPropertySources() {
+ checkBuilderState();
+
+ loadProvidedPropertySources = false;
+
+ return this;
+ }
+
+ /**
+ * Enables the automatic loading of {@link org.apache.tamaya.spi.PropertySourceProvider
+ * property source providers} provided via the SPI API.
+ *
+ * @return the builder instance currently used
+ *
+ * @see org.apache.tamaya.spi.PropertySourceProvider
+ * @see
+ */
+ public ConfigurationBuilder enableProvidedPropertySourceProviders() {
+ checkBuilderState();
+
+ loadProvidedPropertySourceProviders = true;
+
+ return this;
+ }
+
+ /**
+ * Disables the automatic loading of {@link org.apache.tamaya.spi.PropertySourceProvider
+ * property source providers} provided via the SPI API.
+ *
+ * @return the builder instance currently used
+ */
+ public ConfigurationBuilder disableProvidedPropertySourceProviders() {
+ checkBuilderState();
+
+ loadProvidedPropertySourceProviders = false;
+
+ return this;
+ }
+
+ /**
+ * Checks if the automatic loading of {@link org.apache.tamaya.spi.PropertySourceProvider
+ * PropertySourceProviders} is enabled or disabled.
+ *
+ * @return {@code true} if the automatic loading is enabled,
+ * otherwise {@code false}.
+ */
+ public boolean isPropertySourceProvidersLoadingEnabled() {
+ return loadProvidedPropertySourceProviders;
+ }
+
+ //X TODO think on a functonality/API for using the default PropertyConverters and use the configured ones here
+ //X TODO as overrides used first.
+
+
+ /**
+ * Builds a new configuration based on the configuration of this builder instance.
+ *
+ * @return a new {@link org.apache.tamaya.Configuration configuration instance},
+ * never {@code null}.
+ */
+ public Configuration build() {
+ checkBuilderState();
+
+ built = true;
+
+ contextBuilder.loadProvidedPropertyConverters(isPropertyConverterLoadingEnabled());
+ contextBuilder.loadProvidedPropertySources(isPropertySourcesLoadingEnabled());
+ contextBuilder.loadProvidedPropertySourceProviders(isPropertySourceProvidersLoadingEnabled());
+ contextBuilder.loadProvidedPropertyFilters(isLoadProvidedPropertyFilters);
+
+ return new DefaultConfiguration(contextBuilder.build());
+ }
+
+ /**
+ * Mapper to map a URL given as string to an URL instance.
+ */
+ private static class StringToURLMapper implements Function<String, URL> {
+ @Override
+ public URL apply(String u) {
+ try {
+ return new URL(u);
+ } catch (MalformedURLException e) {
+ throw new ConfigException(u + " is not a valid URL", e);
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfigurationContext.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfigurationContext.java b/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfigurationContext.java
new file mode 100644
index 0000000..6c7e845
--- /dev/null
+++ b/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfigurationContext.java
@@ -0,0 +1,406 @@
+/*
+ * 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.PropertyConverter;
+import org.apache.tamaya.TypeLiteral;
+import org.apache.tamaya.core.internal.PropertyConverterManager;
+import org.apache.tamaya.spi.ConfigurationContext;
+import org.apache.tamaya.spi.ConfigurationContextBuilder;
+import org.apache.tamaya.spi.PropertyFilter;
+import org.apache.tamaya.spi.PropertySource;
+import org.apache.tamaya.spi.PropertySourceProvider;
+import org.apache.tamaya.spi.PropertyValueCombinationPolicy;
+import org.apache.tamaya.spi.ServiceContext;
+
+import javax.annotation.Priority;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.StringJoiner;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.StampedLock;
+import java.util.function.Function;
+import java.util.function.Predicate;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static java.util.stream.Collectors.toList;
+
+/**
+ * Implementation of the {@link org.apache.tamaya.spi.ConfigurationContext}
+ * used by the {@link org.apache.tamaya.builder.ConfigurationBuilder}
+ * internally.
+ */
+class ProgrammaticConfigurationContext implements ConfigurationContext {
+
+ /**
+ * The logger used.
+ */
+ private final static Logger LOG = Logger.getLogger(ProgrammaticConfigurationContext.class.getName());
+ /**
+ * Cubcomponent handling {@link org.apache.tamaya.PropertyConverter} instances.
+ */
+ private PropertyConverterManager propertyConverterManager = new PropertyConverterManager();
+
+ /**
+ * The current unmodifiable list of loaded {@link org.apache.tamaya.spi.PropertySource} instances.
+ */
+ private List<PropertySource> immutablePropertySources = new ArrayList<>();
+
+ /**
+ * The current unmodifiable list of loaded {@link org.apache.tamaya.spi.PropertyFilter} instances.
+ */
+ private List<PropertyFilter> immutablePropertyFilters = new ArrayList<>();
+
+ /**
+ * The overriding policy used when combining PropertySources registered to evalute the final configuration
+ * values.
+ */
+ private PropertyValueCombinationPolicy propertyValueCombinationPolicy;
+
+ /**
+ * Lock for internal synchronization.
+ */
+ private StampedLock propertySourceLock = new StampedLock();
+
+
+ /**
+ * The first time the Configuration system gets invoked we do initialize
+ * all our {@link org.apache.tamaya.spi.PropertySource}s and
+ * {@link org.apache.tamaya.spi.PropertyFilter}s which are known at startup.
+ */
+ @SuppressWarnings("unchecked")
+ public ProgrammaticConfigurationContext(Builder builder) {
+ propertyConverterManager = new PropertyConverterManager(builder.loadProvidedPropertyConverters);
+
+ immutablePropertySources = getAllPropertySources(builder).stream()
+ .sorted(this::comparePropertySources)
+ .collect(Collectors.toList());
+
+
+ immutablePropertyFilters = getPropertyFilters(builder).stream()
+ .sorted(this::comparePropertyFilters)
+ .collect(toList());
+
+
+ propertyValueCombinationPolicy = builder.propertyValueCombinationPolicy;
+
+ builder.propertyConverters.forEach((literal, converters) -> {
+ converters.stream().filter(c -> c != null)
+ .forEach(c -> propertyConverterManager.register((TypeLiteral<Object>) literal,
+ (PropertyConverter<Object>) c));
+ });
+
+ LOG.info(() -> "Using " + immutablePropertySources.size() + " property sources: " +
+ createStringList(immutablePropertySources, ps -> ps.getName() + '[' + ps.getClass().getName() + ']'));
+
+
+ LOG.info(() -> "Using " + immutablePropertyFilters.size() + " property filters: " +
+ createStringList(immutablePropertyFilters, f -> f.getClass().getName()));
+
+
+ LOG.info(() -> "Using PropertyValueCombinationPolicy: " + propertyValueCombinationPolicy);
+ }
+
+ private List<PropertyFilter> getPropertyFilters(Builder builder) {
+ List<PropertyFilter> provided = builder.loadProvidedPropertyFilters
+ ? ServiceContext.getInstance().getServices(PropertyFilter.class)
+ : new ArrayList<>(0);
+
+ List<PropertyFilter> configured = builder.propertyFilters;
+
+ return Stream.of(provided, configured).flatMap(Collection::stream)
+ .collect(toList());
+ }
+
+ private List<PropertySource> getAllPropertySources(Builder builder) {
+ List<PropertySource> provided = builder.loadProvidedPropertySources
+ ? ServiceContext.getInstance().getServices(PropertySource.class)
+ : new ArrayList<>(0);
+
+ if (builder.loadProvidedPropertySourceProviders) {
+ List<PropertySourceProvider> providers = ServiceContext.getInstance()
+ .getServices(PropertySourceProvider.class);
+ for (PropertySourceProvider provider : providers) {
+ Collection<PropertySource> sources = provider.getPropertySources();
+ provided.addAll(sources);
+ }
+ }
+
+ List<PropertySource> configured = builder.propertySources;
+
+ return Stream.of(provided, configured).flatMap(Collection::stream)
+ .collect(toList());
+ }
+
+ public void addPropertySources(PropertySource... propertySourcesToAdd) {
+ Lock writeLock = propertySourceLock.asWriteLock();
+ try {
+ writeLock.lock();
+ List<PropertySource> newPropertySources = new ArrayList<>(this.immutablePropertySources);
+ newPropertySources.addAll(Arrays.asList(propertySourcesToAdd));
+ Collections.sort(newPropertySources, this::comparePropertySources);
+
+ this.immutablePropertySources = Collections.unmodifiableList(newPropertySources);
+ } finally {
+ writeLock.unlock();
+ }
+ }
+
+ /**
+ * Order property source reversely, the most important come first.
+ *
+ * @param source1 the first PropertySource
+ * @param source2 the second PropertySource
+ * @return the comparison result.
+ */
+ private int comparePropertySources(PropertySource source1, PropertySource source2) {
+
+ //X TODO this method duplicates org.apache.tamaya.core.internal.DefaultConfigurationContext.PropertySourceComparator.comparePropertySources()
+ //X maybe we should extract the Comperator in an own class for real code-reuse (copy paste == bad code reuse)
+
+ if (source1.getOrdinal() < source2.getOrdinal()) {
+ return -1;
+ } else if (source1.getOrdinal() > source2.getOrdinal()) {
+ return 1;
+ } else {
+ return source1.getClass().getName().compareTo(source2.getClass().getName());
+ }
+ }
+
+ /**
+ * Compare 2 filters for ordering the filter chain.
+ *
+ * @param filter1 the first filter
+ * @param filter2 the second filter
+ * @return the comparison result
+ */
+ private int comparePropertyFilters(PropertyFilter filter1, PropertyFilter filter2) {
+
+ //X TODO this method duplicates org.apache.tamaya.core.internal.DefaultConfigurationContext.PropertySourceComparator.comparePropertyFilters()
+ //X maybe we should extract the Comperator in an own class for real code-reuse (copy paste == bad code reuse)
+
+ Priority prio1 = filter1.getClass().getAnnotation(Priority.class);
+ Priority prio2 = filter2.getClass().getAnnotation(Priority.class);
+ int ord1 = prio1 != null ? prio1.value() : 0;
+ int ord2 = prio2 != null ? prio2.value() : 0;
+
+ if (ord1 < ord2) {
+ return -1;
+ } else if (ord1 > ord2) {
+ return 1;
+ } else {
+ return filter1.getClass().getName().compareTo(filter2.getClass().getName());
+ }
+ }
+
+ @Override
+ public List<PropertySource> getPropertySources() {
+ return immutablePropertySources;
+ }
+
+ public <T> void addPropertyConverter(TypeLiteral<T> typeToConvert, PropertyConverter<T> propertyConverter) {
+ propertyConverterManager.register(typeToConvert, propertyConverter);
+ LOG.info(() -> "Added PropertyConverter: " + propertyConverter.getClass().getName());
+ }
+
+ @Override
+ public Map<TypeLiteral<?>, List<PropertyConverter<?>>> getPropertyConverters() {
+ return propertyConverterManager.getPropertyConverters();
+ }
+
+ @Override
+ public <T> List<PropertyConverter<T>> getPropertyConverters(TypeLiteral<T> targetType) {
+ return propertyConverterManager.getPropertyConverters(targetType);
+ }
+
+ @Override
+ public List<PropertyFilter> getPropertyFilters() {
+ return immutablePropertyFilters;
+ }
+
+ @Override
+ public PropertyValueCombinationPolicy getPropertyValueCombinationPolicy() {
+ return propertyValueCombinationPolicy;
+ }
+
+ private <T> String createStringList(Collection<T> propertySources, Function<T, String> mapper) {
+ StringJoiner joiner = new StringJoiner(", ");
+ propertySources.forEach(t -> joiner.add(mapper.apply(t)));
+ return joiner.toString();
+ }
+
+ @Override
+ public ConfigurationContextBuilder toBuilder() {
+ // @todo Check if it could be useful to support this method, Oliver B. Fischer
+ throw new RuntimeException("This method is currently not supported.");
+ }
+
+ @Override
+ public Collection<PropertySource> getPropertySources(Predicate<PropertySource> selector) {
+ // @todo Check if it could be useful to support this method, Oliver B. Fischer
+ throw new RuntimeException("This method is currently not supported.");
+ }
+
+ /**
+ * The Builder for {@link ProgrammaticConfigurationContext}
+ */
+ public final static class Builder {
+ /**
+ * The current unmodifiable list of loaded {@link org.apache.tamaya.spi.PropertySource} instances.
+ */
+ private List<PropertySource> propertySources = new ArrayList<>();
+
+ /**
+ * The current unmodifiable list of loaded {@link org.apache.tamaya.spi.PropertyFilter} instances.
+ */
+ private List<PropertyFilter> propertyFilters = new ArrayList<>();
+
+ private Map<TypeLiteral<?>, List<PropertyConverter<?>>> propertyConverters = new HashMap<>();
+
+ /**
+ * The overriding policy used when combining PropertySources registered to evalute the final configuration
+ * values.
+ */
+ private PropertyValueCombinationPolicy propertyValueCombinationPolicy =
+ PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR;
+
+ private boolean loadProvidedPropertyConverters;
+ private boolean loadProvidedPropertySources;
+ private boolean loadProvidedPropertySourceProviders;
+ private boolean loadProvidedPropertyFilters;
+
+ public Builder setPropertyValueCombinationPolicy(PropertyValueCombinationPolicy policy) {
+ this.propertyValueCombinationPolicy = Objects.requireNonNull(policy);
+ return this;
+ }
+
+ public Builder addPropertySources(PropertySource... propertySources) {
+ List<PropertySource> filtered = Stream.of(propertySources).filter(this::isNotNull)
+ .collect(toList());
+
+ this.propertySources.addAll(filtered);
+
+ return this;
+ }
+
+ public Builder addPropertySources(Collection<PropertySource> propertySources) {
+ List<PropertySource> filtered = propertySources.stream().filter(this::isNotNull)
+ .collect(toList());
+
+ this.propertySources.addAll(filtered);
+
+ return this;
+ }
+
+ public Builder addPropertySourceProviders(PropertySourceProvider... propertySourceProviders) {
+ List<PropertySourceProvider> providers = Stream.of(propertySourceProviders).filter(this::isNotNull)
+ .collect(toList());
+
+ return addPropertySourceProviders(providers);
+ }
+
+ public Builder addPropertySourceProviders(Collection<PropertySourceProvider> providers) {
+ List<PropertySource> filtered = providers.stream().filter(this::isNotNull)
+ .flatMap(p -> p.getPropertySources().stream())
+ .filter(this::isNotNull)
+ .collect(toList());
+
+ this.propertySources.addAll(filtered);
+
+ return this;
+ }
+
+ public Builder addPropertyFilters(PropertyFilter... propertySources) {
+ List<PropertyFilter> sources = Stream.of(propertySources).filter(this::isNotNull)
+ .collect(toList());
+
+ this.propertyFilters.addAll(sources);
+
+ return this;
+ }
+
+ public Builder addPropertyFilters(Collection<PropertyFilter> propertySources) {
+ List<PropertyFilter> sources = propertySources.stream().filter(this::isNotNull)
+ .collect(toList());
+
+ this.propertyFilters.addAll(sources);
+
+ return this;
+ }
+
+ /**
+ * Should be never used.
+ */
+ @Deprecated
+ public Builder setConfigurationContext(ConfigurationContext configurationContext) {
+ this.addPropertySources(configurationContext.getPropertySources());
+ this.addPropertyFilters(configurationContext.getPropertyFilters());
+ this.propertyValueCombinationPolicy = Objects.requireNonNull(
+ configurationContext.getPropertyValueCombinationPolicy());
+ return this;
+ }
+
+ //X TODO think on a functonality/API for using the default PropertyConverters and use the configured ones here
+ //X TODO as overrides used first.
+
+ public <T> Builder addPropertyConverter(TypeLiteral<T> type, PropertyConverter<T> propertyConverter) {
+ propertyConverters.computeIfAbsent(type, (t) -> new ArrayList<>())
+ .add(propertyConverter);
+
+ return this;
+ }
+
+ public ConfigurationContext build() {
+ return new ProgrammaticConfigurationContext(this);
+ }
+
+
+ public void loadProvidedPropertyConverters(boolean state) {
+ loadProvidedPropertyConverters = state;
+ }
+
+ public void loadProvidedPropertySources(boolean state) {
+ loadProvidedPropertySources = state;
+ }
+
+ public void loadProvidedPropertySourceProviders(boolean state) {
+ loadProvidedPropertySourceProviders = state;
+ }
+
+ public void loadProvidedPropertyFilters(boolean state) {
+ loadProvidedPropertyFilters = state;
+ }
+
+ private <T> boolean isNotNull(T item) {
+ return null != item;
+ }
+ }
+
+
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/main/java/org/apache/tamaya/builder/package-info.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/main/java/org/apache/tamaya/builder/package-info.java b/modules/builder/src/main/java/org/apache/tamaya/builder/package-info.java
new file mode 100644
index 0000000..24c86a9
--- /dev/null
+++ b/modules/builder/src/main/java/org/apache/tamaya/builder/package-info.java
@@ -0,0 +1,28 @@
+/*
+ * 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.
+ */
+/**
+ * This package provides
+ * {@link org.apache.tamaya.builder.ConfigurationBuilder a configuration
+ * builder} that allows to build a configuration manually without
+ * using exclusively on the Service Provider Interface API of Tamaya.
+ *
+ * @see org.apache.tamaya.builder.ConfigurationBuilder
+ * @see org.apache.tamaya.Configuration
+ */
+package org.apache.tamaya.builder;
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/main/java/org/apache/tamaya/modules/builder/ConfigurationBuilder.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/main/java/org/apache/tamaya/modules/builder/ConfigurationBuilder.java b/modules/builder/src/main/java/org/apache/tamaya/modules/builder/ConfigurationBuilder.java
deleted file mode 100644
index 2d7ccac..0000000
--- a/modules/builder/src/main/java/org/apache/tamaya/modules/builder/ConfigurationBuilder.java
+++ /dev/null
@@ -1,580 +0,0 @@
-/*
- * 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.modules.builder;
-
-import org.apache.tamaya.ConfigException;
-import org.apache.tamaya.Configuration;
-import org.apache.tamaya.PropertyConverter;
-import org.apache.tamaya.TypeLiteral;
-import org.apache.tamaya.core.internal.DefaultConfiguration;
-import org.apache.tamaya.format.ConfigurationData;
-import org.apache.tamaya.format.ConfigurationFormats;
-import org.apache.tamaya.format.FlattenedDefaultPropertySource;
-import org.apache.tamaya.spi.ConfigurationContext;
-import org.apache.tamaya.spi.PropertyFilter;
-import org.apache.tamaya.spi.PropertySource;
-import org.apache.tamaya.spi.PropertySourceProvider;
-import org.apache.tamaya.spi.PropertyValueCombinationPolicy;
-
-import java.io.IOException;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Objects;
-import java.util.function.Function;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-
-import static java.lang.String.format;
-
-/**
- * <p>Builder class used for building a configuration manually without relying
- * only on the Service Provider Interface API.</p>
- *
- * <p><strong>Features of the builder</strong></p>
- *
- * <ol>
- * <li>Adding of property converters manually</li>
- * <li>Adding of property sources directly</li>
- * <li>Adding of property sources via URL</li>
- * <li>Adding of property source providers directly</li>
- * <li>Enabling and disabling of via SPI mechanism provided resources as converters,
- * property sources, etc.</li>
- * </ol>
- *
- * <p><strong>Example</strong></p>
- *
- * <pre>{@code ConfigurationBuilder builder = new ConfigurationBuilder();
- * builder.disableProvidedPropertySources() // Do not load provided property
- * .disableProvidedPropertySourceProviders() // sources and providers automatically
- * .addPropertySource("file:/etc/conf.properties"); // Load properties from conf.properties
- *
- * Configuration config = builder.build();
- * }</pre>
- *
- * <p><strong>Support for configuration formats</strong></p>
- *
- * The configuration builder allows you to add property resources
- * via a URL, as shown in the code example above, without implementing
- * a {@link org.apache.tamaya.spi.PropertySource PropertySource} or providing an
- * instance of a {@link org.apache.tamaya.spi.PropertySource PropertySource}.
- * If a property resource in
- * a specific format can be added to configuration builder or not depends
- * on the available implementations of
- * {@link org.apache.tamaya.format.ConfigurationFormat} in the classpath.
- * Which formats are available can be checked via
- * {@link org.apache.tamaya.format.ConfigurationFormats#getFormats()}.
- */
-public class ConfigurationBuilder {
- /** Builder used to create new ConfigurationContext instances. */
- private ProgrammaticConfigurationContext.Builder contextBuilder = new ProgrammaticConfigurationContext.Builder();
-
- /**
- * Flag if the config has already been built.
- * Configuration can be built only once
- */
- private boolean built;
-
- /**
- * Flag if all existing property converter service providers
- * should be loaded if the configuration is build.
- */
- private boolean loadProvidedPropertyConverters = true;
-
- /**
- * Flag if all existing property source service providers
- * will be loaded if the configuration is build.
- */
- private boolean loadProvidedPropertySources = false;
- private boolean loadProvidedPropertySourceProviders = false;
-
- private boolean isLoadProvidedPropertyFilters = false;
-
- /**
- * Creates a new builder instance.
- */
- public ConfigurationBuilder() {
- }
-
- /**
- * Allows to set configuration context during unit tests.
- */
- ConfigurationBuilder setConfigurationContext(ConfigurationContext configurationContext) {
- contextBuilder.setConfigurationContext(configurationContext);
- return this;
- }
-
- /**
- * Adds one resources with properties in an arbitrary format
- * to the configuration to be build.
- *
- * <p>If a specific format is supported depends on the available
- * {@link org.apache.tamaya.format.ConfigurationFormat} implementations.</p>
- *
- * <pre>{@code URL resource = new URL("file:/etc/service/config.json");
- *
- * builder.addPropertySources(resource);}
- * </pre>
- *
- * @param url resource with properties for the the configuration to be build.
- *
- * @return the builder instance currently used
- *
- * @see org.apache.tamaya.format.ConfigurationFormat
- * @see org.apache.tamaya.format.ConfigurationFormats#getFormats()
- */
- public ConfigurationBuilder addPropertySource(URL url) {
- try {
- ConfigurationData data = getConfigurationDataFromURL(url);
-
- FlattenedDefaultPropertySource propertySource = new FlattenedDefaultPropertySource(data);
- addPropertySources(propertySource);
- } catch (IOException e) {
- throw new ConfigException("Failed to read " + url.toString(), e);
- }
-
- return this;
- }
-
- protected ConfigurationData getConfigurationDataFromURL(URL url) throws IOException {
- ConfigurationData data = ConfigurationFormats.readConfigurationData(url);
-
- if (null == data) {
- String mesg = format("No configuration format found which is able " +
- "to read properties from %s.", url.toString());
-
- throw new ConfigException(mesg);
- }
-
- return data;
- }
-
- /**
- * Adds one or more resources with properties in an arbitrary format
- * to the configuration to be build.
- *
- * <p>If a specific format is supported depends on the available
- * {@link org.apache.tamaya.format.ConfigurationFormat} implementations.</p>
- *
- *<pre>{@code URL first = new URL("file:/etc/service/config.json");
- * URL second = new URL("file:/etc/defaults/values.properties");
- *
- * builder.addPropertySources(first, second);}
- *</pre>
- *
- * @param url first resource with properties for the the configuration to be build.
- * @param urls list additional of resources with properties for the configuration to be
- * build.
- *
- * @return the builder instance currently used
- *
- * @see org.apache.tamaya.format.ConfigurationFormat
- * @see org.apache.tamaya.format.ConfigurationFormats#getFormats()
- */
- public ConfigurationBuilder addPropertySource(URL url, URL... urls) {
- Stream.of(Collections.singletonList(url), Arrays.asList(urls))
- .flatMap(Collection::stream)
- .filter(entry -> entry != null)
- .collect(Collectors.toList())
- .forEach(this::addPropertySource);
-
- return this;
- }
-
-
- /**
- * Adds one or more resources with properties in an arbitrary format
- * to the configuration to be build.
- *
- * <p>If a specific format is supported depends on the available
- * {@link org.apache.tamaya.format.ConfigurationFormat} implementations.</p>
- *
- *<pre>{@code builder.addPropertySources("file:/etc/service/config.json",
- * "file:/etc/defaults/values.properties");}
- *</pre>
- *
- * @param url first resource with properties for the the configuration to be build.
- * @param urls list additional of resources with properties for the configuration to be
- * build.
- *
- * @return the builder instance currently used
- *
- * @see org.apache.tamaya.format.ConfigurationFormat
- * @see org.apache.tamaya.format.ConfigurationFormats#getFormats()
- */
- public ConfigurationBuilder addPropertySource(String url, String... urls) {
- Stream.of(Collections.singletonList(url), Arrays.asList(urls))
- .flatMap(Collection::stream)
- .filter(entry -> entry != null)
- .map(new StringToURLMapper())
- .collect(Collectors.toList())
- .forEach(this::addPropertySource);
-
- return this;
- }
-
- /**
- * Adds one or more property source instances to the configuration to be build.
- *
- *<pre>{@code PropertySource first = new CustomPropertySource();
- * PropertySource second = new YetAnotherPropertySource();
- *
- * builder.addPropertySources(first, second)};
- *</pre>
- *
- * @param sources list of property source instances with properties for the
- * configuration to be build.
- *
- * @return the builder instance currently used
- *
- * @see org.apache.tamaya.spi.PropertySource
- */
- public ConfigurationBuilder addPropertySources(PropertySource... sources){
- checkBuilderState();
-
- contextBuilder.addPropertySources(Objects.requireNonNull(sources));
- return this;
- }
-
- private void checkBuilderState() {
- if (built) {
- throw new IllegalStateException("Configuration has already been build.");
- }
- }
-
- /**
- * Adds one or more property source provider instances to the configuration to be build.
- *
- * <pre>{@code PropertySourceProvider jc = new JavaConfigurationProvider();
- *
- * builder.addPropertySources(jc)};
- * </pre>
- *
- * @param providers list of property source provider instances each providing a set
- * of property source instances for the configuration to be build.
- *
- * @return the builder instance currently used
- *
- * @see org.apache.tamaya.spi.PropertySourceProvider
- */
- public ConfigurationBuilder addPropertySourceProviders(PropertySourceProvider... providers){
- contextBuilder.addPropertySourceProviders(providers);
- return this;
- }
-
- /**
- * Adds one or more property filter instances to the configuration to be build.
- *
- * <pre>{@code PropertyFilter quoteReplacingFilter = new QuoteFilter();
- * PropertyFilter commaRemovingFilter = new CommaFilter();
- *
- * builder.addPropertyFilters(commaRemovingFilter, quoteReplacingFilter)};
- * </pre>
- *
- * @param filters list of property filter instances which should be applied
- * to the properties of the configuration to be build.
- *
- * @return the builder instance currently used
- *
- * @see org.apache.tamaya.spi.PropertyFilter
- * @see #disableProvidedPropertyFilters()
- * @see #enabledProvidedPropertyFilters()
- */
- public ConfigurationBuilder addPropertyFilters(PropertyFilter... filters){
- Objects.requireNonNull(filters);
-
- contextBuilder.addPropertyFilters(filters);
- return this;
- }
-
-
- /**
- * @return the builder instance currently used
- */
- public ConfigurationBuilder setPropertyValueCombinationPolicy(PropertyValueCombinationPolicy propertyValueCombinationPolicy){
- contextBuilder.setPropertyValueCombinationPolicy(propertyValueCombinationPolicy);
- return this;
- }
-
- /**
- * Adds a property converter for the a given type to the configuration to
- * be build.
- *
- * <pre>{@code PropertyConverter<MyType> converter = value -> new MyType(value, 42);
- *
- * builder.addPropertyConverter(MyType.class, converter}
- * </pre>
- *
- * @param type the required target type the converter should be applied to
- * @param converter the converter to be used to convert the string property
- * to the given target type.
- *
- * @return the builder instance currently used
- *
- * @see org.apache.tamaya.PropertyConverter
- * @see #enableProvidedPropertyConverters()
- * @see #disableProvidedPropertyConverters()
- */
- public <T> ConfigurationBuilder addPropertyConverter(Class<T> type, PropertyConverter<T> converter) {
- Objects.requireNonNull(type);
- Objects.requireNonNull(converter);
-
- return addPropertyConverter(TypeLiteral.of(type), converter);
- }
-
- /**
- * @return the builder instance currently used
- */
- public <T> ConfigurationBuilder addPropertyConverter(TypeLiteral<T> type, PropertyConverter<T> propertyConverter){
- Objects.requireNonNull(type);
- Objects.requireNonNull(propertyConverter);
-
- contextBuilder.addPropertyConverter(type, propertyConverter);
- return this;
- }
-
- /**
- * Checks if the automatic loading of all {@link org.apache.tamaya.PropertyConverter
- * PropertyConverter} service providers is enabled or disabled.
- *
- * @return {@code true} if the automatic loading is enabled,
- * otherwise {@code false}.
- *
- * @see #enableProvidedPropertyConverters()
- * @see #disableProvidedPropertyConverters()
- * @see #addPropertyConverter(Class, org.apache.tamaya.PropertyConverter)
- * @see #addPropertyConverter(org.apache.tamaya.TypeLiteral, org.apache.tamaya.PropertyConverter)
- */
- public boolean isPropertyConverterLoadingEnabled() {
- return loadProvidedPropertyConverters;
- }
-
- /**
- * Enables the loading of all {@link org.apache.tamaya.PropertyConverter}
- * service providers.
- *
- * @return the builder instance currently used
- *
- * @see org.apache.tamaya.PropertyConverter
- * @see #disableProvidedPropertyConverters()
- * @see #enableProvidedPropertyConverters()
- */
- public ConfigurationBuilder enableProvidedPropertyConverters() {
- checkBuilderState();
-
- loadProvidedPropertyConverters = true;
-
- return this;
- }
-
- /**
- * Disables the automatic loading of all {@link org.apache.tamaya.PropertyConverter}
- * service providers.
- *
- * @return the builder instance currently used
- *
- * @see org.apache.tamaya.PropertyConverter
- * @see #enableProvidedPropertyConverters()
- * @see #addPropertyConverter(Class, org.apache.tamaya.PropertyConverter)
- */
- public ConfigurationBuilder disableProvidedPropertyConverters() {
- checkBuilderState();
-
- loadProvidedPropertyConverters = false;
-
- return this;
- }
-
-
- /**
- * Enables the automatic loading of all {@link org.apache.tamaya.spi.PropertySource}
- * service providers.
- *
- * @return the builder instance currently used
- *
- * @see org.apache.tamaya.spi.PropertySource
- * @see #disableProvidedPropertySources()
- */
- public ConfigurationBuilder enableProvidedPropertySources() {
- checkBuilderState();
-
- loadProvidedPropertySources = true;
-
- return this;
- }
-
- /**
- * Checks if the automatic loading of all {@link org.apache.tamaya.spi.PropertySource
- * PropertySource} service providers is enabled or disabled.
- *
- * @return {@code true} if the automatic loading is enabled,
- * otherwise {@code false}.
- */
- public boolean isPropertySourcesLoadingEnabled() {
- return loadProvidedPropertySources;
- }
-
-
- /**
- * Checks if the automatic loading of all {@link org.apache.tamaya.spi.PropertyFilter
- * PropertyFilter} service providers is enabled or disabled.
- *
- * @return {@code true} if the automatic loading is enabled,
- * otherwise {@code false}.
- */
- public boolean isPropertyFilterLoadingEnabled() {
- return isLoadProvidedPropertyFilters;
- }
-
- /**
- * Enables the automatic loading of all {@link org.apache.tamaya.spi.PropertyFilter}
- * service providers.
- *
- * @return the builder instance currently used
- *
- * @see org.apache.tamaya.spi.PropertyFilter
- * @see #disableProvidedPropertyFilters()
- * @see #addPropertyFilters(org.apache.tamaya.spi.PropertyFilter...)
- */
- public ConfigurationBuilder enabledProvidedPropertyFilters() {
- checkBuilderState();
-
- isLoadProvidedPropertyFilters = true;
-
- return this;
- }
-
- /**
- * Disables the automatic loading of all {@link org.apache.tamaya.spi.PropertyFilter}
- * service providers.
- *
- * @return the builder instance currently used
- *
- * @see org.apache.tamaya.spi.PropertyFilter
- * @see #enabledProvidedPropertyFilters()
- * @see #addPropertyFilters(org.apache.tamaya.spi.PropertyFilter...)
- *
- * @return the builder instance currently used
- */
- public ConfigurationBuilder disableProvidedPropertyFilters() {
- checkBuilderState();
-
- isLoadProvidedPropertyFilters = false;
-
- return this;
- }
-
- /**
- * Disables the automatic loading of all {@link org.apache.tamaya.spi.PropertySource}
- * service providers.
- *
- * @return the builder instance currently used
- *
- * @see org.apache.tamaya.spi.PropertySource
- * @see #enableProvidedPropertySources()
- */
- public ConfigurationBuilder disableProvidedPropertySources() {
- checkBuilderState();
-
- loadProvidedPropertySources = false;
-
- return this;
- }
-
- /**
- * Enables the automatic loading of {@link org.apache.tamaya.spi.PropertySourceProvider
- * property source providers} provided via the SPI API.
- *
- * @return the builder instance currently used
- *
- * @see org.apache.tamaya.spi.PropertySourceProvider
- * @see
- */
- public ConfigurationBuilder enableProvidedPropertySourceProviders() {
- checkBuilderState();
-
- loadProvidedPropertySourceProviders = true;
-
- return this;
- }
-
- /**
- * Disables the automatic loading of {@link org.apache.tamaya.spi.PropertySourceProvider
- * property source providers} provided via the SPI API.
- *
- * @return the builder instance currently used
- */
- public ConfigurationBuilder disableProvidedPropertySourceProviders() {
- checkBuilderState();
-
- loadProvidedPropertySourceProviders = false;
-
- return this;
- }
-
- /**
- * Checks if the automatic loading of {@link org.apache.tamaya.spi.PropertySourceProvider
- * PropertySourceProviders} is enabled or disabled.
- *
- * @return {@code true} if the automatic loading is enabled,
- * otherwise {@code false}.
- */
- public boolean isPropertySourceProvidersLoadingEnabled() {
- return loadProvidedPropertySourceProviders;
- }
-
- //X TODO think on a functonality/API for using the default PropertyConverters and use the configured ones here
- //X TODO as overrides used first.
-
-
- /**
- * Builds a new configuration based on the configuration of this builder instance.
- *
- * @return a new {@link org.apache.tamaya.Configuration configuration instance},
- * never {@code null}.
- */
- public Configuration build() {
- checkBuilderState();
-
- built = true;
-
- contextBuilder.loadProvidedPropertyConverters(isPropertyConverterLoadingEnabled());
- contextBuilder.loadProvidedPropertySources(isPropertySourcesLoadingEnabled());
- contextBuilder.loadProvidedPropertySourceProviders(isPropertySourceProvidersLoadingEnabled());
- contextBuilder.loadProvidedPropertyFilters(isLoadProvidedPropertyFilters);
-
- return new DefaultConfiguration(contextBuilder.build());
- }
-
- /**
- * Mapper to map a URL given as string to an URL instance.
- */
- private static class StringToURLMapper implements Function<String, URL> {
- @Override
- public URL apply(String u) {
- try {
- return new URL(u);
- } catch (MalformedURLException e) {
- throw new ConfigException(u + " is not a valid URL", e);
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/main/java/org/apache/tamaya/modules/builder/ProgrammaticConfigurationContext.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/main/java/org/apache/tamaya/modules/builder/ProgrammaticConfigurationContext.java b/modules/builder/src/main/java/org/apache/tamaya/modules/builder/ProgrammaticConfigurationContext.java
deleted file mode 100644
index e748eaa..0000000
--- a/modules/builder/src/main/java/org/apache/tamaya/modules/builder/ProgrammaticConfigurationContext.java
+++ /dev/null
@@ -1,406 +0,0 @@
-/*
- * 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.modules.builder;
-
-
-import org.apache.tamaya.PropertyConverter;
-import org.apache.tamaya.TypeLiteral;
-import org.apache.tamaya.core.internal.PropertyConverterManager;
-import org.apache.tamaya.spi.ConfigurationContext;
-import org.apache.tamaya.spi.ConfigurationContextBuilder;
-import org.apache.tamaya.spi.PropertyFilter;
-import org.apache.tamaya.spi.PropertySource;
-import org.apache.tamaya.spi.PropertySourceProvider;
-import org.apache.tamaya.spi.PropertyValueCombinationPolicy;
-import org.apache.tamaya.spi.ServiceContext;
-
-import javax.annotation.Priority;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import java.util.StringJoiner;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.StampedLock;
-import java.util.function.Function;
-import java.util.function.Predicate;
-import java.util.logging.Logger;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-
-import static java.util.stream.Collectors.toList;
-
-/**
- * Implementation of the {@link org.apache.tamaya.spi.ConfigurationContext}
- * used by the {@link org.apache.tamaya.modules.builder.ConfigurationBuilder}
- * internally.
- */
-class ProgrammaticConfigurationContext implements ConfigurationContext {
-
- /**
- * The logger used.
- */
- private final static Logger LOG = Logger.getLogger(ProgrammaticConfigurationContext.class.getName());
- /**
- * Cubcomponent handling {@link org.apache.tamaya.PropertyConverter} instances.
- */
- private PropertyConverterManager propertyConverterManager = new PropertyConverterManager();
-
- /**
- * The current unmodifiable list of loaded {@link org.apache.tamaya.spi.PropertySource} instances.
- */
- private List<PropertySource> immutablePropertySources = new ArrayList<>();
-
- /**
- * The current unmodifiable list of loaded {@link org.apache.tamaya.spi.PropertyFilter} instances.
- */
- private List<PropertyFilter> immutablePropertyFilters = new ArrayList<>();
-
- /**
- * The overriding policy used when combining PropertySources registered to evalute the final configuration
- * values.
- */
- private PropertyValueCombinationPolicy propertyValueCombinationPolicy;
-
- /**
- * Lock for internal synchronization.
- */
- private StampedLock propertySourceLock = new StampedLock();
-
-
- /**
- * The first time the Configuration system gets invoked we do initialize
- * all our {@link org.apache.tamaya.spi.PropertySource}s and
- * {@link org.apache.tamaya.spi.PropertyFilter}s which are known at startup.
- */
- @SuppressWarnings("unchecked")
- public ProgrammaticConfigurationContext(Builder builder) {
- propertyConverterManager = new PropertyConverterManager(builder.loadProvidedPropertyConverters);
-
- immutablePropertySources = getAllPropertySources(builder).stream()
- .sorted(this::comparePropertySources)
- .collect(Collectors.toList());
-
-
- immutablePropertyFilters = getPropertyFilters(builder).stream()
- .sorted(this::comparePropertyFilters)
- .collect(toList());
-
-
- propertyValueCombinationPolicy = builder.propertyValueCombinationPolicy;
-
- builder.propertyConverters.forEach((literal, converters) -> {
- converters.stream().filter(c -> c != null)
- .forEach(c -> propertyConverterManager.register((TypeLiteral<Object>) literal,
- (PropertyConverter<Object>) c));
- });
-
- LOG.info(() -> "Using " + immutablePropertySources.size() + " property sources: " +
- createStringList(immutablePropertySources, ps -> ps.getName() + '[' + ps.getClass().getName() + ']'));
-
-
- LOG.info(() -> "Using " + immutablePropertyFilters.size() + " property filters: " +
- createStringList(immutablePropertyFilters, f -> f.getClass().getName()));
-
-
- LOG.info(() -> "Using PropertyValueCombinationPolicy: " + propertyValueCombinationPolicy);
- }
-
- private List<PropertyFilter> getPropertyFilters(Builder builder) {
- List<PropertyFilter> provided = builder.loadProvidedPropertyFilters
- ? ServiceContext.getInstance().getServices(PropertyFilter.class)
- : new ArrayList<>(0);
-
- List<PropertyFilter> configured = builder.propertyFilters;
-
- return Stream.of(provided, configured).flatMap(Collection::stream)
- .collect(toList());
- }
-
- private List<PropertySource> getAllPropertySources(Builder builder) {
- List<PropertySource> provided = builder.loadProvidedPropertySources
- ? ServiceContext.getInstance().getServices(PropertySource.class)
- : new ArrayList<>(0);
-
- if (builder.loadProvidedPropertySourceProviders) {
- List<PropertySourceProvider> providers = ServiceContext.getInstance()
- .getServices(PropertySourceProvider.class);
- for (PropertySourceProvider provider : providers) {
- Collection<PropertySource> sources = provider.getPropertySources();
- provided.addAll(sources);
- }
- }
-
- List<PropertySource> configured = builder.propertySources;
-
- return Stream.of(provided, configured).flatMap(Collection::stream)
- .collect(toList());
- }
-
- public void addPropertySources(PropertySource... propertySourcesToAdd) {
- Lock writeLock = propertySourceLock.asWriteLock();
- try {
- writeLock.lock();
- List<PropertySource> newPropertySources = new ArrayList<>(this.immutablePropertySources);
- newPropertySources.addAll(Arrays.asList(propertySourcesToAdd));
- Collections.sort(newPropertySources, this::comparePropertySources);
-
- this.immutablePropertySources = Collections.unmodifiableList(newPropertySources);
- } finally {
- writeLock.unlock();
- }
- }
-
- /**
- * Order property source reversely, the most important come first.
- *
- * @param source1 the first PropertySource
- * @param source2 the second PropertySource
- * @return the comparison result.
- */
- private int comparePropertySources(PropertySource source1, PropertySource source2) {
-
- //X TODO this method duplicates org.apache.tamaya.core.internal.DefaultConfigurationContext.PropertySourceComparator.comparePropertySources()
- //X maybe we should extract the Comperator in an own class for real code-reuse (copy paste == bad code reuse)
-
- if (source1.getOrdinal() < source2.getOrdinal()) {
- return -1;
- } else if (source1.getOrdinal() > source2.getOrdinal()) {
- return 1;
- } else {
- return source1.getClass().getName().compareTo(source2.getClass().getName());
- }
- }
-
- /**
- * Compare 2 filters for ordering the filter chain.
- *
- * @param filter1 the first filter
- * @param filter2 the second filter
- * @return the comparison result
- */
- private int comparePropertyFilters(PropertyFilter filter1, PropertyFilter filter2) {
-
- //X TODO this method duplicates org.apache.tamaya.core.internal.DefaultConfigurationContext.PropertySourceComparator.comparePropertyFilters()
- //X maybe we should extract the Comperator in an own class for real code-reuse (copy paste == bad code reuse)
-
- Priority prio1 = filter1.getClass().getAnnotation(Priority.class);
- Priority prio2 = filter2.getClass().getAnnotation(Priority.class);
- int ord1 = prio1 != null ? prio1.value() : 0;
- int ord2 = prio2 != null ? prio2.value() : 0;
-
- if (ord1 < ord2) {
- return -1;
- } else if (ord1 > ord2) {
- return 1;
- } else {
- return filter1.getClass().getName().compareTo(filter2.getClass().getName());
- }
- }
-
- @Override
- public List<PropertySource> getPropertySources() {
- return immutablePropertySources;
- }
-
- public <T> void addPropertyConverter(TypeLiteral<T> typeToConvert, PropertyConverter<T> propertyConverter) {
- propertyConverterManager.register(typeToConvert, propertyConverter);
- LOG.info(() -> "Added PropertyConverter: " + propertyConverter.getClass().getName());
- }
-
- @Override
- public Map<TypeLiteral<?>, List<PropertyConverter<?>>> getPropertyConverters() {
- return propertyConverterManager.getPropertyConverters();
- }
-
- @Override
- public <T> List<PropertyConverter<T>> getPropertyConverters(TypeLiteral<T> targetType) {
- return propertyConverterManager.getPropertyConverters(targetType);
- }
-
- @Override
- public List<PropertyFilter> getPropertyFilters() {
- return immutablePropertyFilters;
- }
-
- @Override
- public PropertyValueCombinationPolicy getPropertyValueCombinationPolicy() {
- return propertyValueCombinationPolicy;
- }
-
- private <T> String createStringList(Collection<T> propertySources, Function<T, String> mapper) {
- StringJoiner joiner = new StringJoiner(", ");
- propertySources.forEach(t -> joiner.add(mapper.apply(t)));
- return joiner.toString();
- }
-
- @Override
- public ConfigurationContextBuilder toBuilder() {
- // @todo Check if it could be useful to support this method, Oliver B. Fischer
- throw new RuntimeException("This method is currently not supported.");
- }
-
- @Override
- public Collection<PropertySource> getPropertySources(Predicate<PropertySource> selector) {
- // @todo Check if it could be useful to support this method, Oliver B. Fischer
- throw new RuntimeException("This method is currently not supported.");
- }
-
- /**
- * The Builder for {@link ProgrammaticConfigurationContext}
- */
- public final static class Builder {
- /**
- * The current unmodifiable list of loaded {@link org.apache.tamaya.spi.PropertySource} instances.
- */
- private List<PropertySource> propertySources = new ArrayList<>();
-
- /**
- * The current unmodifiable list of loaded {@link org.apache.tamaya.spi.PropertyFilter} instances.
- */
- private List<PropertyFilter> propertyFilters = new ArrayList<>();
-
- private Map<TypeLiteral<?>, List<PropertyConverter<?>>> propertyConverters = new HashMap<>();
-
- /**
- * The overriding policy used when combining PropertySources registered to evalute the final configuration
- * values.
- */
- private PropertyValueCombinationPolicy propertyValueCombinationPolicy =
- PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR;
-
- private boolean loadProvidedPropertyConverters;
- private boolean loadProvidedPropertySources;
- private boolean loadProvidedPropertySourceProviders;
- private boolean loadProvidedPropertyFilters;
-
- public Builder setPropertyValueCombinationPolicy(PropertyValueCombinationPolicy policy) {
- this.propertyValueCombinationPolicy = Objects.requireNonNull(policy);
- return this;
- }
-
- public Builder addPropertySources(PropertySource... propertySources) {
- List<PropertySource> filtered = Stream.of(propertySources).filter(this::isNotNull)
- .collect(toList());
-
- this.propertySources.addAll(filtered);
-
- return this;
- }
-
- public Builder addPropertySources(Collection<PropertySource> propertySources) {
- List<PropertySource> filtered = propertySources.stream().filter(this::isNotNull)
- .collect(toList());
-
- this.propertySources.addAll(filtered);
-
- return this;
- }
-
- public Builder addPropertySourceProviders(PropertySourceProvider... propertySourceProviders) {
- List<PropertySourceProvider> providers = Stream.of(propertySourceProviders).filter(this::isNotNull)
- .collect(toList());
-
- return addPropertySourceProviders(providers);
- }
-
- public Builder addPropertySourceProviders(Collection<PropertySourceProvider> providers) {
- List<PropertySource> filtered = providers.stream().filter(this::isNotNull)
- .flatMap(p -> p.getPropertySources().stream())
- .filter(this::isNotNull)
- .collect(toList());
-
- this.propertySources.addAll(filtered);
-
- return this;
- }
-
- public Builder addPropertyFilters(PropertyFilter... propertySources) {
- List<PropertyFilter> sources = Stream.of(propertySources).filter(this::isNotNull)
- .collect(toList());
-
- this.propertyFilters.addAll(sources);
-
- return this;
- }
-
- public Builder addPropertyFilters(Collection<PropertyFilter> propertySources) {
- List<PropertyFilter> sources = propertySources.stream().filter(this::isNotNull)
- .collect(toList());
-
- this.propertyFilters.addAll(sources);
-
- return this;
- }
-
- /**
- * Should be never used.
- */
- @Deprecated
- public Builder setConfigurationContext(ConfigurationContext configurationContext) {
- this.addPropertySources(configurationContext.getPropertySources());
- this.addPropertyFilters(configurationContext.getPropertyFilters());
- this.propertyValueCombinationPolicy = Objects.requireNonNull(
- configurationContext.getPropertyValueCombinationPolicy());
- return this;
- }
-
- //X TODO think on a functonality/API for using the default PropertyConverters and use the configured ones here
- //X TODO as overrides used first.
-
- public <T> Builder addPropertyConverter(TypeLiteral<T> type, PropertyConverter<T> propertyConverter) {
- propertyConverters.computeIfAbsent(type, (t) -> new ArrayList<>())
- .add(propertyConverter);
-
- return this;
- }
-
- public ConfigurationContext build() {
- return new ProgrammaticConfigurationContext(this);
- }
-
-
- public void loadProvidedPropertyConverters(boolean state) {
- loadProvidedPropertyConverters = state;
- }
-
- public void loadProvidedPropertySources(boolean state) {
- loadProvidedPropertySources = state;
- }
-
- public void loadProvidedPropertySourceProviders(boolean state) {
- loadProvidedPropertySourceProviders = state;
- }
-
- public void loadProvidedPropertyFilters(boolean state) {
- loadProvidedPropertyFilters = state;
- }
-
- private <T> boolean isNotNull(T item) {
- return null != item;
- }
- }
-
-
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/812b11ea/modules/builder/src/main/java/org/apache/tamaya/modules/builder/package-info.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/main/java/org/apache/tamaya/modules/builder/package-info.java b/modules/builder/src/main/java/org/apache/tamaya/modules/builder/package-info.java
deleted file mode 100644
index dacba73..0000000
--- a/modules/builder/src/main/java/org/apache/tamaya/modules/builder/package-info.java
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * 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.
- */
-/**
- * This package provides
- * {@link org.apache.tamaya.modules.builder.ConfigurationBuilder a configuration
- * builder} that allows to build a configuration manually without
- * using exclusively on the Service Provider Interface API of Tamaya.
- *
- * @see org.apache.tamaya.modules.builder.ConfigurationBuilder
- * @see org.apache.tamaya.Configuration
- */
-package org.apache.tamaya.modules.builder;
\ No newline at end of file