/** * Null argument value doesn't cause trouble when validating method * signature. */ @Test public void testDelegationWithCheckedNullArgument() throws Exception { delegator.setTargetMethod("setName"); delegator.setArguments(new Object[] { null }); delegator.afterPropertiesSet(); delegator.invokeDelegateMethod(); assertNull(foo.getName()); }
/** * Invokes the target method with given arguments. * * @param args arguments for the invoked method * @return object returned by invoked method * * @throws Exception exception thrown when executing the delegate method. */ protected T invokeDelegateMethodWithArguments(Object[] args) throws Exception { MethodInvoker invoker = createMethodInvoker(targetObject, targetMethod); invoker.setArguments(args); return doInvoke(invoker); }
@Override public void afterPropertiesSet() throws Exception { Assert.notNull(targetObject, "targetObject must not be null"); Assert.hasLength(targetMethod, "targetMethod must not be empty"); Assert.state(targetClassDeclaresTargetMethod(), "target class must declare a method with matching name and parameter types"); }
@Before public void setUp() throws Exception { delegator.setTargetObject(foo); delegator.setArguments(null); }
/** * Exception scenario - target method is called with incorrect number of * arguments. */ @Test public void testTooFewArguments() throws Exception { delegator.setTargetMethod("setName"); delegator.afterPropertiesSet(); try { // single argument expected but none provided delegator.invokeDelegateMethod(); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException e) { // expected } }
/** * Regular use - calling methods directly and via delegator leads to same * results */ @Test @Ignore //FIXME public void testDelegationWithMultipleArguments() throws Exception { FooService fooService = new FooService(); delegator.setTargetObject(fooService); delegator.setTargetMethod("processNameValuePair"); delegator.afterPropertiesSet(); final String FOO_NAME = "fooName"; final int FOO_VALUE = 12345; delegator.invokeDelegateMethodWithArguments(new Object[] { FOO_NAME, FOO_VALUE }); Foo foo = fooService.getProcessedFooNameValuePairs().get(0); assertEquals(FOO_NAME, foo.getName()); assertEquals(FOO_VALUE, foo.getValue()); }
/** * Exception scenario - target method is called with invalid arguments. */ @Test public void testInvalidArgumentsForExistingMethod() throws Exception { delegator.setTargetMethod("setName"); delegator.afterPropertiesSet(); try { delegator.invokeDelegateMethodWithArgument(new Object()); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException e) { // expected } }
/** * Exception scenario - incorrect static arguments set. */ @Test public void testIncorrectNumberOfStaticArguments() throws Exception { delegator.setTargetMethod("setName"); // incorrect argument count delegator.setArguments(new Object[] { "first", "second" }); try { delegator.afterPropertiesSet(); fail(); } catch (IllegalStateException e) { // expected } // correct argument count, but invalid argument type delegator.setArguments(new Object[] { new Object() }); try { delegator.afterPropertiesSet(); fail(); } catch (IllegalStateException e) { // expected } }
@Override public void afterPropertiesSet() throws Exception { super.afterPropertiesSet(); Assert.notEmpty(fieldsUsedAsTargetMethodArguments, "fieldsUsedAsTargetMethodArguments must not be empty"); }
MethodInvoker invoker = createMethodInvoker(targetObject, targetMethod);
@Test public void testTooManyArguments() throws Exception { delegator.setTargetMethod("setName"); // single argument expected but two provided delegator.invokeDelegateMethodWithArguments(new Object[] { "name", "anotherName" }); assertEquals("name", foo.getName()); }
/** * Regular use - calling methods directly and via delegator leads to same * results */ @Test public void testDelegation() throws Exception { delegator.setTargetMethod("getName"); delegator.afterPropertiesSet(); assertEquals(foo.getName(), delegator.invokeDelegateMethod()); }
@Override public void afterPropertiesSet() throws Exception { super.afterPropertiesSet(); Assert.notEmpty(fieldsUsedAsTargetMethodArguments, "fieldsUsedAsTargetMethodArguments must not be empty"); }
MethodInvoker invoker = createMethodInvoker(targetObject, targetMethod);
/** * Exception scenario - target method is successfully invoked but throws * exception. Such 'business' exception should be re-thrown as is (without * wrapping). */ @Test public void testDelegateException() throws Exception { delegator.setTargetMethod("fail"); delegator.afterPropertiesSet(); try { delegator.invokeDelegateMethod(); fail(); } catch (Exception expected) { assertEquals(Foo.FAILURE_MESSAGE, expected.getMessage()); } }
/** * Regular use - calling methods directly and via delegator leads to same * results */ @Test public void testDelegationWithArgument() throws Exception { delegator.setTargetMethod("setName"); final String NEW_FOO_NAME = "newFooName"; delegator.afterPropertiesSet(); delegator.invokeDelegateMethodWithArgument(NEW_FOO_NAME); assertEquals(NEW_FOO_NAME, foo.getName()); // using the arguments setter should work equally well foo.setName("foo"); assertTrue(!foo.getName().equals(NEW_FOO_NAME)); delegator.setArguments(new Object[] { NEW_FOO_NAME }); delegator.afterPropertiesSet(); delegator.invokeDelegateMethod(); assertEquals(NEW_FOO_NAME, foo.getName()); }
/** * Invoker the target method with arguments set by * {@link #setArguments(Object[])}. * * @return object returned by invoked method * * @throws Exception exception thrown when executing the delegate method. */ protected T invokeDelegateMethod() throws Exception { MethodInvoker invoker = createMethodInvoker(targetObject, targetMethod); invoker.setArguments(arguments); return doInvoke(invoker); }
@Override public void afterPropertiesSet() throws Exception { Assert.notNull(targetObject, "targetObject must not be null"); Assert.hasLength(targetMethod, "targetMethod must not be empty"); Assert.state(targetClassDeclaresTargetMethod(), "target class must declare a method with matching name and parameter types"); }
/** * Exception scenario - target method is not declared by target object. */ @Test public void testInvalidMethodName() throws Exception { delegator.setTargetMethod("not-existing-method-name"); try { delegator.afterPropertiesSet(); fail("Expected IllegalStateException"); } catch (IllegalStateException e) { // expected } try { delegator.invokeDelegateMethod(); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException e) { // expected } }
/** * Invokes the target method with given argument. * * @param object argument for the target method * @return object returned by target method * * @throws Exception exception thrown when executing the delegate method. */ protected T invokeDelegateMethodWithArgument(Object object) throws Exception { MethodInvoker invoker = createMethodInvoker(targetObject, targetMethod); invoker.setArguments(new Object[] { object }); return doInvoke(invoker); }