@Override public void afterPropertiesSet() throws Exception { super.afterPropertiesSet(); Assert.notEmpty(fieldsUsedAsTargetMethodArguments, "fieldsUsedAsTargetMethodArguments must not be empty"); }
/** * 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 } }
/** * 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()); } }
/** * 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 public void testDelegation() throws Exception { delegator.setTargetMethod("getName"); delegator.afterPropertiesSet(); assertEquals(foo.getName(), delegator.invokeDelegateMethod()); }
/** * Exception scenario - target method is successfully invoked but throws a * {@link Throwable} (not an {@link Exception}). */ @Test public void testDelegateThrowable() throws Exception { delegator.setTargetMethod("failUgly"); delegator.afterPropertiesSet(); try { delegator.invokeDelegateMethod(); fail(); } catch (InvocationTargetThrowableWrapper expected) { assertEquals(Foo.UGLY_FAILURE_MESSAGE, expected.getCause().getMessage()); } }
/** * 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()); }
/** * 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()); }
/** * 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 } }
/** * 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()); }
@Override public void afterPropertiesSet() throws Exception { super.afterPropertiesSet(); Assert.notEmpty(fieldsUsedAsTargetMethodArguments, "fieldsUsedAsTargetMethodArguments must not be empty"); }