You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by st...@apache.org on 2011/08/03 17:00:20 UTC

svn commit: r1153526 - /maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/apache/maven/tck/TckMatchers.java

Author: stephenc
Date: Wed Aug  3 15:00:20 2011
New Revision: 1153526

URL: http://svn.apache.org/viewvc?rev=1153526&view=rev
Log:
forgot to commit this new matcher

Modified:
    maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/apache/maven/tck/TckMatchers.java

Modified: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/apache/maven/tck/TckMatchers.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/apache/maven/tck/TckMatchers.java?rev=1153526&r1=1153525&r2=1153526&view=diff
==============================================================================
--- maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/apache/maven/tck/TckMatchers.java (original)
+++ maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/apache/maven/tck/TckMatchers.java Wed Aug  3 15:00:20 2011
@@ -86,6 +86,17 @@ public class TckMatchers
         return new RunsForLongerThan( ms );
     }
 
+    /**
+     * A matcher that verifies that the a root cause of an exception is of the specified type.
+     *
+     * @param cause the type of exception that caused this.
+     * @return A matcher that verifies that the a root cause of an exception is of the specified type.
+     */
+    public static Matcher<Throwable> hasCause( Class<? extends Throwable> cause )
+    {
+        return new HasCause( cause );
+    }
+
     private static class HasDefaultConstructor
         extends BaseMatcher<Class<?>>
     {
@@ -198,4 +209,30 @@ public class TckMatchers
             description.appendText( "takes longer than " ).appendValue( duration ).appendText( "ms to complete" );
         }
     }
+
+    private static class HasCause
+        extends BaseMatcher<Throwable>
+    {
+        private final Class<? extends Throwable> cause;
+
+        public HasCause( Class<? extends Throwable> cause )
+        {
+            this.cause = cause;
+        }
+
+        public boolean matches( Object item )
+        {
+            Throwable throwable = (Throwable) item;
+            while ( throwable != null && !cause.isInstance( throwable ) )
+            {
+                throwable = throwable.getCause();
+            }
+            return cause.isInstance( throwable );
+        }
+
+        public void describeTo( Description description )
+        {
+            description.appendText( "was caused by a " ).appendValue( cause ).appendText( " being thrown" );
+        }
+    }
 }