private void testRewrite(Method method, Object rewrittenObject, EntityId entityId,
Class<? extends Exception> expectedException, Object... args) throws NoSuchMethodException {
try {
method.invoke(rewrittenObject, args);
} catch (Exception e) {
if (!(e instanceof InvocationTargetException && expectedException.isAssignableFrom(e.getCause().getClass()))) {
Assert.fail(String.format("Got exception %s while expecting %s%s%s", e.getCause(),
ExceptionAuthorizationEnforcer.ExpectedException.class.getName(),
System.lineSeparator(), getFormattedStackTrace(e.getStackTrace())));
}
if (entityId != null) {
if (!ExceptionAuthorizationEnforcer.ExpectedException.class.isAssignableFrom(e.getCause().getClass())) {
Assert.fail(String.format("Exception %s is not assignable from %s to match entity %s", e.getCause(),
ExceptionAuthorizationEnforcer.ExpectedException.class.getName(), entityId));
}
ExceptionAuthorizationEnforcer.ExpectedException exception =
(ExceptionAuthorizationEnforcer.ExpectedException) e.getCause();
if (!exception.getEntityId().equals(entityId)) {
Assert.fail(String.format("Expected %s with entity %s but found %s",
ExceptionAuthorizationEnforcer.ExpectedException.class.getSimpleName(),
entityId, exception.getEntityId()));
}
}
}
}