/** * @return return value of the target method. */ @Override public T read() throws Exception { return invokeDelegateMethod(); }
/** * Invoke the delegate method and return the result. * * @see ItemProcessor#process(Object) */ @Override public O process(I item) throws Exception { return invokeDelegateMethodWithArgument(item); }
/** * Create a new configured instance of {@link MethodInvoker}. */ private MethodInvoker createMethodInvoker(Object targetObject, String targetMethod) { HippyMethodInvoker invoker = new HippyMethodInvoker(); invoker.setTargetObject(targetObject); invoker.setTargetMethod(targetMethod); invoker.setArguments(arguments); return invoker; }
@Test public void testTwoArgsOfSameTypeWithInexactMatch() throws Exception { HippyMethodInvoker invoker = new HippyMethodInvoker(); invoker.setTargetMethod("duplicate"); invoker.setTargetObject(new PlainPojo()); invoker.setArguments(new Object[] { "2", "foo" }); invoker.prepare(); assertEquals("foo.2", invoker.invoke()); }
@Test public void testEmptyParametersEmptyArgs() throws Exception { TestMethodAdapter adapter = new TestMethodAdapter(); adapter.setTargetMethod("empty"); adapter.setTargetObject(new PlainPojo()); assertEquals(".", adapter.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()); }
/** * 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 { super.afterPropertiesSet(); Assert.notEmpty(fieldsUsedAsTargetMethodArguments, "fieldsUsedAsTargetMethodArguments must not be empty"); }
@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()); }
@Override public void write(List<? extends T> items) throws Exception { for (T item : items) { invokeDelegateMethodWithArgument(item); } }
@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); }
@Override public String getMessage(double value, String input) { try { return invokeDelegateMethodWithArguments(new Object[] { value, input }); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new IllegalStateException(e); } }
@SuppressWarnings("unused") public void processNameValuePair(String name, int value) { processedFooNameValuePairs.add(new Foo(name, value)); }
@Test public void testVanillaMethodInvoker() throws Exception { TestMethodAdapter adapter = new TestMethodAdapter(); adapter.setTargetMethod("handle"); adapter.setTargetObject(new PlainPojo()); assertEquals("2.0.foo", adapter.getMessage(2, "foo")); }
/** * 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()); }
public String getMessage() { try { return invokeDelegateMethodWithArguments(new Object[0]); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new IllegalStateException(e); } }
@Test public void testEmptyParameters() throws Exception { TestMethodAdapter adapter = new TestMethodAdapter(); adapter.setTargetMethod("empty"); adapter.setTargetObject(new PlainPojo()); assertEquals(".", adapter.getMessage(2, "foo")); }
@Test public void testWrongOrder() throws Exception { TestMethodAdapter adapter = new TestMethodAdapter(); adapter.setTargetMethod("disorder"); adapter.setTargetObject(new PlainPojo()); assertEquals("2.0.foo", adapter.getMessage(2, "foo")); }
@Test public void testMissingArgument() throws Exception { TestMethodAdapter adapter = new TestMethodAdapter(); adapter.setTargetMethod("missing"); adapter.setTargetObject(new PlainPojo()); assertEquals("foo.foo", adapter.getMessage(2, "foo")); }