commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1243717 - in /commons/sandbox/beanutils2/trunk/src: changes/ main/java/org/apache/commons/beanutils2/ test/java/org/apache/commons/beanutils2/
Date Mon, 13 Feb 2012 22:16:22 GMT
Author: simonetripodi
Date: Mon Feb 13 22:16:21 2012
New Revision: 1243717

URL: http://svn.apache.org/viewvc?rev=1243717&view=rev
Log:
[SANDBOX-387] Implement possibility to find out if a property readable and/or wirtable - patch
submitted by Benedikt Ritter

Added:
    commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/IsReadableTestCase.java
  (with props)
    commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/IsWritableTestCase.java
  (with props)
Modified:
    commons/sandbox/beanutils2/trunk/src/changes/changes.xml
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanAccessor.java
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java

Modified: commons/sandbox/beanutils2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/changes/changes.xml?rev=1243717&r1=1243716&r2=1243717&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/changes/changes.xml (original)
+++ commons/sandbox/beanutils2/trunk/src/changes/changes.xml Mon Feb 13 22:16:21 2012
@@ -26,6 +26,9 @@
     <action dev="simonetripodi" type="update" issue="SANDBOX-390" due-to="Benedikt Ritter">
       Make sure that the internal package does not get exported when packaging as OSGi bundle
     </action>
+    <action dev="simonetripodi" type="update" issue="SANDBOX-387" due-to="Benedikt Ritter">
+      Implement possibility to find out if a property readable and/or wirtable
+    </action>
     <action dev="simonetripodi" type="update" issue="SANDBOX-379" due-to="Benedikt Ritter">
       Implement describe() on DefaultBeanAccessor
     </action>

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanAccessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanAccessor.java?rev=1243717&r1=1243716&r2=1243717&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanAccessor.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanAccessor.java
Mon Feb 13 22:16:21 2012
@@ -52,6 +52,28 @@ public interface BeanAccessor<B>
     IndexedPropertySetterAccessor<B> setMapped( String propertyName )
         throws IllegalAccessException, IntrospectionException, InvocationTargetException,
NoSuchMethodException;
 
+    // checks
+
+    /**
+     * Checks if the specified property name identifies a readable property.
+     *
+     * @param propertyName the name of the property to be checked.
+     * @return {@code true}, if the property is readable.
+     * @throws IntrospectionException TODO
+     */
+    boolean isReadable( String propertyName )
+        throws IntrospectionException;
+
+    /**
+     * Checks if the specified property name identifies a writable property.
+     *
+     * @param propertyName the name of the property to be checked.
+     * @return {@code true}, if the property is writable.
+     * @throws IntrospectionException TODO
+     */
+    boolean isWritable( String propertyName )
+        throws IntrospectionException;
+
     // clone
 
     B cloneBean()

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java?rev=1243717&r1=1243716&r2=1243717&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java
Mon Feb 13 22:16:21 2012
@@ -44,7 +44,7 @@ final class DefaultBeanAccessor<B>
         this.bean = bean;
     }
 
-    // set
+    // get
 
     /**
      * {@inheritDoc}
@@ -158,6 +158,52 @@ final class DefaultBeanAccessor<B>
         return null;
     }
 
+    // checks
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isReadable( String propertyName )
+        throws IntrospectionException
+    {
+        PropertyDescriptor propertyDescriptor = registry.getPropertyDescriptor( bean.getClass(),
propertyName );
+        propertyDescriptor = checkNotNull( propertyDescriptor, "Property '%s' does not exist
in bean of type %s",
+                                           propertyName,
+                                           bean.getClass().getName() );
+
+        if ( propertyDescriptor instanceof IndexedPropertyDescriptor )
+        {
+            IndexedPropertyDescriptor indexed = (IndexedPropertyDescriptor) propertyDescriptor;
+            return indexed.getIndexedReadMethod() != null ? true : false;
+        }
+        else // TODO what about mapped properties?
+        {
+            return propertyDescriptor.getReadMethod() != null ? true : false;
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isWritable( String propertyName )
+        throws IntrospectionException
+    {
+        PropertyDescriptor propertyDescriptor = registry.getPropertyDescriptor( bean.getClass(),
propertyName );
+        propertyDescriptor = checkNotNull( propertyDescriptor, "Property '%s' does not exist
in bean of type %s",
+                                           propertyName,
+                                           bean.getClass().getName() );
+
+        if ( propertyDescriptor instanceof IndexedPropertyDescriptor )
+        {
+            IndexedPropertyDescriptor indexed = (IndexedPropertyDescriptor) propertyDescriptor;
+            return indexed.getIndexedWriteMethod() != null ? true : false;
+        }
+        else // TODO what about mapped properties?
+        {
+            return propertyDescriptor.getWriteMethod() != null ? true : false;
+        }
+    }
+
     /**
      * {@inheritDoc}
      */

