o Added support for smart-stacktrace in base class2

Project: http://git-wip-us.apache.org/repos/asf/maven-surefire/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven-surefire/commit/3fb72473
Tree: http://git-wip-us.apache.org/repos/asf/maven-surefire/tree/3fb72473
Diff: http://git-wip-us.apache.org/repos/asf/maven-surefire/diff/3fb72473

Branch: refs/heads/master
Commit: 3fb724733f35ade8431a066f45de84801353bb93
Parents: efd554f
Author: Kristian Rosenvold <krosenv...@apache.org>
Authored: Wed Dec 19 17:06:45 2012 +0100
Committer: Kristian Rosenvold <krosenv...@apache.org>
Committed: Wed Dec 19 17:06:45 2012 +0100

----------------------------------------------------------------------
 .../surefire/report/ConsoleOutputCapture.java      |    1 +
 .../surefire/report/SmartStackTraceParser.java     |   53 ++++++++++++---
 .../apache/maven/surefire/report/ABaseClass.java   |   29 ++++++++
 .../apache/maven/surefire/report/ASubClass.java    |   24 +++++++
 .../surefire/report/SmartStackTraceParserTest.java |   17 +++++
 5 files changed, 114 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/3fb72473/surefire-api/src/main/java/org/apache/maven/surefire/report/ConsoleOutputCapture.java
----------------------------------------------------------------------
diff --git 
a/surefire-api/src/main/java/org/apache/maven/surefire/report/ConsoleOutputCapture.java
 
b/surefire-api/src/main/java/org/apache/maven/surefire/report/ConsoleOutputCapture.java
index 9f237fd..342fee9 100644
--- 
a/surefire-api/src/main/java/org/apache/maven/surefire/report/ConsoleOutputCapture.java
+++ 
b/surefire-api/src/main/java/org/apache/maven/surefire/report/ConsoleOutputCapture.java
@@ -53,6 +53,7 @@ public class ConsoleOutputCapture
 
         public void write( byte[] buf, int off, int len )
         {
+            System.out.println( "buf = " + buf );
             target.writeTestOutput( buf, off, len, isStdout );
         }
 

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/3fb72473/surefire-providers/common-java5/src/main/java/org/apache/maven/surefire/report/SmartStackTraceParser.java
----------------------------------------------------------------------
diff --git 
a/surefire-providers/common-java5/src/main/java/org/apache/maven/surefire/report/SmartStackTraceParser.java
 
