MethodInvocationDataStoreOperation implements DataStoreOperation<Object, K, E> { @Override public Object execute(DataStore<K, E> store, RepositoryConfiguration configuration, Invocation invocation) { final Object result; try { log.info("Invoking method " + method + " to handle invocation " + invocation); result = method.invoke(instance, invocation.getArguments()); } catch (IllegalAccessException e) { throw new DataOperationExecutionException("Failed to access target method: " + method, e); } catch (InvocationTargetException e) { throw new DataOperationExecutionException("Method call resulted in internal error: " + method, e.getTargetException()); } return result; } MethodInvocationDataStoreOperation(Object instance, Method method); @Override Object execute(DataStore<K, E> store, RepositoryConfiguration configuration, Invocation invocation); Object getInstance(); Method getMethod(); @Override String toString();  }
@Test(expectedExceptions = DataOperationExecutionException.class) public void testAccessingPrivateMethod() throws Exception { final MethodInvocationDataStoreOperation<Object, Object> operation = new MethodInvocationDataStoreOperation<>( new MappedClass(null), MappedClass.class.getDeclaredMethod("privateMethod")); operation.execute(null, null, new ImmutableInvocation(null, new Object[]{})); }
@Test(expectedExceptions = DataOperationExecutionException.class) public void testAccessingErrorThrowingMethod() throws Exception { final MethodInvocationDataStoreOperation<Object, Object> operation = new MethodInvocationDataStoreOperation<>( new MappedClass(null), MappedClass.class.getDeclaredMethod("errorThrowingMethod")); operation.execute(null, null, new ImmutableInvocation(null, new Object[]{})); }
@Test public void testCallingNormalMethod() throws Exception { final Object value = new Object(); final MethodInvocationDataStoreOperation<Object, Object> operation = new MethodInvocationDataStoreOperation<>( new MappedClass(value), MappedClass.class.getDeclaredMethod("validMethod")); final Object result = operation.execute(null, null, new ImmutableInvocation(null, new Object[]{})); assertThat(result, is(value)); }