Added: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/IsReadableTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/IsReadableTestCase.java?rev=1243717&view=auto
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/IsReadableTestCase.java
(added)
+++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/IsReadableTestCase.java
Mon Feb 13 22:16:21 2012
@@ -0,0 +1,107 @@
+package org.apache.commons.beanutils2;
+
+/*
+ * 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.
+ */
+
+import static org.apache.commons.beanutils2.BeanUtils.on;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class IsReadableTestCase
+{
+
+    private TestBean testBean;
+
+    @Before
+    public void setUp()
+    {
+        testBean = new TestBean();
+    }
+
+    @After
+    public void tearDown()
+    {
+        testBean = null;
+    }
+
+    @Test( expected = NullPointerException.class )
+    public void isReadableNull()
+        throws Exception
+    {
+        on( testBean ).isReadable( null );
+    }
+
+    @Test( expected = NullPointerException.class )
+    public void isReadbleUnknown()
+        throws Exception
+    {
+        on( testBean ).isReadable( "unknown" );
+    }
+
+    @Test
+    public void isReadbleIntProperty()
+        throws Exception
+    {
+        assertTrue( "isReadable returned false for a read/write int property!",
+                    on( testBean ).isReadable( "intProperty" ) );
+    }
+
+    @Test
+    public void isReadbleIntArray()
+        throws Exception
+    {
+        assertTrue( "isReadable returned false for a read/write array property!",
+                    on( testBean ).isReadable( "intArray" ) );
+    }
+
+    @Test
+    public void isReadableIntIndexed()
+        throws Exception
+    {
+        assertTrue( "isReadable returned false for a read/write indexed property!",
+                    on( testBean ).isReadable( "intIndexed" ) );
+    }
+
+    @Test
+    public void isReadbleMapProperty()
+        throws Exception
+    {
+        assertTrue( "isReadable returned false for a read/write map property!",
+                    on( testBean ).isReadable( "mapProperty" ) );
+    }
+
+    @Test
+    public void isReadableReadOnlyProperty()
+        throws Exception
+    {
+        assertTrue( "isReadble returned false for a read only property!",
+                    on( testBean ).isReadable( "readOnlyProperty" ) );
+    }
+
+    @Test
+    public void isReadableWriteOnlyProperty()
+        throws Exception
+    {
+        assertFalse( "isReadable returned true for a write only property!",
+                     on( testBean ).isReadable( "writeOnlyProperty" ) );
+    }
+
+}

Propchange: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/IsReadableTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/IsReadableTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/IsReadableTestCase.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/IsWritableTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/IsWritableTestCase.java?rev=1243717&view=auto
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/IsWritableTestCase.java
(added)
+++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/IsWritableTestCase.java
Mon Feb 13 22:16:21 2012
@@ -0,0 +1,107 @@
+package org.apache.commons.beanutils2;
+
+/*
+ * 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.
+ */
+
+import static org.apache.commons.beanutils2.BeanUtils.on;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class IsWritableTestCase
+{
+
+    private TestBean testBean;
+
+    @Before
+    public void setUp()
+    {
+        testBean = new TestBean();
+    }
+
+    @After
+    public void tearDown()
+    {
+        testBean = null;
+    }
+
+    @Test( expected = NullPointerException.class )
+    public void isWritableNull()
+        throws Exception
+    {
+        on( testBean ).isWritable( null );
+    }
+
+    @Test( expected = NullPointerException.class )
+    public void isWritbleUnknown()
+        throws Exception
+    {
+        on( testBean ).isWritable( "unknown" );
+    }
+
+    @Test
+    public void isWritbleIntProperty()
+        throws Exception
+    {
+        assertTrue( "isWritable returned false for a read/write int property!",
+                    on( testBean ).isWritable( "intProperty" ) );
+    }
+
+    @Test
+    public void isWritbleIntArray()
+        throws Exception
+    {
+        assertTrue( "isWritable returned false for a read/write array property!",
+                    on( testBean ).isWritable( "intArray" ) );
+    }
+
+    @Test
+    public void isWritableIntIndexed()
+        throws Exception
+    {
+        assertTrue( "isWritable returned false for a read/write indexed property!",
+                    on( testBean ).isWritable( "intIndexed" ) );
+    }
+
+    @Test
+    public void isWritbleMapProperty()
+        throws Exception
+    {
+        assertTrue( "isWritable returned false for a read/write map property!",
+                    on( testBean ).isWritable( "mapProperty" ) );
+    }
+
+    @Test
+    public void isWritableReadOnlyProperty()
+        throws Exception
+    {
+        assertFalse( "isWriteable returned true for a read only property!",
+                     on( testBean ).isWritable( "readOnlyProperty" ) );
+    }
+
+    @Test
+    public void isWritableWriteOnlyProperty()
+        throws Exception
+    {
+        assertTrue( "isWritable returned false for a write only property!",
+                    on( testBean ).isWritable( "writeOnlyProperty" ) );
+    }
+
+}

Propchange: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/IsWritableTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/IsWritableTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/IsWritableTestCase.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message