TAMAYA-260: Implemented all basic microprofile features. Last 3 failres are to be discussed with MP folks.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/commit/1e2a184f Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/tree/1e2a184f Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/diff/1e2a184f Branch: refs/heads/java8 Commit: 1e2a184f80167576c6b884f004083bf5496cfe95 Parents: d9e2d54 Author: anatole <anat...@apache.org> Authored: Wed Aug 9 00:18:52 2017 +0200 Committer: anatole <anat...@apache.org> Committed: Wed Aug 9 00:18:52 2017 +0200 ---------------------------------------------------------------------- .../microprofile/MicroprofileAdapter.java | 1 + .../microprofile/MicroprofileConfigBuilder.java | 46 +++- .../MicroprofileConfigProviderResolver.java | 8 +- .../MicroprofileConfigurationProducer.java | 104 ++++++++ .../MicroprofileDefaultProperties.java | 33 +++ .../microprofile/TamayaPropertySource.java | 4 +- .../org.apache.tamaya.spi.PropertySource | 20 ++ .../MicroprofileConfigBuilderTest.java | 3 +- .../MicroprofileConfigProviderTest.java | 2 +- .../microprofile/MicroprofileConfigTest.java | 11 +- .../MPSystemPropertiesConfigSource.java | 34 --- .../AutoDiscoveredConfigSourceTest.java | 64 +++++ .../imported/CDIPlainInjectionTest.java | 226 ++++++++++++++++++ .../imported/ConfigProviderTest.java | 143 +++++++++++ .../microprofile/imported/ConverterTest.java | 235 +++++++++++++++++++ .../imported/CustomConfigSourceTest.java | 61 +++++ .../imported/OptionalValuesBean.java | 61 +++++ .../imported/broken/ConfigOwner.java | 39 +++ .../imported/broken/CustomConverterBean.java | 46 ++++ ...MissingConverterOnInstanceInjectionTest.java | 62 +++++ .../MissingValueOnInstanceInjectionTest.java | 61 +++++ .../WrongConverterOnInstanceInjectionTest.java | 63 +++++ .../CustomConfigSourceProvider.java | 53 +++++ .../configsources/CustomDbConfigSource.java | 69 ++++++ .../configsources/SampleYamlConfigSource.java | 58 +++++ .../microprofile/imported/converters/Duck.java | 36 +++ .../imported/converters/DuckConverter.java | 33 +++ .../microprofile/imported/converters/Pizza.java | 45 ++++ .../imported/converters/PizzaConverter.java | 33 +++ .../imported/matchers/AdditionalMatchers.java | 61 +++++ .../tck/TamayaConfigArchiveProcessor.java | 69 ++++++ .../microprofile/tck/TamayaConfigExtension.java | 36 +++ .../META-INF/microprofile-config.properties | 100 ++++++++ ...eclipse.microprofile.config.spi.ConfigSource | 2 +- ...microprofile.config.spi.ConfigSourceProvider | 18 ++ ....jboss.arquillian.core.spi.LoadableExtension | 19 ++ microprofile/src/test/tck-suite.xml | 29 +++ microprofile/tck-suite.xml | 29 --- 38 files changed, 1938 insertions(+), 79 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileAdapter.java ---------------------------------------------------------------------- diff --git a/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileAdapter.java b/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileAdapter.java index 20d5af0..8f5002c 100644 --- a/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileAdapter.java +++ b/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileAdapter.java @@ -75,6 +75,7 @@ public final class MicroprofileAdapter{ for(PropertySource ps:propertySources){ configSources.add(toConfigSource(ps)); } + Collections.reverse(configSources); return configSources; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigBuilder.java ---------------------------------------------------------------------- diff --git a/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigBuilder.java b/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigBuilder.java index 5e6b613..aacd31b 100644 --- a/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigBuilder.java +++ b/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigBuilder.java @@ -21,9 +21,14 @@ package org.apache.tamaya.microprofile; import org.apache.tamaya.ConfigurationProvider; import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.spi.ConfigurationContextBuilder; +import org.apache.tamaya.spi.ServiceContextManager; +import org.apache.tamaya.spisupport.EnvironmentPropertySource; +import org.apache.tamaya.spisupport.PropertySourceComparator; +import org.apache.tamaya.spisupport.SystemPropertySource; import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.config.spi.ConfigBuilder; import org.eclipse.microprofile.config.spi.ConfigSource; +import org.eclipse.microprofile.config.spi.ConfigSourceProvider; import org.eclipse.microprofile.config.spi.Converter; import java.util.Objects; @@ -37,26 +42,63 @@ final class MicroprofileConfigBuilder implements ConfigBuilder{ MicroprofileConfigBuilder(ConfigurationContextBuilder contextBuilder){ this.contextBuilder = Objects.requireNonNull(contextBuilder); + contextBuilder.addDefaultPropertyConverters(); } public ConfigurationContextBuilder getConfigurationContextBuilder(){ return contextBuilder; } + /** + * Add the default config sources appearing on the builder's classpath + * including: + * <ol> + * <li>System properties</li> + * <li>Environment properties</li> + * <li>/META-INF/microprofile-config.properties</li> + * </ol> + * + * @return the ConfigBuilder with the default config sources + */ @Override public ConfigBuilder addDefaultSources() { + contextBuilder.addPropertySources( + new SystemPropertySource(400), + new EnvironmentPropertySource(300), + new MicroprofileDefaultProperties()); + contextBuilder.sortPropertySources(PropertySourceComparator.getInstance() + .setOrdinalKey("config_ordinal")); return this; } + /** + * Add ConfigSources registered using the ServiceLoader. + * @return the ConfigBuilder with the added config sources + */ @Override public ConfigBuilder addDiscoveredSources() { - contextBuilder.addDefaultPropertySources(); + for(ConfigSource configSource: ServiceContextManager.getServiceContext().getServices(ConfigSource.class)){ + contextBuilder.addPropertySources(MicroprofileAdapter.toPropertySource(configSource)); + } + for(ConfigSourceProvider configSourceProvider: ServiceContextManager.getServiceContext().getServices(ConfigSourceProvider.class)){ + contextBuilder.addPropertySources(MicroprofileAdapter.toPropertySources(configSourceProvider.getConfigSources( + Thread.currentThread().getContextClassLoader() + ))); + } + contextBuilder.sortPropertySources(PropertySourceComparator.getInstance()); return this; } + /** + * Add Converters registered using the ServiceLoader. + * @return the ConfigBuilder with the added config converters + */ @Override public ConfigBuilder addDiscoveredConverters() { - contextBuilder.addDefaultPropertyConverters(); + for(Converter<?> converter: ServiceContextManager.getServiceContext().getServices(Converter.class)){ + TypeLiteral lit = TypeLiteral.of(TypeLiteral.of(converter.getClass()).getType()); + contextBuilder.addPropertyConverters(lit, MicroprofileAdapter.toPropertyConverter(converter)); + } return this; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigProviderResolver.java ---------------------------------------------------------------------- diff --git a/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigProviderResolver.java b/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigProviderResolver.java index c35bd35..92a928a 100644 --- a/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigProviderResolver.java +++ b/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigProviderResolver.java @@ -19,6 +19,7 @@ package org.apache.tamaya.microprofile; import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.spi.ConfigurationContextBuilder; import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.config.spi.ConfigBuilder; import org.eclipse.microprofile.config.spi.ConfigProviderResolver; @@ -43,7 +44,12 @@ public class MicroprofileConfigProviderResolver extends ConfigProviderResolver { public Config getConfig(ClassLoader loader) { Config config = this.configs.get(loader); if(config==null){ - config = MicroprofileAdapter.toConfig(ConfigurationProvider.getConfiguration()); + ConfigurationContextBuilder builder = ConfigurationProvider.getConfigurationContextBuilder(); + builder.addDefaultPropertyConverters(); + MicroprofileConfigBuilder microConfigBuilder = new MicroprofileConfigBuilder(builder); + microConfigBuilder.addDefaultSources(); + microConfigBuilder.addDiscoveredSources(); + config = microConfigBuilder.build(); this.configs.put(loader, config); } return config; http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigurationProducer.java ---------------------------------------------------------------------- diff --git a/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigurationProducer.java b/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigurationProducer.java new file mode 100644 index 0000000..2bc3abf --- /dev/null +++ b/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigurationProducer.java @@ -0,0 +1,104 @@ +/* + * 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.microprofile; + +import org.apache.tamaya.*; +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.eclipse.microprofile.config.Config; +import org.eclipse.microprofile.config.ConfigProvider; +import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.eclipse.microprofile.config.spi.ConfigBuilder; +import org.eclipse.microprofile.config.spi.ConfigProviderResolver; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.context.RequestScoped; +import javax.enterprise.inject.New; +import javax.enterprise.inject.Produces; +import javax.enterprise.inject.spi.InjectionPoint; +import java.lang.reflect.AnnotatedElement; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Producer bean for configuration properties. + */ +@ApplicationScoped +public class MicroprofileConfigurationProducer { + + private static final Logger LOGGER = Logger.getLogger(MicroprofileConfigurationProducer.class.getName()); + + @Produces + @ConfigProperty + public Object resolveAndConvert(final InjectionPoint injectionPoint) { + final ConfigProperty annotation = injectionPoint.getAnnotated().getAnnotation(ConfigProperty.class); + String key = annotation.name(); + + // unless the extension is not installed, this should never happen because the extension + // enforces the resolvability of the config + Configuration config = ConfigurationProvider.getConfiguration(); + final Class<?> toType = (Class<?>) injectionPoint.getAnnotated().getBaseType(); + String defaultTextValue = annotation.defaultValue().isEmpty() ? null : annotation.defaultValue(); + String textValue = config.get(key); + ConversionContext.Builder builder = new ConversionContext.Builder(config, + ConfigurationProvider.getConfiguration().getContext(), key, TypeLiteral.of(toType)); + if (injectionPoint.getMember() instanceof AnnotatedElement) { + builder.setAnnotatedElement((AnnotatedElement) injectionPoint.getMember()); + } + ConversionContext conversionContext = builder.build(); + if (textValue == null) { + textValue = defaultTextValue; + } + Object value = null; + if (textValue != null) { + List<PropertyConverter<Object>> converters = ConfigurationProvider.getConfiguration().getContext() + .getPropertyConverters(TypeLiteral.of(toType)); + for (PropertyConverter<Object> converter : converters) { + try { + value = converter.convert(textValue, conversionContext); + if (value != null) { + LOGGER.log(Level.FINEST, "Parsed default value from '" + textValue + "' into " + + injectionPoint); + break; + } + } catch (Exception e) { + LOGGER.log(Level.FINEST, "Failed to convert value '" + textValue + "' for " + + injectionPoint, e); + } + } + } + if (value == null) { + throw new ConfigException(String.format( + "Can't resolve any of the possible config keys: %s to the required target type: %s, supported formats: %s", + key, toType.getName(), conversionContext.getSupportedFormats().toString())); + } + LOGGER.finest(String.format("Injecting %s for key %s in class %s", key, value.toString(), injectionPoint.toString())); + return value; + } + + @Produces + public Config getConfiguration(){ + return ConfigProvider.getConfig(Thread.currentThread().getContextClassLoader()); + } + + @Produces + public ConfigBuilder getConfigBuilder(){ + return ConfigProviderResolver.instance().getBuilder(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileDefaultProperties.java ---------------------------------------------------------------------- diff --git a/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileDefaultProperties.java b/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileDefaultProperties.java new file mode 100644 index 0000000..cfe06bd --- /dev/null +++ b/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileDefaultProperties.java @@ -0,0 +1,33 @@ +/* + * 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.microprofile; + +import org.apache.tamaya.spisupport.PropertiesResourcePropertySource; + + +/** + * Default property source for config properties in the classpath. + */ +public class MicroprofileDefaultProperties extends PropertiesResourcePropertySource{ + + public MicroprofileDefaultProperties() { + super("META-INF/microprofile-config.properties", null); + setDefaultOrdinal(100); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/main/java/org/apache/tamaya/microprofile/TamayaPropertySource.java ---------------------------------------------------------------------- diff --git a/microprofile/src/main/java/org/apache/tamaya/microprofile/TamayaPropertySource.java b/microprofile/src/main/java/org/apache/tamaya/microprofile/TamayaPropertySource.java index a1913e8..f526514 100644 --- a/microprofile/src/main/java/org/apache/tamaya/microprofile/TamayaPropertySource.java +++ b/microprofile/src/main/java/org/apache/tamaya/microprofile/TamayaPropertySource.java @@ -25,6 +25,7 @@ import org.eclipse.microprofile.config.spi.ConfigSource; import java.util.HashMap; import java.util.Map; import java.util.Objects; +import java.util.Optional; /** * Property source implementation that wraps a Microprofile {@link ConfigSource} instance. @@ -48,7 +49,8 @@ public class TamayaPropertySource implements PropertySource{ @Override public String getName() { - return delegate.getName(); + return Optional.ofNullable(delegate.getName()) + .orElse(delegate.toString()); } @Override http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource ---------------------------------------------------------------------- diff --git a/microprofile/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource b/microprofile/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource new file mode 100644 index 0000000..585700b --- /dev/null +++ b/microprofile/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource @@ -0,0 +1,20 @@ +# +# 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. +# + +org.apache.tamaya.microprofile.MicroprofileDefaultProperties \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/test/java/org/apache/tamaya/microprofile/MicroprofileConfigBuilderTest.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/MicroprofileConfigBuilderTest.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/MicroprofileConfigBuilderTest.java index 6076d1b..e7cc6fd 100644 --- a/microprofile/src/test/java/org/apache/tamaya/microprofile/MicroprofileConfigBuilderTest.java +++ b/microprofile/src/test/java/org/apache/tamaya/microprofile/MicroprofileConfigBuilderTest.java @@ -83,7 +83,7 @@ public class MicroprofileConfigBuilderTest { } @Test - @Ignore +// @Ignore public void testBuildDefaultConfig(){ ConfigBuilder builder = ConfigProviderResolver.instance().getBuilder(); assertNotNull(builder); @@ -95,7 +95,6 @@ public class MicroprofileConfigBuilderTest { assertNotNull(config.getValue("java.home", String.class)); ConfigSource src = config.getConfigSources().iterator().next(); assertNotNull(src); - assertEquals(src.getName(), "system-properties"); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/test/java/org/apache/tamaya/microprofile/MicroprofileConfigProviderTest.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/MicroprofileConfigProviderTest.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/MicroprofileConfigProviderTest.java index 0c77506..46f1d2b 100644 --- a/microprofile/src/test/java/org/apache/tamaya/microprofile/MicroprofileConfigProviderTest.java +++ b/microprofile/src/test/java/org/apache/tamaya/microprofile/MicroprofileConfigProviderTest.java @@ -43,7 +43,7 @@ public class MicroprofileConfigProviderTest { count++; System.out.println(count + ": " +name); } - assertEquals(ConfigurationProvider.getConfiguration().getProperties().size(), count); + assertTrue(ConfigurationProvider.getConfiguration().getProperties().size() <= count); } @Test http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/test/java/org/apache/tamaya/microprofile/MicroprofileConfigTest.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/MicroprofileConfigTest.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/MicroprofileConfigTest.java index 96e7123..21d0bb9 100644 --- a/microprofile/src/test/java/org/apache/tamaya/microprofile/MicroprofileConfigTest.java +++ b/microprofile/src/test/java/org/apache/tamaya/microprofile/MicroprofileConfigTest.java @@ -35,15 +35,14 @@ import static org.junit.Assert.*; public class MicroprofileConfigTest { @Test - public void testDefaultConfigAccess(){ + public void testDefaultConfigAccess() { Config config = ConfigProvider.getConfig(); Iterable<ConfigSource> sources = config.getConfigSources(); int count = 0; - for(ConfigSource cs:sources){ + for (ConfigSource cs : sources) { count++; - assertEquals(cs.getClass(), MicroprofileConfigSource.class); } - assertTrue(count == ConfigurationProvider.getConfiguration().getContext().getPropertySources().size()); + assertEquals(count, 5); } @Test @@ -53,8 +52,6 @@ public class MicroprofileConfigTest { for(String key:config.getPropertyNames()){ Optional<String> val = config.getOptionalValue(key, String.class); assertNotNull(val); - assertEquals(val.orElse("N/A"), - ConfigurationProvider.getConfiguration().get(key)); val = config.getOptionalValue(key + System.currentTimeMillis(), String.class); assertNotNull(val); assertFalse(val.isPresent()); @@ -68,8 +65,6 @@ public class MicroprofileConfigTest { for(String key:config.getPropertyNames()){ String val = config.getValue(key, String.class); assertNotNull(val); - assertEquals(val, - ConfigurationProvider.getConfiguration().get(key)); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/test/java/org/apache/tamaya/microprofile/configsources/MPSystemPropertiesConfigSource.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/configsources/MPSystemPropertiesConfigSource.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/configsources/MPSystemPropertiesConfigSource.java deleted file mode 100644 index 62ecbd4..0000000 --- a/microprofile/src/test/java/org/apache/tamaya/microprofile/configsources/MPSystemPropertiesConfigSource.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.microprofile.configsources; - -import org.apache.tamaya.microprofile.MicroprofileConfigSource; -import org.apache.tamaya.spisupport.SystemPropertySource; - -/** - * Microprofile config source based on Tamaya's system property source. - * Created by atsticks on 30.03.17. - */ -public class MPSystemPropertiesConfigSource extends MicroprofileConfigSource { - - public MPSystemPropertiesConfigSource() { - super(new SystemPropertySource()); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/AutoDiscoveredConfigSourceTest.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/AutoDiscoveredConfigSourceTest.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/AutoDiscoveredConfigSourceTest.java new file mode 100644 index 0000000..5aa037e --- /dev/null +++ b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/AutoDiscoveredConfigSourceTest.java @@ -0,0 +1,64 @@ +/* + ******************************************************************************* + * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed 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.microprofile.imported; + +import org.eclipse.microprofile.config.Config; +import org.eclipse.microprofile.config.spi.ConfigProviderResolver; +import org.eclipse.microprofile.config.tck.converters.Pizza; +import org.junit.Assert; +import org.junit.Test; + +/** + * Verify the method addDiscoveredSources() on ConfigBuilder. + * + * @author <a href="mailto:emiji...@uk.ibm.com">Emily Jiang</a> + * @author <a href="mailto:strub...@apache.org">Mark Struberg</a> + */ +public class AutoDiscoveredConfigSourceTest { + + @Test + public void testAutoDiscoveredConfigureSources() { + Config config = ConfigProviderResolver.instance().getBuilder().addDefaultSources().addDiscoveredSources().build(); + Assert.assertEquals(config.getValue("tck.config.test.customDbConfig.key1", String.class), "valueFromDb1"); + } + + @Test + public void testAutoDiscoveredConverterManuallyAdded() { + + Config config = ConfigProviderResolver.instance().getBuilder().addDefaultSources().addDiscoveredSources().addDiscoveredConverters().build(); + Pizza dVaule = config.getValue("tck.config.test.customDbConfig.key3", Pizza.class); + Assert.assertEquals(dVaule.getSize(), "big"); + Assert.assertEquals(dVaule.getFlavor(), "cheese"); + } + + @Test + public void testAutoDiscoveredConverterNotAddedAutomatically() { + Config config = ConfigProviderResolver.instance().getBuilder().addDefaultSources().addDiscoveredSources().build(); + try { + // Pizza is too simple, so Tamaya find's a way to construct it. + Pizza dVaule = config.getValue("tck.config.test.customDbConfig.key3", Pizza.class); + Assert.fail("The auto discovered converter should not be added automatically."); + } + catch (Exception e) { + Assert.assertTrue( e instanceof IllegalArgumentException); + } + + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/CDIPlainInjectionTest.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/CDIPlainInjectionTest.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/CDIPlainInjectionTest.java new file mode 100644 index 0000000..170f55b --- /dev/null +++ b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/CDIPlainInjectionTest.java @@ -0,0 +1,226 @@ +/* + * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed 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.microprofile.imported; + +import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.eclipse.microprofile.config.spi.ConfigSource; +import org.jboss.arquillian.testng.Arquillian; +import org.testng.annotations.Test; + +import javax.enterprise.context.Dependent; +import javax.enterprise.inject.spi.CDI; +import javax.inject.Inject; +import javax.inject.Provider; +import java.util.HashMap; +import java.util.Map; + +import static org.eclipse.microprofile.config.tck.matchers.AdditionalMatchers.floatCloseTo; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +/** + * Test cases for CDI-based API that test retrieving values from the configuration. + * The tests depend only on CDI 1.2. + * @author Ondrej Mihalyi + */ +public class CDIPlainInjectionTest extends Arquillian{ + + private static final String DEFAULT_PROPERTY_BEAN_KEY = + "org.eclipse.microprofile.config.tck.CDIPlainInjectionTest.defaultPropertyBean.configProperty"; + + @Test + public void can_inject_simple_values_when_defined() { + ensure_all_property_values_are_defined(); + + SimpleValuesBean bean = getBeanOfType(SimpleValuesBean.class); + + assertThat(bean.stringProperty, is(equalTo("text"))); + assertThat(bean.booleanProperty, is(true)); + assertThat(bean.intProperty, is(equalTo(5))); + assertThat(bean.longProperty, is(equalTo(10L))); + assertThat(bean.floatProperty, is(floatCloseTo(10.5f, 0.1f))); + assertThat(bean.doubleProperty, is(closeTo(11.5, 0.1))); + + assertThat(bean.booleanObjProperty, is(true)); + assertThat(bean.integerProperty, is(equalTo(5))); + assertThat(bean.longObjProperty, is(equalTo(10L))); + assertThat(bean.floatObjProperty, is(floatCloseTo(10.5f, 0.1f))); + assertThat(bean.doubleObjProperty, is(closeTo(11.5, 0.1))); + + assertThat(bean.doublePropertyWithDefaultValue, is(closeTo(3.1415, 0.1))); + } + + @Test + public void can_inject_dynamic_values_via_CDI_provider() { + clear_all_property_values(); + + DynamicValuesBean bean = getBeanOfType(DynamicValuesBean.class); + + //X TODO clarify how Provider<T> should behave for missing values assertThat(bean.getIntProperty(), is(nullValue())); + + ensure_all_property_values_are_defined(); + + assertThat(bean.getIntProperty(), is(equalTo(5))); + } + + @Test + public void can_inject_default_property_path() { + clear_all_property_values(); + + ensure_all_property_values_are_defined(); + + DefaultPropertyBean bean = getBeanOfType(DefaultPropertyBean.class); + + assertThat(bean.getConfigProperty(), is(equalTo("pathConfigValue"))); + } + + private void ensure_all_property_values_are_defined() { + System.setProperty("my.string.property", "text"); + System.setProperty("my.boolean.property", "true"); + System.setProperty("my.int.property", "5"); + System.setProperty("my.long.property", "10"); + System.setProperty("my.float.property", "10.5"); + System.setProperty("my.double.property", "11.5"); + System.setProperty(DEFAULT_PROPERTY_BEAN_KEY, "pathConfigValue"); + } + + private void clear_all_property_values() { + System.getProperties().remove("my.string.property"); + System.getProperties().remove("my.boolean.property"); + System.getProperties().remove("my.int.property"); + System.getProperties().remove("my.long.property"); + System.getProperties().remove("my.float.property"); + System.getProperties().remove("my.double.property"); + System.getProperties().remove(DEFAULT_PROPERTY_BEAN_KEY); + } + + private <T> T getBeanOfType(Class<T> beanClass) { + return CDI.current().select(beanClass).get(); + } + + @Dependent + public static class SimpleValuesBean { + + @Inject + @ConfigProperty(name="my.string.property") + private String stringProperty; + + @Inject + @ConfigProperty(name="my.boolean.property") + private Boolean booleanObjProperty; + + @Inject + @ConfigProperty(name="my.boolean.property") + private boolean booleanProperty; + + @Inject + @ConfigProperty(name="my.int.property") + private Integer integerProperty; + + @Inject + @ConfigProperty(name="my.int.property") + private int intProperty; + + @Inject + @ConfigProperty(name="my.long.property") + private Long longObjProperty; + + @Inject + @ConfigProperty(name="my.long.property") + private long longProperty; + + @Inject + @ConfigProperty(name="my.float.property") + private Float floatObjProperty; + + @Inject + @ConfigProperty(name="my.float.property") + private float floatProperty; + + @Inject + @ConfigProperty(name="my.double.property") + private Double doubleObjProperty; + + @Inject + @ConfigProperty(name="my.double.property") + private double doubleProperty; + + // the property is not configured in any ConfigSource but its defaultValue will + // be used to set the field. + @Inject + @ConfigProperty(name="my.not.configured.double.property", defaultValue = "3.1415") + private Double doublePropertyWithDefaultValue; + + } + + @Dependent + public static class DynamicValuesBean { + + @Inject + @ConfigProperty(name="my.int.property") + private Provider<Integer> intPropertyProvider; + + public Integer getIntProperty() { + return intPropertyProvider.get(); + } + + } + + @Dependent + public static class DefaultPropertyBean { + @Inject + @ConfigProperty + private String configProperty; + + public String getConfigProperty() { + return configProperty; + } + } + + public static class TestConfigSource implements ConfigSource { + + private Map<String, String> properties; + + public TestConfigSource() { + properties = new HashMap<>(); + properties.put("my.string.property", "text"); + properties.put("my.boolean.property", "true"); + properties.put("my.int.property", "5"); + properties.put("my.long.property", "10"); + properties.put("my.float.property", "10.5"); + properties.put("my.double.property", "11.5"); + } + + @Override + public Map<String, String> getProperties() { + return properties; + } + + @Override + public String getValue(String propertyName) { + return properties.get(propertyName); + } + + @Override + public String getName() { + return this.getClass().getName(); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/ConfigProviderTest.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/ConfigProviderTest.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/ConfigProviderTest.java new file mode 100644 index 0000000..4a6e351 --- /dev/null +++ b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/ConfigProviderTest.java @@ -0,0 +1,143 @@ +/* + * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed 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.microprofile.imported; + +import org.eclipse.microprofile.config.Config; +import org.eclipse.microprofile.config.ConfigProvider; +import org.eclipse.microprofile.config.spi.ConfigSource; +import org.eclipse.microprofile.config.tck.base.AbstractTest; +import org.hamcrest.CoreMatchers; +import org.hamcrest.MatcherAssert; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.junit.Assert; +import org.junit.Test; + +import javax.inject.Inject; +import java.io.*; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.Properties; + + +/** + * @author <a href="mailto:strub...@apache.org">Mark Struberg</a> + */ +public class ConfigProviderTest { + + private Config config = ConfigProvider.getConfig(); + + @Deployment + public static WebArchive deploy() { + JavaArchive testJar = ShrinkWrap + .create(JavaArchive.class, "configProviderTest.jar") + .addPackage(AbstractTest.class.getPackage()) + .addClass(org.eclipse.microprofile.config.tck.ConfigProviderTest.class) + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml") + .as(JavaArchive.class); + + AbstractTest.addFile(testJar, "META-INF/microprofile-config.properties"); + + WebArchive war = ShrinkWrap + .create(WebArchive.class, "configProviderTest.war") + .addAsLibrary(testJar); + return war; + } + + @Test + public void testEnvironmentConfigSource() { + Map<String, String> env = System.getenv(); + for (Map.Entry<String, String> envEntry : env.entrySet()) { + Assert.assertEquals(envEntry.getValue(), config.getValue(envEntry.getKey(), String.class)); + } + } + + @Test + public void testPropertyConfigSource() { + Properties properties = System.getProperties(); + + for (Map.Entry<Object, Object> propEntry : properties.entrySet()) { + String propValue = (String) propEntry.getValue(); + if (propValue != null && propValue.length() > 0) { + Assert.assertEquals(propValue, config.getValue((String) propEntry.getKey(), String.class)); + } + } + } + + @Test + public void testDynamicValueInPropertyConfigSource() { + String configKey = "tck.config.test.systemproperty.dynamic.value"; + String configValue = "myDynamicValue;"; + + System.setProperty(configKey, configValue); + Assert.assertEquals(config.getValue(configKey, String.class), configValue); + } + + @Test + public void testJavaConfigPropertyFilesConfigSource() { + Assert.assertEquals(config.getValue("tck.config.test.javaconfig.properties.key1", String.class), "VALue1"); + } + + @Test + public void testNonExistingConfigKey() { + Assert.assertFalse(config.getOptionalValue("tck.config.test.keydoesnotexist", String.class).isPresent()); + } + + @Test(expected = NoSuchElementException.class) + public void testNonExistingConfigKeyGet() { + config.getValue("tck.config.test.keydoesnotexist", String.class); + } + + @Test + public void testGetConfigSources() { + Iterable<ConfigSource> configSources = config.getConfigSources(); + Assert.assertNotNull(configSources); + + // check descending sorting + int prevOrdinal = Integer.MAX_VALUE; + for (ConfigSource configSource : configSources) { + Assert.assertTrue(configSource.getOrdinal() <= prevOrdinal); + prevOrdinal = configSource.getOrdinal(); + } + + } + + @Test + public void testInjectedConfigSerializable() { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + try (ObjectOutputStream out = new ObjectOutputStream(byteArrayOutputStream)) { + out.writeObject(config); + } catch (IOException ex) { + ex.printStackTrace(); + Assert.fail("Injected config should be serializable, but could not serialize it"); + } + Object readObject = null; + try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()))) { + readObject = in.readObject(); + } catch (IOException | ClassNotFoundException ex) { + ex.printStackTrace(); + Assert.fail("Injected config should be serializable, but could not deserialize a previously serialized instance"); + } + MatcherAssert.assertThat("Deserialized object", readObject, CoreMatchers.instanceOf(Config.class)); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/ConverterTest.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/ConverterTest.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/ConverterTest.java new file mode 100644 index 0000000..e481003 --- /dev/null +++ b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/ConverterTest.java @@ -0,0 +1,235 @@ +/* + * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed 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.microprofile.imported; + +import org.eclipse.microprofile.config.Config; +import org.eclipse.microprofile.config.ConfigProvider; +import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.eclipse.microprofile.config.tck.converters.Duck; +import org.junit.Assert; +import org.junit.Test; + +import javax.inject.Inject; +import java.net.MalformedURLException; +import java.net.URL; +import java.time.*; + +/** + * @author <a href="mailto:strub...@apache.org">Mark Struberg</a> + * @author <a href="mailto:emiji...@uk.ibm.com">Emily Jiang</a> + * @author <a href="mailto:john.d.am...@gmail.com">John D. Ament</a> + */ +public class ConverterTest { + + private Config config = ConfigProvider.getConfig(); + + private Duck namedDuck = config.getValue("tck.config.test.javaconfig.converter.duckname", Duck.class); + + + @Test + public void testInteger() { + Integer value = config.getValue("tck.config.test.javaconfig.converter.integervalue", Integer.class); + Assert.assertEquals(value, Integer.valueOf(1234)); + } + + @Test + public void testInt() { + int value = config.getValue("tck.config.test.javaconfig.converter.integervalue", int.class); + Assert.assertEquals(value, 1234); + } + + @Test(expected = IllegalArgumentException.class) + public void testInteger_Broken() { + Integer value = config.getValue("tck.config.test.javaconfig.converter.integervalue.broken", Integer.class); + } + + @Test + public void testLong() { + Long value = config.getValue("tck.config.test.javaconfig.converter.longvalue", Long.class); + Assert.assertEquals(value, Long.valueOf(1234567890)); + } + + @Test + public void testlong() { + long primitiveValue = config.getValue("tck.config.test.javaconfig.converter.longvalue", long.class); + Assert.assertEquals(primitiveValue, 1234567890L); + } + + @Test(expected = IllegalArgumentException.class) + public void testLong_Broken() { + config.getValue("tck.config.test.javaconfig.converter.longvalue.broken", Long.class); + } + + @Test + public void testFloat() { + Float value = config.getValue("tck.config.test.javaconfig.converter.floatvalue", Float.class); + Assert.assertEquals(value, 12.34f, 0.0f); + } + + @Test + public void testfloat() { + float value = config.getValue("tck.config.test.javaconfig.converter.floatvalue", float.class); + Assert.assertEquals(value, 12.34f, 0f); + } + + @Test(expected = IllegalArgumentException.class) + public void testFloat_Broken() { + config.getValue("tck.config.test.javaconfig.converter.floatvalue.broken", Float.class); + } + + @Test + public void testDouble() { + Double value = config.getValue("tck.config.test.javaconfig.converter.doublevalue", Double.class); + Assert.assertEquals(value, 12.34d, 0.0d); + } + + @Test + public void testdouble() { + double value = config.getValue("tck.config.test.javaconfig.converter.doublevalue", double.class); + Assert.assertEquals(value,12.34d, 0d); + } + + @Test(expected = IllegalArgumentException.class) + public void testDouble_Broken() { + Double value = config.getValue("tck.config.test.javaconfig.converter.doublevalue.broken", Double.class); + } + + @Test + public void testDuration() { + Duration value = config.getValue("tck.config.test.javaconfig.converter.durationvalue", Duration.class); + Assert.assertEquals(value, Duration.parse("PT15M")); + } + + @Test(expected = IllegalArgumentException.class) + public void testDuration_Broken() { + Duration value = config.getValue("tck.config.test.javaconfig.converter.durationvalue.broken", Duration.class); + } + + @Test + public void testLocalTime() { + LocalTime value = config.getValue("tck.config.test.javaconfig.converter.localtimevalue", LocalTime.class); + Assert.assertEquals(value, LocalTime.parse("10:37")); + } + + @Test(expected = IllegalArgumentException.class) + public void testLocalTime_Broken() { + LocalTime value = config.getValue("tck.config.test.javaconfig.converter.localtimevalue.broken", LocalTime.class); + } + + @Test + public void testLocalDate() { + LocalDate value = config.getValue("tck.config.test.javaconfig.converter.localdatevalue", LocalDate.class); + Assert.assertEquals(value, LocalDate.parse("2017-12-24")); + } + + @Test(expected = IllegalArgumentException.class) + public void testLocalDate_Broken() { + LocalDate value = config.getValue("tck.config.test.javaconfig.converter.localdatevalue.broken", LocalDate.class); + } + + @Test + public void testLocalDateTime() { + LocalDateTime value = config.getValue("tck.config.test.javaconfig.converter.localdatetimevalue", LocalDateTime.class); + Assert.assertEquals(value, LocalDateTime.parse("2017-12-24T10:25:30")); + } + + @Test(expected = IllegalArgumentException.class) + public void testLocalDateTime_Broken() { + LocalDateTime value = config.getValue("tck.config.test.javaconfig.converter.localdatetimevalue.broken", LocalDateTime.class); + } + + @Test + public void testOffsetDateTime() { + OffsetDateTime value = config.getValue("tck.config.test.javaconfig.converter.offsetdatetimevalue", OffsetDateTime.class); + Assert.assertEquals(value, OffsetDateTime.parse("2007-12-03T10:15:30+01:00")); + } + + @Test(expected = IllegalArgumentException.class) + public void testOffsetDateTime_Broken() { + OffsetDateTime value = config.getValue("tck.config.test.javaconfig.converter.offsetdatetimevalue.broken", OffsetDateTime.class); + } + + @Test + public void testOffsetTime() { + OffsetTime value = config.getValue("tck.config.test.javaconfig.converter.offsettimevalue", OffsetTime.class); + OffsetTime parsed = OffsetTime.parse("13:45:30.123456789+02:00"); + Assert.assertEquals(value, parsed); + } + + @Test(expected = IllegalArgumentException.class) + public void testOffsetTime_Broken() { + OffsetTime value = config.getValue("tck.config.test.javaconfig.converter.offsettimevalue.broken", OffsetTime.class); + } + + @Test + public void testInstant() { + Instant value = config.getValue("tck.config.test.javaconfig.converter.instantvalue", Instant.class); + Assert.assertEquals(value, Instant.parse("2015-06-02T21:34:33.616Z")); + } + + @Test(expected = IllegalArgumentException.class) + public void testInstant_Broken() { + Instant value = config.getValue("tck.config.test.javaconfig.converter.instantvalue.broken", Instant.class); + } + + @Test + public void testBoolean() { + Assert.assertTrue(config.getValue("tck.config.test.javaconfig.configvalue.boolean.true", Boolean.class)); + Assert.assertTrue(config.getValue("tck.config.test.javaconfig.configvalue.boolean.true", boolean.class)); + Assert.assertTrue(config.getValue("tck.config.test.javaconfig.configvalue.boolean.true_uppercase", Boolean.class)); + Assert.assertTrue(config.getValue("tck.config.test.javaconfig.configvalue.boolean.true_mixedcase", Boolean.class)); + Assert.assertFalse(config.getValue("tck.config.test.javaconfig.configvalue.boolean.false", Boolean.class)); + + Assert.assertTrue(config.getValue("tck.config.test.javaconfig.configvalue.boolean.one", Boolean.class)); + Assert.assertFalse(config.getValue("tck.config.test.javaconfig.configvalue.boolean.zero", Boolean.class)); + Assert.assertFalse(config.getValue("tck.config.test.javaconfig.configvalue.boolean.seventeen", Boolean.class)); + + Assert.assertTrue(config.getValue("tck.config.test.javaconfig.configvalue.boolean.yes", Boolean.class)); + Assert.assertTrue(config.getValue("tck.config.test.javaconfig.configvalue.boolean.yes_uppercase", Boolean.class)); + Assert.assertTrue(config.getValue("tck.config.test.javaconfig.configvalue.boolean.yes_mixedcase", Boolean.class)); + Assert.assertFalse(config.getValue("tck.config.test.javaconfig.configvalue.boolean.no", Boolean.class)); + + Assert.assertTrue(config.getValue("tck.config.test.javaconfig.configvalue.boolean.y", Boolean.class)); + Assert.assertTrue(config.getValue("tck.config.test.javaconfig.configvalue.boolean.y_uppercase", Boolean.class)); + Assert.assertFalse(config.getValue("tck.config.test.javaconfig.configvalue.boolean.n", Boolean.class)); + + Assert.assertTrue(config.getValue("tck.config.test.javaconfig.configvalue.boolean.on", Boolean.class)); + Assert.assertTrue(config.getValue("tck.config.test.javaconfig.configvalue.boolean.on_uppercase", Boolean.class)); + Assert.assertTrue(config.getValue("tck.config.test.javaconfig.configvalue.boolean.on_mixedcase", Boolean.class)); + Assert.assertFalse(config.getValue("tck.config.test.javaconfig.configvalue.boolean.off", Boolean.class)); + Assert.assertFalse(config.getValue("tck.config.test.javaconfig.configvalue.boolean.off", boolean.class)); + } + + @Test + public void testCustomConverter() { + Assert.assertEquals(namedDuck.getName(), "Hannelore"); + } + + @Test + public void testURLConverter() throws MalformedURLException { + URL url = config.getValue("tck.config.test.javaconfig.converter.urlvalue", URL.class); + Assert.assertEquals(url, new URL("http://microprofile.io")); + } + + @Test(expected = IllegalArgumentException.class) + public void testURLConverterBroken() throws Exception { + URL ignored = config.getValue("tck.config.test.javaconfig.converter.urlvalue.broken", URL.class); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/CustomConfigSourceTest.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/CustomConfigSourceTest.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/CustomConfigSourceTest.java new file mode 100644 index 0000000..79a62f6 --- /dev/null +++ b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/CustomConfigSourceTest.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed 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.microprofile.imported; + +import org.eclipse.microprofile.config.Config; +import org.eclipse.microprofile.config.ConfigProvider; +import org.junit.Assert; +import org.junit.Test; + +import javax.inject.Inject; + +import static org.eclipse.microprofile.config.tck.base.AbstractTest.addFile; + +/** + * @author <a href="mailto:strub...@apache.org">Mark Struberg</a> + */ +public class CustomConfigSourceTest { + + private Config config = ConfigProvider.getConfig(); + +// @Deployment +// public static WebArchive deploy() { +// JavaArchive testJar = ShrinkWrap +// .create(JavaArchive.class, "customConfigSourceTest.jar") +// .addClasses(org.eclipse.microprofile.config.tck.CustomConfigSourceTest.class, CustomDbConfigSource.class, CustomConfigSourceProvider.class) +// .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml") +// .addAsServiceProvider(ConfigSource.class, CustomDbConfigSource.class) +// .addAsServiceProvider(ConfigSourceProvider.class, CustomConfigSourceProvider.class) +// .as(JavaArchive.class); +// +// addFile(testJar, "META-INF/microprofile-config.properties"); +// +// WebArchive war = ShrinkWrap +// .create(WebArchive.class, "customConfigSourceTest.war") +// .addAsLibrary(testJar); +// return war; +// } + + + @Test + public void testConfigSourceProvider() { + Assert.assertEquals(config.getValue("tck.config.test.customDbConfig.key1", String.class), "valueFromDb1"); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/OptionalValuesBean.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/OptionalValuesBean.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/OptionalValuesBean.java new file mode 100644 index 0000000..bc9df03 --- /dev/null +++ b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/OptionalValuesBean.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed 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.microprofile.imported; +/** + * Declare a bean for config property injections. + * @author <a href="mailto:strub...@apache.org">Mark Struberg</a> + * @author <a href="mailto:emiji...@uk.ibm.com">Emily Jiang</a> + */ + +import org.eclipse.microprofile.config.inject.ConfigProperty; + +import javax.enterprise.context.Dependent; +import javax.inject.Inject; +import java.util.Optional; + +@Dependent +public class OptionalValuesBean { + @Inject + @ConfigProperty(name="my.optional.int.property") + private Optional<Integer> intProperty; + + @Inject + @ConfigProperty(name="my.notexisting.property") + private Optional<Integer> notexistingProperty; + + private Optional<String> stringValue; + + @Inject + public void setStringValue(@ConfigProperty(name="my.optional.string.property") Optional<String> stringValue) { + this.stringValue = stringValue; + } + + public Optional<String> getStringValue() { + return stringValue; + } + + public Optional<Integer> getIntProperty() { + return intProperty; + } + + public Optional<Integer> getNotexistingProperty() { + return notexistingProperty; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/broken/ConfigOwner.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/broken/ConfigOwner.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/broken/ConfigOwner.java new file mode 100644 index 0000000..e6763b2 --- /dev/null +++ b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/broken/ConfigOwner.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed 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.microprofile.imported.broken; + +import org.eclipse.microprofile.config.inject.ConfigProperty; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; + +/** + * A bean contains a config property injection + * + * @author <a href="mailto:strub...@apache.org">Mark Struberg</a> + * @author <a href="mailto:emiji...@uk.ibm.com">Emily Jiang</a> + */ +@RequestScoped +public class ConfigOwner { + + @Inject + @ConfigProperty(name="my.long.value") + private Long configValue; +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/broken/CustomConverterBean.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/broken/CustomConverterBean.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/broken/CustomConverterBean.java new file mode 100644 index 0000000..51b7028 --- /dev/null +++ b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/broken/CustomConverterBean.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed 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.microprofile.imported.broken; + +import org.eclipse.microprofile.config.inject.ConfigProperty; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; + +/** + * A bean contains a config property injection, which converts to CustomType + * + * @author <a href="mailto:strub...@apache.org">Mark Struberg</a> + * @author <a href="mailto:emiji...@uk.ibm.com">Emily Jiang</a> + */ + +public class CustomConverterBean { + + @RequestScoped + public static class ConfigOwner { + + @Inject + @ConfigProperty(name="my.customtype.value") + private CustomType configValue; + } + + public static class CustomType { + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/broken/MissingConverterOnInstanceInjectionTest.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/broken/MissingConverterOnInstanceInjectionTest.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/broken/MissingConverterOnInstanceInjectionTest.java new file mode 100644 index 0000000..863594c --- /dev/null +++ b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/broken/MissingConverterOnInstanceInjectionTest.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed 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.microprofile.imported.broken; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.container.test.api.ShouldThrowException; +import org.jboss.arquillian.testng.Arquillian; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.asset.StringAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.testng.annotations.Test; + +import javax.enterprise.inject.spi.DeploymentException; + +/** + * Verify that a Converter exists which can handle the configured string. + * + * @author <a href="mailto:strub...@apache.org">Mark Struberg</a> + */ +public class MissingConverterOnInstanceInjectionTest extends Arquillian { + + @ShouldThrowException(DeploymentException.class) + @Deployment + public static WebArchive deploy() { + JavaArchive testJar = ShrinkWrap + .create(JavaArchive.class, "missingConverterOnInstanceInjectionTest.jar") + .addClass(CustomConverterBean.class) + .addAsManifestResource(new StringAsset("my.customtype.value=xxxxx"), "microprofile-config.properties") + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml") + .as(JavaArchive.class); + + WebArchive war = ShrinkWrap + .create(WebArchive.class, "missingConverterOnInstanceInjectionTest.war") + .addAsLibrary(testJar); + return war; + } + + @Test + public void test() { + } + + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/broken/MissingValueOnInstanceInjectionTest.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/broken/MissingValueOnInstanceInjectionTest.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/broken/MissingValueOnInstanceInjectionTest.java new file mode 100644 index 0000000..776000a --- /dev/null +++ b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/broken/MissingValueOnInstanceInjectionTest.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed 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.microprofile.imported.broken; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.container.test.api.ShouldThrowException; +import org.jboss.arquillian.testng.Arquillian; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.testng.annotations.Test; + +import javax.enterprise.inject.spi.DeploymentException; + +/** + * Verify that injectng a native value which is not configured will lead to a deployment error. + * + * @author <a href="mailto:strub...@apache.org">Mark Struberg</a> + */ +public class MissingValueOnInstanceInjectionTest extends Arquillian { + + @ShouldThrowException(DeploymentException.class) + @Deployment + public static WebArchive deploy() { + JavaArchive testJar = ShrinkWrap + .create(JavaArchive.class, "missingValueOnInstanceInjectionTest.jar") + .addClass(ConfigOwner.class) + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml") + .as(JavaArchive.class); + + WebArchive war = ShrinkWrap + .create(WebArchive.class, "missingValueOnInstanceInjectionTest.war") + .addAsLibrary(testJar); + return war; + } + + @Test + public void test() { + } + + + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/broken/WrongConverterOnInstanceInjectionTest.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/broken/WrongConverterOnInstanceInjectionTest.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/broken/WrongConverterOnInstanceInjectionTest.java new file mode 100644 index 0000000..5075ef2 --- /dev/null +++ b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/broken/WrongConverterOnInstanceInjectionTest.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed 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.microprofile.imported.broken; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.container.test.api.ShouldThrowException; +import org.jboss.arquillian.testng.Arquillian; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.asset.StringAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.testng.annotations.Test; + +import javax.enterprise.inject.spi.DeploymentException; + +/** + * Verify that injectng a native value also has a valid converter which can handle the configured string. + * + * @author <a href="mailto:strub...@apache.org">Mark Struberg</a> + */ +public class WrongConverterOnInstanceInjectionTest extends Arquillian { + + @ShouldThrowException(DeploymentException.class) + @Deployment + public static WebArchive deploy() { + JavaArchive testJar = ShrinkWrap + .create(JavaArchive.class, "wrongConverterOnInstanceInjectionTest.jar") + .addClass(ConfigOwner.class) + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml") + .addAsManifestResource(new StringAsset("my.long.value=xxxxx"), "microprofile-config.properties") + .as(JavaArchive.class); + + WebArchive war = ShrinkWrap + .create(WebArchive.class, "wrongConverterOnInstanceInjectionTest.war") + .addAsLibrary(testJar); + return war; + } + + @Test + public void test() { + } + + + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/configsources/CustomConfigSourceProvider.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/configsources/CustomConfigSourceProvider.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/configsources/CustomConfigSourceProvider.java new file mode 100644 index 0000000..003f73a --- /dev/null +++ b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/configsources/CustomConfigSourceProvider.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed 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.microprofile.imported.configsources; + +import org.eclipse.microprofile.config.spi.ConfigSource; +import org.eclipse.microprofile.config.spi.ConfigSourceProvider; + +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; + + +/** + * @author <a href="mailto:strub...@apache.org">Mark Struberg</a> + */ +public class CustomConfigSourceProvider implements ConfigSourceProvider { + + @Override + public Iterable<ConfigSource> getConfigSources(ClassLoader forClassLoader) { + List<ConfigSource> detectedConfigSources = new ArrayList<>(); + + Enumeration<URL> yamlFiles = null; + try { + yamlFiles = forClassLoader.getResources("sampleconfig.yaml"); + } + catch (IOException e) { + throw new RuntimeException(e); + } + while (yamlFiles.hasMoreElements()) { + detectedConfigSources.add(new SampleYamlConfigSource(yamlFiles.nextElement())); + } + return detectedConfigSources; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/configsources/CustomDbConfigSource.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/configsources/CustomDbConfigSource.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/configsources/CustomDbConfigSource.java new file mode 100644 index 0000000..28ba343 --- /dev/null +++ b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/configsources/CustomDbConfigSource.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed 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.microprofile.imported.configsources; + +import org.eclipse.microprofile.config.spi.ConfigSource; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author <a href="mailto:strub...@apache.org">Mark Struberg</a> + */ +public class CustomDbConfigSource implements ConfigSource { + + private Map<String, String> configValues = new HashMap<>(); + + public CustomDbConfigSource() { + configValues.put("tck.config.test.customDbConfig.key1", "valueFromDb1"); + configValues.put("tck.config.test.customDbConfig.key2", "valueFromDb2"); + configValues.put("tck.config.test.customDbConfig.key3", "big:cheese"); + } + + @Override + public int getOrdinal() { + return 112; + } + + @Override + public Map<String, String> getProperties() { + return readPropertiesFromDb(); + } + + @Override + public String getValue(String key) { + return readPropertyFromDb(key); + } + + @Override + public String getName() { + return "customDbConfig"; + } + + private Map<String, String> readPropertiesFromDb() { + return configValues; + } + + private String readPropertyFromDb(String key) { + return configValues.get(key); + } + + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/configsources/SampleYamlConfigSource.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/configsources/SampleYamlConfigSource.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/configsources/SampleYamlConfigSource.java new file mode 100644 index 0000000..34a612e --- /dev/null +++ b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/configsources/SampleYamlConfigSource.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed 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.microprofile.imported.configsources; + +import org.eclipse.microprofile.config.spi.ConfigSource; + +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + +/** + * @author <a href="mailto:strub...@apache.org">Mark Struberg</a> + */ +public class SampleYamlConfigSource implements ConfigSource { + private Map<String, String> config = new HashMap<>(); + + public SampleYamlConfigSource(URL url) { + config.put("tck.config.test.sampleyaml.key1", "yamlvalue1"); + } + + @Override + public int getOrdinal() { + return 110; + } + + @Override + public Map<String, String> getProperties() { + return config; + } + + @Override + public String getValue(String key) { + return config.get(key); + } + + @Override + public String getName() { + return null; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/converters/Duck.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/converters/Duck.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/converters/Duck.java new file mode 100644 index 0000000..e5bf4b1 --- /dev/null +++ b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/converters/Duck.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed 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.microprofile.imported.converters; + +/** + * @author <a href="mailto:strub...@apache.org">Mark Struberg</a> + */ +public class Duck { + private final String name; + + + public Duck(String name) { + this.name = name; + } + + public String getName() { + return name; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/converters/DuckConverter.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/converters/DuckConverter.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/converters/DuckConverter.java new file mode 100644 index 0000000..a8eff34 --- /dev/null +++ b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/converters/DuckConverter.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed 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.microprofile.imported.converters; + +import org.eclipse.microprofile.config.spi.Converter; + +/** + * @author <a href="mailto:strub...@apache.org">Mark Struberg</a> + */ +public class DuckConverter implements Converter<Duck> { + + @Override + public Duck convert(String value) { + return new Duck(value); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/converters/Pizza.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/converters/Pizza.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/converters/Pizza.java new file mode 100644 index 0000000..147756a --- /dev/null +++ b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/converters/Pizza.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed 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.microprofile.imported.converters; + +/** + * @author <a href="mailto:emiji...@uk.ibm.com">Emily Jiang</a> + */ +public class Pizza { + private String flavor; + private String size; + + + public Pizza(String... parts) { + if (parts.length ==2) { + size = parts[0]; + flavor = parts[1]; + } + + } + + public String getSize() { + return size; + } + + public String getFlavor() { + return flavor; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1e2a184f/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/converters/PizzaConverter.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/converters/PizzaConverter.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/converters/PizzaConverter.java new file mode 100644 index 0000000..0cdb388 --- /dev/null +++ b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/converters/PizzaConverter.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed 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.microprofile.imported.converters; + +import org.eclipse.microprofile.config.spi.Converter; + +/** + * @author <a href="mailto:emiji...@uk.ibm.com">Emily Jiang</a> + */ +public class PizzaConverter implements Converter<Pizza> { + + @Override + public Pizza convert(String value) { + return new Pizza(value.split(":")); + } +}