Repository: incubator-tamaya-extensions Updated Branches: refs/heads/master 26381baac -> 0a4fdbb54
TAMAYA-182: Added additional constructors for better support for programmtic filter creation using the builder API. Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/commit/0a4fdbb5 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/0a4fdbb5 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/0a4fdbb5 Branch: refs/heads/master Commit: 0a4fdbb5432c3a428fc0e6b8ecc6ec4c29d5fae4 Parents: 26381ba Author: anatole <[email protected]> Authored: Wed Nov 9 01:39:29 2016 +0100 Committer: anatole <[email protected]> Committed: Wed Nov 9 01:39:29 2016 +0100 ---------------------------------------------------------------------- modules/filter/pom.xml | 6 + .../tamaya/filter/ConfigurationFilter.java | 36 +++--- .../org/apache/tamaya/filter/FilterContext.java | 118 +++++++++++++++++++ .../tamaya/filter/ProgrammableFilter.java | 118 ------------------- .../tamaya/filter/RegexPropertyFilter.java | 56 --------- .../tamaya/filter/ConfigurationFilterTest.java | 30 ++--- .../tamaya/filter/ProgrammableFilterTest.java | 100 ++++++++-------- .../tamaya/filter/RegexPropertyFilterTest.java | 55 --------- .../tamaya/spisupport/RegexPropertyFilter.java | 82 +++++++++++++ .../spisupport/RegexPropertyFilterTest.java | 57 +++++++++ 10 files changed, 350 insertions(+), 308 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/0a4fdbb5/modules/filter/pom.xml ---------------------------------------------------------------------- diff --git a/modules/filter/pom.xml b/modules/filter/pom.xml index a0f9584..5355a6c 100644 --- a/modules/filter/pom.xml +++ b/modules/filter/pom.xml @@ -57,6 +57,12 @@ under the License. <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> + <dependency> + <groupId>org.apache.tamaya.ext</groupId> + <artifactId>tamaya-spisupport</artifactId> + <version>0.3-incubating-SNAPSHOT</version> + <scope>test</scope> + </dependency> </dependencies> <build> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/0a4fdbb5/modules/filter/src/main/java/org/apache/tamaya/filter/ConfigurationFilter.java ---------------------------------------------------------------------- diff --git a/modules/filter/src/main/java/org/apache/tamaya/filter/ConfigurationFilter.java b/modules/filter/src/main/java/org/apache/tamaya/filter/ConfigurationFilter.java index c2739a1..4848bdc 100644 --- a/modules/filter/src/main/java/org/apache/tamaya/filter/ConfigurationFilter.java +++ b/modules/filter/src/main/java/org/apache/tamaya/filter/ConfigurationFilter.java @@ -18,7 +18,6 @@ */ package org.apache.tamaya.filter; -import org.apache.tamaya.spi.FilterContext; import org.apache.tamaya.spi.PropertyFilter; @@ -47,17 +46,17 @@ public final class ConfigurationFilter implements PropertyFilter{ } }; - private static final ThreadLocal<ProgrammableFilter> THREADED_MAP_FILTERS = new ThreadLocal<ProgrammableFilter>(){ + private static final ThreadLocal<FilterContext> THREADED_MAP_FILTERS = new ThreadLocal<FilterContext>(){ @Override - protected ProgrammableFilter initialValue() { - return new ProgrammableFilter(); + protected FilterContext initialValue() { + return new FilterContext(); } }; - private static final ThreadLocal<ProgrammableFilter> THREADED_SINGLE_FILTERS = new ThreadLocal<ProgrammableFilter>(){ + private static final ThreadLocal<FilterContext> THREADED_VALUE_FILTERS = new ThreadLocal<FilterContext>(){ @Override - protected ProgrammableFilter initialValue() { - return new ProgrammableFilter(); + protected FilterContext initialValue() { + return new FilterContext(); } }; @@ -72,7 +71,7 @@ public final class ConfigurationFilter implements PropertyFilter{ /** * Seactivates metadata filtering also on global map access for this thread. - * @see #clearFilters() + * @see #cleanupFilterContext() * @param filtered true,to enable metadata filtering (default). */ public static void setMetadataFiltered(boolean filtered){ @@ -80,35 +79,38 @@ public final class ConfigurationFilter implements PropertyFilter{ } /** - * Access the filtering configuration that is used for filtering single property values accessed. + * Access the filtering configuration that is used on the current thread for + * filtering single property values accessed. + * * @return the filtering config, never null. */ - public static ProgrammableFilter getSingleFilters(){ - return THREADED_SINGLE_FILTERS.get(); + public static FilterContext getSingleValueFilterContext(){ + return THREADED_VALUE_FILTERS.get(); } /** - * Access the filtering configuration that is used for filtering configuration properties accessed as full + * Access the filtering configuration that is used used on the current thread + * for filtering configuration properties accessed as full * map. * @return the filtering config, never null. */ - public static ProgrammableFilter getMapFilters(){ + public static FilterContext getMapFilterContext(){ return THREADED_MAP_FILTERS.get(); } /** * Removes all programmable filters active on the current thread. */ - public static void clearFilters(){ + public static void cleanupFilterContext(){ THREADED_MAP_FILTERS.get().clearFilters(); - THREADED_SINGLE_FILTERS.get().clearFilters(); + THREADED_VALUE_FILTERS.get().clearFilters(); THREADED_METADATA_FILTERED.set(true); } @Override - public String filterProperty(String valueToBeFiltered, FilterContext context) { + public String filterProperty(String valueToBeFiltered, org.apache.tamaya.spi.FilterContext context) { if(context.isSinglePropertyScoped()){ - for(PropertyFilter pred: THREADED_SINGLE_FILTERS.get().getFilters()){ + for(PropertyFilter pred: THREADED_VALUE_FILTERS.get().getFilters()){ valueToBeFiltered = pred.filterProperty(valueToBeFiltered, context); } }else{ http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/0a4fdbb5/modules/filter/src/main/java/org/apache/tamaya/filter/FilterContext.java ---------------------------------------------------------------------- diff --git a/modules/filter/src/main/java/org/apache/tamaya/filter/FilterContext.java b/modules/filter/src/main/java/org/apache/tamaya/filter/FilterContext.java new file mode 100644 index 0000000..596c67f --- /dev/null +++ b/modules/filter/src/main/java/org/apache/tamaya/filter/FilterContext.java @@ -0,0 +1,118 @@ +/* + * 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.filter; + +import org.apache.tamaya.spi.PropertyFilter; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +/** + * A set of property filter and accessor methods. This class is built for + * usage within a single threaded context, so it is NOT thread-safe. + */ +public final class FilterContext implements PropertyFilter{ + /** The filters. */ + private List<PropertyFilter> filters = new ArrayList<>(); + + /** + * Add a filter. + * @param filter the filter. + */ + public void addFilter(PropertyFilter filter){ + filters.add(filter); + } + + /** + * Adds a filter at given position. + * @param pos the position. + * @param filter the filter. + */ + public void addFilter(int pos, PropertyFilter filter){ + filters.add(pos, filter); + } + + /** + * Removes a filter at a given position. + * @param pos the position. + * @return the filter removed, or null. + */ + public PropertyFilter removeFilter(int pos){ + return filters.remove(pos); + } + + /** + * Removes a filter. + * @param filter the filter to be removed, not null. + */ + public void removeFilter(PropertyFilter filter) { + filters.remove(filter); + } + + /** + * Clears all filters. + */ + public void clearFilters(){ + filters.clear(); + } + + /** + * Set the filters. + * @param filters the filters to be applied. + */ + public void setFilters(PropertyFilter... filters){ + setFilters(Arrays.asList(filters)); + } + + /** + * Set the filters. + * @param filters the filters to be applied. + */ + public void setFilters(Collection<PropertyFilter> filters) { + this.filters.clear(); + this.filters.addAll(filters); + } + + /** + * Get all filters. + * @return all filters. + */ + public List<PropertyFilter> getFilters(){ + return Collections.unmodifiableList(filters); + } + + @Override + public String filterProperty(String valueToBeFiltered, org.apache.tamaya.spi.FilterContext context) { + for(PropertyFilter filter:filters){ + valueToBeFiltered = filter.filterProperty(valueToBeFiltered, context); + } + return valueToBeFiltered; + } + + @Override + public String toString() { + return "ProgrammableFilter{" + + "filters=" + filters + + '}'; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/0a4fdbb5/modules/filter/src/main/java/org/apache/tamaya/filter/ProgrammableFilter.java ---------------------------------------------------------------------- diff --git a/modules/filter/src/main/java/org/apache/tamaya/filter/ProgrammableFilter.java b/modules/filter/src/main/java/org/apache/tamaya/filter/ProgrammableFilter.java deleted file mode 100644 index ec6040c..0000000 --- a/modules/filter/src/main/java/org/apache/tamaya/filter/ProgrammableFilter.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * 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.filter; - -import org.apache.tamaya.spi.FilterContext; -import org.apache.tamaya.spi.PropertyFilter; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -/** - * A set of property filter and accessor methods. - */ -public final class ProgrammableFilter implements PropertyFilter{ - /** The filters. */ - private List<PropertyFilter> filters = new ArrayList<>(); - - /** - * Add a filter. - * @param filter the filter. - */ - public void addFilter(PropertyFilter filter){ - filters.add(filter); - } - - /** - * Adds a filter at given position. - * @param pos the position. - * @param filter the filter. - */ - public void addFilter(int pos, PropertyFilter filter){ - filters.add(pos, filter); - } - - /** - * Removes a filter at a given position. - * @param pos the position. - * @return the filter removed, or null. - */ - public PropertyFilter removeFilter(int pos){ - return filters.remove(pos); - } - - /** - * Removes a filter. - * @param filter the filter to be removed, not null. - */ - public void removeFilter(PropertyFilter filter) { - filters.remove(filter); - } - - /** - * Clears all filters. - */ - public void clearFilters(){ - filters.clear(); - } - - /** - * Set the filters. - * @param filters the filters to be applied. - */ - public void setFilters(PropertyFilter... filters){ - setFilters(Arrays.asList(filters)); - } - - /** - * Set the filters. - * @param filters the filters to be applied. - */ - public void setFilters(Collection<PropertyFilter> filters) { - this.filters.clear(); - this.filters.addAll(filters); - } - - /** - * Get all filters. - * @return all filters. - */ - public List<PropertyFilter> getFilters(){ - return Collections.unmodifiableList(filters); - } - - @Override - public String filterProperty(String valueToBeFiltered, FilterContext context) { - for(PropertyFilter filter:filters){ - valueToBeFiltered = filter.filterProperty(valueToBeFiltered, context); - } - return valueToBeFiltered; - } - - @Override - public String toString() { - return "ProgrammableFilter{" + - "filters=" + filters + - '}'; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/0a4fdbb5/modules/filter/src/main/java/org/apache/tamaya/filter/RegexPropertyFilter.java ---------------------------------------------------------------------- diff --git a/modules/filter/src/main/java/org/apache/tamaya/filter/RegexPropertyFilter.java b/modules/filter/src/main/java/org/apache/tamaya/filter/RegexPropertyFilter.java deleted file mode 100644 index 1ed43ba..0000000 --- a/modules/filter/src/main/java/org/apache/tamaya/filter/RegexPropertyFilter.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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.filter; - -import org.apache.tamaya.spi.FilterContext; -import org.apache.tamaya.spi.PropertyFilter; - -import java.util.Objects; - -/** - * Predicate filtering using a regex expression operating on the key. - */ -public final class RegexPropertyFilter implements PropertyFilter{ - /** The expression used to filter. */ - private String expression; - - /** - * Creates a new regex filter expression. - * @param expression the reged expression, not null. - */ - public RegexPropertyFilter(String expression){ - this.expression = Objects.requireNonNull(expression); - } - - @Override - public String filterProperty(String valueToBeFiltered, FilterContext context) { - if(context.getKey().matches(expression)){ - return null; - } - return valueToBeFiltered; - } - - @Override - public String toString() { - return "RegexPropertyFilter{" + - "expression='" + expression + '\'' + - '}'; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/0a4fdbb5/modules/filter/src/test/java/org/apache/tamaya/filter/ConfigurationFilterTest.java ---------------------------------------------------------------------- diff --git a/modules/filter/src/test/java/org/apache/tamaya/filter/ConfigurationFilterTest.java b/modules/filter/src/test/java/org/apache/tamaya/filter/ConfigurationFilterTest.java index 9d0da1f..73602c8 100644 --- a/modules/filter/src/test/java/org/apache/tamaya/filter/ConfigurationFilterTest.java +++ b/modules/filter/src/test/java/org/apache/tamaya/filter/ConfigurationFilterTest.java @@ -42,80 +42,80 @@ public class ConfigurationFilterTest { @Test public void testGetSingleFilters() throws Exception { Configuration config = ConfigurationProvider.getConfiguration(); - assertNotNull(ConfigurationFilter.getSingleFilters()); + assertNotNull(ConfigurationFilter.getSingleValueFilterContext()); PropertyFilter testFilter = new PropertyFilter() { @Override public String filterProperty(String value, FilterContext context) { return context.getKey() + ":testGetSingleFilters"; } }; - ConfigurationFilter.getSingleFilters().addFilter(testFilter); + ConfigurationFilter.getSingleValueFilterContext().addFilter(testFilter); assertEquals("user.home:testGetSingleFilters", config.get("user.home")); - ConfigurationFilter.getSingleFilters().removeFilter(testFilter); + ConfigurationFilter.getSingleValueFilterContext().removeFilter(testFilter); assertNotSame("user.home:testGetSingleFilters", config.get("user.home")); } @Test public void testRemoveSingleFiltersAt0() throws Exception { Configuration config = ConfigurationProvider.getConfiguration(); - assertNotNull(ConfigurationFilter.getSingleFilters()); + assertNotNull(ConfigurationFilter.getSingleValueFilterContext()); PropertyFilter testFilter = new PropertyFilter() { @Override public String filterProperty(String value, FilterContext context) { return context.getKey() + ":testGetSingleFilters"; } }; - ConfigurationFilter.getSingleFilters().addFilter(testFilter); + ConfigurationFilter.getSingleValueFilterContext().addFilter(testFilter); assertEquals("user.home:testGetSingleFilters", config.get("user.home")); - ConfigurationFilter.getSingleFilters().removeFilter(0); + ConfigurationFilter.getSingleValueFilterContext().removeFilter(0); assertNotSame("user.home:testGetSingleFilters", config.get("user.home")); } @Test public void testGetMapFilters() throws Exception { Configuration config = ConfigurationProvider.getConfiguration(); - assertNotNull(ConfigurationFilter.getMapFilters()); + assertNotNull(ConfigurationFilter.getMapFilterContext()); PropertyFilter testFilter = new PropertyFilter() { @Override public String filterProperty(String value, FilterContext context) { return context.getKey() + ":testGetMapFilters"; } }; - ConfigurationFilter.getMapFilters().addFilter(testFilter); + ConfigurationFilter.getMapFilterContext().addFilter(testFilter); assertEquals("user.home:testGetMapFilters", config.getProperties().get("user.home")); - ConfigurationFilter.getSingleFilters().removeFilter(testFilter); + ConfigurationFilter.getSingleValueFilterContext().removeFilter(testFilter); assertNotSame("user.home:testGetSingleFilters", config.getProperties().get("user.home")); } @Test public void testRemoveMapFilterAt0() throws Exception { Configuration config = ConfigurationProvider.getConfiguration(); - assertNotNull(ConfigurationFilter.getMapFilters()); + assertNotNull(ConfigurationFilter.getMapFilterContext()); PropertyFilter testFilter = new PropertyFilter() { @Override public String filterProperty(String value, FilterContext context) { return context.getKey() + ":testGetMapFilters"; } }; - ConfigurationFilter.getMapFilters().addFilter(testFilter); + ConfigurationFilter.getMapFilterContext().addFilter(testFilter); assertEquals("user.home:testGetMapFilters", config.getProperties().get("user.home")); - ConfigurationFilter.getMapFilters().removeFilter(0); + ConfigurationFilter.getMapFilterContext().removeFilter(0); assertNotSame("user.home:testGetSingleFilters", config.getProperties().get("user.home")); } @Test public void testClearFilters() throws Exception { Configuration config = ConfigurationProvider.getConfiguration(); - assertNotNull(ConfigurationFilter.getSingleFilters()); + assertNotNull(ConfigurationFilter.getSingleValueFilterContext()); PropertyFilter testFilter = new PropertyFilter() { @Override public String filterProperty(String value, FilterContext context) { return context.getKey() + ":testGetSingleFilters"; } }; - ConfigurationFilter.getSingleFilters().addFilter(testFilter); + ConfigurationFilter.getSingleValueFilterContext().addFilter(testFilter); assertEquals("user.home:testGetSingleFilters", config.get("user.home")); - ConfigurationFilter.clearFilters(); + ConfigurationFilter.cleanupFilterContext(); assertNotSame("user.home:testGetSingleFilters", config.get("user.home")); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/0a4fdbb5/modules/filter/src/test/java/org/apache/tamaya/filter/ProgrammableFilterTest.java ---------------------------------------------------------------------- diff --git a/modules/filter/src/test/java/org/apache/tamaya/filter/ProgrammableFilterTest.java b/modules/filter/src/test/java/org/apache/tamaya/filter/ProgrammableFilterTest.java index 73db677..3bbd482 100644 --- a/modules/filter/src/test/java/org/apache/tamaya/filter/ProgrammableFilterTest.java +++ b/modules/filter/src/test/java/org/apache/tamaya/filter/ProgrammableFilterTest.java @@ -18,8 +18,8 @@ */ package org.apache.tamaya.filter; -import org.apache.tamaya.spi.FilterContext; import org.apache.tamaya.spi.PropertyFilter; +import org.apache.tamaya.spisupport.RegexPropertyFilter; import org.junit.Test; import java.util.Arrays; @@ -29,96 +29,101 @@ import java.util.Map; import static org.junit.Assert.*; /** - * Tests for {@link ProgrammableFilter}. Created by atsticks on 11.02.16. + * Tests for {@link FilterContext}. Created by atsticks on 11.02.16. */ public class ProgrammableFilterTest { @Test public void testAddRemoveFilter() throws Exception { - ProgrammableFilter filter = new ProgrammableFilter(); - RegexPropertyFilter regexFilter = new RegexPropertyFilter("test\\..*"); + FilterContext filter = new FilterContext(); + RegexPropertyFilter regexFilter = new RegexPropertyFilter(); + regexFilter.setIncludes("test\\..*"); Map<String,String> map = new HashMap<>(); map.put("test1", "test1"); map.put("test2", "test2"); map.put("test.test3", "test.test3"); - assertEquals(filter.filterProperty("test1", new FilterContext("test1", map, true)), "test1"); - assertEquals(filter.filterProperty("test2", new FilterContext("test2", map, true)), "test2"); - assertEquals(filter.filterProperty("test.test3", new FilterContext("test.test3", map, true)), "test.test3"); + assertEquals(filter.filterProperty("test1", new org.apache.tamaya.spi.FilterContext("test1", map, true)), "test1"); + assertEquals(filter.filterProperty("test2", new org.apache.tamaya.spi.FilterContext("test2", map, true)), "test2"); + assertEquals(filter.filterProperty("test.test3", new org.apache.tamaya.spi.FilterContext("test.test3", map, true)), "test.test3"); filter.addFilter(regexFilter); - assertEquals(filter.filterProperty("test1", new FilterContext("test1", map, true)), "test1"); - assertEquals(filter.filterProperty("test2", new FilterContext("test2", map, true)), "test2"); - assertNull(filter.filterProperty("test.test3", new FilterContext("test.test3", map, true))); + assertEquals(filter.filterProperty("test1", new org.apache.tamaya.spi.FilterContext("test1", map, true)), "test1"); + assertEquals(filter.filterProperty("test2", new org.apache.tamaya.spi.FilterContext("test2", map, true)), "test2"); + assertNull(filter.filterProperty("test.test3", new org.apache.tamaya.spi.FilterContext("test.test3", map, true))); filter.removeFilter(0); - assertEquals(filter.filterProperty("test1", new FilterContext("test1", map, true)), "test1"); - assertEquals(filter.filterProperty("test2", new FilterContext("test2", map, true)), "test2"); - assertEquals(filter.filterProperty("test.test3", new FilterContext("test.test3", map, true)), "test.test3"); + assertEquals(filter.filterProperty("test1", new org.apache.tamaya.spi.FilterContext("test1", map, true)), "test1"); + assertEquals(filter.filterProperty("test2", new org.apache.tamaya.spi.FilterContext("test2", map, true)), "test2"); + assertEquals(filter.filterProperty("test.test3", new org.apache.tamaya.spi.FilterContext("test.test3", map, true)), "test.test3"); filter.addFilter(0, regexFilter); - assertEquals(filter.filterProperty("test1", new FilterContext("test1", map, true)), "test1"); - assertEquals(filter.filterProperty("test2", new FilterContext("test2", map, true)), "test2"); - assertNull(filter.filterProperty("test.test3", new FilterContext("test.test3", map, true))); + assertEquals(filter.filterProperty("test1", new org.apache.tamaya.spi.FilterContext("test1", map, true)), "test1"); + assertEquals(filter.filterProperty("test2", new org.apache.tamaya.spi.FilterContext("test2", map, true)), "test2"); + assertNull(filter.filterProperty("test.test3", new org.apache.tamaya.spi.FilterContext("test.test3", map, true))); } @Test public void testClearFilters() throws Exception { - ProgrammableFilter filter = new ProgrammableFilter(); - RegexPropertyFilter regexFilter = new RegexPropertyFilter("test\\..*"); + FilterContext filter = new FilterContext(); + RegexPropertyFilter regexFilter = new RegexPropertyFilter(); + regexFilter.setIncludes("test\\..*"); Map<String,String> map = new HashMap<>(); map.put("test1", "test1"); map.put("test2", "test2"); map.put("test.test3", "test.test3"); - assertEquals(filter.filterProperty("test1", new FilterContext("test1", map, true)), "test1"); - assertEquals(filter.filterProperty("test2", new FilterContext("test2", map, true)), "test2"); - assertEquals(filter.filterProperty("test.test3", new FilterContext("test.test3", map, true)), "test.test3"); + assertEquals(filter.filterProperty("test1", new org.apache.tamaya.spi.FilterContext("test1", map, true)), "test1"); + assertEquals(filter.filterProperty("test2", new org.apache.tamaya.spi.FilterContext("test2", map, true)), "test2"); + assertEquals(filter.filterProperty("test.test3", new org.apache.tamaya.spi.FilterContext("test.test3", map, true)), "test.test3"); filter.addFilter(regexFilter); - assertEquals(filter.filterProperty("test1", new FilterContext("test1", map, true)), "test1"); - assertEquals(filter.filterProperty("test2", new FilterContext("test2", map, true)), "test2"); - assertNull(filter.filterProperty("test.test3", new FilterContext("test.test3", map, true))); + assertEquals(filter.filterProperty("test1", new org.apache.tamaya.spi.FilterContext("test1", map, true)), "test1"); + assertEquals(filter.filterProperty("test2", new org.apache.tamaya.spi.FilterContext("test2", map, true)), "test2"); + assertNull(filter.filterProperty("test.test3", new org.apache.tamaya.spi.FilterContext("test.test3", map, true))); filter.clearFilters(); - assertEquals(filter.filterProperty("test1", new FilterContext("test1", map, true)), "test1"); - assertEquals(filter.filterProperty("test2", new FilterContext("test2", map, true)), "test2"); - assertEquals(filter.filterProperty("test.test3", new FilterContext("test.test3", map, true)), "test.test3"); + assertEquals(filter.filterProperty("test1", new org.apache.tamaya.spi.FilterContext("test1", map, true)), "test1"); + assertEquals(filter.filterProperty("test2", new org.apache.tamaya.spi.FilterContext("test2", map, true)), "test2"); + assertEquals(filter.filterProperty("test.test3", new org.apache.tamaya.spi.FilterContext("test.test3", map, true)), "test.test3"); } @Test public void testSetFilters() throws Exception { - ProgrammableFilter filter = new ProgrammableFilter(); - RegexPropertyFilter regexFilter = new RegexPropertyFilter("test\\..*"); + FilterContext filter = new FilterContext(); + RegexPropertyFilter regexFilter = new RegexPropertyFilter(); + regexFilter.setIncludes("test\\..*"); Map<String,String> map = new HashMap<>(); map.put("test1", "test1"); map.put("test2", "test2"); map.put("test.test3", "test.test3"); - assertEquals(filter.filterProperty("test1", new FilterContext("test1", map, true)), "test1"); - assertEquals(filter.filterProperty("test2", new FilterContext("test2", map, true)), "test2"); - assertEquals(filter.filterProperty("test.test3", new FilterContext("test.test3", map, true)), "test.test3"); + assertEquals(filter.filterProperty("test1", new org.apache.tamaya.spi.FilterContext("test1", map, true)), "test1"); + assertEquals(filter.filterProperty("test2", new org.apache.tamaya.spi.FilterContext("test2", map, true)), "test2"); + assertEquals(filter.filterProperty("test.test3", new org.apache.tamaya.spi.FilterContext("test.test3", map, true)), "test.test3"); filter.setFilters(regexFilter); - assertEquals(filter.filterProperty("test1", new FilterContext("test1", map, true)), "test1"); - assertEquals(filter.filterProperty("test2", new FilterContext("test2", map, true)), "test2"); - assertNull(filter.filterProperty("test.test3", new FilterContext("test.test3", map, true))); + assertEquals(filter.filterProperty("test1", new org.apache.tamaya.spi.FilterContext("test1", map, true)), "test1"); + assertEquals(filter.filterProperty("test2", new org.apache.tamaya.spi.FilterContext("test2", map, true)), "test2"); + assertNull(filter.filterProperty("test.test3", new org.apache.tamaya.spi.FilterContext("test.test3", map, true))); } @Test public void testSetFilters1() throws Exception { - ProgrammableFilter filter = new ProgrammableFilter(); - RegexPropertyFilter regexFilter = new RegexPropertyFilter("test\\..*"); + FilterContext filter = new FilterContext(); + RegexPropertyFilter regexFilter = new RegexPropertyFilter(); + regexFilter.setIncludes("test\\..*"); Map<String,String> map = new HashMap<>(); map.put("test1", "test1"); map.put("test2", "test2"); map.put("test.test3", "test.test3"); - assertEquals(filter.filterProperty("test1", new FilterContext("test1", map, true)), "test1"); - assertEquals(filter.filterProperty("test2", new FilterContext("test2", map, true)), "test2"); - assertEquals(filter.filterProperty("test.test3", new FilterContext("test.test3", map, true)), "test.test3"); + assertEquals(filter.filterProperty("test1", new org.apache.tamaya.spi.FilterContext("test1", map, true)), "test1"); + assertEquals(filter.filterProperty("test2", new org.apache.tamaya.spi.FilterContext("test2", map, true)), "test2"); + assertEquals(filter.filterProperty("test.test3", new org.apache.tamaya.spi.FilterContext("test.test3", map, true)), "test.test3"); filter.setFilters(Arrays.asList(new PropertyFilter[]{regexFilter})); - assertEquals(filter.filterProperty("test1", new FilterContext("test1", map, true)), "test1"); - assertEquals(filter.filterProperty("test2", new FilterContext("test2", map, true)), "test2"); - assertNull(filter.filterProperty("test.test3", new FilterContext("test.test3", map, true))); + assertEquals(filter.filterProperty("test1", new org.apache.tamaya.spi.FilterContext("test1", map, true)), "test1"); + assertEquals(filter.filterProperty("test2", new org.apache.tamaya.spi.FilterContext("test2", map, true)), "test2"); + assertNull(filter.filterProperty("test.test3", new org.apache.tamaya.spi.FilterContext("test.test3", map, true))); } @Test public void testGetFilters() throws Exception { - ProgrammableFilter filter = new ProgrammableFilter(); + FilterContext filter = new FilterContext(); assertNotNull(filter.getFilters()); assertTrue(filter.getFilters().isEmpty()); - RegexPropertyFilter regexFilter = new RegexPropertyFilter("test\\..*"); + RegexPropertyFilter regexFilter = new RegexPropertyFilter(); + regexFilter.setIncludes("test\\..*"); filter.addFilter(regexFilter); assertNotNull(filter.getFilters()); assertFalse(filter.getFilters().isEmpty()); @@ -128,11 +133,12 @@ public class ProgrammableFilterTest { @Test public void testToString() throws Exception { - ProgrammableFilter filter = new ProgrammableFilter(); + FilterContext filter = new FilterContext(); assertFalse(filter.toString().contains("test\\..*")); assertTrue(filter.toString().contains("ProgrammableFilter")); assertFalse(filter.toString().contains("RegexPropertyFilter")); - RegexPropertyFilter regexFilter = new RegexPropertyFilter("test\\..*"); + RegexPropertyFilter regexFilter = new RegexPropertyFilter(); + regexFilter.setIncludes("test\\..*"); filter.addFilter(regexFilter); assertTrue(filter.toString().contains("test\\..*")); assertTrue(filter.toString().contains("ProgrammableFilter")); http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/0a4fdbb5/modules/filter/src/test/java/org/apache/tamaya/filter/RegexPropertyFilterTest.java ---------------------------------------------------------------------- diff --git a/modules/filter/src/test/java/org/apache/tamaya/filter/RegexPropertyFilterTest.java b/modules/filter/src/test/java/org/apache/tamaya/filter/RegexPropertyFilterTest.java deleted file mode 100644 index a560b04..0000000 --- a/modules/filter/src/test/java/org/apache/tamaya/filter/RegexPropertyFilterTest.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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.filter; - -import org.apache.tamaya.spi.FilterContext; - -import java.util.HashMap; -import java.util.Map; - -import static org.junit.Assert.*; - -/** - * Tests for {@link RegexPropertyFilter}. Created by anatole on 11.02.16. - */ -public class RegexPropertyFilterTest { - - @org.junit.Test - public void testFilterProperty() throws Exception { - RegexPropertyFilter filter = new RegexPropertyFilter("test\\..*"); - Map<String,String> map = new HashMap<>(); - map.put("test1", "test1"); - map.put("test2", "test2"); - map.put("test.test3", "test.test3"); - assertEquals(filter.filterProperty("test1", new FilterContext("test1", map, true)), "test1"); - assertEquals(filter.filterProperty("test2", new FilterContext("test2", map, true)), "test2"); - assertNull(filter.filterProperty("test.test3", new FilterContext("test.test3", map, true))); - filter = new RegexPropertyFilter(".*"); - assertNull(filter.filterProperty("test1", new FilterContext("test1", map, true))); - assertNull(filter.filterProperty("test2", new FilterContext("test2", map, true))); - assertNull(filter.filterProperty("test.test3", new FilterContext("test.test3", map, true))); - } - - @org.junit.Test - public void testToString() throws Exception { - RegexPropertyFilter filter = new RegexPropertyFilter("test\\..*"); - assertTrue(filter.toString().contains("test\\..*")); - assertTrue(filter.toString().contains("RegexPropertyFilter")); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/0a4fdbb5/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/RegexPropertyFilter.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/RegexPropertyFilter.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/RegexPropertyFilter.java new file mode 100644 index 0000000..85580a7 --- /dev/null +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/RegexPropertyFilter.java @@ -0,0 +1,82 @@ +/* + * 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.spi.FilterContext; +import org.apache.tamaya.spi.PropertyFilter; + +import java.util.Arrays; +import java.util.List; + +/** + * Predicate filtering using a regex expression operating on the key. It allows either + * to define the target keys to be selected (includes), or to be excluded (excludes). + */ +public final class RegexPropertyFilter implements PropertyFilter{ + /** The expression used to include entries that match. */ + private List<String> includes; + /** The expression used to exclude entries that match. */ + private List<String> excludes; + + /** + * Sets the regex expression to be applied on the key to filter the corresponding entry + * if matching. + * @param expressions the regular expression for inclusion, not null. + */ + public void setIncludes(String... expressions){ + this.includes= Arrays.asList(expressions); + } + + /** + * Sets the regex expression to be applied on the key to remove the corresponding entries + * if matching. + * @param expressions the regular expressions for exclusion, not null. + */ + public void setExcludes(String... expressions){ + this.excludes= Arrays.asList(expressions); + } + + @Override + public String filterProperty(String valueToBeFiltered, FilterContext context) { + if(includes!=null){ + for(String expression:includes){ + if(!context.getKey().matches(expression)){ + return null; + } + } + } + if(excludes!=null){ + for(String expression:excludes){ + if(context.getKey().matches(expression)){ + return null; + } + } + } + return valueToBeFiltered; + } + + @Override + public String toString() { + return "RegexPropertyFilter{" + + "includes='" + includes + '\'' + + "excludes='" + excludes + '\'' + + '}'; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/0a4fdbb5/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/RegexPropertyFilterTest.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/RegexPropertyFilterTest.java b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/RegexPropertyFilterTest.java new file mode 100644 index 0000000..789edd0 --- /dev/null +++ b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/RegexPropertyFilterTest.java @@ -0,0 +1,57 @@ +/* + * 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.spi.FilterContext; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.*; + +/** + * Tests for {@link RegexPropertyFilter}. Created by anatole on 11.02.16. + */ +public class RegexPropertyFilterTest { + + @org.junit.Test + public void testFilterProperty() throws Exception { + RegexPropertyFilter filter = new RegexPropertyFilter(); + filter.setIncludes("test\\..*"); + Map<String,String> map = new HashMap<>(); + map.put("test1", "test1"); + map.put("test2", "test2"); + map.put("test.test3", "test.test3"); + assertEquals(filter.filterProperty("test1", new FilterContext("test1", map, true)), "test1"); + assertEquals(filter.filterProperty("test2", new FilterContext("test2", map, true)), "test2"); + assertNull(filter.filterProperty("test.test3", new FilterContext("test.test3", map, true))); + filter = new RegexPropertyFilter(); + assertNull(filter.filterProperty("test1", new FilterContext("test1", map, true))); + assertNull(filter.filterProperty("test2", new FilterContext("test2", map, true))); + assertNull(filter.filterProperty("test.test3", new FilterContext("test.test3", map, true))); + } + + @org.junit.Test + public void testToString() throws Exception { + RegexPropertyFilter filter = new RegexPropertyFilter(); + filter.setIncludes("test\\..*"); + assertTrue(filter.toString().contains("test\\..*")); + assertTrue(filter.toString().contains("RegexPropertyFilter")); + } +} \ No newline at end of file
