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
public String toString()

View File

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