Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master 5bee1915a -> 6ebf82a63


Adds Exceptions.getFirstThrowableMatching

Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/474d95ba
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/474d95ba
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/474d95ba

Branch: refs/heads/master
Commit: 474d95ba223cb0746198b215cd94386db5dcc36d
Parents: ddcebe8
Author: Aled Sage <[email protected]>
Authored: Thu Oct 22 20:58:08 2015 +0100
Committer: Aled Sage <[email protected]>
Committed: Thu Oct 22 20:58:08 2015 +0100

----------------------------------------------------------------------
 .../brooklyn/util/exceptions/Exceptions.java    |  5 +
 .../util/exceptions/ExceptionsTest.java         | 97 +++++++++++++++++++-
 2 files changed, 99 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/474d95ba/utils/common/src/main/java/org/apache/brooklyn/util/exceptions/Exceptions.java
----------------------------------------------------------------------
diff --git 
a/utils/common/src/main/java/org/apache/brooklyn/util/exceptions/Exceptions.java
 
b/utils/common/src/main/java/org/apache/brooklyn/util/exceptions/Exceptions.java
index c185890..9439faa 100644
--- 
a/utils/common/src/main/java/org/apache/brooklyn/util/exceptions/Exceptions.java
+++ 
b/utils/common/src/main/java/org/apache/brooklyn/util/exceptions/Exceptions.java
@@ -124,6 +124,11 @@ public class Exceptions {
         return (T) Iterables.tryFind(getCausalChain(from), 
instanceOf(clazz)).orNull();
     }
 
+    /** returns the first exception that matches the filter, or null */
+    public static Throwable getFirstThrowableMatching(Throwable from, 
Predicate<? super Throwable> filter) {
+        return Iterables.tryFind(getCausalChain(from), filter).orNull();
+    }
+
     /** returns the first exception in the call chain which is not of common 
uninteresting types
      * (ie excluding ExecutionException and PropagatedRuntimeExceptions); 
      * or the original throwable if all are uninteresting 

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/474d95ba/utils/common/src/test/java/org/apache/brooklyn/util/exceptions/ExceptionsTest.java
----------------------------------------------------------------------
diff --git 
a/utils/common/src/test/java/org/apache/brooklyn/util/exceptions/ExceptionsTest.java
 
b/utils/common/src/test/java/org/apache/brooklyn/util/exceptions/ExceptionsTest.java
index 13e077f..0e7d590 100644
--- 
a/utils/common/src/test/java/org/apache/brooklyn/util/exceptions/ExceptionsTest.java
+++ 
b/utils/common/src/test/java/org/apache/brooklyn/util/exceptions/ExceptionsTest.java
@@ -18,23 +18,114 @@
  */
 package org.apache.brooklyn.util.exceptions;
 
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+
 import java.util.ConcurrentModificationException;
 import java.util.concurrent.ExecutionException;
 
 import org.apache.brooklyn.util.collections.MutableSet;
-import org.apache.brooklyn.util.exceptions.Exceptions;
-import org.apache.brooklyn.util.exceptions.ExceptionsTest;
-import org.apache.brooklyn.util.exceptions.PropagatedRuntimeException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
+import com.google.common.base.Predicates;
+
 public class ExceptionsTest {
 
     private static final Logger log = 
LoggerFactory.getLogger(ExceptionsTest.class);
     
     @Test
+    public void testPropagateRuntimeException() throws Exception {
+        NullPointerException tothrow = new NullPointerException("simulated");
+        try {
+            throw Exceptions.propagate(tothrow);
+        } catch (NullPointerException e) {
+            assertEquals(e, tothrow);
+        }
+    }
+    
+    @Test
+    public void testPropagateCheckedException() throws Exception {
+        Exception tothrow = new Exception("simulated");
+        try {
+            throw Exceptions.propagate(tothrow);
+        } catch (RuntimeException e) {
+            assertEquals(e.getCause(), tothrow);
+        }
+    }
+    
+    @Test
+    public void testPropagateIfFatalPropagatesInterruptedException() throws 
Exception {
+        InterruptedException tothrow = new InterruptedException("simulated");
+        try {
+            Exceptions.propagateIfFatal(tothrow);
+            fail();
+        } catch (RuntimeException e) {
+            assertTrue(Thread.interrupted()); // note this clears the 
interrupted flag as well
+            assertEquals(e.getCause(), tothrow);
+        }
+    }
+    
+    @Test
+    public void testPropagateIfFatalPropagatesRuntimeInterruptedException() 
throws Exception {
+        RuntimeInterruptedException tothrow = new 
RuntimeInterruptedException(new InterruptedException("simulated"));
+        try {
+            Exceptions.propagateIfFatal(tothrow);
+            fail();
+        } catch (RuntimeInterruptedException e) {
+            assertTrue(Thread.interrupted()); // note this clears the 
interrupted flag as well
+            assertEquals(e, tothrow);
+        }
+    }
+    
+    @Test
+    public void testPropagateIfFatalPropagatesError() throws Exception {
+        Error tothrow = new Error();
+        try {
+            Exceptions.propagateIfFatal(tothrow);
+            fail();
+        } catch (Error e) {
+            assertEquals(e, tothrow);
+        }
+    }
+    
+    @Test
+    public void testPropagateIfFatalDoesNotPropagatesNormalException() throws 
Exception {
+        Exception e = new Exception();
+        Exceptions.propagateIfFatal(e);
+        
+        RuntimeException re = new RuntimeException();
+        Exceptions.propagateIfFatal(re);
+        
+        Throwable t = new Throwable();
+        Exceptions.propagateIfFatal(t);
+    }
+    
+    @Test
+    public void testGetFirstThrowableOfType() throws Exception {
+        NullPointerException npe = new NullPointerException("simulated");
+        IllegalStateException ise = new IllegalStateException("simulated", 
npe);
+        
+        assertEquals(Exceptions.getFirstThrowableOfType(ise, 
IllegalStateException.class), ise);
+        assertEquals(Exceptions.getFirstThrowableOfType(ise, 
NullPointerException.class), npe);
+        assertNull(Exceptions.getFirstThrowableOfType(ise, 
IndexOutOfBoundsException.class));
+    }
+    
+    @Test
+    public void testGetFirstThrowableMatching() throws Exception {
+        NullPointerException npe = new NullPointerException("simulated");
+        IllegalStateException ise = new IllegalStateException("simulated", 
npe);
+        
+        assertEquals(Exceptions.getFirstThrowableMatching(ise, 
Predicates.instanceOf(IllegalStateException.class)), ise);
+        assertEquals(Exceptions.getFirstThrowableMatching(ise, 
Predicates.instanceOf(NullPointerException.class)), npe);
+        assertNull(Exceptions.getFirstThrowableMatching(ise, 
Predicates.alwaysFalse()));
+    }
+    
+    @Test
     public void test12CollapseCompound() throws Exception {
         RuntimeException e = Exceptions.create("test1", MutableSet.of(new 
IllegalStateException("test2"), new IllegalStateException("test3")));
         assert12StandardChecks(e, false);

Reply via email to