Issue #2075 cleanup MultiException to better use suppressed (#2105)

* Issue #2075 cleanup multiexceptio to better use suppressed
* Update MultiException.java fixes from review
Signed-off-by: Greg Wilkins <gregw@webtide.com>
This commit is contained in:
Greg Wilkins 2018-01-23 08:34:07 +01:00
parent ce09e48b5b
commit 9713dbccd5
2 changed files with 74 additions and 13 deletions

View File

@ -176,6 +176,34 @@ public class MultiException extends Exception
} }
} }
/* ------------------------------------------------------------ */
/** Throw an Exception, potentially with suppress.
* If this multi exception is empty then no action is taken. If the first
* exception added is an Error or Exception, then it is throw with
* any additional exceptions added as suppressed. Otherwise a MultiException
* is thrown, with all exceptions added as suppressed.
* @exception Exception the Error or Exception if at least one is added.
*/
public void ifExceptionThrowSuppressed()
throws Exception
{
if(nested == null || nested.size()==0)
return;
Throwable th=nested.get(0);
if (!Error.class.isInstance(th) && !Exception.class.isInstance(th))
th = new MultiException(Collections.emptyList());
for (Throwable s : nested)
if (s!=th)
th.addSuppressed(s);
if (Error.class.isInstance(th))
throw (Error)th;
throw (Exception)th;
}
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
@Override @Override
public String toString() public String toString()

View File

@ -19,9 +19,11 @@
package org.eclipse.jetty.util; package org.eclipse.jetty.util;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import org.junit.Assert; import org.hamcrest.Matchers;
import org.junit.Test; import org.junit.Test;
import java.io.IOException; import java.io.IOException;
@ -38,6 +40,7 @@ public class MultiExceptionTest
me.ifExceptionThrow(); me.ifExceptionThrow();
me.ifExceptionThrowMulti(); me.ifExceptionThrowMulti();
me.ifExceptionThrowRuntime(); me.ifExceptionThrowRuntime();
me.ifExceptionThrowSuppressed();
assertEquals("Stack trace should not be filled out", 0, me.getStackTrace().length); assertEquals("Stack trace should not be filled out", 0, me.getStackTrace().length);
} }
@ -81,6 +84,16 @@ public class MultiExceptionTest
assertTrue(e.getCause()==io); assertTrue(e.getCause()==io);
} }
try
{
me.ifExceptionThrowSuppressed();
assertTrue(false);
}
catch(IOException e)
{
assertTrue(e==io);
}
me = new MultiException(); me = new MultiException();
RuntimeException run = new RuntimeException("one"); RuntimeException run = new RuntimeException("one");
me.add(run); me.add(run);
@ -98,10 +111,10 @@ public class MultiExceptionTest
assertEquals("Stack trace should not be filled out", 0, me.getStackTrace().length); assertEquals("Stack trace should not be filled out", 0, me.getStackTrace().length);
} }
private MultiException multiExceptionWithTwo() { private MultiException multiExceptionWithIoRt() {
MultiException me = new MultiException(); MultiException me = new MultiException();
IOException io = new IOException("one"); IOException io = new IOException("one");
RuntimeException run = new RuntimeException("one"); RuntimeException run = new RuntimeException("two");
me.add(io); me.add(io);
me.add(run); me.add(run);
assertEquals(2,me.size()); assertEquals(2,me.size());
@ -110,10 +123,22 @@ public class MultiExceptionTest
return me; return me;
} }
private MultiException multiExceptionWithRtIo() {
MultiException me = new MultiException();
RuntimeException run = new RuntimeException("one");
IOException io = new IOException("two");
me.add(run);
me.add(io);
assertEquals(2,me.size());
assertEquals("Stack trace should not be filled out", 0, me.getStackTrace().length);
return me;
}
@Test @Test
public void testTwo() throws Exception public void testTwo() throws Exception
{ {
MultiException me = multiExceptionWithTwo(); MultiException me = multiExceptionWithIoRt();
try try
{ {
me.ifExceptionThrow(); me.ifExceptionThrow();
@ -125,7 +150,7 @@ public class MultiExceptionTest
assertTrue(e.getStackTrace().length > 0); assertTrue(e.getStackTrace().length > 0);
} }
me = multiExceptionWithTwo(); me = multiExceptionWithIoRt();
try try
{ {
me.ifExceptionThrowMulti(); me.ifExceptionThrowMulti();
@ -137,7 +162,7 @@ public class MultiExceptionTest
assertTrue(e.getStackTrace().length > 0); assertTrue(e.getStackTrace().length > 0);
} }
me = multiExceptionWithTwo(); me = multiExceptionWithIoRt();
try try
{ {
me.ifExceptionThrowRuntime(); me.ifExceptionThrowRuntime();
@ -149,11 +174,7 @@ public class MultiExceptionTest
assertTrue(e.getStackTrace().length > 0); assertTrue(e.getStackTrace().length > 0);
} }
RuntimeException run = new RuntimeException("one"); me = multiExceptionWithRtIo();
me = new MultiException();
me.add(run);
me.add(run);
try try
{ {
me.ifExceptionThrowRuntime(); me.ifExceptionThrowRuntime();
@ -162,9 +183,21 @@ public class MultiExceptionTest
catch(RuntimeException e) catch(RuntimeException e)
{ {
assertTrue(e.getCause() instanceof MultiException); assertTrue(e.getCause() instanceof MultiException);
Assert.assertEquals(e.getCause().getCause(), run);
assertTrue(e.getStackTrace().length > 0); assertTrue(e.getStackTrace().length > 0);
} }
me = multiExceptionWithRtIo();
try
{
me.ifExceptionThrowSuppressed();
assertTrue(false);
}
catch(RuntimeException e)
{
assertNull(e.getCause());
assertEquals(1,e.getSuppressed().length,1);
assertEquals(IOException.class,e.getSuppressed()[0].getClass());
}
} }
@Test @Test