You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2014/05/24 22:09:44 UTC

svn commit: r1597343 - in /commons/proper/beanutils/trunk/src: main/java/org/apache/commons/beanutils/SuppressPropertiesBeanIntrospector.java test/java/org/apache/commons/beanutils/SuppressPropertiesBeanIntrospectorTestCase.java

Author: oheger
Date: Sat May 24 20:09:43 2014
New Revision: 1597343

URL: http://svn.apache.org/r1597343
Log:
[BEANUTILS-463] Added a specialized BeanIntrospector for suppressing properties.

This class can remove properties which have been detected by another
BeanIntrospector object.

Added:
    commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/SuppressPropertiesBeanIntrospector.java
    commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/SuppressPropertiesBeanIntrospectorTestCase.java

Added: commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/SuppressPropertiesBeanIntrospector.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/SuppressPropertiesBeanIntrospector.java?rev=1597343&view=auto
==============================================================================
--- commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/SuppressPropertiesBeanIntrospector.java (added)
+++ commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/SuppressPropertiesBeanIntrospector.java Sat May 24 20:09:43 2014
@@ -0,0 +1,64 @@
+package org.apache.commons.beanutils;
+
+import java.beans.IntrospectionException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * <p>
+ * A specialized {@code BeanIntrospector} implementation which suppresses some properties.
+ * </p>
+ * <p>
+ * An instance of this class is passed a set with the names of the properties it should
+ * process. During introspection of a bean class it removes all these properties from the
+ * {@link IntrospectionContext}. So effectively, properties added by a different
+ * {@code BeanIntrospector} are removed again.
+ * </p>
+ *
+ * @version $Id$
+ * @since 1.9.2
+ */
+public class SuppressPropertiesBeanIntrospector implements BeanIntrospector {
+    /** A set with the names of the properties to be suppressed. */
+    private final Set<String> propertyNames;
+
+    /**
+     * Creates a new instance of {@code SuppressPropertiesBeanIntrospector} and sets the
+     * names of the properties to be suppressed.
+     *
+     * @param propertiesToSuppress the names of the properties to be suppressed (must not
+     * be <b>null</b>)
+     * @throws IllegalArgumentException if the collection with property names is
+     * <b>null</b>
+     */
+    public SuppressPropertiesBeanIntrospector(Collection<String> propertiesToSuppress) {
+        if (propertiesToSuppress == null) {
+            throw new IllegalArgumentException("Property names must not be null!");
+        }
+
+        propertyNames = Collections.unmodifiableSet(new HashSet<String>(
+                propertiesToSuppress));
+    }
+
+    /**
+     * Returns a (unmodifiable) set with the names of the properties which are suppressed
+     * by this {@code BeanIntrospector}.
+     *
+     * @return a set with the names of the suppressed properties
+     */
+    public Set<String> getSuppressedProperties() {
+        return propertyNames;
+    }
+
+    /**
+     * {@inheritDoc} This implementation removes all properties from the given context it
+     * is configured for.
+     */
+    public void introspect(IntrospectionContext icontext) throws IntrospectionException {
+        for (String property : getSuppressedProperties()) {
+            icontext.removePropertyDescriptor(property);
+        }
+    }
+}

