Repository: incubator-tamaya Updated Branches: refs/heads/master f3c2ed3be -> 890875804
TAMAYA-60 It is now configurable if all PropertyConverter service providers will be loaded automatically or not. Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/3628c35e Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/3628c35e Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/3628c35e Branch: refs/heads/master Commit: 3628c35e36d12165803951f910ff898d8813bb13 Parents: f3c2ed3 Author: Oliver B. Fischer <[email protected]> Authored: Sat Feb 7 20:52:59 2015 +0100 Committer: Oliver B. Fischer <[email protected]> Committed: Sat Feb 7 20:52:59 2015 +0100 ---------------------------------------------------------------------- .../tamaya/builder/ConfigurationBuilder.java | 8 ++++- .../ProgrammaticConfigurationContext.java | 7 +++- .../builder/ConfigurationBuilderTest.java | 35 +++++++++++++++++++ .../tamaya/builder/util/types/CustomTypeC.java | 36 ++++++++++++++++++++ .../types/CustomTypeCPropertyConverter.java | 28 +++++++++++++++ .../org.apache.tamaya.PropertyConverter | 19 +++++++++++ 6 files changed, 131 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3628c35e/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 index e327672..cbfc6a5 100644 --- a/modules/builder/src/main/java/org/apache/tamaya/builder/ConfigurationBuilder.java +++ b/modules/builder/src/main/java/org/apache/tamaya/builder/ConfigurationBuilder.java @@ -32,7 +32,7 @@ import java.util.Objects; /* TODO LIST FOR TAMAYA-60 * - * - configurable loading of provided PropertyConverter + * - configurable loading of provided PropertyConverter DONE * - configurable loading of provided PropertySources * - configurable loading of provided PropertySourceProviders * - adding sources via URL @@ -126,6 +126,8 @@ public class ConfigurationBuilder { * @see #disableProvidedPropertyConverters() */ public ConfigurationBuilder enableProvidedPropertyConverters() { + checkBuilderState(); + loadProvidedPropertyConverters = true; return this; @@ -139,6 +141,8 @@ public class ConfigurationBuilder { * @see #enableProvidedPropertyConverters() */ public ConfigurationBuilder disableProvidedPropertyConverters() { + checkBuilderState(); + loadProvidedPropertyConverters = false; return this; @@ -157,6 +161,8 @@ public class ConfigurationBuilder { built = true; + contextBuilder.loadProvidedPropertyConverters(isPropertyConverterLoadingEnabled()); + return new DefaultConfiguration(contextBuilder.build()); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3628c35e/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 index c8e0875..dceabab 100644 --- a/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfigurationContext.java +++ b/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfigurationContext.java @@ -88,6 +88,7 @@ class ProgrammaticConfigurationContext implements ConfigurationContext { * {@link org.apache.tamaya.spi.PropertyFilter}s which are known at startup. */ public ProgrammaticConfigurationContext(Builder builder) { + propertyConverterManager = new PropertyConverterManager(builder.loadProvidedPropertyConverters); immutablePropertySources.addAll(builder.propertySources); Collections.sort(immutablePropertySources, this::comparePropertySources); immutablePropertySources = Collections.unmodifiableList(immutablePropertySources); @@ -238,6 +239,8 @@ class ProgrammaticConfigurationContext implements ConfigurationContext { private PropertyValueCombinationPolicy propertyValueCombinationPolicy = PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR; + private boolean loadProvidedPropertyConverters; + public Builder setPropertyValueCombinationPolicy(PropertyValueCombinationPolicy policy) { this.propertyValueCombinationPolicy = Objects.requireNonNull(policy); return this; @@ -307,7 +310,9 @@ class ProgrammaticConfigurationContext implements ConfigurationContext { } - + public void loadProvidedPropertyConverters(boolean state) { + loadProvidedPropertyConverters = state; + } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3628c35e/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 index a833217..cd5e3eb 100644 --- a/modules/builder/src/test/java/org/apache/tamaya/builder/ConfigurationBuilderTest.java +++ b/modules/builder/src/test/java/org/apache/tamaya/builder/ConfigurationBuilderTest.java @@ -25,6 +25,7 @@ import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.builder.util.mockito.NotMockedAnswer; 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.PropertySource; import org.hamcrest.CoreMatchers; import org.hamcrest.Matchers; @@ -34,6 +35,7 @@ import org.junit.Test; 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; @@ -357,4 +359,37 @@ public class ConfigurationBuilderTest { assertThat(builder.isPropertyConverterLoadingEnabled(), is(false)); } + @Test(expected = ConfigException.class) + public void bla() { + PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER); + + doReturn("source").when(source).getName(); + doReturn("A").when(source).get("key"); + + ConfigurationBuilder builder = new ConfigurationBuilder().addPropertySources(source) + .disableProvidedPropertyConverters(); + + Configuration config = builder.build(); + + config.get("key", CustomTypeC.class); + } + + @Test + public void bla2() { + PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER); + + doReturn("source").when(source).getName(); + doReturn("A").when(source).get("key"); + + ConfigurationBuilder builder = new ConfigurationBuilder().addPropertySources(source) + .enableProvidedPropertyConverters(); + + Configuration config = builder.build(); + + CustomTypeC result = config.get("key", CustomTypeC.class); + + assertThat(result, notNullValue()); + assertThat(result.getValue(), equalTo("A")); + } + } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3628c35e/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/3628c35e/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..ed51277 --- /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<CustomTypeC> { + @Override + public CustomTypeC convert(String value) { + return CustomTypeC.produceFrom(value); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3628c35e/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 new file mode 100644 index 0000000..b9e0d44 --- /dev/null +++ b/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.PropertyConverter @@ -0,0 +1,19 @@ +# +# 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 current 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. +# +org.apache.tamaya.builder.util.types.CustomTypeCPropertyConverter \ No newline at end of file
