[java-support] branch master updated: Add support classes for Predicate use within Resolvers.

Brent Putman putmanb at georgetown.edu
Sun Jun 5 17:02:14 EDT 2016


This is an automated email from the git hooks/post-receive script.

putmanb pushed a commit to branch master
in repository java-support.

The following commit(s) were added to refs/heads/master by this push:
       new  7d58b9c   Add support classes for Predicate use within Resolvers.
7d58b9c is described below

commit 7d58b9c5231221cb886ac97b16aaeedb3d8f76ed
Author: Brent Putman <putmanb at georgetown.edu>
AuthorDate: Sun Jun 5 17:02:04 2016 -0400

    Add support classes for Predicate use within Resolvers.
    
    This is in support of OSJ-134.
---
 .../resolver/CriterionPredicateRegistry.java       | 210 +++++++++++++++++++++
 .../java/support/resolver/ResolverSupport.java     | 120 ++++++++++++
 .../resolver/CriterionPredicateRegistryTest.java   | 109 +++++++++++
 .../support/resolver/EvaluableFooCriterion.java    |  27 +++
 .../resolver/EvaluableTestFooCriterion.java        |  36 ++++
 .../utilities/java/support/resolver/Foo.java       |  22 +++
 .../java/support/resolver/FooPredicate.java        |  35 ++++
 .../java/support/resolver/ResolverSupportTest.java | 180 ++++++++++++++++++
 .../java/support/resolver/TestCriterion.java       |  25 +++
 .../test-criterion-predicate-mappings.properties   |   1 +
 10 files changed, 765 insertions(+)

