Author: simonetripodi
Date: Fri Jan 27 16:39:35 2012
New Revision: 1236744

URL: http://svn.apache.org/viewvc?rev=1236744&view=rev
Log:
started implementing the getProperty() feature

Added:
    
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetPropertyTestCase.java
   (with props)
Modified:
    
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/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=1236744&r1=1236743&r2=1236744&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
 Fri Jan 27 16:39:35 2012
@@ -17,6 +17,7 @@ package org.apache.commons.beanutils2;
  * limitations under the License.
  */
 
+import java.beans.IntrospectionException;
 import java.lang.reflect.InvocationTargetException;
 import java.util.Map;
 
@@ -26,7 +27,7 @@ public interface BeanAccessor<B>
     // get
 
     BeanAccessor<?> getProperty( String name )
-        throws IllegalAccessException, InvocationTargetException, 
NoSuchMethodException;
+        throws IllegalAccessException, IntrospectionException, 
InvocationTargetException, NoSuchMethodException;
 
     BeanAccessor<?> getIndexedProperty( String name )
         throws IllegalAccessException, InvocationTargetException, 
NoSuchMethodException;

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=1236744&r1=1236743&r2=1236744&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
 Fri Jan 27 16:39:35 2012
@@ -17,6 +17,10 @@ package org.apache.commons.beanutils2;
  * limitations under the License.
  */
 
+import static org.apache.commons.beanutils2.Assertions.checkNotNull;
+
+import java.beans.IntrospectionException;
+import java.beans.PropertyDescriptor;
 import java.lang.reflect.InvocationTargetException;
 import java.util.Map;
 
@@ -24,6 +28,8 @@ final class DefaultBeanAccessor<B>
     implements BeanAccessor<B>
 {
 
+    private final PropertyDescriptorsRegistry registry = 
PropertyDescriptorsRegistry.getInstance();
+
     private final B bean;
 
     public DefaultBeanAccessor( B bean )
@@ -31,11 +37,19 @@ final class DefaultBeanAccessor<B>
         this.bean = bean;
     }
 
+    /**
+     * {@inheritDoc}
+     */
     public BeanAccessor<?> getProperty( String name )
-        throws IllegalAccessException, InvocationTargetException, 
NoSuchMethodException
+        throws IllegalAccessException, IntrospectionException, 
InvocationTargetException, NoSuchMethodException
     {
-        // TODO Auto-generated method stub
-        return null;
+        PropertyDescriptor propertyDescriptor = 
registry.getPropertyDescriptor( bean.getClass(), name );
+        propertyDescriptor = checkNotNull( propertyDescriptor, "Property '%s' 
does not exist in bean of type %s",
+                                           name, bean.getClass().getName() );
+
+        Object newBean = propertyDescriptor.getReadMethod().invoke( bean );
+
+        return new DefaultBeanAccessor<Object>( newBean );
     }
 
     public BeanAccessor<?> getIndexedProperty( String name )

Added: 
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetPropertyTestCase.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetPropertyTestCase.java?rev=1236744&view=auto
==============================================================================
--- 
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetPropertyTestCase.java
 (added)
+++ 
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetPropertyTestCase.java
 Fri Jan 27 16:39:35 2012
@@ -0,0 +1,57 @@
+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.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public final class GetPropertyTestCase
+{
+
+    private TestBean bean;
+
+    @Before
+    public void setUp()
+    {
+        bean = new TestBean();
+    }
+
+    @Before
+    public void tearDown()
+    {
+        bean = null;
+    }
+
+    /**
+     * Test getSimpleProperty on a boolean property.
+     */
+    @Test
+    public void getSimpleBoolean()
+        throws Exception
+    {
+        Object value = on( bean ).getProperty( "booleanProperty" ).get();
+        assertNotNull( "Got a value", value );
+        assertTrue( "Got correct type", ( value instanceof Boolean ) );
+        assertTrue( "Got correct value", ( (Boolean) value ).booleanValue() == 
bean.getBooleanProperty() );
+    }
+
+}

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

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

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


Reply via email to