From 58d719872b1df427c44d1ab05501177725397d87 Mon Sep 17 00:00:00 2001 From: ondrejm Date: Wed, 1 Feb 2017 12:52:22 +0100 Subject: [PATCH 01/12] Added CDI API and tests in TCK Tests require JUnit, Hamcrest, and Arquillian with embedded Weld container adapter. Checkstyle config adapted to match best-practices for test code. Signed-off-by: Ondrej Mihalyi --- api/pom.xml | 5 + .../config/inject/ConfigProperty.java | 28 +++ .../config/inject/ConfigRegistry.java | 25 ++ pom.xml | 27 +- tck/nb-configuration.xml | 25 ++ tck/pom.xml | 129 ++++++++++ .../config/tck/CDIPlainInjectionTest.java | 235 ++++++++++++++++++ .../config/tck/CDITestsFunctions.java | 43 ++++ .../tck/MicroProfileConfigTestConnector.java | 11 + .../tck/matchers/AdditionalMatchers.java | 36 +++ 10 files changed, 563 insertions(+), 1 deletion(-) create mode 100644 api/src/main/java/io/microprofile/config/inject/ConfigProperty.java create mode 100644 api/src/main/java/io/microprofile/config/inject/ConfigRegistry.java create mode 100644 tck/nb-configuration.xml create mode 100644 tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java create mode 100644 tck/src/main/java/org/eclipse/microprofile/config/tck/CDITestsFunctions.java create mode 100644 tck/src/main/java/org/eclipse/microprofile/config/tck/MicroProfileConfigTestConnector.java create mode 100644 tck/src/main/java/org/eclipse/microprofile/config/tck/matchers/AdditionalMatchers.java diff --git a/api/pom.xml b/api/pom.xml index 6026e589..019126a1 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -23,6 +23,11 @@ provided true + + javax.enterprise + cdi-api + provided + diff --git a/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java b/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java new file mode 100644 index 00000000..7f7dad79 --- /dev/null +++ b/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java @@ -0,0 +1,28 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package io.microprofile.config.inject; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; +import javax.enterprise.util.Nonbinding; +import javax.inject.Qualifier; + +/** + * Binds the injected value with a configured value + * @author Ondrej Mihalyi + */ +@Qualifier +@Retention(RUNTIME) +@Target({METHOD, FIELD, PARAMETER, TYPE}) +public @interface ConfigProperty { + @Nonbinding + String value(); +} diff --git a/api/src/main/java/io/microprofile/config/inject/ConfigRegistry.java b/api/src/main/java/io/microprofile/config/inject/ConfigRegistry.java new file mode 100644 index 00000000..8650d26b --- /dev/null +++ b/api/src/main/java/io/microprofile/config/inject/ConfigRegistry.java @@ -0,0 +1,25 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package io.microprofile.config.inject; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; +import javax.inject.Qualifier; + +/** + * Marks injection points that should be provided by the config implementation (container) + * @author Ondrej Mihalyi + */ +@Qualifier +@Retention(RUNTIME) +@Target({METHOD, FIELD, PARAMETER, TYPE}) +public @interface ConfigRegistry { +} diff --git a/pom.xml b/pom.xml index b8407c93..f4cf1c56 100644 --- a/pom.xml +++ b/pom.xml @@ -32,13 +32,38 @@ tck spec + + + + + javax.enterprise + cdi-api + 1.2 + + + org.jboss.arquillian + arquillian-bom + 1.1.12.Final + import + pom + + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + ${checkstyle.version} + + + org.apache.maven.plugins maven-checkstyle-plugin - ${checkstyle.version} verify-style diff --git a/tck/nb-configuration.xml b/tck/nb-configuration.xml new file mode 100644 index 00000000..26b847be --- /dev/null +++ b/tck/nb-configuration.xml @@ -0,0 +1,25 @@ + + + + + + * + words + 4 + 4 + 8 + 80 + true + project + + diff --git a/tck/pom.xml b/tck/pom.xml index 285624a8..4cdd3e04 100644 --- a/tck/pom.xml +++ b/tck/pom.xml @@ -27,6 +27,135 @@ 6.9.9 compile + + + junit + junit + 4.12 + compile + + + org.hamcrest + hamcrest-core + + + + + + org.hamcrest + hamcrest-all + 1.3 + compile + + + + org.jboss.arquillian.junit + arquillian-junit-container + compile + + + + org.jboss.arquillian.container + arquillian-weld-embedded + 2.0.0.Beta4 + compile + + + + org.jboss.weld + weld-core + 2.4.1.Final + compile + + + + org.jboss.shrinkwrap + shrinkwrap-api + compile + + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java new file mode 100644 index 00000000..b4be8e41 --- /dev/null +++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java @@ -0,0 +1,235 @@ +package org.eclipse.microprofile.config.tck; + +import io.microprofile.config.inject.ConfigProperty; +import io.microprofile.config.inject.ConfigRegistry; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.Date; +import java.util.Optional; +import javax.enterprise.context.Dependent; +import javax.enterprise.inject.spi.CDI; +import javax.inject.Inject; +import javax.inject.Provider; +import static org.eclipse.microprofile.config.tck.matchers.AdditionalMatchers.floatCloseTo; +import static org.hamcrest.Matchers.closeTo; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import static org.junit.Assert.assertThat; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * + * @author Ondrej Mihalyi + */ +@RunWith(Arquillian.class) +public class CDIPlainInjectionTest { + + @Deployment + public static Archive deployment() { + return CDITestsFunctions.add_implementation_resources(ShrinkWrap.create(JavaArchive.class) + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")); + } + + @Test + public void can_inject_simple_values_when_defined() { + ensure_all_property_values_are_defined(); + + SimpleValuesBean bean = get_bean_of_type(SimpleValuesBean.class); + + assertThat(bean.stringProperty, is(equalTo("text"))); + assertThat(bean.boolProperty, 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.dateProperty, is(equalTo(CDITestsFunctions.toDate("2017-01-30")))); + assertThat(bean.localDateProperty, is(CDITestsFunctions.toDate("2016-01-30"))); + assertThat(bean.dateTimeProperty, is(CDITestsFunctions.toDate("2015-01-30T10:00"))); + } + + @Test + public void can_inject_optional_values() { + ensure_property_undefined("non-existent.string.property"); + ensure_property_defined("my.string.property", "text"); + + OptionalValuesBean bean = get_bean_of_type(OptionalValuesBean.class); + + assertThat(bean.noStringProperty.isPresent(), is(false)); + assertThat(bean.stringProperty.isPresent(), is(true)); + assertThat(bean.stringProperty.get(), is(equalTo("text"))); + } + + @Test + public void can_inject_dynamic_values_via_CDI_provider() { + clear_all_property_values(); + + DynamicValuesBean bean = get_bean_of_type(DynamicValuesBean.class); + + assertThat(bean.getIntProperty(), is(nullValue())); + + ensure_all_property_values_are_defined(); + + assertThat(bean.getIntProperty(), is(equalTo(5))); + } + + @Test + public void can_inject_dynamic_values_via_custom_interface() { + clear_all_property_values(); + + CustomConfigBean bean = get_bean_of_type(CustomConfigBean.class); + + assertThat(bean.getIntProperty(), is(nullValue())); + assertThat(bean.getStringProperty(), is(nullValue())); + + ensure_all_property_values_are_defined(); + + assertThat(bean.getIntProperty(), is(equalTo(5))); + assertThat(bean.getStringProperty(), is(equalTo("text"))); + } + + private void ensure_all_property_values_are_defined() { + ensure_property_defined("my.string.property", "text"); + ensure_property_defined("my.boolean.property", "true"); + ensure_property_defined("my.int.property", "5"); + ensure_property_defined("my.long.property", "10"); + ensure_property_defined("my.float.property", "10.5"); + ensure_property_defined("my.double.property", "11.5"); + ensure_property_defined("my.date.property", "2017-01-30"); + ensure_property_defined("my.localdate.property", "2016-01-30"); + ensure_property_defined("my.datetime.property", "2015-01-30T10:00"); + } + + private void ensure_property_defined(String key, String value) { + // setting configuration via system properties + System.setProperty(key, value); + } + + private void ensure_property_undefined(String key) { + // clearing configuration in system properties if previously set + System.getProperties().remove(key); + } + + private static T get_bean_of_type(Class beanClass) { + return CDI.current().select(beanClass).get(); + } + + private void clear_all_property_values() { + ensure_property_undefined("my.string.property"); + ensure_property_undefined("my.boolean.property"); + ensure_property_undefined("my.int.property"); + ensure_property_undefined("my.long.property"); + ensure_property_undefined("my.float.property"); + ensure_property_undefined("my.double.property"); + ensure_property_undefined("my.date.property"); + ensure_property_undefined("my.localdate.property"); + ensure_property_undefined("my.datetime.property"); + } + + @Dependent + public static class SimpleValuesBean { + + @Inject + @ConfigProperty("my.string.property") + String stringProperty; + + @Inject + @ConfigProperty("my.boolean.property") + Boolean boolProperty; + + @Inject + @ConfigProperty("my.int.property") + Integer intProperty; + + @Inject + @ConfigProperty("my.long.property") + Long longProperty; + + @Inject + @ConfigProperty("my.float.property") + Float floatProperty; + + @Inject + @ConfigProperty("my.double.property") + Double doubleProperty; + + @Inject + @ConfigProperty("my.date.property") + Date dateProperty; + + @Inject + @ConfigProperty("my.localdate.property") + LocalDate localDateProperty; + + @Inject + @ConfigProperty("my.datetime.property") + LocalDateTime dateTimeProperty; + + } + + @Dependent + public static class OptionalValuesBean { + + @Inject + @ConfigProperty("my.string.property") + Optional stringProperty; + + @Inject + @ConfigProperty("non-existent.string.property") + Optional noStringProperty; + + } + + @Dependent + public static class DynamicValuesBean { + + @Inject + @ConfigProperty("my.int.property") + Provider intPropertyProvider; + + public Integer getIntProperty() { + try { + return intPropertyProvider.get(); + } + catch (Exception e) { + return null; + } + } + + } + + @Dependent + public static class CustomConfigBean { + + @Inject + CustomConfig config; + + public Integer getIntProperty() { + return config.getIntProperty(); + } + + public String getStringProperty() { + return config.getStringProperty().orElse(null); + } + + } + + @ConfigRegistry + public interface CustomConfig { + + @ConfigProperty("my.integer.property") + Integer getIntProperty(); + + @ConfigProperty("my.string.property") + Optional getStringProperty(); + + } + +} diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDITestsFunctions.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDITestsFunctions.java new file mode 100644 index 00000000..8870a650 --- /dev/null +++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDITestsFunctions.java @@ -0,0 +1,43 @@ +package org.eclipse.microprofile.config.tck; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.ZonedDateTime; +import java.util.Date; +import java.util.Iterator; +import java.util.ServiceLoader; +import org.jboss.shrinkwrap.api.Archive; + +/** + * + * @author Ondrej Mihalyi + */ +public final class CDITestsFunctions { + + private CDITestsFunctions() { + // utility class + } + + public static Archive add_implementation_resources(Archive deployment) { + Iterator itConnectors = ServiceLoader.load(MicroProfileConfigTestConnector.class).iterator(); + if (itConnectors.hasNext()) { + return itConnectors.next().modifyDeployment(deployment); + } + else { + return deployment; + } + } + + public static Date toDate(String isoDateTime) { + return Date.from(ZonedDateTime.parse(isoDateTime).toInstant()); + } + + public static LocalDate toLocalDate(String isoDateTime) { + return LocalDate.parse(isoDateTime); + } + + public static LocalDateTime toLocalDateTime(String isoDateTime) { + return LocalDateTime.parse(isoDateTime); + } + +} diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/MicroProfileConfigTestConnector.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/MicroProfileConfigTestConnector.java new file mode 100644 index 00000000..501f833e --- /dev/null +++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/MicroProfileConfigTestConnector.java @@ -0,0 +1,11 @@ +package org.eclipse.microprofile.config.tck; + +import org.jboss.shrinkwrap.api.Archive; + +/** + * + * @author Ondrej Mihalyi + */ +public interface MicroProfileConfigTestConnector { + Archive modifyDeployment(Archive deployment); +} diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/matchers/AdditionalMatchers.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/matchers/AdditionalMatchers.java new file mode 100644 index 00000000..3e2a75ee --- /dev/null +++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/matchers/AdditionalMatchers.java @@ -0,0 +1,36 @@ +package org.eclipse.microprofile.config.tck.matchers; + +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import static org.hamcrest.Matchers.closeTo; + +/** + * + * @author Ondrej Mihalyi + */ +public final class AdditionalMatchers { + + private AdditionalMatchers() { + // utility class + } + + public static Matcher floatCloseTo(float value, float range) { + return new BaseMatcher() { + + Matcher doubleMatcher = null; + + @Override + public boolean matches(Object item) { + return (doubleMatcher = closeTo(value, range)).matches(item); + } + + @Override + public void describeTo(Description description) { + doubleMatcher.describeTo(description); + } + }; + } + + +} From e5b15b24aabb828c039a972455b20afe8ffb3de4 Mon Sep 17 00:00:00 2001 From: ondrejm Date: Wed, 1 Feb 2017 13:07:22 +0100 Subject: [PATCH 02/12] Doc for ConfigProperty Signed-off-by: Ondrej Mihalyi --- .../config/inject/ConfigProperty.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java b/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java index 7f7dad79..5d644a73 100644 --- a/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java +++ b/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java @@ -16,13 +16,39 @@ import javax.inject.Qualifier; /** - * Binds the injected value with a configured value + * Binds the injection point with a configured value. + * Should annotate injection points of type String and all types with appropriate converters. + *

+ * The injection point can be of type {@code Optional}, where {@code TYPE} is one of supported types. + * In that case, the optional value will be defined if the property exists in the coniguration. + * Otherwise the value will not be present. + *

+ * Example: + *

+ * 
+ * 
+ *    {@code @Inject}
+ *    {@code @ConfigProperty("my.long.property")}
+ *    Long injectedLongValue;  // injects value of my.long.property property
+ *
+ *    {@code @Inject}
+ *    {@code @ConfigProperty("my.long.property")}
+ *    java.util.Optional{@code } injectedOptionalLongValue;  // injects value of my.long.property property exists, or empty Optional if not
+ *
+ *    {@code @Inject}
+ *    {@code @ConfigProperty("my.long.property")}
+ *    javax.inject.Provider{@code } longValueInjector;  // injects a provider for the value of my.long.property property to resolve the property dynamically
+ * 
+ * 
* @author Ondrej Mihalyi */ @Qualifier @Retention(RUNTIME) @Target({METHOD, FIELD, PARAMETER, TYPE}) public @interface ConfigProperty { + /** + * @return Name (key) of the config property to inject + */ @Nonbinding String value(); } From f4920a2019148481ae36b31f13218c77efe7a848 Mon Sep 17 00:00:00 2001 From: ondrejm Date: Wed, 1 Feb 2017 19:34:18 +0100 Subject: [PATCH 03/12] Replaced Provider with Instance for cleaner code Signed-off-by: Ondrej Mihalyi --- .../config/tck/CDIPlainInjectionTest.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java index b4be8e41..95fd83d5 100644 --- a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java +++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java @@ -7,9 +7,9 @@ import java.util.Date; import java.util.Optional; import javax.enterprise.context.Dependent; +import javax.enterprise.inject.Instance; import javax.enterprise.inject.spi.CDI; import javax.inject.Inject; -import javax.inject.Provider; import static org.eclipse.microprofile.config.tck.matchers.AdditionalMatchers.floatCloseTo; import static org.hamcrest.Matchers.closeTo; import static org.hamcrest.Matchers.equalTo; @@ -66,7 +66,7 @@ public void can_inject_optional_values() { assertThat(bean.stringProperty.isPresent(), is(true)); assertThat(bean.stringProperty.get(), is(equalTo("text"))); } - + @Test public void can_inject_dynamic_values_via_CDI_provider() { clear_all_property_values(); @@ -116,7 +116,7 @@ private void ensure_property_undefined(String key) { // clearing configuration in system properties if previously set System.getProperties().remove(key); } - + private static T get_bean_of_type(Class beanClass) { return CDI.current().select(beanClass).get(); } @@ -186,20 +186,19 @@ public static class OptionalValuesBean { Optional noStringProperty; } - + @Dependent public static class DynamicValuesBean { @Inject @ConfigProperty("my.int.property") - Provider intPropertyProvider; + Instance intPropertyProvider; public Integer getIntProperty() { - try { - return intPropertyProvider.get(); - } - catch (Exception e) { + if (intPropertyProvider.isUnsatisfied()) { return null; + } else { + return intPropertyProvider.get(); } } From f256476c348cfbc64b405a273bc690c32e59c9cb Mon Sep 17 00:00:00 2001 From: ondrejm Date: Wed, 1 Feb 2017 19:55:35 +0100 Subject: [PATCH 04/12] Added more documentation Signed-off-by: Ondrej Mihalyi --- .../config/inject/ConfigProperty.java | 9 +++-- .../config/inject/ConfigRegistry.java | 36 ++++++++++++++++++- .../config/tck/CDIPlainInjectionTest.java | 25 +++++-------- .../config/tck/CDITestsFunctions.java | 17 ++++++++- .../tck/MicroProfileConfigTestConnector.java | 3 +- 5 files changed, 67 insertions(+), 23 deletions(-) diff --git a/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java b/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java index 5d644a73..894b1044 100644 --- a/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java +++ b/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java @@ -29,15 +29,18 @@ * * {@code @Inject} * {@code @ConfigProperty("my.long.property")} - * Long injectedLongValue; // injects value of my.long.property property + * Long injectedLongValue; + * // injects value of my.long.property property * * {@code @Inject} * {@code @ConfigProperty("my.long.property")} - * java.util.Optional{@code } injectedOptionalLongValue; // injects value of my.long.property property exists, or empty Optional if not + * java.util.Optional{@code } injectedOptionalLongValue; + * // injects value of my.long.property property exists, or empty Optional if not * * {@code @Inject} * {@code @ConfigProperty("my.long.property")} - * javax.inject.Provider{@code } longValueInjector; // injects a provider for the value of my.long.property property to resolve the property dynamically + * javax.inject.Provider{@code } longValueInjector; + * // injects a provider for the value of my.long.property property to resolve the property dynamically * * * @author Ondrej Mihalyi diff --git a/api/src/main/java/io/microprofile/config/inject/ConfigRegistry.java b/api/src/main/java/io/microprofile/config/inject/ConfigRegistry.java index 8650d26b..861cc384 100644 --- a/api/src/main/java/io/microprofile/config/inject/ConfigRegistry.java +++ b/api/src/main/java/io/microprofile/config/inject/ConfigRegistry.java @@ -15,7 +15,41 @@ import javax.inject.Qualifier; /** - * Marks injection points that should be provided by the config implementation (container) + * Marks an interface, for which an instance that provides configured values should be provided by the container. + *

+ * The interface can only contain Java Bean getters that return either a valid configuration type + * (either String or a type with appropriate converter). + *

+ * Methods in the annotated interface need to be annotated with {@link ConfigProperty} to define the configuration key. + * If a configuration property with that key exists, the particular method will return its value. If the property does not exist, + * an exception of type {@link java.util.NoSuchElementException} is thrown instead. + *

+ * The methods can also return Optional. If they do, the return value is the same as in + * {@link io.microprofile.config.Config#getValue(String, Class)}, where Class is the generic type in the Optional. + *

+ * Example: + *

+ * 
+ *    {@code @ConfigRegistry}
+ *    public interface MyConfig {
+ * 
+ *        {@code @ConfigProperty("my.long.property")}
+ *        Long getLongValue();  
+ *              // returns the value of my.long.property property or throws an exception if it does not exist
+ *
+ *        {@code @ConfigProperty("my.long.property")}
+ *        java.util.Optional{@code } getOptionalLongValue();  
+ *              // returns the optional value of my.long.property property
+ *    }
+ * 
+ * 
+ * ...and usage: + *
+ * 
+ *        {@code @Inject}
+ *        MyConfig myConfig;
+ * 
+ * 
* @author Ondrej Mihalyi */ @Qualifier diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java index 95fd83d5..ccd9b563 100644 --- a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java +++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java @@ -8,8 +8,10 @@ import java.util.Optional; import javax.enterprise.context.Dependent; import javax.enterprise.inject.Instance; -import javax.enterprise.inject.spi.CDI; import javax.inject.Inject; +import static org.eclipse.microprofile.config.tck.CDITestsFunctions.ensure_property_defined; +import static org.eclipse.microprofile.config.tck.CDITestsFunctions.ensure_property_undefined; +import static org.eclipse.microprofile.config.tck.CDITestsFunctions.get_bean_of_type; import static org.eclipse.microprofile.config.tck.matchers.AdditionalMatchers.floatCloseTo; import static org.hamcrest.Matchers.closeTo; import static org.hamcrest.Matchers.equalTo; @@ -26,7 +28,9 @@ import org.junit.runner.RunWith; /** - * + * Test cases for CDI-based API that test retrieving values from the configuration. + * The tests depend only on CDI 1.2. They are designed to run with CDI arquillian connector, + * but they should run with any container that provides CDI 1.2. * @author Ondrej Mihalyi */ @RunWith(Arquillian.class) @@ -107,20 +111,6 @@ private void ensure_all_property_values_are_defined() { ensure_property_defined("my.datetime.property", "2015-01-30T10:00"); } - private void ensure_property_defined(String key, String value) { - // setting configuration via system properties - System.setProperty(key, value); - } - - private void ensure_property_undefined(String key) { - // clearing configuration in system properties if previously set - System.getProperties().remove(key); - } - - private static T get_bean_of_type(Class beanClass) { - return CDI.current().select(beanClass).get(); - } - private void clear_all_property_values() { ensure_property_undefined("my.string.property"); ensure_property_undefined("my.boolean.property"); @@ -197,7 +187,8 @@ public static class DynamicValuesBean { public Integer getIntProperty() { if (intPropertyProvider.isUnsatisfied()) { return null; - } else { + } + else { return intPropertyProvider.get(); } } diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDITestsFunctions.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDITestsFunctions.java index 8870a650..48b9ee46 100644 --- a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDITestsFunctions.java +++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDITestsFunctions.java @@ -6,10 +6,11 @@ import java.util.Date; import java.util.Iterator; import java.util.ServiceLoader; +import javax.enterprise.inject.spi.CDI; import org.jboss.shrinkwrap.api.Archive; /** - * + * Utility class for common functionality used in test scenarios. * @author Ondrej Mihalyi */ public final class CDITestsFunctions { @@ -40,4 +41,18 @@ public static LocalDateTime toLocalDateTime(String isoDateTime) { return LocalDateTime.parse(isoDateTime); } + public static void ensure_property_defined(String key, String value) { + // setting configuration via system properties + System.setProperty(key, value); + } + + public static void ensure_property_undefined(String key) { + // clearing configuration in system properties if previously set + System.getProperties().remove(key); + } + + public static T get_bean_of_type(Class beanClass) { + return CDI.current().select(beanClass).get(); + } + } diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/MicroProfileConfigTestConnector.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/MicroProfileConfigTestConnector.java index 501f833e..f66abeb1 100644 --- a/tck/src/main/java/org/eclipse/microprofile/config/tck/MicroProfileConfigTestConnector.java +++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/MicroProfileConfigTestConnector.java @@ -3,7 +3,8 @@ import org.jboss.shrinkwrap.api.Archive; /** - * + * An implementation of this interface can be provided by the config implementation using {@link java.util.ServiceLoader} + * in order to provide additional configuration for the tests (e.g. dependencies for a tested ShrinkWrap archive) * @author Ondrej Mihalyi */ public interface MicroProfileConfigTestConnector { From 290a32f17a9de30ed95e5c4f185f74dea6e7341d Mon Sep 17 00:00:00 2001 From: ondrejm Date: Wed, 1 Feb 2017 20:07:08 +0100 Subject: [PATCH 05/12] Added option to overwrite checkstyle method format. Methods in TCK are also allowed underscores in the middle of method names to improve legibility. Signed-off-by: Ondrej Mihalyi --- pom.xml | 3 +- tck/pom.xml | 87 +------------------ .../config/tck/CDIPlainInjectionTest.java | 26 +++--- .../tck/matchers/AdditionalMatchers.java | 2 +- 4 files changed, 20 insertions(+), 98 deletions(-) diff --git a/pom.xml b/pom.xml index f4cf1c56..4cab847f 100644 --- a/pom.xml +++ b/pom.xml @@ -15,6 +15,7 @@ 1.8 2.17 + ^_?[a-z][a-zA-Z0-9]*$ @@ -96,7 +97,7 @@ - + diff --git a/tck/pom.xml b/tck/pom.xml index 4cdd3e04..465dad3e 100644 --- a/tck/pom.xml +++ b/tck/pom.xml @@ -13,7 +13,10 @@ microprofile-config-tck 1.0-SNAPSHOT - + + ^_?[a-z][a-zA-Z0-9_]*$ + + org.eclipse.microprofile.config.api @@ -76,86 +79,4 @@ - - - - org.apache.maven.plugins - maven-checkstyle-plugin - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java index ccd9b563..5c3d86c9 100644 --- a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java +++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java @@ -128,39 +128,39 @@ public static class SimpleValuesBean { @Inject @ConfigProperty("my.string.property") - String stringProperty; + private String stringProperty; @Inject @ConfigProperty("my.boolean.property") - Boolean boolProperty; + private Boolean boolProperty; @Inject @ConfigProperty("my.int.property") - Integer intProperty; + private Integer intProperty; @Inject @ConfigProperty("my.long.property") - Long longProperty; + private Long longProperty; @Inject @ConfigProperty("my.float.property") - Float floatProperty; + private Float floatProperty; @Inject @ConfigProperty("my.double.property") - Double doubleProperty; + private Double doubleProperty; @Inject @ConfigProperty("my.date.property") - Date dateProperty; + private Date dateProperty; @Inject @ConfigProperty("my.localdate.property") - LocalDate localDateProperty; + private LocalDate localDateProperty; @Inject @ConfigProperty("my.datetime.property") - LocalDateTime dateTimeProperty; + private LocalDateTime dateTimeProperty; } @@ -169,11 +169,11 @@ public static class OptionalValuesBean { @Inject @ConfigProperty("my.string.property") - Optional stringProperty; + private Optional stringProperty; @Inject @ConfigProperty("non-existent.string.property") - Optional noStringProperty; + private Optional noStringProperty; } @@ -182,7 +182,7 @@ public static class DynamicValuesBean { @Inject @ConfigProperty("my.int.property") - Instance intPropertyProvider; + private Instance intPropertyProvider; public Integer getIntProperty() { if (intPropertyProvider.isUnsatisfied()) { @@ -199,7 +199,7 @@ public Integer getIntProperty() { public static class CustomConfigBean { @Inject - CustomConfig config; + private CustomConfig config; public Integer getIntProperty() { return config.getIntProperty(); diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/matchers/AdditionalMatchers.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/matchers/AdditionalMatchers.java index 3e2a75ee..048173b1 100644 --- a/tck/src/main/java/org/eclipse/microprofile/config/tck/matchers/AdditionalMatchers.java +++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/matchers/AdditionalMatchers.java @@ -18,7 +18,7 @@ private AdditionalMatchers() { public static Matcher floatCloseTo(float value, float range) { return new BaseMatcher() { - Matcher doubleMatcher = null; + private Matcher doubleMatcher = null; @Override public boolean matches(Object item) { From dc5f18337a65163bfe003ca24d3431c8d1df9109 Mon Sep 17 00:00:00 2001 From: ondrejm Date: Wed, 1 Feb 2017 21:08:31 +0100 Subject: [PATCH 06/12] Removed netbeans configuration file Signed-off-by: Ondrej Mihalyi --- .gitignore | 2 +- .../config/inject/ConfigProperty.java | 26 +++-- .../config/inject/ConfigRegistry.java | 59 ------------ .../microprofile/config/ConfigValue.java | 49 ++++++++++ tck/nb-configuration.xml | 25 ----- .../config/tck/CDIPlainInjectionTest.java | 94 +++++++------------ .../config/tck/CDITestsFunctions.java | 30 +++--- .../tck/MicroProfileConfigTestConnector.java | 12 --- .../tck/matchers/AdditionalMatchers.java | 17 ++++ 9 files changed, 135 insertions(+), 179 deletions(-) delete mode 100644 api/src/main/java/io/microprofile/config/inject/ConfigRegistry.java create mode 100644 api/src/main/java/org/eclipse/microprofile/config/ConfigValue.java delete mode 100644 tck/nb-configuration.xml delete mode 100644 tck/src/main/java/org/eclipse/microprofile/config/tck/MicroProfileConfigTestConnector.java diff --git a/.gitignore b/.gitignore index d25df776..cfea104d 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,4 @@ tck/.checkstyle tck/bin/ .project build/ -.classpath \ No newline at end of file +.classpath diff --git a/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java b/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java index 894b1044..ee80ee80 100644 --- a/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java +++ b/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java @@ -1,8 +1,20 @@ /* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. + * Copyright (c) 2016-2017 Ondrej Mihalyi, Payara Services Ltd. and others + * + * 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 io.microprofile.config.inject; import static java.lang.annotation.ElementType.TYPE; @@ -37,10 +49,10 @@ * java.util.Optional{@code } injectedOptionalLongValue; * // injects value of my.long.property property exists, or empty Optional if not * - * {@code @Inject} - * {@code @ConfigProperty("my.long.property")} - * javax.inject.Provider{@code } longValueInjector; - * // injects a provider for the value of my.long.property property to resolve the property dynamically + * {@literal @Inject} + * {@literal @ConfigProperty("my.long.property")} + * {@literal ConfigValue} longConfigValue; + * // injects a ConfigValue for the value of my.long.property property to resolve the property dynamically * * * @author Ondrej Mihalyi diff --git a/api/src/main/java/io/microprofile/config/inject/ConfigRegistry.java b/api/src/main/java/io/microprofile/config/inject/ConfigRegistry.java deleted file mode 100644 index 861cc384..00000000 --- a/api/src/main/java/io/microprofile/config/inject/ConfigRegistry.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package io.microprofile.config.inject; - -import static java.lang.annotation.ElementType.TYPE; -import static java.lang.annotation.ElementType.FIELD; -import static java.lang.annotation.ElementType.PARAMETER; -import static java.lang.annotation.ElementType.METHOD; -import static java.lang.annotation.RetentionPolicy.RUNTIME; -import java.lang.annotation.Retention; -import java.lang.annotation.Target; -import javax.inject.Qualifier; - -/** - * Marks an interface, for which an instance that provides configured values should be provided by the container. - *

- * The interface can only contain Java Bean getters that return either a valid configuration type - * (either String or a type with appropriate converter). - *

- * Methods in the annotated interface need to be annotated with {@link ConfigProperty} to define the configuration key. - * If a configuration property with that key exists, the particular method will return its value. If the property does not exist, - * an exception of type {@link java.util.NoSuchElementException} is thrown instead. - *

- * The methods can also return Optional. If they do, the return value is the same as in - * {@link io.microprofile.config.Config#getValue(String, Class)}, where Class is the generic type in the Optional. - *

- * Example: - *

- * 
- *    {@code @ConfigRegistry}
- *    public interface MyConfig {
- * 
- *        {@code @ConfigProperty("my.long.property")}
- *        Long getLongValue();  
- *              // returns the value of my.long.property property or throws an exception if it does not exist
- *
- *        {@code @ConfigProperty("my.long.property")}
- *        java.util.Optional{@code } getOptionalLongValue();  
- *              // returns the optional value of my.long.property property
- *    }
- * 
- * 
- * ...and usage: - *
- * 
- *        {@code @Inject}
- *        MyConfig myConfig;
- * 
- * 
- * @author Ondrej Mihalyi - */ -@Qualifier -@Retention(RUNTIME) -@Target({METHOD, FIELD, PARAMETER, TYPE}) -public @interface ConfigRegistry { -} diff --git a/api/src/main/java/org/eclipse/microprofile/config/ConfigValue.java b/api/src/main/java/org/eclipse/microprofile/config/ConfigValue.java new file mode 100644 index 00000000..1bf25768 --- /dev/null +++ b/api/src/main/java/org/eclipse/microprofile/config/ConfigValue.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2016-2017 Ondrej Mihalyi, Payara Services Ltd. and others + * + * 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.eclipse.microprofile.config; + +/** + * Accessor to a configured value. Suitable when a configured value should be + * accessed dynamically or additional metadata is necessary on top of the + * configured value. + * + * @author Ondrej Mihalyi + * + * @param Type of the configuration value + */ +public interface ConfigValue { + + /** + * Returns the converted resolved value. + * + * @return the resolved value + */ + T_VALUE getValue(); + + /** + * Whether the value is defined in one of the config sources or not. + * @return True if a value for the key is defined, false otherwise + */ + boolean isValueAvailable(); + + /** + * Returns the key used to retrieve the configuration value. + * + * @return the original key + */ + String getKey(); +} diff --git a/tck/nb-configuration.xml b/tck/nb-configuration.xml deleted file mode 100644 index 26b847be..00000000 --- a/tck/nb-configuration.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - * - words - 4 - 4 - 8 - 80 - true - project - - diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java index 5c3d86c9..cdf772c1 100644 --- a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java +++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java @@ -1,7 +1,23 @@ +/* + * Copyright (c) 2016-2017 Ondrej Mihalyi, Payara Services Ltd. and others + * + * 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.eclipse.microprofile.config.tck; import io.microprofile.config.inject.ConfigProperty; -import io.microprofile.config.inject.ConfigRegistry; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Date; @@ -9,9 +25,10 @@ import javax.enterprise.context.Dependent; import javax.enterprise.inject.Instance; import javax.inject.Inject; -import static org.eclipse.microprofile.config.tck.CDITestsFunctions.ensure_property_defined; -import static org.eclipse.microprofile.config.tck.CDITestsFunctions.ensure_property_undefined; -import static org.eclipse.microprofile.config.tck.CDITestsFunctions.get_bean_of_type; +import org.eclipse.microprofile.config.ConfigValue; +import static org.eclipse.microprofile.config.tck.testsupport.TestSetup.ensure_property_defined; +import static org.eclipse.microprofile.config.tck.testsupport.TestSetup.ensure_property_undefined; +import static org.eclipse.microprofile.config.tck.testsupport.TestSetup.get_bean_of_type; import static org.eclipse.microprofile.config.tck.matchers.AdditionalMatchers.floatCloseTo; import static org.hamcrest.Matchers.closeTo; import static org.hamcrest.Matchers.equalTo; @@ -29,8 +46,7 @@ /** * Test cases for CDI-based API that test retrieving values from the configuration. - * The tests depend only on CDI 1.2. They are designed to run with CDI arquillian connector, - * but they should run with any container that provides CDI 1.2. + * The tests depend only on CDI 1.2. * @author Ondrej Mihalyi */ @RunWith(Arquillian.class) @@ -38,8 +54,8 @@ public class CDIPlainInjectionTest { @Deployment public static Archive deployment() { - return CDITestsFunctions.add_implementation_resources(ShrinkWrap.create(JavaArchive.class) - .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")); + return ShrinkWrap.create(JavaArchive.class) + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); } @Test @@ -72,31 +88,17 @@ public void can_inject_optional_values() { } @Test - public void can_inject_dynamic_values_via_CDI_provider() { + public void can_inject_dynamic_values_via_ConfigValue() { clear_all_property_values(); DynamicValuesBean bean = get_bean_of_type(DynamicValuesBean.class); - assertThat(bean.getIntProperty(), is(nullValue())); - - ensure_all_property_values_are_defined(); - - assertThat(bean.getIntProperty(), is(equalTo(5))); - } - - @Test - public void can_inject_dynamic_values_via_custom_interface() { - clear_all_property_values(); - - CustomConfigBean bean = get_bean_of_type(CustomConfigBean.class); - - assertThat(bean.getIntProperty(), is(nullValue())); - assertThat(bean.getStringProperty(), is(nullValue())); + assertThat(bean.getIntPropertyValue().isValueAvailable(), is(false)); ensure_all_property_values_are_defined(); - assertThat(bean.getIntProperty(), is(equalTo(5))); - assertThat(bean.getStringProperty(), is(equalTo("text"))); + assertThat(bean.getIntPropertyValue().isValueAvailable(), is(true)); + assertThat(bean.getIntPropertyValue().getValue(), is(equalTo(5))); } private void ensure_all_property_values_are_defined() { @@ -182,44 +184,12 @@ public static class DynamicValuesBean { @Inject @ConfigProperty("my.int.property") - private Instance intPropertyProvider; - - public Integer getIntProperty() { - if (intPropertyProvider.isUnsatisfied()) { - return null; - } - else { - return intPropertyProvider.get(); - } - } - - } + private ConfigValue intPropertyValue; - @Dependent - public static class CustomConfigBean { - - @Inject - private CustomConfig config; - - public Integer getIntProperty() { - return config.getIntProperty(); + public ConfigValue getIntPropertyValue() { + return intPropertyValue; } - - public String getStringProperty() { - return config.getStringProperty().orElse(null); - } - - } - - @ConfigRegistry - public interface CustomConfig { - - @ConfigProperty("my.integer.property") - Integer getIntProperty(); - - @ConfigProperty("my.string.property") - Optional getStringProperty(); - + } } diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDITestsFunctions.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDITestsFunctions.java index 48b9ee46..396e2cc8 100644 --- a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDITestsFunctions.java +++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDITestsFunctions.java @@ -1,13 +1,27 @@ +/* + * Copyright (c) 2016-2017 Ondrej Mihalyi, Payara Services Ltd. and others + * + * 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.eclipse.microprofile.config.tck; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZonedDateTime; import java.util.Date; -import java.util.Iterator; -import java.util.ServiceLoader; import javax.enterprise.inject.spi.CDI; -import org.jboss.shrinkwrap.api.Archive; /** * Utility class for common functionality used in test scenarios. @@ -19,16 +33,6 @@ private CDITestsFunctions() { // utility class } - public static Archive add_implementation_resources(Archive deployment) { - Iterator itConnectors = ServiceLoader.load(MicroProfileConfigTestConnector.class).iterator(); - if (itConnectors.hasNext()) { - return itConnectors.next().modifyDeployment(deployment); - } - else { - return deployment; - } - } - public static Date toDate(String isoDateTime) { return Date.from(ZonedDateTime.parse(isoDateTime).toInstant()); } diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/MicroProfileConfigTestConnector.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/MicroProfileConfigTestConnector.java deleted file mode 100644 index f66abeb1..00000000 --- a/tck/src/main/java/org/eclipse/microprofile/config/tck/MicroProfileConfigTestConnector.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.eclipse.microprofile.config.tck; - -import org.jboss.shrinkwrap.api.Archive; - -/** - * An implementation of this interface can be provided by the config implementation using {@link java.util.ServiceLoader} - * in order to provide additional configuration for the tests (e.g. dependencies for a tested ShrinkWrap archive) - * @author Ondrej Mihalyi - */ -public interface MicroProfileConfigTestConnector { - Archive modifyDeployment(Archive deployment); -} diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/matchers/AdditionalMatchers.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/matchers/AdditionalMatchers.java index 048173b1..060cd7e6 100644 --- a/tck/src/main/java/org/eclipse/microprofile/config/tck/matchers/AdditionalMatchers.java +++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/matchers/AdditionalMatchers.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2016-2017 Ondrej Mihalyi, Payara Services Ltd. and others + * + * 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.eclipse.microprofile.config.tck.matchers; import org.hamcrest.BaseMatcher; From 83b8fd55edbdb06c53401812676ef229f2f5b32b Mon Sep 17 00:00:00 2001 From: ondrejm Date: Wed, 22 Feb 2017 01:01:13 +0100 Subject: [PATCH 07/12] Adding description of how a default config key is generated. Signed-off-by: Ondrej Mihalyi --- .../java/io/microprofile/config/inject/ConfigProperty.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java b/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java index ee80ee80..35e36527 100644 --- a/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java +++ b/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java @@ -62,8 +62,12 @@ @Target({METHOD, FIELD, PARAMETER, TYPE}) public @interface ConfigProperty { /** + * The kay of the config property used to look up the configuration value. + * If it is not specified, it will be derived automatically as{@code .}, + * where {@code injection_point_name} is either a field name or a property name in case of field/property injection. Otherwise the value has to be provided. + * * @return Name (key) of the config property to inject */ @Nonbinding - String value(); + String value() default ""; } From f6ab693e638df1bced4169eb4f3e95d037c6e66d Mon Sep 17 00:00:00 2001 From: ondrejm Date: Wed, 22 Feb 2017 01:07:55 +0100 Subject: [PATCH 08/12] Improved description of how a default config key is generated. Signed-off-by: Ondrej Mihalyi --- .../java/io/microprofile/config/inject/ConfigProperty.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java b/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java index 35e36527..583c56ec 100644 --- a/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java +++ b/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java @@ -63,8 +63,10 @@ public @interface ConfigProperty { /** * The kay of the config property used to look up the configuration value. - * If it is not specified, it will be derived automatically as{@code .}, - * where {@code injection_point_name} is either a field name or a property name in case of field/property injection. Otherwise the value has to be provided. + * If it is not specified, it will be derived automatically as {@code .}, + * where {@code injection_point_name} is either a field name or a property name in case of field/property injection, + * {@code class_name} is the simple name of the class being injected to. + * If one of the {@code class_name} or {@code injection_point_name} cannot be determined, the value has to be provided. * * @return Name (key) of the config property to inject */ From e93a16a9bdd9910f285276dd43fbd7b12db180dd Mon Sep 17 00:00:00 2001 From: ondrejm Date: Tue, 28 Feb 2017 16:53:29 +0100 Subject: [PATCH 09/12] Fixes of compile errors. After rebasing to sign off my commits. Signed-off-by: ondrejm Signed-off-by: Ondrej Mihalyi --- .../config/inject/ConfigProperty.java | 10 +++++----- .../config/tck/CDIPlainInjectionTest.java | 9 ++++----- .../TestSetup.java} | 16 +++------------- 3 files changed, 12 insertions(+), 23 deletions(-) rename tck/src/main/java/org/eclipse/microprofile/config/tck/{CDITestsFunctions.java => testsupport/TestSetup.java} (78%) diff --git a/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java b/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java index 583c56ec..70525408 100644 --- a/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java +++ b/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java @@ -39,14 +39,14 @@ *
  * 
  * 
- *    {@code @Inject}
- *    {@code @ConfigProperty("my.long.property")}
+ *    {@literal @Inject}
+ *    {@literal @ConfigProperty("my.long.property")}
  *    Long injectedLongValue;  
  *            // injects value of my.long.property property
  *
- *    {@code @Inject}
- *    {@code @ConfigProperty("my.long.property")}
- *    java.util.Optional{@code } injectedOptionalLongValue;  
+ *    {@literal @Inject}
+ *    {@literal @ConfigProperty("my.long.property")}
+ *    java.util.Optional{@literal } injectedOptionalLongValue;  
  *            // injects value of my.long.property property exists, or empty Optional if not
  *
  *   {@literal @Inject}
diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java
index cdf772c1..6c80202b 100644
--- a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java
+++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java
@@ -17,13 +17,13 @@
 
 package org.eclipse.microprofile.config.tck;
 
+import org.eclipse.microprofile.config.tck.testsupport.TestSetup;
 import io.microprofile.config.inject.ConfigProperty;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.util.Date;
 import java.util.Optional;
 import javax.enterprise.context.Dependent;
-import javax.enterprise.inject.Instance;
 import javax.inject.Inject;
 import org.eclipse.microprofile.config.ConfigValue;
 import static org.eclipse.microprofile.config.tck.testsupport.TestSetup.ensure_property_defined;
@@ -33,7 +33,6 @@
 import static org.hamcrest.Matchers.closeTo;
 import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.nullValue;
 import org.jboss.arquillian.container.test.api.Deployment;
 import org.jboss.arquillian.junit.Arquillian;
 import org.jboss.shrinkwrap.api.Archive;
@@ -70,9 +69,9 @@ public void can_inject_simple_values_when_defined() {
         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.dateProperty, is(equalTo(CDITestsFunctions.toDate("2017-01-30"))));
-        assertThat(bean.localDateProperty, is(CDITestsFunctions.toDate("2016-01-30")));
-        assertThat(bean.dateTimeProperty, is(CDITestsFunctions.toDate("2015-01-30T10:00")));
+        assertThat(bean.dateProperty, is(equalTo(TestSetup.toDate("2017-01-30"))));
+        assertThat(bean.localDateProperty, is(TestSetup.toDate("2016-01-30")));
+        assertThat(bean.dateTimeProperty, is(TestSetup.toDate("2015-01-30T10:00")));
     }
 
     @Test
diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDITestsFunctions.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/testsupport/TestSetup.java
similarity index 78%
rename from tck/src/main/java/org/eclipse/microprofile/config/tck/CDITestsFunctions.java
rename to tck/src/main/java/org/eclipse/microprofile/config/tck/testsupport/TestSetup.java
index 396e2cc8..1c0d7a94 100644
--- a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDITestsFunctions.java
+++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/testsupport/TestSetup.java
@@ -15,10 +15,8 @@
  *
  */
 
-package org.eclipse.microprofile.config.tck;
+package org.eclipse.microprofile.config.tck.testsupport;
 
-import java.time.LocalDate;
-import java.time.LocalDateTime;
 import java.time.ZonedDateTime;
 import java.util.Date;
 import javax.enterprise.inject.spi.CDI;
@@ -27,9 +25,9 @@
  * Utility class for common functionality used in test scenarios.
  * @author Ondrej Mihalyi
  */
-public final class CDITestsFunctions {
+public final class TestSetup {
     
-    private CDITestsFunctions() {
+    private TestSetup() {
         // utility class
     }
 
@@ -37,14 +35,6 @@ public static Date toDate(String isoDateTime) {
         return Date.from(ZonedDateTime.parse(isoDateTime).toInstant());
     }
 
-    public static LocalDate toLocalDate(String isoDateTime) {
-        return LocalDate.parse(isoDateTime);
-    }
-
-    public static LocalDateTime toLocalDateTime(String isoDateTime) {
-        return LocalDateTime.parse(isoDateTime);
-    }
-
     public static void ensure_property_defined(String key, String value) {
         // setting configuration via system properties
         System.setProperty(key, value);

From 5a67cd44a15711c066fcca0c4e075ce93f132a84 Mon Sep 17 00:00:00 2001
From: ondrejm 
Date: Tue, 28 Feb 2017 17:05:10 +0100
Subject: [PATCH 10/12] Fixes of copyright headers after review.

Signed-off-by: ondrejm 
Signed-off-by: Ondrej Mihalyi 
---
 .../main/java/io/microprofile/config/inject/ConfigProperty.java | 2 +-
 .../main/java/org/eclipse/microprofile/config/ConfigValue.java  | 2 +-
 .../eclipse/microprofile/config/tck/CDIPlainInjectionTest.java  | 2 +-
 .../eclipse/microprofile/config/tck/testsupport/TestSetup.java  | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java b/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java
index 70525408..3141f1f6 100644
--- a/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java
+++ b/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016-2017 Ondrej Mihalyi, Payara Services Ltd. and others
+ * Copyright (c) 2016-2017 Payara Services Ltd. and others
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
diff --git a/api/src/main/java/org/eclipse/microprofile/config/ConfigValue.java b/api/src/main/java/org/eclipse/microprofile/config/ConfigValue.java
index 1bf25768..aa7e043f 100644
--- a/api/src/main/java/org/eclipse/microprofile/config/ConfigValue.java
+++ b/api/src/main/java/org/eclipse/microprofile/config/ConfigValue.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016-2017 Ondrej Mihalyi, Payara Services Ltd. and others
+ * Copyright (c) 2016-2017 Payara Services Ltd. and others
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java
index 6c80202b..daff2fcb 100644
--- a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java
+++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016-2017 Ondrej Mihalyi, Payara Services Ltd. and others
+ * Copyright (c) 2016-2017 Payara Services Ltd. and others
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/testsupport/TestSetup.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/testsupport/TestSetup.java
index 1c0d7a94..0ada3a1e 100644
--- a/tck/src/main/java/org/eclipse/microprofile/config/tck/testsupport/TestSetup.java
+++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/testsupport/TestSetup.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016-2017 Ondrej Mihalyi, Payara Services Ltd. and others
+ * Copyright (c) 2016-2017 Payara Services Ltd. and others
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.

From 04a0b217d641d5566ace116f85c6ba13938442f6 Mon Sep 17 00:00:00 2001
From: ondrejm 
Date: Tue, 28 Feb 2017 17:11:37 +0100
Subject: [PATCH 11/12] Removed injection of a config value without the
 ConfigValue wrapper. According to the discussion on github, injecting values
 or Optional is discouraged, as it cannot be dynamically updated.

Signed-off-by: ondrejm 
Signed-off-by: Ondrej Mihalyi 
---
 .../config/inject/ConfigProperty.java         | 16 +---
 .../config/tck/CDIPlainInjectionTest.java     | 90 +------------------
 2 files changed, 5 insertions(+), 101 deletions(-)

diff --git a/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java b/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java
index 3141f1f6..08723973 100644
--- a/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java
+++ b/api/src/main/java/io/microprofile/config/inject/ConfigProperty.java
@@ -29,25 +29,11 @@
 
 /**
  * Binds the injection point with a configured value.
- * Should annotate injection points of type String and all types with appropriate converters. 
- * 

- * The injection point can be of type {@code Optional}, where {@code TYPE} is one of supported types. - * In that case, the optional value will be defined if the property exists in the coniguration. - * Otherwise the value will not be present. + * Should annotate injection points of type ConfigValue, where T can be String and all types which have appropriate converters. *

* Example: *

  * 
- * 
- *    {@literal @Inject}
- *    {@literal @ConfigProperty("my.long.property")}
- *    Long injectedLongValue;  
- *            // injects value of my.long.property property
- *
- *    {@literal @Inject}
- *    {@literal @ConfigProperty("my.long.property")}
- *    java.util.Optional{@literal } injectedOptionalLongValue;  
- *            // injects value of my.long.property property exists, or empty Optional if not
  *
  *   {@literal @Inject}
  *   {@literal @ConfigProperty("my.long.property")}
diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java
index daff2fcb..fe295d4d 100644
--- a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java
+++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java
@@ -57,47 +57,19 @@ public static Archive deployment() {
                 .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
     }
 
-    @Test
-    public void can_inject_simple_values_when_defined() {
-        ensure_all_property_values_are_defined();
-
-        SimpleValuesBean bean = get_bean_of_type(SimpleValuesBean.class);
-
-        assertThat(bean.stringProperty, is(equalTo("text")));
-        assertThat(bean.boolProperty, 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.dateProperty, is(equalTo(TestSetup.toDate("2017-01-30"))));
-        assertThat(bean.localDateProperty, is(TestSetup.toDate("2016-01-30")));
-        assertThat(bean.dateTimeProperty, is(TestSetup.toDate("2015-01-30T10:00")));
-    }
-
-    @Test
-    public void can_inject_optional_values() {
-        ensure_property_undefined("non-existent.string.property");
-        ensure_property_defined("my.string.property", "text");
-
-        OptionalValuesBean bean = get_bean_of_type(OptionalValuesBean.class);
-
-        assertThat(bean.noStringProperty.isPresent(), is(false));
-        assertThat(bean.stringProperty.isPresent(), is(true));
-        assertThat(bean.stringProperty.get(), is(equalTo("text")));
-    }
-
     @Test
     public void can_inject_dynamic_values_via_ConfigValue() {
         clear_all_property_values();
 
         DynamicValuesBean bean = get_bean_of_type(DynamicValuesBean.class);
+        ConfigValue intPropertyValue = bean.getIntPropertyValue();
 
-        assertThat(bean.getIntPropertyValue().isValueAvailable(), is(false));
+        assertThat(intPropertyValue.isValueAvailable(), is(false));
 
         ensure_all_property_values_are_defined();
 
-        assertThat(bean.getIntPropertyValue().isValueAvailable(), is(true));
-        assertThat(bean.getIntPropertyValue().getValue(), is(equalTo(5)));
+        assertThat(intPropertyValue.isValueAvailable(), is(true));
+        assertThat(intPropertyValue.getValue(), is(equalTo(5)));
     }
 
     private void ensure_all_property_values_are_defined() {
@@ -124,60 +96,6 @@ private void clear_all_property_values() {
         ensure_property_undefined("my.datetime.property");
     }
 
-    @Dependent
-    public static class SimpleValuesBean {
-
-        @Inject
-        @ConfigProperty("my.string.property")
-        private String stringProperty;
-
-        @Inject
-        @ConfigProperty("my.boolean.property")
-        private Boolean boolProperty;
-
-        @Inject
-        @ConfigProperty("my.int.property")
-        private Integer intProperty;
-
-        @Inject
-        @ConfigProperty("my.long.property")
-        private Long longProperty;
-
-        @Inject
-        @ConfigProperty("my.float.property")
-        private Float floatProperty;
-
-        @Inject
-        @ConfigProperty("my.double.property")
-        private Double doubleProperty;
-
-        @Inject
-        @ConfigProperty("my.date.property")
-        private Date dateProperty;
-
-        @Inject
-        @ConfigProperty("my.localdate.property")
-        private LocalDate localDateProperty;
-
-        @Inject
-        @ConfigProperty("my.datetime.property")
-        private LocalDateTime dateTimeProperty;
-
-    }
-
-    @Dependent
-    public static class OptionalValuesBean {
-
-        @Inject
-        @ConfigProperty("my.string.property")
-        private Optional stringProperty;
-
-        @Inject
-        @ConfigProperty("non-existent.string.property")
-        private Optional noStringProperty;
-
-    }
-
     @Dependent
     public static class DynamicValuesBean {
 

From 8975d2c047493d4cdcd289246a133b5e713a0952 Mon Sep 17 00:00:00 2001
From: Ondrej Mihalyi 
Date: Wed, 1 Mar 2017 10:17:39 +0100
Subject: [PATCH 12/12] Removed unused imports

Signed-off-by: Ondrej Mihalyi 
---
 .../microprofile/config/tck/CDIPlainInjectionTest.java     | 7 -------
 1 file changed, 7 deletions(-)

diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java
index fe295d4d..143b6dee 100644
--- a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java
+++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java
@@ -17,20 +17,13 @@
 
 package org.eclipse.microprofile.config.tck;
 
-import org.eclipse.microprofile.config.tck.testsupport.TestSetup;
 import io.microprofile.config.inject.ConfigProperty;
-import java.time.LocalDate;
-import java.time.LocalDateTime;
-import java.util.Date;
-import java.util.Optional;
 import javax.enterprise.context.Dependent;
 import javax.inject.Inject;
 import org.eclipse.microprofile.config.ConfigValue;
 import static org.eclipse.microprofile.config.tck.testsupport.TestSetup.ensure_property_defined;
 import static org.eclipse.microprofile.config.tck.testsupport.TestSetup.ensure_property_undefined;
 import static org.eclipse.microprofile.config.tck.testsupport.TestSetup.get_bean_of_type;
-import static org.eclipse.microprofile.config.tck.matchers.AdditionalMatchers.floatCloseTo;
-import static org.hamcrest.Matchers.closeTo;
 import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.is;
 import org.jboss.arquillian.container.test.api.Deployment;