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/06 00:25:38 UTC
svn commit: r1154397 - 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 22:25:37 2011
New Revision: 1154397
URL: http://svn.apache.org/viewvc?rev=1154397&view=rev
Log:
MSANDBOX-51 ExceptionUtils are now finished!
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=1154397&r1=1154396&r2=1154397&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 22:25:37 2011
@@ -21,6 +21,7 @@ package org.codehaus.plexus.util;
import java.io.PrintStream;
import java.io.PrintWriter;
+import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;
@@ -336,43 +337,100 @@ public class ExceptionUtils
return -1;
}
- public static void printRootCauseStackTrace( Throwable t, PrintStream stream )
+ /**
+ * Print the stacktrace of root cause of the given Throwable to System.err
+ * @param throwable
+ * @see #getCause(Throwable)
+ */
+ public static void printRootCauseStackTrace( Throwable throwable )
{
- System.out.println("TODO IMPLEMENT");
- //X TODO implement
+ if ( throwable == null )
+ {
+ // weird, but needed for backward compat...
+ throw new IndexOutOfBoundsException( "Throwable param must not be null" );
+ }
+
+ Throwable rootCause = getRootCause( throwable );
+ if ( rootCause == null )
+ {
+ rootCause = throwable;
+ }
+ rootCause.printStackTrace();
}
- public static void printRootCauseStackTrace( Throwable t )
+ /**
+ * Print the stacktrace of root cause of the given Throwable to the PrintStream
+ * @param throwable
+ * @see #getCause(Throwable)
+ */
+ public static void printRootCauseStackTrace( Throwable throwable, PrintStream stream )
{
- System.out.println("TODO IMPLEMENT");
- //X TODO implement
+ Throwable rootCause = getRootCause( throwable );
+ if ( rootCause == null )
+ {
+ rootCause = throwable;
+ }
+ rootCause.printStackTrace(stream);
}
- public static void printRootCauseStackTrace( Throwable t, PrintWriter writer )
+ /**
+ * Print the stacktrace of root cause of the given Throwable to the PrintWriter
+ * @param throwable
+ * @see #getCause(Throwable)
+ */
+ public static void printRootCauseStackTrace( Throwable throwable, PrintWriter writer )
{
- System.out.println("TODO IMPLEMENT");
- //X TODO implement
+ Throwable rootCause = getRootCause( throwable );
+ if ( rootCause == null )
+ {
+ rootCause = throwable;
+ }
+ rootCause.printStackTrace( writer );
+ writer.flush();
}
- public static String[] getRootCauseStackTrace( Throwable t )
+ /**
+ * The stacktrace frames for the root cause of the given Throwable
+ * @param throwable
+ * @return String with the Stacktrace of the Throwable
+ * @see #getCause(Throwable)
+ */
+ public static String[] getRootCauseStackTrace( Throwable throwable )
{
- System.out.println("TODO IMPLEMENT");
- //X TODO implement
- return null;
+ Throwable rootCause = getRootCause( throwable );
+ if ( rootCause == null )
+ {
+ rootCause = throwable;
+ }
+
+ return getStackFrames( rootCause );
}
- public static String getStackTrace( Throwable t )
+ /**
+ * The stacktrace for the given Throwable
+ * @param throwable
+ * @return String with the Stacktrace of the Throwable
+ */
+ public static String getStackTrace( Throwable throwable )
{
- System.out.println("TODO IMPLEMENT");
- //X TODO implement
- return null;
+ StringWriter stringWriter = new StringWriter();
+ PrintWriter printWriter = new PrintWriter( stringWriter, true );
+
+ throwable.printStackTrace( printWriter );
+
+ return stringWriter.getBuffer().toString();
}
- public static String getFullStackTrace( Throwable t )
+ /**
+ * The stacktrace for the given Throwable
+ * @param throwable
+ * @return String with the Stacktrace of the Throwable
+ */
+ public static String getFullStackTrace( Throwable throwable )
{
- System.out.println("TODO IMPLEMENT");
- //X TODO implement
- return null;
+ // nowadays this is the same...
+
+ return getStackTrace(throwable);
}
/**
@@ -392,11 +450,34 @@ public class ExceptionUtils
return true;
}
- public static String[] getStackFrames( Throwable t )
+ /**
+ * Get all the separate single lines of the stacktrace for the Throwable
+ * @param throwable
+ * @return the lines of the stack trace for the throwable
+ */
+ public static String[] getStackFrames( Throwable throwable )
{
- System.out.println("TODO IMPLEMENT");
- //X TODO implement
- return null;
+ StackTraceElement[] stackTraceElements = throwable.getStackTrace();
+
+ String[] retVal = new String[ stackTraceElements.length + 1 ];
+
+ retVal[ 0 ] = throwable.getClass().getName() + ": " + throwable.getMessage();
+
+ int i = 1;
+
+ for ( StackTraceElement stackTraceElement : stackTraceElements )
+ {
+ StringBuilder sb = new StringBuilder( "\tat " );
+ sb.append( stackTraceElement.getClassName() );
+ sb.append( "." );
+ sb.append( stackTraceElement.getMethodName() );
+ sb.append( "(" ).append( stackTraceElement.getFileName() ).append( "):" );
+ sb.append(stackTraceElement.getLineNumber());
+
+ retVal[ i++ ] = sb.toString();
+ }
+
+ return retVal;
}
}
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=1154397&r1=1154396&r2=1154397&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 22:25:37 2011
@@ -32,7 +32,6 @@ import java.io.PrintStream;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
-import java.util.List;
import java.util.logging.Logger;
import static org.hamcrest.CoreMatchers.*;
@@ -154,9 +153,17 @@ public class ExceptionUtilsTest extends
String fullStackTraceStart = "java.lang.NullPointerException: dooh just a random, nullpointer\n"
+ "\tat org.codehaus.plexus.util.ExceptionUtilsTest.testGetFullStackTrace(ExceptionUtilsTest.java";
+ String fullStackTrace = ExceptionUtils.getFullStackTrace( npe );
assertThat( "getFullStackTrace start with"
- , ExceptionUtils.getFullStackTrace( npe )
+ , fullStackTrace
, JUnitMatchers.containsString( fullStackTraceStart ) );
+
+ SQLException sqlException = new SQLException( npe );
+ fullStackTrace = ExceptionUtils.getFullStackTrace( sqlException );
+ assertThat( "getFullStackTrace start with"
+ , fullStackTrace
+ , JUnitMatchers.containsString( fullStackTraceStart ) );
+
}
@Test
@@ -222,11 +229,19 @@ public class ExceptionUtilsTest extends
NullPointerException npe = new NullPointerException( "dooh just a random, nullpointer" );
String stackTrace = ExceptionUtils.getStackTrace( npe );
- assertNotNull(stackTrace);
+ assertNotNull( stackTrace );
assertTrue( "wrong stacktrace: " + stackTrace,
stackTrace.startsWith( "java.lang.NullPointerException: dooh just a random, nullpointer\n" +
"\tat org.codehaus.plexus.util.ExceptionUtilsTest.testGetStackTrace(ExceptionUtilsTest.java" ));
+ SQLException sqlException = new SQLException( npe );
+ stackTrace = ExceptionUtils.getStackTrace( sqlException );
+ assertNotNull( stackTrace );
+ assertTrue( "wrong stacktrace: " + stackTrace,
+ stackTrace.startsWith( "java.sql.SQLException: java.lang.NullPointerException: "
+ + "dooh just a random, nullpointer\n"
+ + "\tat org.codehaus.plexus.util.ExceptionUtilsTest.testGetStackTrace(ExceptionUtilsTest.java" ));
+
// NPE safe test
try
{
@@ -453,8 +468,10 @@ public class ExceptionUtilsTest extends
, JUnitMatchers.containsString("java.lang.NullPointerException: dooh just a random, nullpointer"
+ "\n\tat org.codehaus.plexus.util.ExceptionUtilsTest."
+ "testPrintRootCauseStackTrace(ExceptionUtilsTest.java:"));
+ outStream.close();
bao.reset();
+ outStream = new PrintStream( bao );
PrintWriter printWriter = new PrintWriter( outStream );
ExceptionUtils.printRootCauseStackTrace( sqlException, printWriter );
assertThat( "stackFrames"
@@ -481,7 +498,7 @@ public class ExceptionUtilsTest extends
try
{
ExceptionUtils.printRootCauseStackTrace( npe, (PrintStream) null );
- fail("indexOfThrowable with too large inces");
+ fail("printRootCauseStackTrace( x, null) NPE expected");
}
catch ( NullPointerException e )
{
@@ -491,7 +508,7 @@ public class ExceptionUtilsTest extends
try
{
ExceptionUtils.printRootCauseStackTrace( npe, (PrintWriter) null );
- fail("indexOfThrowable with too large inces");
+ fail("printRootCauseStackTrace( x, null) NPE expected");
}
catch ( NullPointerException e )
{