public Throwable trigger(final Class<?> from, final Throwable toThrow) { if (null == from) throw new NullPointerException(); wrap(toThrow); // test if wrappable // DON'T put wrap(toThrow)! We want the stack trace of the call to // check(*), not of the call to this method! return throwables.put(from, toThrow); }
@Test public void testGetInputSocketMustForwardTheCallToTheGivenController() { final Throwable expected = new RuntimeException(); trigger(MockController.class, expected); try { getArchiveInputSocket(); fail(); } catch (final RuntimeException got) { if (!contains(got, expected)) throw got; } }
@Test public void testGetOutputSocketMustForwardTheCallToTheGivenController() { final Throwable expected = new RuntimeException(); trigger(MockController.class, expected); try { getArchiveOutputSocket(); fail(); } catch (final RuntimeException got) { if (!contains(got, expected)) throw got; } }
@Override void cat(final TestInputStream in, final TestOutputStream out) throws IOException { final ThrowControl control = TestConfig.get().getThrowControl(); control.trigger(ThrowingInputStream.class, expected); final ThrowingInputStream tis = new ThrowingInputStream(in, control); try { Streams.cat(tis, out); fail(); } catch (final IOException got) { if (!contains(got, expected)) throw got; } catch (final RuntimeException got) { if (!contains(got, expected)) throw got; } catch (final Error got) { if (!contains(got, expected)) throw got; } Streams.cat(in, out); } }.run();
private <X extends Throwable> void check( final Class<?> thiz, final Class<X> throwz) throws X { final Throwable toThrow = throwables.remove(thiz); if (null != toThrow) if (throwz.isInstance(toThrow)) throw throwz.cast(wrap(toThrow)); else throwables.put(thiz, toThrow); // restore // No match, now recursively check interfaces first and then super // classes. // This may result in redundant checks for interfaces. for (final Class<?> ic : thiz.getInterfaces()) check(ic, throwz); final Class<?> sc = thiz.getSuperclass(); if (null != sc) check(sc, throwz); } }
@Override void cat(final TestInputStream in, final TestOutputStream out) throws IOException { final ThrowControl control = TestConfig.get().getThrowControl(); control.trigger(ThrowingOutputStream.class, expected); final ThrowingOutputStream tos = new ThrowingOutputStream(out); Streams.cat(in, out); try { Streams.cat(in, tos); fail(); } catch (final IOException got) { if (!contains(got, expected)) throw got; } catch (final RuntimeException got) { if (!contains(got, expected)) throw got; } catch (final Error got) { if (!contains(got, expected)) throw got; } } }.run();
if (!contains(got, expected)) throw got; } finally {
if (!contains(got, expected)) throw got; } finally {