Skip to content

Commit

Permalink
add support for Map injection
Browse files Browse the repository at this point in the history
  • Loading branch information
rmanibus committed Aug 31, 2022
1 parent 026a784 commit 7792571
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 2 deletions.
9 changes: 7 additions & 2 deletions spec/src/main/asciidoc/configexamples.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ public class InjectedConfigUsageSample {
@ConfigProperty(name="myprj.some.supplier.timeout", defaultValue="100")
private java.util.function.Supplier<Long> 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<Integer, String> 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;
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> myStringStringMap;

@Inject
@ConfigProperty(name = "tck.config.test.javaconfig.converter.map.integer.string")
private Map<Integer, String> myIntegerStringMap;

@Inject
@ConfigProperty(name = "tck.config.test.javaconfig.converter.map.string.integer")
private Map<String, Integer> myStringIntegerMap;

@Inject
@ConfigProperty(name = "tck.config.test.javaconfig.converter.map.enum.enum")
private Map<EnumKey, EnumValue> myEnumEnumMap;

public Map<String, String> getMyStringStringMap() {
return myStringStringMap;
}

public Map<Integer, String> getMyIntegerStringMap() {
return myIntegerStringMap;
}

public Map<String, Integer> getMyStringIntegerMap() {
return myStringIntegerMap;
}

public Map<EnumKey, EnumValue> getMyEnumEnumMap() {
return myEnumEnumMap;
}

public static enum EnumKey {
key1,
key2;
}

public static enum EnumValue {
enum1,
enum2;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7792571

Please sign in to comment.