b/surefire-providers/common-java5/src/main/java/org/apache/maven/surefire/report/SmartStackTraceParser.java
index e3245a6..4f46ac7 100644
--- 
a/surefire-providers/common-java5/src/main/java/org/apache/maven/surefire/report/SmartStackTraceParser.java
+++ 
b/surefire-providers/common-java5/src/main/java/org/apache/maven/surefire/report/SmartStackTraceParser.java
@@ -22,6 +22,7 @@ package org.apache.maven.surefire.report;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import org.apache.maven.shared.utils.StringUtils;
 
 /**
  * @author Kristian Rosenvold
@@ -38,30 +39,54 @@ public class SmartStackTraceParser
 
     private String testClassName;
 
+    private final Class testClass;
+
 
     public SmartStackTraceParser( Class testClass, Throwable throwable )
     {
         this( testClass.getName(), throwable );
     }
 
-    public SmartStackTraceParser( String testClass, Throwable throwable )
+    public SmartStackTraceParser( String testClassName, Throwable throwable )
     {
-        this.testClassName = testClass;
-        this.simpleName = testClassName.substring( testClassName.lastIndexOf( 
"." ) + 1 );
+        this.testClassName = testClassName;
+        this.testClass = getClass( testClassName );
+        this.simpleName = this.testClassName.substring( 
this.testClassName.lastIndexOf( "." ) + 1 );
         this.throwable = new SafeThrowable( throwable );
         stackTrace = throwable.getStackTrace();
     }
 
+    private static Class getClass( String name )
+    {
+        try
+        {
+            return Class.forName( name );
+        }
+        catch ( ClassNotFoundException e )
+        {
+            throw new RuntimeException( e );
+        }
+    }
+
+
+    private static String getSimpleName(String className){
+        int i = className.lastIndexOf( "." );
+        return className.substring(  i + 1 );
+    }
     @SuppressWarnings( "ThrowableResultOfMethodCallIgnored" )
     public String getString()
     {
         StringBuilder result = new StringBuilder();
         result.append( simpleName );
         result.append( "#" );
-        List<StackTraceElement> stackTraceElements = focusOnClass( stackTrace, 
testClassName );
+        List<StackTraceElement> stackTraceElements = focusOnClass( stackTrace, 
testClass );
         Collections.reverse( stackTraceElements );
         for ( StackTraceElement stackTraceElement : stackTraceElements )
         {
+            if (!stackTraceElement.getClassName().equals(  testClassName )){
+                result.append( "<" ).append( getSimpleName( 
stackTraceElement.getClassName() )); // Add the name of the superclas
+                result.append( "#" );
+            }
             result.append( stackTraceElement.getMethodName() ).append( "(" 
).append(
                 stackTraceElement.getLineNumber() ).append( ")" );
             result.append( "." );
@@ -110,17 +135,25 @@ public class SmartStackTraceParser
         return stackTrace[0].getClassName().equals( testClassName );
     }
 
-    static List<StackTraceElement> focusOnClass( StackTraceElement[] 
stackTrace, String className )
+    static List<StackTraceElement> focusOnClass( StackTraceElement[] 
stackTrace, Class clazz )
     {
         List<StackTraceElement> result = new ArrayList<StackTraceElement>();
         for ( StackTraceElement element : stackTrace )
         {
-            if ( className.equals( element.getClassName() ) )
-            {
-                result.add( element );
-            }
+            if ( isInSupers( clazz, element.getClassName() ) ) {
+            result.add( element );
         }
-        return result;
+        } return result;
+    }
+
+
+    private static boolean isInSupers( Class testClass, String lookFor )
+    {
+        while ( !testClass.getName().equals( lookFor ) && 
testClass.getSuperclass() != null )
+        {
+            testClass = testClass.getSuperclass();
+        }
+        return testClass.getName().equals( lookFor );
     }
 
     private static Throwable findInnermost( Throwable t )

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/3fb72473/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/ABaseClass.java
----------------------------------------------------------------------
diff --git 
a/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/ABaseClass.java
 
b/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/ABaseClass.java
new file mode 100644
index 0000000..c4387d2
--- /dev/null
+++ 
b/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/ABaseClass.java
@@ -0,0 +1,29 @@
+package org.apache.maven.surefire.report;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+public class ABaseClass
+{
+    public void npe()
+    {
+        throw new NullPointerException( "It was null" );
+    }
+}

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/3fb72473/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/ASubClass.java
----------------------------------------------------------------------
diff --git 
a/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/ASubClass.java
 
b/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/ASubClass.java
new file mode 100644
index 0000000..985447e
--- /dev/null
+++ 
b/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/ASubClass.java
@@ -0,0 +1,24 @@
+package org.apache.maven.surefire.report;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+public class ASubClass extends ABaseClass
+{
+}

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/3fb72473/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/SmartStackTraceParserTest.java
----------------------------------------------------------------------
diff --git 
a/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/SmartStackTraceParserTest.java
 
b/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/SmartStackTraceParserTest.java
index a3fd02c..cb4f658 100644
--- 
a/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/SmartStackTraceParserTest.java
+++ 
b/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/SmartStackTraceParserTest.java
@@ -115,6 +115,23 @@ public class SmartStackTraceParserTest
         }
     }
 
+    public void testFailureInBaseClass()
+        throws Exception
+    {
+        ASubClass aTestClass = new ASubClass();
+        try
+        {
+            aTestClass.npe();
+        }
+        catch ( NullPointerException e )
+        {
+            SmartStackTraceParser smartStackTraceParser = new 
SmartStackTraceParser( ASubClass.class, e );
+            String res = smartStackTraceParser.getString();
+            Assert.assertTrue(
+                "ASubClass#<ABaseClass#npe(27) >> NullPointerException It was 
null".equals( res ) );
+
+        }
+    }
 
     static class ADifferen0tTestClass
     {

Reply via email to