add script junit:assert location to stack trace


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@990075 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/sigil/gogo/junit/src/org/apache/felix/sigil/gogo/junit/SigilTestAdapter.java b/sigil/gogo/junit/src/org/apache/felix/sigil/gogo/junit/SigilTestAdapter.java
index fcb51b8..1755b73 100644
--- a/sigil/gogo/junit/src/org/apache/felix/sigil/gogo/junit/SigilTestAdapter.java
+++ b/sigil/gogo/junit/src/org/apache/felix/sigil/gogo/junit/SigilTestAdapter.java
@@ -18,7 +18,6 @@
  */
 package org.apache.felix.sigil.gogo.junit;
 
-
 import java.lang.reflect.InvocationTargetException;
 import java.util.Arrays;
 
@@ -29,34 +28,71 @@
 import org.osgi.service.command.CommandSession;
 import org.osgi.service.command.Function;
 
-
 public class SigilTestAdapter
 {
-    public TestCase newTest( final CommandSession session, final String name, final Function f, final Object... args )
+    public TestCase newTest(final CommandSession session, final String name,
+        final Function f, final Object... args)
     {
-        return new TestCase( name )
+        return new TestCase(name)
         {
             public void runTest() throws Throwable
             {
                 try
                 {
-                    f.execute( session, Arrays.asList( args ) );
+                    f.execute(session, Arrays.asList(args));
                 }
-                catch ( InvocationTargetException e )
+                catch (Throwable t)
                 {
-                    throw e.getCause();
+                    if (t instanceof InvocationTargetException)
+                        t = t.getCause();
+
+                    // stack trace is no use for identifying the location of
+                    // junit:assert methods in scripts.
+                    // So add gogo script location to stack trace.
+                    Object loc = session.get(".location");
+                    if (loc != null)
+                    {
+                        // file:/path/to/file:line.column
+                        String sloc = (String) loc;
+                        String fileName = sloc;
+                        int lineNumber = 0;
+
+                        if (sloc.matches(".*:[\\d.]+$"))
+                        {
+                            int colon = sloc.lastIndexOf(':');
+                            fileName = sloc.substring(0, colon);
+                            String number = sloc.substring(colon + 1);
+                            int dot = number.indexOf('.');
+                            if (dot > 0)
+                                number = number.substring(0, dot);
+                            lineNumber = Integer.parseInt(number);
+                        }
+
+                        StackTraceElement[] trace = t.getStackTrace();
+                        StackTraceElement element = new StackTraceElement(
+                            "SigilTestAdaptor", "runTest", fileName, lineNumber);
+
+                        StackTraceElement[] ev = new StackTraceElement[1 + trace.length];
+                        int i = 0;
+                        ev[i++] = element;
+                        for (StackTraceElement e : trace)
+                            ev[i++] = e;
+
+                        t.setStackTrace(ev);
+                    }
+
+                    throw t;
                 }
             }
         };
     }
 
-
-    public TestSuite newTestSuite( String name, Test... tests )
+    public TestSuite newTestSuite(String name, Test... tests)
     {
-        TestSuite suite = new TestSuite( name );
-        for ( Test t : tests )
+        TestSuite suite = new TestSuite(name);
+        for (Test t : tests)
         {
-            suite.addTest( t );
+            suite.addTest(t);
         }
         return suite;
     }