Added: commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/SuppressPropertiesBeanIntrospectorTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/SuppressPropertiesBeanIntrospectorTestCase.java?rev=1597343&view=auto
==============================================================================
--- commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/SuppressPropertiesBeanIntrospectorTestCase.java (added)
+++ commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/SuppressPropertiesBeanIntrospectorTestCase.java Sat May 24 20:09:43 2014
@@ -0,0 +1,127 @@
+package org.apache.commons.beanutils;
+
+import java.beans.IntrospectionException;
+import java.beans.PropertyDescriptor;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+import junit.framework.TestCase;
+
+/**
+ * Test class for {@code SuppressPropertiesBeanIntrospector}.
+ *
+ * @version $Id$
+ */
+public class SuppressPropertiesBeanIntrospectorTestCase extends TestCase {
+    /**
+     * Tries to create an instance without properties.
+     */
+    public void testInitNoPropertyNames() {
+        try {
+            new SuppressPropertiesBeanIntrospector(null);
+            fail("Missing properties not detected!");
+        } catch (IllegalArgumentException iaex) {
+            // ok
+        }
+    }
+
+    /**
+     * Tests whether the expected properties have been removed during introspection.
+     */
+    public void testRemovePropertiesDuringIntrospection() throws IntrospectionException {
+        String[] properties = { "test", "other", "oneMore" };
+        SuppressPropertiesBeanIntrospector introspector = new SuppressPropertiesBeanIntrospector(
+                Arrays.asList(properties));
+        IntrospectionContextTestImpl context = new IntrospectionContextTestImpl();
+
+        introspector.introspect(context);
+        assertEquals("Wrong number of removed properties", properties.length, context
+                .getRemovedProperties().size());
+        for (String property : properties) {
+            assertTrue("Property not removed: " + property, context
+                    .getRemovedProperties().contains(property));
+        }
+    }
+
+    /**
+     * Tests that a defensive copy is created from the collection with properties to be
+     * removed.
+     */
+    public void testPropertyNamesDefensiveCopy() throws IntrospectionException {
+        Collection<String> properties = new HashSet<String>();
+        properties.add("prop1");
+        SuppressPropertiesBeanIntrospector introspector = new SuppressPropertiesBeanIntrospector(
+                properties);
+        properties.add("prop2");
+        IntrospectionContextTestImpl context = new IntrospectionContextTestImpl();
+
+        introspector.introspect(context);
+        assertEquals("Wrong number of removed properties", 1, context
+                .getRemovedProperties().size());
+        assertTrue("Wrong removed property",
+                context.getRemovedProperties().contains("prop1"));
+    }
+
+    /**
+     * Tests that the set with properties to be removed cannot be modified.
+     */
+    public void testGetSuppressedPropertiesModify() {
+        SuppressPropertiesBeanIntrospector introspector = new SuppressPropertiesBeanIntrospector(
+                Arrays.asList("p1", "p2"));
+        Set<String> properties = introspector.getSuppressedProperties();
+        try {
+            properties.add("anotherProperty");
+            fail("Could modify properties");
+        } catch (UnsupportedOperationException uoex) {
+            // ok
+        }
+    }
+
+    /**
+     * A test implementation of IntrospectionContext which collects the properties which
+     * have been removed.
+     */
+    private static class IntrospectionContextTestImpl implements IntrospectionContext {
+        /** Stores the names of properties which have been removed. */
+        private final Set<String> removedProperties = new HashSet<String>();
+
+        /**
+         * Returns the names of properties which have been removed.
+         *
+         * @return the set with removed properties
+         */
+        public Set<String> getRemovedProperties() {
+            return removedProperties;
+        }
+
+        public Class<?> getTargetClass() {
+            throw new UnsupportedOperationException("Unexpected method call!");
+        }
+
+        public void addPropertyDescriptor(PropertyDescriptor desc) {
+            throw new UnsupportedOperationException("Unexpected method call!");
+        }
+
+        public void addPropertyDescriptors(PropertyDescriptor[] descriptors) {
+            throw new UnsupportedOperationException("Unexpected method call!");
+        }
+
+        public boolean hasProperty(String name) {
+            throw new UnsupportedOperationException("Unexpected method call!");
+        }
+
+        public PropertyDescriptor getPropertyDescriptor(String name) {
+            throw new UnsupportedOperationException("Unexpected method call!");
+        }
+
+        public void removePropertyDescriptor(String name) {
+            removedProperties.add(name);
+        }
+
+        public Set<String> propertyNames() {
+            throw new UnsupportedOperationException("Unexpected method call!");
+        }
+    }
+}