diff --git a/src/main/java/net/shibboleth/utilities/java/support/resolver/CriterionPredicateRegistry.java b/src/main/java/net/shibboleth/utilities/java/support/resolver/CriterionPredicateRegistry.java
new file mode 100644
index 0000000..4fb598a
--- /dev/null
+++ b/src/main/java/net/shibboleth/utilities/java/support/resolver/CriterionPredicateRegistry.java
@@ -0,0 +1,210 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development, 
+ * Inc. (UCAID) under one or more contributor license agreements.  See the 
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID 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 net.shibboleth.utilities.java.support.resolver;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Predicate;
+
+import net.shibboleth.utilities.java.support.logic.Constraint;
+import net.shibboleth.utilities.java.support.primitive.StringSupport;
+
+/**
+ * A registry which manages mappings from types of {@link Criterion} to types of {@link Predicate}
+ * which can evaluate that criterion's data against a particular target type.
+ *
+ * <p>
+ * Each predicate's implementation that is registered <strong>MUST</strong> implement a 
+ * single-arg constructor which takes an instance of the {@link Criterion} to be evaluated.
+ * The predicate instance is instantiated reflectively based on this requirement.
+ * </p>
+ * 
+ * @param <T> the target type which the returned predicates evaluate
+ */
+public class CriterionPredicateRegistry<T> {
+
+    /** Logger. */
+    private Logger log = LoggerFactory.getLogger(CriterionPredicateRegistry.class);
+
+    /** Storage for the registry mappings. */
+    private Map<Class<? extends Criterion>, Class<? extends Predicate<T>>> registry;
+    
+    /** Constructor. */
+    public CriterionPredicateRegistry() {
+        registry = new HashMap<>();
+    }
+
+    /**
+     * Get an instance of {@link Predicate} which can evaluate the supplied criterion's
+     * requirements against a target of the specified type.
+     * 
+     * @param criterion the criterion to be evaluated
+     * @return an predicate instance representing the specified criterion's requirements
+     * @throws ResolverException thrown if there is an error reflectively instantiating a new instance of
+     *             the predicate type based on class information stored in the registry
+     */
+    @Nullable public Predicate<T> getPredicate(@Nonnull final Criterion criterion) throws ResolverException {
+        Constraint.isNotNull(criterion, "Criterion to map cannot be null");
+        
+        final Class<? extends Predicate<T>> predicateClass = lookup(criterion.getClass());
+
+        if (predicateClass != null) {
+            log.debug("Registry located Predicate class {} for Criterion class {}", predicateClass.getName(),
+                    criterion.getClass().getName());
+
+            try {
+                final Constructor<? extends Predicate<T>> constructor = 
+                        predicateClass.getConstructor(new Class[] { criterion.getClass() });
+
+                return constructor.newInstance(new Object[] { criterion });
+
+            } catch (final SecurityException | InstantiationException | IllegalAccessException 
+                    | IllegalArgumentException | InvocationTargetException | NoSuchMethodException e) {
+                log.error("Error instantiating new Predicate instance", e);
+                throw new ResolverException("Could not create new Predicate instance", e);
+            }
+        } else {
+            log.debug("Registry did not locate Predicate implementation registered for Criterion class {}", 
+                    criterion.getClass().getName());
+            return null;
+        }
+    }
+    
+
+    /**
+     * Lookup the predicate class type which is registered for the specified Criterion class.
+     * 
+     * @param clazz the Criterion class subtype to lookup
+     * @return the registered predicate class type
+     */
+    @Nullable protected Class<? extends Predicate<T>> lookup(@Nonnull final Class<? extends Criterion> clazz) {
+        Constraint.isNotNull(clazz, "Criterion class to lookup cannot be null");
+        return registry.get(clazz);
+    }
+
+    /**
+     * Register a {@link Predicate} class for a criterion class.
+     * 
+     * @param criterionClass class subtype of {@link Criterion}
+     * @param predicateClass the predicate class type
+     */
+    public void register(@Nonnull final Class<? extends Criterion> criterionClass,
+            @Nonnull final Class<? extends Predicate<T>> predicateClass) {
+        Constraint.isNotNull(criterionClass, "Criterion class to register cannot be null");
+        Constraint.isNotNull(predicateClass, "Predicate class to register cannot be null");
+        
+        log.debug("Registering class {} as Predicate for Criterion class {}", 
+                predicateClass.getName(), criterionClass.getName());
+
+        registry.put(criterionClass, predicateClass);
+    }
+
+    /**
+     * Deregister a criterion-evaluator mapping.
+     * 
+     * @param criterionClass class subtype of {@link Criterion}
+     */
+    public void deregister(@Nonnull final Class<? extends Criterion> criterionClass) {
+        Constraint.isNotNull(criterionClass, "Criterion class to unregister cannot be null");
+        
+        log.debug("Deregistering Predicate for Criterion class {}", criterionClass.getName());
+        registry.remove(criterionClass);
+    }
+
+    /**
+     * Clear all mappings from the registry.
+     */
+    public void clearRegistry() {
+        log.debug("Clearing Criterion Predicate registry");
+        registry.clear();
+    }
+
+    /**
+     * Load criterion -> predicate mappings from a classpath resource.
+     * 
+     * @param classpathResource the classpath resource path from which to load mapping properites
+     */
+    public void loadMappings(@Nonnull final String classpathResource) {
+        final String resource = Constraint.isNotNull(StringSupport.trimOrNull(classpathResource),
+                "Classpath resource was null or empty");
+        try (final InputStream inStream = this.getClass().getResourceAsStream(resource)) {
+            if (inStream == null) {
+                log.error("Could not open resource stream from resource '{}'", resource);
+                return;
+            }
+            final Properties mappings = new Properties();
+            mappings.load(inStream);
+            loadMappings(mappings);
+        } catch (final IOException e) {
+            log.error("Error load mappings from resource '{}'", resource, e);
+            return;
+        }
+    }
+
+    /**
+     * Load a set of criterion -> predicate mappings from the supplied properties set.
+     * 
+     * @param mappings properties set where the key is the criterion class name, the value is the predicate class name
+     */
+    public void loadMappings(@Nonnull final Properties mappings) {
+        Constraint.isNotNull(mappings, "Mappings to load cannot be null");
+        
+        for (final Object key : mappings.keySet()) {
+            if (!(key instanceof String)) {
+                log.error("Properties key was not an instance of String, was '{}', skipping...", 
+                        key.getClass().getName());
+                continue;
+            }
+            final String criterionName = (String) key;
+            final String predicateName = mappings.getProperty(criterionName);
+
+            final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+            Class criterionClass = null;
+            try {
+                criterionClass = classLoader.loadClass(criterionName);
+            } catch (final ClassNotFoundException e) {
+                log.error("Could not find Criterion class '{}', skipping registration", criterionName);
+                continue;
+            }
+
+            Class predicateClass = null;
+            try {
+                predicateClass = classLoader.loadClass(predicateName);
+            } catch (final ClassNotFoundException e) {
+                log.error("Could not find Predicate class '{}', skipping registration", criterionName);
+                continue;
+            }
+
+            register(criterionClass, predicateClass);
+        }
+
+    }
+    
+}
diff --git a/src/main/java/net/shibboleth/utilities/java/support/resolver/ResolverSupport.java b/src/main/java/net/shibboleth/utilities/java/support/resolver/ResolverSupport.java
new file mode 100644
index 0000000..a48e740
--- /dev/null
+++ b/src/main/java/net/shibboleth/utilities/java/support/resolver/ResolverSupport.java
@@ -0,0 +1,120 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development, 
+ * Inc. (UCAID) under one or more contributor license agreements.  See the 
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID 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 net.shibboleth.utilities.java.support.resolver;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
+import com.google.common.collect.Iterables;
+
+/**
+ * Support class for resolver implementations.
+ */
+public final class ResolverSupport {
+    
+    /** Constructor. */
+    private ResolverSupport() {}
+    
+    /**
+     * Obtain a set of {@link Predicate} based on a {@link CriteriaSet}.
+     * 
+     * @param criteriaSet the criteria set to evaluate
+     * @param predicateCriterionType the optional type to evaluate and extract directly from the criteria set
+     * @param registry the optional registry of mappings from {@link Criterion} to {@link Predicate}
+     * 
+     * @return a set of predicates, possibly empty
+     * 
+     * @throws ResolverException if there is a fatal error evaluating the criteria set
+     * 
+     * @param <T> the type of target which the returned predicates can evaluate
+     * @param <E> the type of criterion predicates to extract directly from the criteria set
+     */
+    @Nonnull
+    public static <T, E extends Predicate<T>> Set<Predicate<T>> getPredicates(@Nullable final CriteriaSet criteriaSet, 
+            @Nullable final Class<E> predicateCriterionType, @Nullable final CriterionPredicateRegistry<T> registry) 
+                    throws ResolverException {
+        
+        if (criteriaSet == null) {
+            return Collections.emptySet();
+        }
+        
+        final Set<Predicate<T>> predicates = new HashSet<>(criteriaSet.size());
+        
+        for (final Criterion criterion : criteriaSet) {
+            if (predicateCriterionType != null && predicateCriterionType.isInstance(criterion)) {
+                predicates.add(predicateCriterionType.cast(criterion));
+            } else if (registry != null) {
+                final Predicate<T> predicate = registry.getPredicate(criterion);
+                if (predicate != null) {
+                    predicates.add(predicate);
+                }
+            }
+        }
+        
+        return predicates;
+    }
+    
+    /**
+     * Return a filtered {@link Iterable} of the specified candidates based on the supplied set of {@link Predicate}
+     * and the satisfyAny flag.
+     * 
+     * @param candidates the candidates to filter
+     * @param predicates the predicates with which to filter
+     * @param satisfyAny if true the predicates will be logically OR-ed, otherwise they are logically AND-ed
+     * @param onEmptyPredicatesReturnEmpty if true and no predicates are supplied, then return an empty iterable;
+     *          otherwise return the original input candidates
+     * 
+     * @return the filtered iteration of the candidates
+     * 
+     * @param <T> the type of target candidates
+     */
+    @Nonnull
+    public static <T> Iterable<T> getFilteredIterable(@Nullable final Iterable<T> candidates, 
+            @Nullable final Set<Predicate<T>> predicates, final boolean satisfyAny, 
+            final boolean onEmptyPredicatesReturnEmpty) {
+        
+        if (candidates == null || !candidates.iterator().hasNext()) {
+            return Collections.emptySet();
+        }
+        
+        if (predicates == null || predicates.isEmpty()) {
+            if (onEmptyPredicatesReturnEmpty) {
+                return Collections.emptySet();
+            } else {
+                return candidates;
+            }
+        }
+        
+        Predicate<T> predicate;
+        if (satisfyAny) {
+            predicate = Predicates.or(predicates);
+        } else {
+            predicate = Predicates.and(predicates);
+        }
+        
+        return Iterables.filter(candidates, predicate);
+    }
+    
+
+}
diff --git a/src/test/java/net/shibboleth/utilities/java/support/resolver/CriterionPredicateRegistryTest.java b/src/test/java/net/shibboleth/utilities/java/support/resolver/CriterionPredicateRegistryTest.java
new file mode 100644
index 0000000..b912071
--- /dev/null
+++ b/src/test/java/net/shibboleth/utilities/java/support/resolver/CriterionPredicateRegistryTest.java
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development, 
+ * Inc. (UCAID) under one or more contributor license agreements.  See the 
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID 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 net.shibboleth.utilities.java.support.resolver;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+import org.junit.Assert;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import com.google.common.base.Predicate;
+
+public class CriterionPredicateRegistryTest {
+    
+    private TestCriterion fooCriterion;
+    
+    private Class<TestCriterion> testCriterionClass = TestCriterion.class;
+    private Class<FooPredicate> fooPredicateClass = FooPredicate.class;
+    
+    @BeforeMethod
+    public void setUp() {
+        fooCriterion = new TestCriterion();
+    }
+    
+    @Test
+    public void testExplictRegisterDeregister() throws ResolverException {
+        CriterionPredicateRegistry<Foo> registry = new CriterionPredicateRegistry<>();
+        Predicate<Foo> predicate;
+        
+        Assert.assertNull(registry.getPredicate(fooCriterion));
+        
+        registry.register(testCriterionClass, fooPredicateClass);
+        predicate = registry.getPredicate(fooCriterion);
+        Assert.assertNotNull(predicate);
+        Assert.assertTrue(fooPredicateClass.isInstance(predicate));
+        
+        registry.deregister(testCriterionClass);
+        Assert.assertNull(registry.getPredicate(fooCriterion));
+        
+        registry.register(testCriterionClass, fooPredicateClass);
+        predicate = registry.getPredicate(fooCriterion);
+        Assert.assertNotNull(predicate);
+        registry.clearRegistry();
+        predicate = registry.getPredicate(fooCriterion);
+        Assert.assertNull(predicate);
+        
+    }
+    
+    @Test
+    public void testRelativeClassPathResourceLoad() throws ResolverException {
+        CriterionPredicateRegistry<Foo> registry = new CriterionPredicateRegistry<>();
+        
+        Assert.assertNull(registry.getPredicate(fooCriterion));
+        
+        registry.loadMappings("test-criterion-predicate-mappings.properties");
+        
+        Predicate<Foo> predicate = registry.getPredicate(fooCriterion);
+        Assert.assertNotNull(predicate);
+        Assert.assertTrue(fooPredicateClass.isInstance(predicate));
+    }
+    
+    @Test
+    public void testAbsoluteClassPathResourceLoad() throws ResolverException {
+        CriterionPredicateRegistry<Foo> registry = new CriterionPredicateRegistry<>();
+        
+        Assert.assertNull(registry.getPredicate(fooCriterion));
+        
+        registry.loadMappings("/net/shibboleth/utilities/java/support/resolver/test-criterion-predicate-mappings.properties");
+        
+        Predicate<Foo> predicate = registry.getPredicate(fooCriterion);
+        Assert.assertNotNull(predicate);
+        Assert.assertTrue(fooPredicateClass.isInstance(predicate));
+    }
+    
+    @Test
+    public void testPropertiesLoad() throws ResolverException, IOException {
+        CriterionPredicateRegistry<Foo> registry = new CriterionPredicateRegistry<>();
+        
+        Assert.assertNull(registry.getPredicate(fooCriterion));
+        
+        Properties properties = new Properties();
+        try (InputStream is = this.getClass().getResourceAsStream("test-criterion-predicate-mappings.properties")) {
+            properties.load(is);;
+        }
+        registry.loadMappings(properties);
+        
+        Predicate<Foo> predicate = registry.getPredicate(fooCriterion);
+        Assert.assertNotNull(predicate);
+        Assert.assertTrue(fooPredicateClass.isInstance(predicate));
+    }
+
+}
diff --git a/src/test/java/net/shibboleth/utilities/java/support/resolver/EvaluableFooCriterion.java b/src/test/java/net/shibboleth/utilities/java/support/resolver/EvaluableFooCriterion.java
new file mode 100644
index 0000000..cfdb498
--- /dev/null
+++ b/src/test/java/net/shibboleth/utilities/java/support/resolver/EvaluableFooCriterion.java
@@ -0,0 +1,27 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development, 
+ * Inc. (UCAID) under one or more contributor license agreements.  See the 
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID 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 net.shibboleth.utilities.java.support.resolver;
+
+import com.google.common.base.Predicate;
+
+/**
+ *
+ */
+public interface EvaluableFooCriterion extends Criterion, Predicate<Foo> {
+
+}
diff --git a/src/test/java/net/shibboleth/utilities/java/support/resolver/EvaluableTestFooCriterion.java b/src/test/java/net/shibboleth/utilities/java/support/resolver/EvaluableTestFooCriterion.java
new file mode 100644
index 0000000..5b010b3
--- /dev/null
+++ b/src/test/java/net/shibboleth/utilities/java/support/resolver/EvaluableTestFooCriterion.java
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development, 
+ * Inc. (UCAID) under one or more contributor license agreements.  See the 
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID 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 net.shibboleth.utilities.java.support.resolver;
+
+public class EvaluableTestFooCriterion implements EvaluableFooCriterion {
+    
+    private boolean result;
+    
+    public EvaluableTestFooCriterion() {
+        this(false);
+    }
+    
+    public EvaluableTestFooCriterion(final boolean flag) {
+        result = flag;
+    }
+
+    public boolean apply(Foo input) {
+        return result;
+    }
+
+}
diff --git a/src/test/java/net/shibboleth/utilities/java/support/resolver/Foo.java b/src/test/java/net/shibboleth/utilities/java/support/resolver/Foo.java
new file mode 100644
index 0000000..63286c7
--- /dev/null
+++ b/src/test/java/net/shibboleth/utilities/java/support/resolver/Foo.java
@@ -0,0 +1,22 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development, 
+ * Inc. (UCAID) under one or more contributor license agreements.  See the 
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID 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 net.shibboleth.utilities.java.support.resolver;
+
+public class Foo {
+
+}
diff --git a/src/test/java/net/shibboleth/utilities/java/support/resolver/FooPredicate.java b/src/test/java/net/shibboleth/utilities/java/support/resolver/FooPredicate.java
new file mode 100644
index 0000000..5709406
--- /dev/null
+++ b/src/test/java/net/shibboleth/utilities/java/support/resolver/FooPredicate.java
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development, 
+ * Inc. (UCAID) under one or more contributor license agreements.  See the 
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID 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 net.shibboleth.utilities.java.support.resolver;
+
+import com.google.common.base.Predicate;
+
+/**
+ *
+ */
+public class FooPredicate implements Predicate<Foo> {
+    
+    public FooPredicate(TestCriterion criterion) {
+        // just a mock class
+    }
+
+    public boolean apply(Foo input) {
+        return true;
+    }
+
+}
diff --git a/src/test/java/net/shibboleth/utilities/java/support/resolver/ResolverSupportTest.java b/src/test/java/net/shibboleth/utilities/java/support/resolver/ResolverSupportTest.java
new file mode 100644
index 0000000..9ea40af
--- /dev/null
+++ b/src/test/java/net/shibboleth/utilities/java/support/resolver/ResolverSupportTest.java
@@ -0,0 +1,180 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development, 
+ * Inc. (UCAID) under one or more contributor license agreements.  See the 
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID 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 net.shibboleth.utilities.java.support.resolver;
+
+import java.util.Set;
+
+import org.junit.Assert;
+import org.testng.annotations.Test;
+
+import com.google.common.base.Predicate;
+import com.google.common.collect.Sets;
+
+public class ResolverSupportTest {
+    
+    @Test
+    public void testGetPredicates() throws ResolverException {
+        CriterionPredicateRegistry<Foo> registry = new CriterionPredicateRegistry<>();
+        registry.register(TestCriterion.class, FooPredicate.class);
+        
+        EvaluableTestFooCriterion evaluableCriterion = new EvaluableTestFooCriterion();
+        
+        Set<Predicate<Foo>> predicates;
+        
+        // Null criteria
+        predicates = ResolverSupport.getPredicates(null, EvaluableFooCriterion.class, registry);
+        Assert.assertNotNull(predicates);
+        Assert.assertEquals(predicates.size(), 0);
+        
+        CriteriaSet criteria = new CriteriaSet();
+        
+        predicates = ResolverSupport.getPredicates(criteria, EvaluableFooCriterion.class, registry);
+        Assert.assertNotNull(predicates);
+        Assert.assertEquals(predicates.size(), 0);
+        
+        criteria.clear();
+        criteria.add(evaluableCriterion);
+        predicates = ResolverSupport.getPredicates(criteria, EvaluableFooCriterion.class, registry);
+        Assert.assertNotNull(predicates);
+        Assert.assertEquals(predicates.size(), 1);
+        Assert.assertTrue(predicates.contains(evaluableCriterion));
+        
+        criteria.clear();
+        criteria.add(new TestCriterion());
+        predicates = ResolverSupport.getPredicates(criteria, EvaluableFooCriterion.class, registry);
+        Assert.assertNotNull(predicates);
+        Assert.assertEquals(predicates.size(), 1);
+        Assert.assertTrue(FooPredicate.class.isInstance(predicates.iterator().next()));
+        
+    }
+    
+    @Test
+    public void testGetFilteredIterable() {
+        Foo foo1 = new Foo();
+        Foo foo2 = new Foo();
+        
+        Iterable<Foo> result;
+        Set<Foo> resultSet;
+        
+        //Null candidates
+        result = ResolverSupport.getFilteredIterable(null,
+                Sets.<Predicate<Foo>>newHashSet(new EvaluableTestFooCriterion(true)),
+                false, false);
+        Assert.assertNotNull(result);
+        resultSet = Sets.newHashSet(result);
+        Assert.assertEquals(resultSet.size(), 0);
+        
+        //Empty candidates
+        result = ResolverSupport.getFilteredIterable(Sets.<Foo>newHashSet(),
+                Sets.<Predicate<Foo>>newHashSet(new EvaluableTestFooCriterion(true)),
+                false, false);
+        Assert.assertNotNull(result);
+        resultSet = Sets.newHashSet(result);
+        Assert.assertEquals(resultSet.size(), 0);
+        
+        // Single predicate tests
+        
+        // predicate = true
+        result = ResolverSupport.getFilteredIterable(Sets.<Foo>newHashSet(foo1, foo2), 
+                Sets.<Predicate<Foo>>newHashSet(new EvaluableTestFooCriterion(true)),
+                false, false);
+        Assert.assertNotNull(result);
+        resultSet = Sets.newHashSet(result);
+        Assert.assertEquals(resultSet.size(), 2);
+        Assert.assertTrue(resultSet.contains(foo1));
+        Assert.assertTrue(resultSet.contains(foo2));
+        
+        // predicate = false
+        result = ResolverSupport.getFilteredIterable(Sets.<Foo>newHashSet(foo1, foo2), 
+                Sets.<Predicate<Foo>>newHashSet(new EvaluableTestFooCriterion(false)),
+                false, false);
+        Assert.assertNotNull(result);
+        resultSet = Sets.newHashSet(result);
+        Assert.assertEquals(resultSet.size(), 0);
+        
+        // Multiple predicate tests
+        
+        // satisfyAny = false, predicates all true
+        result = ResolverSupport.getFilteredIterable(Sets.<Foo>newHashSet(foo1, foo2), 
+                Sets.<Predicate<Foo>>newHashSet(new EvaluableTestFooCriterion(true), new EvaluableTestFooCriterion(true)),
+                false, false);
+        Assert.assertNotNull(result);
+        resultSet = Sets.newHashSet(result);
+        Assert.assertEquals(resultSet.size(), 2);
+        Assert.assertTrue(resultSet.contains(foo1));
+        Assert.assertTrue(resultSet.contains(foo2));
+        
+        // satisfyAny = false, predicates true + false
+        result = ResolverSupport.getFilteredIterable(Sets.<Foo>newHashSet(foo1, foo2), 
+                Sets.<Predicate<Foo>>newHashSet(new EvaluableTestFooCriterion(true), new EvaluableTestFooCriterion(false)),
+                false, false);
+        Assert.assertNotNull(result);
+        resultSet = Sets.newHashSet(result);
+        Assert.assertEquals(resultSet.size(), 0);
+        
+        // satisfyAny = true, predicates true + false
+        result = ResolverSupport.getFilteredIterable(Sets.<Foo>newHashSet(foo1, foo2), 
+                Sets.<Predicate<Foo>>newHashSet(new EvaluableTestFooCriterion(true), new EvaluableTestFooCriterion(false)),
+                true, false);
+        Assert.assertNotNull(result);
+        resultSet = Sets.newHashSet(result);
+        Assert.assertEquals(resultSet.size(), 2);
+        Assert.assertTrue(resultSet.contains(foo1));
+        Assert.assertTrue(resultSet.contains(foo2));
+        
+        // Empty predicates tests
+        
+        // onEmptyPredicatesReturnEmpty = false
+        result = ResolverSupport.getFilteredIterable(Sets.<Foo>newHashSet(foo1, foo2), 
+                Sets.<Predicate<Foo>>newHashSet(),
+                false, false);
+        Assert.assertNotNull(result);
+        resultSet = Sets.newHashSet(result);
+        Assert.assertEquals(resultSet.size(), 2);
+        Assert.assertTrue(resultSet.contains(foo1));
+        Assert.assertTrue(resultSet.contains(foo2));
+        
+        // onEmptyPredicatesReturnEmpty = false, predicates = null
+        result = ResolverSupport.getFilteredIterable(Sets.<Foo>newHashSet(foo1, foo2), 
+                null,
+                false, false);
+        Assert.assertNotNull(result);
+        resultSet = Sets.newHashSet(result);
+        Assert.assertEquals(resultSet.size(), 2);
+        Assert.assertTrue(resultSet.contains(foo1));
+        Assert.assertTrue(resultSet.contains(foo2));
+        
+        // onEmptyPredicatesReturnEmpty = true
+        result = ResolverSupport.getFilteredIterable(Sets.<Foo>newHashSet(foo1, foo2), 
+                Sets.<Predicate<Foo>>newHashSet(),
+                false, true);
+        Assert.assertNotNull(result);
+        resultSet = Sets.newHashSet(result);
+        Assert.assertEquals(resultSet.size(), 0);
+        
+        // onEmptyPredicatesReturnEmpty = true, predicates = null
+        result = ResolverSupport.getFilteredIterable(Sets.<Foo>newHashSet(foo1, foo2), 
+                null,
+                false, true);
+        Assert.assertNotNull(result);
+        resultSet = Sets.newHashSet(result);
+        Assert.assertEquals(resultSet.size(), 0);
+        
+    }
+
+}
diff --git a/src/test/java/net/shibboleth/utilities/java/support/resolver/TestCriterion.java b/src/test/java/net/shibboleth/utilities/java/support/resolver/TestCriterion.java
new file mode 100644
index 0000000..0b854db
--- /dev/null
+++ b/src/test/java/net/shibboleth/utilities/java/support/resolver/TestCriterion.java
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development, 
+ * Inc. (UCAID) under one or more contributor license agreements.  See the 
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID 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 net.shibboleth.utilities.java.support.resolver;
+
+/**
+ *
+ */
+public class TestCriterion implements Criterion {
+
+}
diff --git a/src/test/resources/net/shibboleth/utilities/java/support/resolver/test-criterion-predicate-mappings.properties b/src/test/resources/net/shibboleth/utilities/java/support/resolver/test-criterion-predicate-mappings.properties
new file mode 100644
index 0000000..341de22
--- /dev/null
+++ b/src/test/resources/net/shibboleth/utilities/java/support/resolver/test-criterion-predicate-mappings.properties
@@ -0,0 +1 @@
+net.shibboleth.utilities.java.support.resolver.TestCriterion = net.shibboleth.utilities.java.support.resolver.FooPredicate
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list