protected void prepare(final IOException failure)
throws Exception
{
// faking IndexUpdater that will fail with given exception for given "failing" repository
final IndexUpdater realUpdater = lookup(IndexUpdater.class);
// predicate to match invocation when the arguments are for the failingRepository
final Predicate<Object[]> fetchAndUpdateIndexMethodArgumentsPredicate = new Predicate<Object[]>()
{
@Override
public boolean apply(@Nullable Object[] input) {
if (input != null) {
final IndexUpdateRequest req = (IndexUpdateRequest) input[0];
if (req != null) {
return req.getIndexingContext().getId().startsWith(failingRepository.getId());
}
}
return false;
}
};
// method we want to fail and count
final Method fetchAndUpdateIndexMethod =
IndexUpdater.class.getMethod("fetchAndUpdateIndex", new Class[]{IndexUpdateRequest.class});
fetchFailingInvocationHandler =
new FailingInvocationHandler(new PassThruInvocationHandler(realUpdater), fetchAndUpdateIndexMethod,
fetchAndUpdateIndexMethodArgumentsPredicate, failure);
fetchCountingInvocationHandler =
new CountingInvocationHandler(fetchFailingInvocationHandler, fetchAndUpdateIndexMethod);
final IndexUpdater fakeUpdater =
(IndexUpdater) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{IndexUpdater.class},
fetchCountingInvocationHandler);
final Scanner fakeScanner = new Scanner()
{