Repository: incubator-tamaya Updated Branches: refs/heads/master afc3e1031 -> 98432a50e
Added some tests for the DefaultConfiguration. Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/98432a50 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/98432a50 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/98432a50 Branch: refs/heads/master Commit: 98432a50e94227fee9117da281b18baa8f9bb994 Parents: afc3e10 Author: Oliver B. Fischer <[email protected]> Authored: Thu Feb 5 18:45:38 2015 +0100 Committer: Oliver B. Fischer <[email protected]> Committed: Thu Feb 5 18:45:38 2015 +0100 ---------------------------------------------------------------------- java8/core/pom.xml | 4 + .../org/apache/tamaya/core/NotMockedAnswer.java | 55 +++++ .../core/internal/DefaultConfigurationTest.java | 204 +++++++++++++++++++ .../builder/util/mockito/NotMockedAnswer.java | 1 + pom.xml | 1 + 5 files changed, 265 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/98432a50/java8/core/pom.xml ---------------------------------------------------------------------- diff --git a/java8/core/pom.xml b/java8/core/pom.xml index 5489ca9..73bdca9 100644 --- a/java8/core/pom.xml +++ b/java8/core/pom.xml @@ -47,6 +47,10 @@ under the License. <artifactId>junit</artifactId> </dependency> <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + </dependency> + <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-library</artifactId> </dependency> http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/98432a50/java8/core/src/test/java/org/apache/tamaya/core/NotMockedAnswer.java ---------------------------------------------------------------------- diff --git a/java8/core/src/test/java/org/apache/tamaya/core/NotMockedAnswer.java b/java8/core/src/test/java/org/apache/tamaya/core/NotMockedAnswer.java new file mode 100644 index 0000000..c2a35d8 --- /dev/null +++ b/java8/core/src/test/java/org/apache/tamaya/core/NotMockedAnswer.java @@ -0,0 +1,55 @@ +/* + * 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.core; + +import org.mockito.exceptions.base.MockitoException; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +import java.io.Serializable; + +// @todo This is a duplicated class +public class NotMockedAnswer implements Answer<Object>, Serializable { + public final static NotMockedAnswer NOT_MOCKED_ANSWER = new NotMockedAnswer(); + + private NotMockedAnswer() { + } + + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + StringBuilder msgBuilder = new StringBuilder(); + + msgBuilder.append("Invocation of method not mocked: ") + .append(invocation.getMethod().toGenericString()); + + if (invocation.getArguments().length > 0) { + msgBuilder.append(" Supplied arguments: "); + + for (int i = 0; i < invocation.getArguments().length; i++) { + msgBuilder.append(invocation.getArguments()[i]); + + if (i - 1 < invocation.getArguments().length) { + msgBuilder.append(", "); + } + } + } + + throw new MockitoException(msgBuilder.toString()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/98432a50/java8/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationTest.java ---------------------------------------------------------------------- diff --git a/java8/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationTest.java b/java8/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationTest.java new file mode 100644 index 0000000..8b1c2e0 --- /dev/null +++ b/java8/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationTest.java @@ -0,0 +1,204 @@ +/* + * 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.core.internal; + +import org.apache.tamaya.spi.ConfigurationContext; +import org.apache.tamaya.spi.PropertyFilter; +import org.apache.tamaya.spi.PropertySource; +import org.junit.Test; +import org.mockito.Mockito; + +import javax.annotation.Priority; +import java.util.ArrayList; +import java.util.List; + +import static java.util.Collections.singletonList; +import static org.apache.tamaya.core.NotMockedAnswer.NOT_MOCKED_ANSWER; +import static org.apache.tamaya.spi.PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; + +public class DefaultConfigurationTest { + private FilterAppendingA FILTER_APPENDING_A = new FilterAppendingA(); + private FilterPrependingB FILTER_PREPENDING_B = new FilterPrependingB(); + private FilterPrependingC FILTER_PREPENDING_C = new FilterPrependingC(); + private FilterPrependingD FILTER_PREPENDING_D = new FilterPrependingD(); + + /* = + * =- Section with tests for filtering properties with PropertyFilters + * = + */ + + @Test + public void filteringOfGivenValueWorksWithOneFilterWithoutPriority() { + PropertySource source = Mockito.mock(PropertySource.class, NOT_MOCKED_ANSWER); + + doReturn("V").when(source).get(eq("key")); + doReturn(0).when(source).getOrdinal(); + doReturn("source").when(source).getName(); + + ConfigurationContext context = Mockito.mock(ConfigurationContext.class, NOT_MOCKED_ANSWER); + + doReturn(singletonList(source)).when(context).getPropertySources(); + doReturn(singletonList(FILTER_APPENDING_A)).when(context).getPropertyFilters(); + doReturn(DEFAULT_OVERRIDING_COLLECTOR).when(context).getPropertyValueCombinationPolicy(); + + DefaultConfiguration configuration = new DefaultConfiguration(context); + + String value = configuration.get("key"); + + verify(source, atLeastOnce()).get(eq("key")); + verifyNoMoreInteractions(source); + + assertThat(value, equalTo("VA$")); + } + + @Test + public void filteringOfGivenValueWorksWithTwoFiltersWithoutPriority() { + List<PropertyFilter> filters = new ArrayList<>(); + + FilterAppendingA filterA = spy(FILTER_APPENDING_A); + FilterPrependingB filterB = spy(FILTER_PREPENDING_B); + filters.add(filterA); + filters.add(filterB); + + PropertySource source = Mockito.mock(PropertySource.class, NOT_MOCKED_ANSWER); + + doReturn("V").when(source).get(eq("key")); + doReturn(0).when(source).getOrdinal(); + doReturn("source").when(source).getName(); + + ConfigurationContext context = Mockito.mock(ConfigurationContext.class, NOT_MOCKED_ANSWER); + + doReturn(singletonList(source)).when(context).getPropertySources(); + doReturn(filters).when(context).getPropertyFilters(); + doReturn(DEFAULT_OVERRIDING_COLLECTOR).when(context).getPropertyValueCombinationPolicy(); + + DefaultConfiguration configuration = new DefaultConfiguration(context); + + String value = configuration.get("key"); + + assertThat(value, equalTo("$BVA$")); + + verify(source, atLeastOnce()).get(eq("key")); + verifyNoMoreInteractions(source); + } + + @Test + public void filteringOfGivenValueWorksWithOneFilterWithPriority() { + PropertySource source = Mockito.mock(PropertySource.class, NOT_MOCKED_ANSWER); + + doReturn("V").when(source).get(eq("key")); + doReturn(0).when(source).getOrdinal(); + doReturn("source").when(source).getName(); + + ConfigurationContext context = Mockito.mock(ConfigurationContext.class, NOT_MOCKED_ANSWER); + + doReturn(singletonList(source)).when(context).getPropertySources(); + doReturn(singletonList(FILTER_PREPENDING_C)).when(context).getPropertyFilters(); + doReturn(DEFAULT_OVERRIDING_COLLECTOR).when(context).getPropertyValueCombinationPolicy(); + + DefaultConfiguration configuration = new DefaultConfiguration(context); + + String value = configuration.get("key"); + + verify(source, atLeastOnce()).get(eq("key")); + verifyNoMoreInteractions(source); + + assertThat(value, equalTo("$CV")); + } + + @Test + public void filteringOfGivenValueWorksWithTwoFiltersWithDifferentPriorities() { + assertThat(FILTER_PREPENDING_C.getClass().isAnnotationPresent(Priority.class), is(true)); + assertThat(FILTER_PREPENDING_D.getClass().isAnnotationPresent(Priority.class), is(true)); + + List<PropertyFilter> filters = new ArrayList<>(); + + filters.add(FILTER_PREPENDING_C); + filters.add(FILTER_PREPENDING_D); + + PropertySource source = Mockito.mock(PropertySource.class, NOT_MOCKED_ANSWER); + + doReturn("V").when(source).get(eq("key")); + doReturn(0).when(source).getOrdinal(); + doReturn("source").when(source).getName(); + + ConfigurationContext context = Mockito.mock(ConfigurationContext.class, NOT_MOCKED_ANSWER); + + doReturn(singletonList(source)).when(context).getPropertySources(); + doReturn(filters).when(context).getPropertyFilters(); + doReturn(DEFAULT_OVERRIDING_COLLECTOR).when(context).getPropertyValueCombinationPolicy(); + + DefaultConfiguration configuration = new DefaultConfiguration(context); + + String value = configuration.get("key"); + + verify(source, atLeastOnce()).get(eq("key")); + verifyNoMoreInteractions(source); + + assertThat(value, equalTo("$D$CV")); + } + + private static class FilterAppendingA implements PropertyFilter { + @Override + public String filterProperty(String key, String value) { + return value.endsWith("A$") + ? value + : value + "A$"; + } + } + + private static class FilterPrependingB implements PropertyFilter { + @Override + public String filterProperty(String key, String value) { + return value.startsWith("$B") + ? value + : "$B" + value; + } + } + + @Priority(987) + private static class FilterPrependingC implements PropertyFilter { + @Override + public String filterProperty(String key, String value) { + return value.contains("$C") + ? value + : "$C" + value; + } + } + + @Priority(111) + private static class FilterPrependingD implements PropertyFilter { + @Override + public String filterProperty(String key, String value) { + return value.contains("$D") + ? value + : "$D" + value; + } + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/98432a50/modules/builder/src/test/java/org/apache/tamaya/builder/util/mockito/NotMockedAnswer.java ---------------------------------------------------------------------- diff --git a/modules/builder/src/test/java/org/apache/tamaya/builder/util/mockito/NotMockedAnswer.java b/modules/builder/src/test/java/org/apache/tamaya/builder/util/mockito/NotMockedAnswer.java index d0ec79b..10b3734 100644 --- a/modules/builder/src/test/java/org/apache/tamaya/builder/util/mockito/NotMockedAnswer.java +++ b/modules/builder/src/test/java/org/apache/tamaya/builder/util/mockito/NotMockedAnswer.java @@ -24,6 +24,7 @@ import org.mockito.stubbing.Answer; import java.io.Serializable; +// @todo This is a duplicated class public class NotMockedAnswer implements Answer<Object>, Serializable { public final static NotMockedAnswer NOT_MOCKED_ANSWER = new NotMockedAnswer(); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/98432a50/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 60da8fe..037a9a0 100644 --- a/pom.xml +++ b/pom.xml @@ -244,6 +244,7 @@ under the License. <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>${mockito.version}</version> + <scope>test</scope> </dependency> <dependency>
