You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2012/02/01 07:59:54 UTC
svn commit: r1238950 - in /commons/sandbox/beanutils2/trunk/src: changes/
main/java/org/apache/commons/beanutils2/
test/java/org/apache/commons/beanutils2/
Author: simonetripodi
Date: Wed Feb 1 06:59:54 2012
New Revision: 1238950
URL: http://svn.apache.org/viewvc?rev=1238950&view=rev
Log:
[SANDBOX-377] Implement invoke(Exact)Method(...) on DefaultBeanAccessor - patch provided by Benedikt Ritter
Added:
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MethodsTestCase.java (with props)
Modified:
commons/sandbox/beanutils2/trunk/src/changes/changes.xml
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/ArgumentsAccessor.java
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=1238950&r1=1238949&r2=1238950&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/changes/changes.xml (original)
+++ commons/sandbox/beanutils2/trunk/src/changes/changes.xml Wed Feb 1 06:59:54 2012
@@ -23,6 +23,9 @@
</properties>
<body>
<release version="0.1" date="201?-??-??" description="First release.">
+ <action dev="simonetripodi" type="update" issue="SANDBOX-377" due-to="Benedikt Ritter">
+ Implement invoke(Exact)Method(...) on DefaultBeanAccessor
+ </action>
<action dev="simonetripodi" type="update" issue="SANDBOX-376" due-to="Benedikt Ritter">
Extract magic numbers in AccessibleObjectsRegistry.getObjectTransformationCosts(...) to constants
</action>
Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/ArgumentsAccessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/ArgumentsAccessor.java?rev=1238950&r1=1238949&r2=1238950&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/ArgumentsAccessor.java (original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/ArgumentsAccessor.java Wed Feb 1 06:59:54 2012
@@ -21,10 +21,26 @@ package org.apache.commons.beanutils2;
import java.lang.reflect.InvocationTargetException;
+/**
+ * A {@code ArgumentAccessor} can be used to pass arguments to a constructor or method.
+ */
public interface ArgumentsAccessor
{
- BeanAccessor<?> withArguments( Argument<?>...arguments )
+ /**
+ * Passes the given arguments to the called constructor or method. Note that if you wont to pass primitive
+ * arguments, you have to call {@link Argument#argument(Class, Object)} and pass the primitive class. This
+ * especially applies if you called {@link ClassAccessor#invokeExactConstructor(Argument...)},
+ * {@link ClassAccessor#invokeExactStaticMethod(String)} or {@link BeanAccessor#invokeExactMethod(String)} before.
+ *
+ * @param arguments the arguments to be passed. May be empty.
+ * @return a {@link BeanAccessor} for the result of the method invocation.
+ * @throws IllegalArgumentException if the invoking the constructor or method would throw one.
+ * @throws IllegalAccessException if the invoked constructor or method would throw one.
+ * @throws InvocationTargetException TODO
+ * @throws NoSuchMethodException if no method with a fitting signature can be found.
+ */
+ BeanAccessor<?> withArguments( Argument<?>... arguments )
throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException;
}
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=1238950&r1=1238949&r2=1238950&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 Wed Feb 1 06:59:54 2012
@@ -63,9 +63,29 @@ public interface BeanAccessor<B>
// methods invocation
+ /**
+ * Invokes the method with name {@code methodName}. Arguments are casted to fit the methods signature, if possible.
+ *
+ * @param methodName the name of the method to invoke. Must not be {@code null}!
+ * @return a {@link ArgumentsAccessor} to specify any arguments
+ * @throws NoSuchMethodException if there no method with the name {@code methodName} can be found or the arguments
+ * can not be casted so set they fit the methods signature.
+ * @throws IllegalAccessException if the method is not visible to the caller
+ * @throws InvocationTargetException TODO
+ */
ArgumentsAccessor invokeMethod( String methodName )
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException;
+ /**
+ * Invoke the method with name {@code methodName} and the exact arguments.
+ *
+ * @param methodName the name of the method to invoke. Must not be {@code null}!
+ * @return a {@link ArgumentsAccessor} to specify any arguments
+ * @throws NoSuchMethodException if no method with the name {@code methodName} cand be found that accepts the exact
+ * arguments.
+ * @throws IllegalAccessException if the method is not visible to the caller
+ * @throws InvocationTargetException TODO
+ */
ArgumentsAccessor invokeExactMethod( String methodName )
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException;
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=1238950&r1=1238949&r2=1238950&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 Wed Feb 1 06:59:54 2012
@@ -128,18 +128,28 @@ final class DefaultBeanAccessor<B>
}
+ /**
+ * {@inheritDoc}
+ */
public ArgumentsAccessor invokeMethod( String methodName )
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
{
- // TODO Auto-generated method stub
- return null;
+ @SuppressWarnings( "unchecked" )
+ Class<B> beanClass = (Class<B>) bean.getClass();
+ checkNotNull(methodName, "Impossible to execute null method in %s", beanClass.getName());
+ return new DefaultArgumentsAccessor( beanClass, false, methodName, bean );
}
+ /**
+ * {@inheritDoc}
+ */
public ArgumentsAccessor invokeExactMethod( String methodName )
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
{
- // TODO Auto-generated method stub
- return null;
+ @SuppressWarnings( "unchecked" )
+ Class<B> beanClass = (Class<B>) bean.getClass();
+ checkNotNull(methodName, "Impossible to execute null method in %s", beanClass.getName());
+ return new DefaultArgumentsAccessor( beanClass, true, methodName, bean );
}
}
Added: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MethodsTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MethodsTestCase.java?rev=1238950&view=auto
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MethodsTestCase.java (added)
+++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MethodsTestCase.java Wed Feb 1 06:59:54 2012
@@ -0,0 +1,198 @@
+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.Argument.argument;
+import static org.apache.commons.beanutils2.BeanUtils.on;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test case for invoking methods on instances.
+ */
+public class MethodsTestCase
+{
+
+ private TestBean testBean;
+
+ @Before
+ public void setUp()
+ {
+ testBean = new TestBean();
+ }
+
+ @After
+ public void tearDown()
+ {
+ testBean = null;
+ }
+
+ @Test
+ public void invokeMethodGetBooleanProperty()
+ throws Exception
+ {
+ Object value = on( testBean ).invokeMethod( "getBooleanProperty" ).withArguments().get();
+ assertNotNull( value );
+ assertTrue( value instanceof Boolean );
+ assertEquals( testBean.getBooleanProperty(), ( (Boolean) value ).booleanValue() );
+ }
+
+ @Test
+ public void invokeMethodSetBooleanProperty()
+ throws Exception
+ {
+ on( testBean ).invokeMethod( "setBooleanProperty" ).withArguments( argument( false ) );
+ assertFalse( testBean.getBooleanProperty() );
+ }
+
+ @Test
+ public void invokeMethodSetBooleanPropertyWithWrapper()
+ throws Exception
+ {
+ on( testBean ).invokeMethod( "setBooleanProperty" ).withArguments( argument( new Boolean( false ) ) );
+ assertFalse( testBean.getBooleanProperty() );
+ }
+
+ @Test
+ public void invokeMethodGetIntProperty()
+ throws Exception
+ {
+ Object value = on( testBean ).invokeMethod( "getIntProperty" ).withArguments().get();
+ assertNotNull( value );
+ assertTrue( value instanceof Integer );
+ assertEquals( testBean.getIntProperty(), ( (Integer) value ).intValue() );
+ }
+
+ @Test
+ public void invokeMethodSetIntProperty()
+ throws Exception
+ {
+ on( testBean ).invokeMethod( "setIntProperty" ).withArguments( argument( 47 ) );
+ assertEquals( testBean.getIntProperty(), 47 );
+ }
+
+ @Test
+ public void invokeMethodSetIntPropertyWithWrapper()
+ throws Exception
+ {
+ on( testBean ).invokeMethod( "setIntProperty" ).withArguments( argument( new Integer( 47 ) ) );
+ assertEquals( testBean.getIntProperty(), 47 );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void invokeMethodNull()
+ throws Exception
+ {
+ on( testBean ).invokeMethod( null );
+ }
+
+ @Test( expected = NoSuchMethodException.class )
+ public void invokeMethodNonExistent()
+ throws Exception
+ {
+ on( testBean ).invokeMethod( "nonExistent" ).withArguments();
+ }
+
+ @Test
+ public void invokeExactGetBooleanProperty()
+ throws Exception
+ {
+ Object value = on( testBean ).invokeExactMethod( "getBooleanProperty" ).withArguments().get();
+ assertNotNull( value );
+ assertTrue( value instanceof Boolean );
+ assertTrue( testBean.getBooleanProperty() == (Boolean) value );
+ }
+
+ @Test
+ public void invokeExactMethodSetBooleanProperty()
+ throws Exception
+ {
+ on( testBean ).invokeExactMethod( "setBooleanProperty" ).withArguments( argument( boolean.class, false ) );
+ assertFalse( testBean.getBooleanProperty() );
+ }
+
+ @Test( expected = NoSuchMethodException.class )
+ public void invokeExactMethodSetBooleanPropertyWithWrapper()
+ throws Exception
+ {
+ on( testBean ).invokeExactMethod( "setBooleanProperty" ).withArguments( argument( new Boolean( false ) ) );
+ }
+
+ @Test
+ public void invokeExactMethodSetBooleanPropertyWithWrapperAsPrimitive()
+ throws Exception
+ {
+ on( testBean ).invokeExactMethod( "setBooleanProperty" ).withArguments( argument( boolean.class,
+ new Boolean( false ) ) );
+ assertFalse( testBean.getBooleanProperty() );
+ }
+
+ @Test
+ public void invokeExactGetIntProperty()
+ throws Exception
+ {
+ Object value = on( testBean ).invokeExactMethod( "getIntProperty" ).withArguments().get();
+ assertNotNull( value );
+ assertTrue( value instanceof Integer );
+ assertEquals( testBean.getIntProperty(), ( (Integer) value ).intValue() );
+ }
+
+ @Test
+ public void invokeExactMethodSetIntProperty()
+ throws Exception
+ {
+ on( testBean ).invokeExactMethod( "setIntProperty" ).withArguments( argument( int.class, 47 ) );
+ assertEquals( testBean.getIntProperty(), 47 );
+ }
+
+ @Test( expected = NoSuchMethodException.class )
+ public void invokeExactMethodSetIntPropertyWithWrapper()
+ throws Exception
+ {
+ on( testBean ).invokeExactMethod( "setIntProperty" ).withArguments( argument( new Integer( 47 ) ) );
+ }
+
+ @Test
+ public void invokeExactMethodSetIntPropertyWithWrapperAsPrimitive()
+ throws Exception
+ {
+ on( testBean ).invokeExactMethod( "setIntProperty" ).withArguments( argument( int.class, new Integer( 47 ) ) );
+ assertEquals( testBean.getIntProperty(), 47 );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void invokeExactMethodNull()
+ throws Exception
+ {
+ on( testBean ).invokeExactMethod( null );
+ }
+
+ @Test( expected = NoSuchMethodException.class )
+ public void invokeExactMethodNonExistent()
+ throws Exception
+ {
+ on( testBean ).invokeExactMethod( "nonExistent" ).withArguments();
+ }
+
+}
Propchange: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MethodsTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MethodsTestCase.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MethodsTestCase.java
------------------------------------------------------------------------------
svn:mime-type = text/plain