Skip to content

Commit 7792571

Browse files
committed
add support for Map injection
1 parent 026a784 commit 7792571

File tree

4 files changed

+215
-2
lines changed

4 files changed

+215
-2
lines changed

spec/src/main/asciidoc/configexamples.asciidoc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ public class InjectedConfigUsageSample {
121121
@ConfigProperty(name="myprj.some.supplier.timeout", defaultValue="100")
122122
private java.util.function.Supplier<Long> timeout;
123123
124+
//Injects a Map to resolve multiple configuration parameters under the same configuration key.
125+
@Inject
126+
@ConfigProperty(name = "myprj.some.reasons", defaultValue = "200=OK;201=Created")
127+
private java.util.Map<Integer, String> reasons;
128+
124129
//The following code injects an Array, List or Set for the `myPets` property,
125130
//where its value is a comma separated value ( myPets=dog,cat,dog\\,cat)
126131
@Inject @ConfigProperty(name="myPets") private String[] myArrayPets;
@@ -371,8 +376,8 @@ The table below defines the conversion rules, including some special edge case s
371376
|===
372377

373378
=== Remove config properties
374-
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.
375-
When injecting a property that has been deleted, `DeploymentException` will be thrown unless the return type is `Optional`.
379+
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.
380+
When injecting a property that has been deleted, `DeploymentException` will be thrown unless the return type is `Optional`.
376381

377382
=== Aggregate related properties into a CDI bean
378383

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2017 Contributors to the Eclipse Foundation
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* You may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.eclipse.microprofile.config.tck;
20+
21+
import jakarta.enterprise.context.Dependent;
22+
import jakarta.inject.Inject;
23+
import org.eclipse.microprofile.config.inject.ConfigProperty;
24+
import org.eclipse.microprofile.config.tck.converters.Pizza;
25+
26+
import java.net.URI;
27+
import java.net.URL;
28+
import java.time.Duration;
29+
import java.time.Instant;
30+
import java.time.LocalDate;
31+
import java.time.LocalDateTime;
32+
import java.time.LocalTime;
33+
import java.time.OffsetDateTime;
34+
import java.time.OffsetTime;
35+
import java.util.List;
36+
import java.util.Map;
37+
import java.util.Set;
38+
39+
@Dependent
40+
public class MapConverterBean {
41+
42+
@Inject
43+
@ConfigProperty(name = "tck.config.test.javaconfig.converter.map.string.string")
44+
private Map<String, String> myStringStringMap;
45+
46+
@Inject
47+
@ConfigProperty(name = "tck.config.test.javaconfig.converter.map.integer.string")
48+
private Map<Integer, String> myIntegerStringMap;
49+
50+
@Inject
51+
@ConfigProperty(name = "tck.config.test.javaconfig.converter.map.string.integer")
52+
private Map<String, Integer> myStringIntegerMap;
53+
54+
@Inject
55+
@ConfigProperty(name = "tck.config.test.javaconfig.converter.map.enum.enum")
56+
private Map<EnumKey, EnumValue> myEnumEnumMap;
57+
58+
public Map<String, String> getMyStringStringMap() {
59+
return myStringStringMap;
60+
}
61+
62+
public Map<Integer, String> getMyIntegerStringMap() {
63+
return myIntegerStringMap;
64+
}
65+
66+
public Map<String, Integer> getMyStringIntegerMap() {
67+
return myStringIntegerMap;
68+
}
69+
70+
public Map<EnumKey, EnumValue> getMyEnumEnumMap() {
71+
return myEnumEnumMap;
72+
}
73+
74+
public static enum EnumKey {
75+
key1,
76+
key2;
77+
}
78+
79+
public static enum EnumValue {
80+
enum1,
81+
enum2;
82+
}
83+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (c) 2017, 2021 Contributors to the Eclipse Foundation
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* You may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package org.eclipse.microprofile.config.tck;
21+
22+
import jakarta.inject.Inject;
23+
import org.eclipse.microprofile.config.Config;
24+
import org.eclipse.microprofile.config.spi.Converter;
25+
import org.eclipse.microprofile.config.tck.MapConverterBean.EnumKey;
26+
import org.eclipse.microprofile.config.tck.MapConverterBean.EnumValue;
27+
import org.eclipse.microprofile.config.tck.converters.Pizza;
28+
import org.eclipse.microprofile.config.tck.converters.PizzaConverter;
29+
import org.eclipse.microprofile.config.tck.util.AdditionalAssertions;
30+
import org.jboss.arquillian.container.test.api.Deployment;
31+
import org.jboss.arquillian.testng.Arquillian;
32+
import org.jboss.shrinkwrap.api.ShrinkWrap;
33+
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
34+
import org.jboss.shrinkwrap.api.spec.JavaArchive;
35+
import org.jboss.shrinkwrap.api.spec.WebArchive;
36+
import org.testng.Assert;
37+
import org.testng.annotations.Test;
38+
39+
import java.net.MalformedURLException;
40+
import java.net.URI;
41+
import java.net.URISyntaxException;
42+
import java.net.URL;
43+
import java.time.Duration;
44+
import java.time.Instant;
45+
import java.time.LocalDate;
46+
import java.time.LocalDateTime;
47+
import java.time.LocalTime;
48+
import java.time.OffsetDateTime;
49+
import java.time.OffsetTime;
50+
import java.util.Arrays;
51+
import java.util.Collections;
52+
import java.util.LinkedHashSet;
53+
import java.util.List;
54+
import java.util.Optional;
55+
import java.util.Set;
56+
57+
import static org.eclipse.microprofile.config.tck.base.AbstractTest.addFile;
58+
import static org.eclipse.microprofile.config.tck.util.AdditionalAssertions.assertURLArrayEquals;
59+
import static org.eclipse.microprofile.config.tck.util.AdditionalAssertions.assertURLListEquals;
60+
import static org.eclipse.microprofile.config.tck.util.AdditionalAssertions.assertURLSetEquals;
61+
62+
/**
63+
* Test the implicit converter handling.
64+
**/
65+
public class MapConverterTest extends Arquillian {
66+
67+
@Deployment
68+
public static WebArchive deploy() {
69+
JavaArchive testJar = ShrinkWrap
70+
.create(JavaArchive.class, "mapConverterTest.jar")
71+
.addPackage(PizzaConverter.class.getPackage())
72+
.addClasses(MapConverterTest.class, MapConverterBean.class, AdditionalAssertions.class)
73+
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
74+
.addAsServiceProvider(Converter.class, PizzaConverter.class)
75+
.as(JavaArchive.class);
76+
addFile(testJar, "META-INF/microprofile-config.properties");
77+
WebArchive war = ShrinkWrap
78+
.create(WebArchive.class, "mapConverterTest.war")
79+
.addAsLibrary(testJar);
80+
return war;
81+
}
82+
83+
@Inject
84+
private MapConverterBean converterBean;
85+
86+
/////////////////////////////////// Test Map//////////////////////////
87+
88+
89+
@Test
90+
public void testStringStringMapInjection() {
91+
Assert.assertEquals(converterBean.getMyStringStringMap().size(), 2);
92+
Assert.assertEquals(converterBean.getMyStringStringMap().get("key1"), "string.string.value1");
93+
Assert.assertEquals(converterBean.getMyStringStringMap().get("key2"), "string.string.value2");
94+
}
95+
96+
@Test
97+
public void testStringIntegerMapInjection() {
98+
Assert.assertEquals(converterBean.getMyStringIntegerMap().size(), 2);
99+
Assert.assertEquals(converterBean.getMyStringIntegerMap().get("key1"), Integer.valueOf(100));
100+
Assert.assertEquals(converterBean.getMyStringIntegerMap().get("key2"), Integer.valueOf(200));
101+
}
102+
103+
@Test
104+
public void testIntegerStringMapInjection() {
105+
Assert.assertEquals(converterBean.getMyIntegerStringMap().size(), 2);
106+
Assert.assertEquals(converterBean.getMyIntegerStringMap().get(100), "integer.string.value1");
107+
Assert.assertEquals(converterBean.getMyIntegerStringMap().get(200), "integer.string.value2");
108+
}
109+
110+
@Test
111+
public void testEnumEnumMapInjection() {
112+
Assert.assertEquals(converterBean.getMyEnumEnumMap().size(), 2);
113+
Assert.assertEquals(converterBean.getMyEnumEnumMap().get(EnumKey.key1), EnumValue.enum1);
114+
Assert.assertEquals(converterBean.getMyEnumEnumMap().get(EnumKey.key2), EnumValue.enum2);
115+
}
116+
}

tck/src/main/resources/internal/META-INF/microprofile-config.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,12 @@ tck.config.test.javaconfig.converter.urlvalues=http://microprofile.io,http://ope
167167
tck.config.test.javaconfig.converter.class=org.eclipse.microprofile.config.tck.ClassConverterTest
168168
tck.config.test.javaconfig.converter.class.array=org.eclipse.microprofile.config.tck.ClassConverterTest,java.lang.String
169169

170+
171+
tck.config.test.javaconfig.converter.map.string.string.key1=string.string.value1
172+
tck.config.test.javaconfig.converter.map.string.string.key2=string.string.value2
173+
tck.config.test.javaconfig.converter.map.integer.string.100=integer.string.value1
174+
tck.config.test.javaconfig.converter.map.integer.string.200=integer.string.value2
175+
tck.config.test.javaconfig.converter.map.string.integer.key1=100
176+
tck.config.test.javaconfig.converter.map.string.integer.key2=200
177+
tck.config.test.javaconfig.converter.map.enum.enum.key1=value1
178+
tck.config.test.javaconfig.converter.map.enum.enum.key2=value2

0 commit comments

Comments
 (0)