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/05 21:44:16 UTC
svn commit: r1154362 - in /maven/sandbox/trunk/plexus-utils-commons-bridge:
plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/ExceptionUtils.java
plexus-utils-tck/src/test/java/org/codehaus/plexus/util/ExceptionUtilsTest.java
Author: struberg
Date: Fri Aug 5 19:44:16 2011
New Revision: 1154362
URL: http://svn.apache.org/viewvc?rev=1154362&view=rev
Log:
MSANDBOX-51 ExceptionUtils continued
Modified:
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/ExceptionUtils.java
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/ExceptionUtilsTest.java
Modified: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/ExceptionUtils.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/ExceptionUtils.java?rev=1154362&r1=1154361&r2=1154362&view=diff
==============================================================================
--- maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/ExceptionUtils.java (original)
+++ maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/ExceptionUtils.java Fri Aug 5 19:44:16 2011
@@ -24,6 +24,7 @@ import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;
+import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
/**
@@ -222,31 +223,116 @@ public class ExceptionUtils
return rootCause;
}
+ /**
+ * Determine the number of causale Throwables. That is the amount of
+ * all Throwables in the exception chain.
+ * @param throwable
+ * @return the amount of Throwables in the exception chain.
+ */
public static int getThrowableCount( Throwable throwable )
{
- System.out.println("TODO IMPLEMENT");
- //X TODO implement
- return -1;
+ Throwable[] throwables = getThrowables( throwable );
+
+ if ( throwables != null )
+ {
+ return throwables.length;
+ }
+ return 0;
}
+ /**
+ * Get an array of all Throwables in the 'cause' chain.
+ * This will also evaluate special cause rools for SQLExceptions and
+ * TargetInvocationExceptions.
+ *
+ * @param throwable
+ * @return array with all causal Throwables
+ */
public static Throwable[] getThrowables( Throwable throwable )
{
- System.out.println("TODO IMPLEMENT");
- //X TODO implement
- return null;
+ ArrayList<Throwable> throwables = new ArrayList<Throwable>();
+
+
+ Throwable rootCause = throwable;
+ int depth = 0;
+
+ while ( rootCause != null )
+ {
+ if ( depth >= MAX_ROOT_CAUSE_DEPTH )
+ {
+ // maximum depth level reached!
+ break;
+ }
+
+ throwables.add( rootCause );
+
+ Throwable nextRootCause = getCause( rootCause );
+
+ if ( nextRootCause == null )
+ {
+ break;
+ }
+
+ rootCause = nextRootCause;
+ depth++;
+ }
+
+ return throwables.toArray( new Throwable[ throwables.size() ] );
+
}
- public static int indexOfThrowable( Throwable throwable, Class type )
+ /**
+ * Determines all the nested Throwables and calculate the index of the
+ * Throwable with the given type
+ * @param throwable
+ * @param type
+ * @return the index of the type in the Throwable chain, or <code>-1</code> if it isn't contained.
+ *
+ * @see #indexOfThrowable(Throwable, Class, int)
+ */
+ public static int indexOfThrowable( Throwable throwable, Class<? extends Throwable> type )
{
- System.out.println("TODO IMPLEMENT");
- //X TODO implement
- return -1;
+ return indexOfThrowable( throwable, type, 0 );
}
- public static int indexOfThrowable( Throwable throwable, Class type, int fromIndex )
+ /**
+ * Determines all the nested Throwables and calculate the index of the
+ * Throwable with the given type starting with the given fromIndex
+ * @param throwable
+ * @param type
+ * @param fromIndex the index to start with
+ * @return the index of the type in the Throwable chain, or <code>-1</code> if it isn't contained.
+ *
+ * @see #indexOfThrowable(Throwable, Class)
+ */
+ public static int indexOfThrowable( Throwable throwable, Class<? extends Throwable> type, int fromIndex )
{
- System.out.println("TODO IMPLEMENT");
- //X TODO implement
+ if ( throwable == null )
+ {
+ // this is how the old plexus method failed ...
+ throw new IndexOutOfBoundsException( "Throwable to check must not be null" );
+ }
+
+ if ( type != null)
+ {
+ Throwable[] throwables = getThrowables( throwable );
+
+ if ( fromIndex >= throwables.length )
+ {
+ throw new IndexOutOfBoundsException( "fromIndex is too large" );
+ }
+
+ for ( int i = fromIndex; i < throwables.length; i++ )
+ {
+ Throwable t = throwables[ i ];
+
+ if ( t.getClass().equals( type ) )
+ {
+ return i;
+ }
+ }
+ }
+
return -1;
}
@@ -289,10 +375,20 @@ public class ExceptionUtils
return null;
}
+ /**
+ * Since Throwable itself has a getCause() method since Java-1.4
+ * we can safely assume that all Throwables are nested.
+ *
+ * @param throwable
+ * @return
+ */
public static boolean isNestedThrowable( Throwable throwable )
{
- System.out.println("TODO IMPLEMENT");
- //X TODO implement
+ if ( throwable == null )
+ {
+ return false;
+ }
+
return true;
}
Modified: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/ExceptionUtilsTest.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/ExceptionUtilsTest.java?rev=1154362&r1=1154361&r2=1154362&view=diff
==============================================================================
--- maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/ExceptionUtilsTest.java (original)
+++ maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/ExceptionUtilsTest.java Fri Aug 5 19:44:16 2011
@@ -172,6 +172,10 @@ public class ExceptionUtilsTest extends
assertThat( "getThrowables"
, ExceptionUtils.getThrowables( testException )
, equalTo( expectedExceptions ) );
+
+ // NPE safe test
+ // this method should NOT throw a NPE on a null argument!
+ ExceptionUtils.getThrowables( null );
}
@@ -210,33 +214,6 @@ public class ExceptionUtilsTest extends
}
/**
- * @see ExceptionUtils#getStackFrameList(Throwable)
- */
- @Test
- public void testGetStackFrameList()
- {
- NullPointerException npe = new NullPointerException( "dooh just a random, nullpointer" );
-
- List<String> exceptionFrames = ExceptionUtils.getStackFrameList( npe );
- assertNotNull( exceptionFrames );
- assertTrue( exceptionFrames.size() > 1 );
- assertThat( "exceptionFrame", exceptionFrames.get( 0 )
- , JUnitMatchers.containsString( "at org.codehaus.plexus.util.ExceptionUtilsTest."
- + "testGetStackFrameList(ExceptionUtilsTest.java" ) );
-
- // NPE safe test
- try
- {
- ExceptionUtils.getStackFrameList( null );
- fail( "getStackFrameList(null) NPE expected" );
- }
- catch ( NullPointerException e )
- {
- //nothing to do, Exception was expected
- }
- }
-
- /**
* @see ExceptionUtils#getStackTrace(Throwable)
*/
@Test
@@ -292,56 +269,6 @@ public class ExceptionUtilsTest extends
}
/**
- * @see ExceptionUtils#getStackFrames(String)
- */
- @Test
- public void testGetStackFrames_String()
- {
- String stackTrace = "java.lang.NullPointerException: mymessage\n" +
- "\tat org.codehaus.plexus.util.ExceptionUtilsTest.testGetStackTrace(ExceptionUtilsTest.java:237)\n" +
- "\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n" +
- "\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)\n" +
- "\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)\n" +
- "\tat java.lang.reflect.Method.invoke(Method.java:597)\n" +
- "\tat org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)\n" +
- "\tat org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)\n" +
- "\tat org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)\n" +
- "\tat org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)\n" +
- "\tat org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)\n" +
- "\tat org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)\n" +
- "\tat org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)\n" +
- "\tat org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)\n" +
- "\tat org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)\n" +
- "\tat org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)\n" +
- "\tat org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)\n" +
- "\tat org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)\n" +
- "\tat org.junit.runners.ParentRunner.run(ParentRunner.java:236)\n" +
- "\tat org.junit.runner.JUnitCore.run(JUnitCore.java:157)\n" +
- "\tat com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)\n" +
- "\tat com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199)\n" +
- "\tat com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)";
-
- String[] stackFrames = ExceptionUtils.getStackFrames( stackTrace );
- assertNotNull( stackFrames );
- assertEquals(23, stackFrames.length);
-
- assertEquals( "java.lang.NullPointerException: mymessage", stackFrames[0] );
- assertThat( "stackFrames", stackFrames[1]
- , JUnitMatchers.containsString( "at org.codehaus.plexus.util.ExceptionUtilsTest."
- + "testGetStackTrace(ExceptionUtilsTest.java" ) );
-
- try
- {
- ExceptionUtils.getStackFrames( (String) null );
- fail( "getStackFrames(null) NPE expected" );
- }
- catch ( NullPointerException e )
- {
- //nothing to do, Exception was expected
- }
- }
-
- /**
* @see ExceptionUtils#getThrowableCount(Throwable)
*/
@Test
@@ -415,7 +342,7 @@ public class ExceptionUtilsTest extends
try
{
ExceptionUtils.indexOfThrowable( testException, TestException.class, 3 );
- fail( "indexOfThrowable with too large inces" );
+ fail( "indexOfThrowable with too large fromIndex" );
}
catch ( IndexOutOfBoundsException e )
{