Repository: incubator-tamaya Updated Branches: refs/heads/master f255aa9b9 -> 27c0d76bb
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilderTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilderTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilderTest.java new file mode 100644 index 0000000..c586969 --- /dev/null +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilderTest.java @@ -0,0 +1,220 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.tamaya.spisupport; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.spi.ConfigurationBuilder; +import org.apache.tamaya.spi.*; +import org.junit.Test; + +import java.util.Collections; +import java.util.Map; + +import static org.junit.Assert.*; + +/** + * Tests for {@link DefaultConfigurationBuilder} by atsticks on 06.09.16. + */ +public class DefaultConfigurationBuilderTest { + + private TestPropertySource testPropertySource = new TestPropertySource(){}; + + @Test + public void setContext() throws Exception { + ConfigurationContext context = ConfigurationProvider.getConfiguration().getContext(); + ConfigurationBuilder b = new DefaultConfigurationBuilder() + .setContext(context); + assertEquals(context, b.build().getContext()); + } + + @Test + public void setConfiguration() throws Exception { + Configuration cfg = ConfigurationProvider.getConfiguration(); + ConfigurationBuilder b = new DefaultConfigurationBuilder() + .setConfiguration(cfg); + assertEquals(cfg, b.build()); + } + + @Test + public void addPropertySources_Array() throws Exception { + PropertySource testPS2 = new TestPropertySource("addPropertySources_Array_2"); + ConfigurationBuilder b = new DefaultConfigurationBuilder() + .addPropertySources(testPropertySource, testPS2); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertEquals(2, ctx.getPropertySources().size()); + assertTrue(ctx.getPropertySources().contains(testPropertySource)); + assertTrue(ctx.getPropertySources().contains(testPS2)); + } + + @Test + public void removePropertySources_Array() throws Exception { + PropertySource testPS2 = new TestPropertySource("addPropertySources_Array_2"); + ConfigurationBuilder b = new DefaultConfigurationBuilder() + .addPropertySources(testPropertySource, testPS2); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertEquals(2, ctx.getPropertySources().size()); + assertTrue(ctx.getPropertySources().contains(testPropertySource)); + assertTrue(ctx.getPropertySources().contains(testPS2)); + b = new DefaultConfigurationBuilder() + .addPropertySources(testPropertySource, testPS2); + b.removePropertySources(testPropertySource); + cfg = b.build(); + ctx = cfg.getContext(); + assertEquals(1, ctx.getPropertySources().size()); + assertFalse(ctx.getPropertySources().contains(testPropertySource)); + assertTrue(ctx.getPropertySources().contains(testPS2)); + } + + @Test + public void addPropertyFilters_Array() throws Exception { + PropertyFilter filter1 = (value, context) -> value; + PropertyFilter filter2 = (value, context) -> value; + DefaultConfigurationBuilder b = new DefaultConfigurationBuilder(); + b.addPropertyFilters(filter1, filter2); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertEquals(2, ctx.getPropertyFilters().size()); + b = new DefaultConfigurationBuilder(); + b.addPropertyFilters(filter1, filter2); + b.addPropertyFilters(filter1, filter2); + assertEquals(2, ctx.getPropertyFilters().size()); + } + + @Test + public void removePropertyFilters_Array() throws Exception { + PropertyFilter filter1 = (value, context) -> value; + PropertyFilter filter2 = (value, context) -> value; + ConfigurationBuilder b = new DefaultConfigurationBuilder() + .addPropertyFilters(filter1, filter2); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertEquals(2, ctx.getPropertyFilters().size()); + b = new DefaultConfigurationBuilder() + .addPropertyFilters(filter1, filter2); + b.removePropertyFilters(filter1); + cfg = b.build(); + ctx = cfg.getContext(); + assertEquals(1, ctx.getPropertyFilters().size()); + assertFalse(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + } + + @Test + @SuppressWarnings({ "rawtypes", "unchecked" }) + public void addPropertyConverter() throws Exception { + PropertyConverter converter = (value, context) -> value.toLowerCase(); + ConfigurationBuilder b = new DefaultConfigurationBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + assertEquals(1, ctx.getPropertyConverters().size()); + b = new DefaultConfigurationBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + b.addPropertyConverters(TypeLiteral.of(String.class), converter); + assertEquals(1, ctx.getPropertyConverters().size()); + } + + @Test + @SuppressWarnings({ "rawtypes", "unchecked" }) + public void removePropertyConverters_Array() throws Exception { + PropertyConverter converter = (value, context) -> value.toLowerCase(); + ConfigurationBuilder b = new DefaultConfigurationBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + assertEquals(1, ctx.getPropertyConverters(TypeLiteral.of(String.class)).size()); + b = new DefaultConfigurationBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + b.removePropertyConverters(TypeLiteral.of(String.class), converter); + cfg = b.build(); + ctx = cfg.getContext(); + assertFalse(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).isEmpty()); + } + + @Test + public void setPropertyValueCombinationPolicy() throws Exception { + PropertyValueCombinationPolicy combPol = (currentValue, key, propertySource) -> currentValue; + ConfigurationBuilder b = new DefaultConfigurationBuilder() + .setPropertyValueCombinationPolicy(combPol); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertEquals(ctx.getPropertyValueCombinationPolicy(), combPol); + } + + @Test + public void build() throws Exception { + assertNotNull(new DefaultConfigurationBuilder().build()); + } + + @Test + public void bla() throws Exception { + ConfigurationBuilder builder = ConfigurationProvider.getConfigurationBuilder(); + builder.addDefaultPropertyConverters(); + } + + private static class TestPropertySource implements PropertySource{ + + private String id; + + public TestPropertySource(){ + this(null); + } + + public TestPropertySource(String id){ + this.id = id; + } + + @Override + public int getOrdinal() { + return 200; + } + + @Override + public String getName() { + return id!=null?id:"TestPropertySource"; + } + + @Override + public PropertyValue get(String key) { + return PropertyValue.of(key, key + "Value", getName()); + } + + @Override + public Map<String, PropertyValue> getProperties() { + return Collections.emptyMap(); + } + + @Override + public boolean isScannable() { + return false; + } + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultServiceContextTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultServiceContextTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultServiceContextTest.java new file mode 100644 index 0000000..9a25528 --- /dev/null +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultServiceContextTest.java @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.tamaya.spisupport; + +import org.apache.tamaya.ConfigException; +import org.apache.tamaya.spi.ConfigurationProviderSpi; +import org.junit.Assert; +import org.junit.Test; + +import javax.annotation.Priority; +import java.util.Collection; +import java.util.List; + +public class DefaultServiceContextTest { + + /** + * context to test + */ + private final DefaultServiceContext context = new DefaultServiceContext(); + + + @Test + public void testGetService() { + ConfigurationProviderSpi providerSpi = context.getService(ConfigurationProviderSpi.class); + Assert.assertNotNull(providerSpi); + Assert.assertTrue(providerSpi instanceof TestConfigurationProvider); + } + + @Test(expected = ConfigException.class) + public void testGetService_multipleServicesWithoutPriority_shouldThrowConfigException() { + context.getService(InvalidPriorityInterface.class); + } + + @Test + public void testGetService_multipleService_shouldReturnServiceWithHighestPriority() { + MultiImplsInterface service = context.getService(MultiImplsInterface.class); + + Assert.assertNotNull(service); + Assert.assertTrue(service instanceof MultiImpl2); + } + + @Test + public void testGetService_noImpl_shouldReturnEmptyOpional() { + NoImplInterface service = context.getService(NoImplInterface.class); + Assert.assertNull(service); + } + + + @Test + public void testGetServices_shouldReturnServices() { + { + Collection<InvalidPriorityInterface> services = context.getServices(InvalidPriorityInterface.class); + Assert.assertNotNull(services); + Assert.assertEquals(2, services.size()); + + for (InvalidPriorityInterface service : services) { + Assert.assertTrue(service instanceof InvalidPriorityImpl1 || service instanceof InvalidPriorityImpl2); + } + } + + { + List<MultiImplsInterface> services = context.getServices(MultiImplsInterface.class); + Assert.assertNotNull(services); + Assert.assertEquals(3, services.size()); + + Assert.assertTrue(services.get(0) instanceof MultiImpl2); + Assert.assertTrue(services.get(1) instanceof MultiImpl1); + Assert.assertTrue(services.get(2) instanceof MultiImpl3); + } + } + + @Test + public void testGetServices_redundantAccessToServices() { + for(int i=0;i<10;i++){ + Collection<InvalidPriorityInterface> services = context.getServices(InvalidPriorityInterface.class); + Assert.assertNotNull(services); + Assert.assertEquals(2, services.size()); + for (InvalidPriorityInterface service : services) { + Assert.assertTrue(service instanceof InvalidPriorityImpl1 || service instanceof InvalidPriorityImpl2); + } + } + } + + @Test + public void testGetServices_noImpl_shouldReturnEmptyList() { + Collection<NoImplInterface> services = context.getServices(NoImplInterface.class); + Assert.assertNotNull(services); + Assert.assertTrue(services.isEmpty()); + } + + + // some test interfaces and classes + + public interface InvalidPriorityInterface { + } + + @Priority(value = 50) + public static class InvalidPriorityImpl1 implements InvalidPriorityInterface { + } + + @Priority(value = 50) + public static class InvalidPriorityImpl2 implements InvalidPriorityInterface { + } + + + public interface MultiImplsInterface { + } + + public static class MultiImpl1 implements MultiImplsInterface { + } + + @Priority(value = 500) + public static class MultiImpl2 implements MultiImplsInterface { + } + + @Priority(value = -10) + public static class MultiImpl3 implements MultiImplsInterface { + } + + private interface NoImplInterface { + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/spi-support/src/test/java/org/apache/tamaya/spisupport/TestConfigurationProvider.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/TestConfigurationProvider.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/TestConfigurationProvider.java new file mode 100644 index 0000000..9af9a5c --- /dev/null +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/TestConfigurationProvider.java @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.tamaya.spisupport; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.spi.ConfigurationBuilder; +import org.apache.tamaya.spi.ConfigurationContext; +import org.apache.tamaya.spi.ConfigurationContextBuilder; +import org.apache.tamaya.spi.ConfigurationProviderSpi; +import org.osgi.service.component.annotations.Component; + +import java.util.Objects; + +/** + * Implementation of the Configuration API. This class uses the current {@link ConfigurationContext} to evaluate the + * chain of {@link org.apache.tamaya.spi.PropertySource} and {@link org.apache.tamaya.spi.PropertyFilter} + * instance to evaluate the current Configuration. + */ +@Component(service = ConfigurationProviderSpi.class) +public class TestConfigurationProvider implements ConfigurationProviderSpi { + + private Configuration config = new DefaultConfigurationBuilder() + .addDefaultPropertyConverters() + .addDefaultPropertyFilters() + .addDefaultPropertySources() + .build(); + @Override + public Configuration getConfiguration() { + return config; + } + + @Override + public Configuration createConfiguration(ConfigurationContext context) { + return new DefaultConfiguration(context); + } + + @Override + public ConfigurationBuilder getConfigurationBuilder() { + return new DefaultConfigurationBuilder(); + } + + @Override + public ConfigurationContextBuilder getConfigurationContextBuilder() { + return new DefaultConfigurationContextBuilder(); + } + + @Override + public void setConfiguration(Configuration config) { + Objects.requireNonNull(config.getContext()); + this.config = Objects.requireNonNull(config); + } + + @Override + public boolean isConfigurationSettable() { + return true; + } + + /** + * @deprecated use {@link Configuration#getContext()} instead. + */ + @Deprecated + @Override + public ConfigurationContext getConfigurationContext() { + return this.config.getContext(); + } + + /** + * @deprecated the context should be given upon creation of the {@link Configuration} + */ + @Deprecated + @Override + public void setConfigurationContext(ConfigurationContext context){ + this.config = new DefaultConfigurationBuilder(context).build(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi b/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi new file mode 100644 index 0000000..aae53e4 --- /dev/null +++ b/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +org.apache.tamaya.spisupport.TestConfigurationProvider \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spisupport.DefaultServiceContextTest$InvalidPriorityInterface ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spisupport.DefaultServiceContextTest$InvalidPriorityInterface b/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spisupport.DefaultServiceContextTest$InvalidPriorityInterface new file mode 100644 index 0000000..c894501 --- /dev/null +++ b/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spisupport.DefaultServiceContextTest$InvalidPriorityInterface @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +org.apache.tamaya.spisupport.DefaultServiceContextTest$InvalidPriorityImpl1 +org.apache.tamaya.spisupport.DefaultServiceContextTest$InvalidPriorityImpl2 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spisupport.DefaultServiceContextTest$MultiImplsInterface ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spisupport.DefaultServiceContextTest$MultiImplsInterface b/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spisupport.DefaultServiceContextTest$MultiImplsInterface new file mode 100644 index 0000000..71f7c46 --- /dev/null +++ b/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spisupport.DefaultServiceContextTest$MultiImplsInterface @@ -0,0 +1,20 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +org.apache.tamaya.spisupport.DefaultServiceContextTest$MultiImpl1 +org.apache.tamaya.spisupport.DefaultServiceContextTest$MultiImpl2 +org.apache.tamaya.spisupport.DefaultServiceContextTest$MultiImpl3 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/jqassistant/serviceloader-rules.xml ---------------------------------------------------------------------- diff --git a/jqassistant/serviceloader-rules.xml b/jqassistant/serviceloader-rules.xml index 082430a..c8dd907 100644 --- a/jqassistant/serviceloader-rules.xml +++ b/jqassistant/serviceloader-rules.xml @@ -113,9 +113,9 @@ under the License. // service configurations in version 1.0.0. // I reported this problem already to the project // Oliver B. Fischer, 15.5.5 - AND NOT sl.fileName IN ['/META-INF/services/org.apache.tamaya.core.internal.DefaultServiceContextTest$MultiImplsInterface', - '/META-INF/services/org.apache.tamaya.core.internal.DefaultServiceContextTest$InvalidPriorityInterface', - '/META-INF/services/org.apache.tamaya.core.internal.DefaultServiceContextTest$InvalidPriorityInterface'] + AND NOT sl.fileName IN ['/META-INF/services/org.apache.tamaya.spisupport.DefaultServiceContextTest$MultiImplsInterface', + '/META-INF/services/org.apache.tamaya.spisupport.DefaultServiceContextTest$InvalidPriorityInterface', + '/META-INF/services/org.apache.tamaya.spisupport.DefaultServiceContextTest$InvalidPriorityInterface'] RETURN sl.fileName AS serviceConfiguration, entry.name AS class