/*
 * 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.
 */
package org.apache.felix.sigil.gogo.junit;


import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

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 )
    {
        return new TestCase( name )
        {
            public void runTest() throws Throwable
            {
                try
                {
                    f.execute( session, Arrays.asList( args ) );
                }
                catch ( InvocationTargetException e )
                {
                    throw e.getCause();
                }
            }
        };
    }


    public TestSuite newTestSuite( String name, Test... tests )
    {
        TestSuite suite = new TestSuite( name );
        for ( Test t : tests )
        {
            suite.addTest( t );
        }
        return suite;
    }
}

@@ -847,17 +847,16 @@
 junit;%0A%0A
-%0A
 import j
@@ -1113,17 +1113,16 @@
 ction;%0A%0A
-%0A
 public c
@@ -1173,17 +1173,16 @@
 newTest(
- 
 final Co
@@ -1221,16 +1221,24 @@
 ng name,
+%0A       
  final F
@@ -1268,17 +1268,16 @@
 ... args
- 
 )%0A    %7B%0A
@@ -1304,22 +1304,20 @@
 estCase(
- 
 name
- 
 )%0A      
@@ -1453,17 +1453,16 @@
 execute(
- 
 session,
@@ -1480,16 +1480,13 @@
 ist(
- 
 args
- ) 
+)
 );%0A 
@@ -1529,95 +1529,1802 @@
 ch (
- InvocationTargetException e )%0A                %7B%0A                    throw e.getCause()
+Throwable t)%0A                %7B%0A                    if (t instanceof InvocationTargetException)%0A                        t = t.getCause();%0A%0A                    // stack trace is no use for identifying the location of%0A                    // junit:assert methods in scripts.%0A                    // So add gogo script location to stack trace.%0A                    Object loc = session.get(%22.location%22);%0A                    if (loc != null)%0A                    %7B%0A                        // file:/path/to/file:line.column%0A                        String sloc = (String) loc;%0A                        String fileName = sloc;%0A                        int lineNumber = 0;%0A%0A                        if (sloc.matches(%22.*:%5B%5C%5Cd.%5D+$%22))%0A                        %7B%0A                            int colon = sloc.lastIndexOf(':');%0A                            fileName = sloc.substring(0, colon);%0A                            String number = sloc.substring(colon + 1);%0A                            int dot = number.indexOf('.');%0A                            if (dot %3E 0)%0A                                number = number.substring(0, dot);%0A                            lineNumber = Integer.parseInt(number);%0A                        %7D%0A%0A                        StackTraceElement%5B%5D trace = t.getStackTrace();%0A                        StackTraceElement element = new StackTraceElement(%0A                            %22SigilTestAdaptor%22, %22runTest%22, fileName, lineNumber);%0A%0A                        StackTraceElement%5B%5D ev = new StackTraceElement%5B1 + trace.length%5D;%0A                        int i = 0;%0A                        ev%5Bi++%5D = element;%0A                        for (StackTraceElement e : trace)%0A                            ev%5Bi++%5D = e;%0A%0A                        t.setStackTrace(ev);%0A                    %7D%0A%0A                    throw t
 ;%0A  
@@ -3371,17 +3371,16 @@
 %0A    %7D%0A%0A
-%0A
     publ
@@ -3405,17 +3405,16 @@
 stSuite(
- 
 String n
@@ -3427,25 +3427,24 @@
 est... tests
- 
 )%0A    %7B%0A    
@@ -3483,14 +3483,12 @@
 ite(
- 
 name
- 
 );%0A 
@@ -3499,17 +3499,16 @@
    for (
- 
 Test t :
@@ -3513,17 +3513,16 @@
  : tests
- 
 )%0A      
@@ -3551,19 +3551,17 @@
 addTest(
- t 
+t
 );%0A     

add script junit:assert location to stack trace