From 08dd2a1d682241f52f805247d56404e253b092e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hermann?= Date: Wed, 31 Aug 2022 13:01:16 -0400 Subject: [PATCH] add support for Map injection --- .../src/main/asciidoc/configexamples.asciidoc | 9 +- .../config/tck/MapConverterBean.java | 83 +++++++++++++ .../config/tck/MapConverterTest.java | 116 ++++++++++++++++++ .../META-INF/microprofile-config.properties | 9 ++ 4 files changed, 215 insertions(+), 2 deletions(-) create mode 100644 tck/src/main/java/org/eclipse/microprofile/config/tck/MapConverterBean.java create mode 100644 tck/src/main/java/org/eclipse/microprofile/config/tck/MapConverterTest.java diff --git a/spec/src/main/asciidoc/configexamples.asciidoc b/spec/src/main/asciidoc/configexamples.asciidoc index 3b674072..096f2188 100644 --- a/spec/src/main/asciidoc/configexamples.asciidoc +++ b/spec/src/main/asciidoc/configexamples.asciidoc @@ -121,6 +121,11 @@ public class InjectedConfigUsageSample { @ConfigProperty(name="myprj.some.supplier.timeout", defaultValue="100") private java.util.function.Supplier timeout; + //Injects a Map to resolve multiple configuration parameters under the same configuration key. + @Inject + @ConfigProperty(name = "myprj.some.reasons", defaultValue = "200=OK;201=Created") + private java.util.Map reasons; + //The following code injects an Array, List or Set for the `myPets` property, //where its value is a comma separated value ( myPets=dog,cat,dog\\,cat) @Inject @ConfigProperty(name="myPets") private String[] myArrayPets; @@ -371,8 +376,8 @@ The table below defines the conversion rules, including some special edge case s |=== === Remove config properties -Sometimes, there is a need to remove a property. This can be done by setting an empty value or a value causing the corresponding converter returning `null` in a config source. -When injecting a property that has been deleted, `DeploymentException` will be thrown unless the return type is `Optional`. +Sometimes, there is a need to remove a property. This can be done by setting an empty value or a value causing the corresponding converter returning `null` in a config source. +When injecting a property that has been deleted, `DeploymentException` will be thrown unless the return type is `Optional`. === Aggregate related properties into a CDI bean diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/MapConverterBean.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/MapConverterBean.java new file mode 100644 index 00000000..51eab8f6 --- /dev/null +++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/MapConverterBean.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 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.eclipse.microprofile.config.tck; + +import jakarta.enterprise.context.Dependent; +import jakarta.inject.Inject; +import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.eclipse.microprofile.config.tck.converters.Pizza; + +import java.net.URI; +import java.net.URL; +import java.time.Duration; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.OffsetDateTime; +import java.time.OffsetTime; +import java.util.List; +import java.util.Map; +import java.util.Set; + +@Dependent +public class MapConverterBean { + + @Inject + @ConfigProperty(name = "tck.config.test.javaconfig.converter.map.string.string") + private Map myStringStringMap; + + @Inject + @ConfigProperty(name = "tck.config.test.javaconfig.converter.map.integer.string") + private Map myIntegerStringMap; + + @Inject + @ConfigProperty(name = "tck.config.test.javaconfig.converter.map.string.integer") + private Map myStringIntegerMap; + + @Inject + @ConfigProperty(name = "tck.config.test.javaconfig.converter.map.enum.enum") + private Map myEnumEnumMap; + + public Map getMyStringStringMap() { + return myStringStringMap; + } + + public Map getMyIntegerStringMap() { + return myIntegerStringMap; + } + + public Map getMyStringIntegerMap() { + return myStringIntegerMap; + } + + public Map getMyEnumEnumMap() { + return myEnumEnumMap; + } + + public enum EnumKey { + key1, + key2; + } + + public enum EnumValue { + enum1, + enum2; + } +} diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/MapConverterTest.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/MapConverterTest.java new file mode 100644 index 00000000..c0dfc567 --- /dev/null +++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/MapConverterTest.java @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2017, 2021 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.eclipse.microprofile.config.tck; + +import jakarta.inject.Inject; +import org.eclipse.microprofile.config.Config; +import org.eclipse.microprofile.config.spi.Converter; +import org.eclipse.microprofile.config.tck.MapConverterBean.EnumKey; +import org.eclipse.microprofile.config.tck.MapConverterBean.EnumValue; +import org.eclipse.microprofile.config.tck.converters.Pizza; +import org.eclipse.microprofile.config.tck.converters.PizzaConverter; +import org.eclipse.microprofile.config.tck.util.AdditionalAssertions; +import org.jboss.arquillian.container.test.api.Deployment; +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.Assert; +import org.testng.annotations.Test; + +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.time.Duration; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.OffsetDateTime; +import java.time.OffsetTime; +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; + +import static org.eclipse.microprofile.config.tck.base.AbstractTest.addFile; +import static org.eclipse.microprofile.config.tck.util.AdditionalAssertions.assertURLArrayEquals; +import static org.eclipse.microprofile.config.tck.util.AdditionalAssertions.assertURLListEquals; +import static org.eclipse.microprofile.config.tck.util.AdditionalAssertions.assertURLSetEquals; + +/** + * Test the implicit converter handling. + **/ +public class MapConverterTest extends Arquillian { + + @Deployment + public static WebArchive deploy() { + JavaArchive testJar = ShrinkWrap + .create(JavaArchive.class, "mapConverterTest.jar") + .addPackage(PizzaConverter.class.getPackage()) + .addClasses(MapConverterTest.class, MapConverterBean.class, AdditionalAssertions.class) + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml") + .addAsServiceProvider(Converter.class, PizzaConverter.class) + .as(JavaArchive.class); + addFile(testJar, "META-INF/microprofile-config.properties"); + WebArchive war = ShrinkWrap + .create(WebArchive.class, "mapConverterTest.war") + .addAsLibrary(testJar); + return war; + } + + @Inject + private MapConverterBean converterBean; + + /////////////////////////////////// Test Map////////////////////////// + + + @Test + public void testStringStringMapInjection() { + Assert.assertEquals(converterBean.getMyStringStringMap().size(), 2); + Assert.assertEquals(converterBean.getMyStringStringMap().get("key1"), "string.string.value1"); + Assert.assertEquals(converterBean.getMyStringStringMap().get("key2"), "string.string.value2"); + } + + @Test + public void testStringIntegerMapInjection() { + Assert.assertEquals(converterBean.getMyStringIntegerMap().size(), 2); + Assert.assertEquals(converterBean.getMyStringIntegerMap().get("key1"), Integer.valueOf(100)); + Assert.assertEquals(converterBean.getMyStringIntegerMap().get("key2"), Integer.valueOf(200)); + } + + @Test + public void testIntegerStringMapInjection() { + Assert.assertEquals(converterBean.getMyIntegerStringMap().size(), 2); + Assert.assertEquals(converterBean.getMyIntegerStringMap().get(100), "integer.string.value1"); + Assert.assertEquals(converterBean.getMyIntegerStringMap().get(200), "integer.string.value2"); + } + + @Test + public void testEnumEnumMapInjection() { + Assert.assertEquals(converterBean.getMyEnumEnumMap().size(), 2); + Assert.assertEquals(converterBean.getMyEnumEnumMap().get(EnumKey.key1), EnumValue.enum1); + Assert.assertEquals(converterBean.getMyEnumEnumMap().get(EnumKey.key2), EnumValue.enum2); + } +} diff --git a/tck/src/main/resources/internal/META-INF/microprofile-config.properties b/tck/src/main/resources/internal/META-INF/microprofile-config.properties index da8d3f15..4102f2fd 100644 --- a/tck/src/main/resources/internal/META-INF/microprofile-config.properties +++ b/tck/src/main/resources/internal/META-INF/microprofile-config.properties @@ -167,3 +167,12 @@ tck.config.test.javaconfig.converter.urlvalues=http://microprofile.io,http://ope tck.config.test.javaconfig.converter.class=org.eclipse.microprofile.config.tck.ClassConverterTest tck.config.test.javaconfig.converter.class.array=org.eclipse.microprofile.config.tck.ClassConverterTest,java.lang.String + +tck.config.test.javaconfig.converter.map.string.string.key1=string.string.value1 +tck.config.test.javaconfig.converter.map.string.string.key2=string.string.value2 +tck.config.test.javaconfig.converter.map.integer.string.100=integer.string.value1 +tck.config.test.javaconfig.converter.map.integer.string.200=integer.string.value2 +tck.config.test.javaconfig.converter.map.string.integer.key1=100 +tck.config.test.javaconfig.converter.map.string.integer.key2=200 +tck.config.test.javaconfig.converter.map.enum.enum.key1=value1 +tck.config.test.javaconfig.converter.map.enum.enum.key2=value2