TransferableExceptionWrapper extends RuntimeException { public static TransferableExceptionWrapper wrap(Throwable exception) { if (exception == null) { return null; } if (exception instanceof TransferableExceptionWrapper) { return (TransferableExceptionWrapper)exception; } else { return new TransferableExceptionWrapper(exception); } } TransferableExceptionWrapper(Throwable wrapped); static TransferableExceptionWrapper wrap(Throwable exception); String getOriginalClassName(); String getOriginalMessage(); @Override String getMessage();  }
@Test public void testWrapSingle() throws ClassNotFoundException { IOException wrapped = new IOException("testWrapSimple-Message"); TransferableExceptionWrapper wrapper = TransferableExceptionWrapper.wrap(wrapped); checkWrapper(wrapped, wrapper); }
@Test public void testWrapWithCauses() throws ClassNotFoundException { IllegalArgumentException wrappedCause = new IllegalArgumentException("testWrapWithCauses-cause"); String message = "testWrapWithCauses-Message"; IOException wrapped = new IOException(message); wrapped.initCause(wrappedCause); TransferableExceptionWrapper wrapper = TransferableExceptionWrapper.wrap(wrapped); checkWrapper(wrapped, wrapper); checkWrapperOrSame(wrapped.getCause(), wrapper.getCause()); }
@Test public void testWrapNotSerializable() throws ClassNotFoundException { Exception wrapped = new NotSerializableTestException("testWrapNotSerializable-Message"); TransferableExceptionWrapper wrapper = TransferableExceptionWrapper.wrap(wrapped); checkWrapper(wrapped, wrapper); }
@Test public void testWithSuppressed() throws Exception { Assume.assumeTrue("This test is for Java7 only", JavaVersion.current().isJava7Compatible()); IOException wrapped = new IOException("testWithSuppressed-Message"); Method addSuppressed = ReflectionUtils.tryGetPublicMethod( wrapped.getClass(), "addSuppressed", Void.TYPE, Throwable.class); assertNotNull("Expected to have addSuppressed in Java 7", addSuppressed); Exception suppressed1 = new NotSerializableTestException("testWithSuppressed-suppressed1"); Exception suppressed2 = new IOException("testWithSuppressed-suppressed2"); addSuppressed.invoke(wrapped, suppressed1); addSuppressed.invoke(wrapped, suppressed2); TransferableExceptionWrapper wrapper = TransferableExceptionWrapper.wrap(wrapped); checkWrapper(wrapped, wrapper); Method getSuppressed = ReflectionUtils.tryGetPublicMethod( wrapper.getClass(), "getSuppressed", Throwable[].class); assertNotNull("Expected to have getSuppressed in Java 7", getSuppressed); Throwable[] wrappedSuppressed = (Throwable[])getSuppressed.invoke(wrapper); assertEquals("Must wrapp suppressed exceptions as well.", 2, wrappedSuppressed.length); checkWrapperOrSame(suppressed1, wrappedSuppressed[0]); checkWrapperOrSame(suppressed2, wrappedSuppressed[1]); }