From 9621821eddd828643761ccbf89c32adb7f5ce821 Mon Sep 17 00:00:00 2001 From: Urban Malc Date: Mon, 9 Sep 2019 15:17:03 +0200 Subject: [PATCH] Formatting and minor fixes --- .../ee/config/microprofile/ConfigImpl.java | 48 ++--- .../adapters/ConfigSourceAdapter.java | 77 ++++--- .../tests/ArrayInjectionTest.java | 201 +++++++++--------- src/test/resources/config.yaml | 18 +- 4 files changed, 165 insertions(+), 179 deletions(-) diff --git a/src/main/java/com/kumuluz/ee/config/microprofile/ConfigImpl.java b/src/main/java/com/kumuluz/ee/config/microprofile/ConfigImpl.java index f713309..8690983 100644 --- a/src/main/java/com/kumuluz/ee/config/microprofile/ConfigImpl.java +++ b/src/main/java/com/kumuluz/ee/config/microprofile/ConfigImpl.java @@ -17,28 +17,22 @@ * out of or in connection with the software or the use or other dealings in the * software. See the License for the specific language governing permissions and * limitations under the License. -*/ + */ package com.kumuluz.ee.config.microprofile; +import com.kumuluz.ee.config.microprofile.converters.ImplicitConverter; +import com.kumuluz.ee.config.microprofile.utils.AlternativeTypesUtil; +import org.eclipse.microprofile.config.Config; +import org.eclipse.microprofile.config.spi.ConfigSource; +import org.eclipse.microprofile.config.spi.Converter; + import java.io.Serializable; import java.lang.reflect.Array; import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.NoSuchElementException; -import java.util.Optional; -import java.util.logging.Logger; +import java.util.*; import java.util.regex.Pattern; import java.util.stream.Collectors; -import org.eclipse.microprofile.config.Config; -import org.eclipse.microprofile.config.spi.ConfigSource; -import org.eclipse.microprofile.config.spi.Converter; - -import com.kumuluz.ee.config.microprofile.converters.ImplicitConverter; -import com.kumuluz.ee.config.microprofile.utils.AlternativeTypesUtil; - /** * Microprofile Config implementation that exposes KumuluzEE configuration framework. * @@ -48,13 +42,11 @@ * @since 1.1 */ public class ConfigImpl implements Config, Serializable { - private static final Logger logger = Logger.getLogger(ConfigImpl.class.getCanonicalName()); + private static final String ARRAY_SEPARATOR_REGEX = "(? converters; private List configSources; - private static final String ARRAY_SEPARATOR_REGEX = "(? configSources, Map converters) { this.configSources = configSources; this.converters = converters; @@ -111,7 +103,7 @@ public T convert(String value, Class asType) { Array.set(arr, i, a.get(i)); } - return (T)arr; + return (T) arr; } Converter converter = getConverter(asType); @@ -120,7 +112,7 @@ public T convert(String value, Class asType) { public List convertList(String value, Class listType) { - String[] tokens = value.split(ARRAY_SEPARATOR_REGEX); + String[] tokens = value.split(ARRAY_SEPARATOR_REGEX); Converter converter = getConverter(listType); List convertedList = new ArrayList<>(tokens.length); @@ -152,13 +144,13 @@ private Converter getConverter(Class asType) { return converter; } - @Override - public Iterable getPropertyNames() { - return this.configSources.stream().flatMap(e -> e.getPropertyNames().stream()).collect(Collectors.toSet()); - } + @Override + public Iterable getPropertyNames() { + return this.configSources.stream().flatMap(e -> e.getPropertyNames().stream()).collect(Collectors.toSet()); + } - @Override - public Iterable getConfigSources() { - return this.configSources; - } -} \ No newline at end of file + @Override + public Iterable getConfigSources() { + return this.configSources; + } +} diff --git a/src/main/java/com/kumuluz/ee/config/microprofile/adapters/ConfigSourceAdapter.java b/src/main/java/com/kumuluz/ee/config/microprofile/adapters/ConfigSourceAdapter.java index 49dd13c..af118d8 100644 --- a/src/main/java/com/kumuluz/ee/config/microprofile/adapters/ConfigSourceAdapter.java +++ b/src/main/java/com/kumuluz/ee/config/microprofile/adapters/ConfigSourceAdapter.java @@ -20,15 +20,14 @@ */ package com.kumuluz.ee.config.microprofile.adapters; +import com.kumuluz.ee.configuration.ConfigurationSource; +import org.eclipse.microprofile.config.spi.ConfigSource; + import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; -import org.eclipse.microprofile.config.spi.ConfigSource; - -import com.kumuluz.ee.configuration.ConfigurationSource; - /** * Adapts KumuluzEE configuration framework {@link ConfigurationSource} to MicroProfile Config {@link ConfigSource}. * @@ -37,6 +36,7 @@ * @since 1.3 */ public class ConfigSourceAdapter implements ConfigSource { + private ConfigurationSource configurationSource; public ConfigSourceAdapter(ConfigurationSource configurationSource) { @@ -56,21 +56,21 @@ public Map getProperties() { public int getOrdinal() { return configurationSource.getOrdinal(); } - + @Override public String getValue(String s) { - String val = configurationSource.get(s).orElse(null); - - if (val != null) { - Optional listSize = this.configurationSource.getListSize(s); - - //this is a list or an array - if (listSize.isPresent()) { - //we ignore the returned value and build the array - return buildArray(s, listSize.get()); - } - } - + String val = configurationSource.get(s).orElse(null); + + if (val != null) { + Optional listSize = this.configurationSource.getListSize(s); + + //this is a list or an array + if (listSize.isPresent()) { + //we ignore the returned value and build the array + return buildArray(s, listSize.get()); + } + } + return val; } @@ -106,28 +106,23 @@ private void buildPropertiesMap(Map map, String prefix) { } } } - - private String buildArray(String propertyName, int size) { - StringBuilder sb = new StringBuilder(); - - for (int i = 0; i < size; i++) { - String prefix = String.format("%s[%d]", propertyName, i); - Optional> objectKeys = this.configurationSource.getMapKeys(prefix); - - //array item is an object, so we just omit it - if (objectKeys.isPresent()) { - sb.append(""); - } - else { - Optional item = this.configurationSource.get(String.format("%s[%d]", propertyName, i)); - item.ifPresent(sb::append); - } - - if (i < size - 1) { - sb.append(','); - } - } - - return sb.toString(); - } + + private String buildArray(String propertyName, int size) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < size; i++) { + String prefix = String.format("%s[%d]", propertyName, i); + Optional> objectKeys = this.configurationSource.getMapKeys(prefix); + + if (!objectKeys.isPresent()) { + Optional item = this.configurationSource.get(String.format("%s[%d]", propertyName, i)); + if (i > 0) { + sb.append(','); + } + item.ifPresent(sb::append); + } // else array item is an object, so we just omit it + } + + return sb.toString(); + } } diff --git a/src/test/java/com/kumuluz/ee/config/microprofile/tests/ArrayInjectionTest.java b/src/test/java/com/kumuluz/ee/config/microprofile/tests/ArrayInjectionTest.java index 1137a61..249e514 100644 --- a/src/test/java/com/kumuluz/ee/config/microprofile/tests/ArrayInjectionTest.java +++ b/src/test/java/com/kumuluz/ee/config/microprofile/tests/ArrayInjectionTest.java @@ -1,101 +1,100 @@ -/* - * Copyright (c) 2014-2017 Kumuluz and/or its affiliates - * and other contributors as indicated by the @author tags and - * the contributor list. - * - * Licensed under the MIT License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/MIT - * - * The software is provided "AS IS", WITHOUT WARRANTY OF ANY KIND, express or - * implied, including but not limited to the warranties of merchantability, - * fitness for a particular purpose and noninfringement. in no event shall the - * authors or copyright holders be liable for any claim, damages or other - * liability, whether in an action of contract, tort or otherwise, arising from, - * out of or in connection with the software or the use or other dealings in the - * software. See the License for the specific language governing permissions and - * limitations under the License. -*/ -package com.kumuluz.ee.config.microprofile.tests; - -import java.util.Arrays; -import java.util.List; -import java.util.Optional; - -import javax.inject.Inject; - -import org.eclipse.microprofile.config.inject.ConfigProperty; -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.testng.Assert; -import org.testng.annotations.Test; - -/** - * Tests that check MP Config array/list injection from KumuluzEE's config.yaml - * - * @author Yog Sothoth - * @since 1.4 - * - */ -@Test -public class ArrayInjectionTest extends Arquillian { - @Deployment - public static JavaArchive deploy() { - JavaArchive testJar = ShrinkWrap - .create(JavaArchive.class, "arrayInjectionTest.jar") - .addClasses(ArrayInjectionTest.class) - .addAsResource("config.yaml") - .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml") - .as(JavaArchive.class); - return testJar; - } - - @Inject - @ConfigProperty(name="parameter.arrayParameter") - private List listParam; - - @Inject - @ConfigProperty(name="parameter.arrayParameter") - private String[] arrayParam; - - @Inject - @ConfigProperty(name="parameter.arrayParameter") - private Optional arrayParamOpt; - - @Inject - @ConfigProperty(name="parameter.stringParameter") - private Optional stringParam; - - @Inject - @ConfigProperty(name="parameter.arrayParameter[0]") - private String arrayItemParam; - - private static final String[] TEST_TARGET = - new String[]{"one ", " two", " [three, four] "}; - - @Test - public void testNotParsingDelimeters() { - Assert.assertEquals("[here be, dragons]", stringParam.get()); - } - - @Test - public void testArrayItemInjection() { - Assert.assertEquals(TEST_TARGET[0], arrayItemParam); - } - - @Test - public void testArrayInjection() { - Assert.assertEquals(TEST_TARGET, arrayParamOpt.get()); - Assert.assertEquals(TEST_TARGET, arrayParam); - } - - @Test - public void testListInjection() { - Assert.assertEquals(Arrays.asList(TEST_TARGET), listParam); - } -} +/* + * Copyright (c) 2014-2017 Kumuluz and/or its affiliates + * and other contributors as indicated by the @author tags and + * the contributor list. + * + * Licensed under the MIT License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/MIT + * + * The software is provided "AS IS", WITHOUT WARRANTY OF ANY KIND, express or + * implied, including but not limited to the warranties of merchantability, + * fitness for a particular purpose and noninfringement. in no event shall the + * authors or copyright holders be liable for any claim, damages or other + * liability, whether in an action of contract, tort or otherwise, arising from, + * out of or in connection with the software or the use or other dealings in the + * software. See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.kumuluz.ee.config.microprofile.tests; + +import org.eclipse.microprofile.config.inject.ConfigProperty; +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.testng.Assert; +import org.testng.annotations.Test; + +import javax.inject.Inject; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +/** + * Tests that check MP Config array/list injection from KumuluzEE's config.yaml + * + * @author Yog Sothoth + * @since 1.4 + */ +@Test +public class ArrayInjectionTest extends Arquillian { + + private static final String[] TEST_TARGET = + new String[]{"one ", " two", " [three, four] "}; + + @Inject + @ConfigProperty(name = "parameter.arrayParameter") + private List listParam; + + @Inject + @ConfigProperty(name = "parameter.arrayParameter") + private String[] arrayParam; + + @Inject + @ConfigProperty(name = "parameter.arrayParameter") + private Optional arrayParamOpt; + + @Inject + @ConfigProperty(name = "parameter.stringParameter") + private Optional stringParam; + + @Inject + @ConfigProperty(name = "parameter.arrayParameter[0]") + private String arrayItemParam; + + @Deployment + public static JavaArchive deploy() { + JavaArchive testJar = ShrinkWrap + .create(JavaArchive.class, "arrayInjectionTest.jar") + .addClasses(ArrayInjectionTest.class) + .addAsResource("config.yaml") + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml") + .as(JavaArchive.class); + return testJar; + } + + @Test + public void testNotParsingDelimeters() { + Assert.assertEquals("[here be, dragons]", stringParam.get()); + } + + @Test + public void testArrayItemInjection() { + Assert.assertEquals(TEST_TARGET[0], arrayItemParam); + } + + @Test + public void testArrayInjection() { + Assert.assertEquals(TEST_TARGET, arrayParamOpt.get()); + Assert.assertEquals(TEST_TARGET, arrayParam); + } + + @Test + public void testListInjection() { + Assert.assertEquals(Arrays.asList(TEST_TARGET), listParam); + } +} diff --git a/src/test/resources/config.yaml b/src/test/resources/config.yaml index 06fd70a..e328381 100644 --- a/src/test/resources/config.yaml +++ b/src/test/resources/config.yaml @@ -1,9 +1,9 @@ -parameter: - stringParameter: "[here be, dragons]" - arrayParameter: - - "one " - - " two" - - " [three\\, four] " - - five: - six: seven - eight: nine \ No newline at end of file +parameter: + stringParameter: "[here be, dragons]" + arrayParameter: + - "one " + - " two" + - " [three\\, four] " + - five: + six: seven + eight: nine