// Path: driver/src/com/leandog/brazenhead/TestRunInformation.java
// public class TestRunInformation {
//     
//     private static String packageName;
//     private static String fullLauncherName;
//     private static Solo robotium;
//     private static Brazenhead brazenhead;
//     
//     public static void initialize(final Bundle arguments) {
//         if( null == arguments ) return;
//         packageName = arguments.getString("packageName");
//         fullLauncherName = arguments.getString("fullLauncherName");
//     }
//     
//     public static String getPackageName() {
//         return packageName;
//     }
//     
//     public static String getFullLauncherName() {
//         return fullLauncherName;
//     }
//     
//     public static void setSolo(final Solo solo) {
//         robotium = solo;
//     }
//     
//     public static Solo getSolo() {
//         return robotium;
//     }
// 
//     public static Brazenhead getBrazenhead() {
//         return brazenhead;
//     }
// 
//     public static void setBrazenhead(Brazenhead brazenheadInstance) {
//         brazenhead = brazenheadInstance;
//     }
//     
// }
// 
// Path: driver/src/com/leandog/brazenhead/exceptions/CommandNotFoundException.java
// public class CommandNotFoundException extends Exception {
// 
//     private static final long serialVersionUID = 1L;
// 
//     public CommandNotFoundException(final String methodName, final Class<?> targetClass, final Class<?>... arguments) {
//         super(missingCommandMessage(methodName, targetClass, arguments));
//     }
// 
//     public CommandNotFoundException(final Command command, final Object theTarget, final Class<?>... arguments) {
//         super(missingCommandMessage(command.getName(), theTarget.getClass(), arguments));
//     }
// 
//     private static String missingCommandMessage(final String methodName, final Class<?> targetClass, final Class<?>... arguments) {
//         return String.format("The %s%s method was not found on %s.",
//                 methodName,
//                 stringFor(arguments),
//                 targetClass.getName());
//     }
// 
//     private static String stringFor(Class<?>[] arguments) {
//         if (arguments.length == 0) {
//             return "()";
//         }
// 
//         final StringBuilder messageBuilder = new StringBuilder();
//         messageBuilder.append("(");
//         for (final Class<?> argumentType : arguments) {
//             messageBuilder.append(argumentType.getName() + ", ");
//         }
// 
//         messageBuilder.replace(last(messageBuilder, 2), last(messageBuilder, 0), ")");
//         return messageBuilder.toString();
//     }
// 
//     private static int last(final StringBuilder messageBuilder, int count) {
//         return messageBuilder.length() - count;
//     }
// }


import java.lang.reflect.Method;
import java.util.*;
import java.util.Map.Entry;
import com.leandog.brazenhead.TestRunInformation;
import com.leandog.brazenhead.exceptions.CommandNotFoundException;
package com.leandog.brazenhead.commands;



public class CommandRunner {

    private Object theLastResult;

    Map<String, Object> variables = new HashMap<String, Object>();

    public void execute(final Command... commands) throws Exception {
        clearLastRun();

        for (final Command command : commands) {
            theLastResult = findMethod(command).invoke(theTargetFor(command), theArguments(command));

            if (command.hasVariable()) {
                storeVariable(command);
            }
        }
    }

    public Object theLastResult() {
        return theLastResult;
    }

    private Method findMethod(final Command command) throws CommandNotFoundException {
        return new MethodFinder().find(command.getName()).on(theTargetFor(command).getClass()).with(argumentTypesFor(command));
    }

    private void clearLastRun() {
        theLastResult = null;
        variables.clear();
    }

    private Object theTargetFor(final Command command) {
        switch (command.getTarget()) {
        case Brazenhead:
// Path: driver/src/com/leandog/brazenhead/TestRunInformation.java
// public class TestRunInformation {
//     
//     private static String packageName;
//     private static String fullLauncherName;
//     private static Solo robotium;
//     private static Brazenhead brazenhead;
//     
//     public static void initialize(final Bundle arguments) {
//         if( null == arguments ) return;
//         packageName = arguments.getString("packageName");
//         fullLauncherName = arguments.getString("fullLauncherName");
//     }
//     
//     public static String getPackageName() {
//         return packageName;
//     }
//     
//     public static String getFullLauncherName() {
//         return fullLauncherName;
//     }
//     
//     public static void setSolo(final Solo solo) {
//         robotium = solo;
//     }
//     
//     public static Solo getSolo() {
//         return robotium;
//     }
// 
//     public static Brazenhead getBrazenhead() {
//         return brazenhead;
//     }
// 
//     public static void setBrazenhead(Brazenhead brazenheadInstance) {
//         brazenhead = brazenheadInstance;
//     }
//     
// }
// 
// Path: driver/src/com/leandog/brazenhead/exceptions/CommandNotFoundException.java
// public class CommandNotFoundException extends Exception {
// 
//     private static final long serialVersionUID = 1L;
// 
//     public CommandNotFoundException(final String methodName, final Class<?> targetClass, final Class<?>... arguments) {
//         super(missingCommandMessage(methodName, targetClass, arguments));
//     }
// 
//     public CommandNotFoundException(final Command command, final Object theTarget, final Class<?>... arguments) {
//         super(missingCommandMessage(command.getName(), theTarget.getClass(), arguments));
//     }
// 
//     private static String missingCommandMessage(final String methodName, final Class<?> targetClass, final Class<?>... arguments) {
//         return String.format("The %s%s method was not found on %s.",
//                 methodName,
//                 stringFor(arguments),
//                 targetClass.getName());
//     }
// 
//     private static String stringFor(Class<?>[] arguments) {
//         if (arguments.length == 0) {
//             return "()";
//         }
// 
//         final StringBuilder messageBuilder = new StringBuilder();
//         messageBuilder.append("(");
//         for (final Class<?> argumentType : arguments) {
//             messageBuilder.append(argumentType.getName() + ", ");
//         }
// 
//         messageBuilder.replace(last(messageBuilder, 2), last(messageBuilder, 0), ")");
//         return messageBuilder.toString();
//     }
// 
//     private static int last(final StringBuilder messageBuilder, int count) {
//         return messageBuilder.length() - count;
//     }
// }


// Path: driver/src/com/leandog/brazenhead/commands/CommandRunner.java
import java.lang.reflect.Method;
import java.util.*;
import java.util.Map.Entry;
import com.leandog.brazenhead.TestRunInformation;
import com.leandog.brazenhead.exceptions.CommandNotFoundException;

package com.leandog.brazenhead.commands;



public class CommandRunner {

    private Object theLastResult;

    Map<String, Object> variables = new HashMap<String, Object>();

    public void execute(final Command... commands) throws Exception {
        clearLastRun();

        for (final Command command : commands) {
            theLastResult = findMethod(command).invoke(theTargetFor(command), theArguments(command));

            if (command.hasVariable()) {
                storeVariable(command);
            }
        }
    }

    public Object theLastResult() {
        return theLastResult;
    }

    private Method findMethod(final Command command) throws CommandNotFoundException {
        return new MethodFinder().find(command.getName()).on(theTargetFor(command).getClass()).with(argumentTypesFor(command));
    }

    private void clearLastRun() {
        theLastResult = null;
        variables.clear();
    }

    private Object theTargetFor(final Command command) {
        switch (command.getTarget()) {
        case Brazenhead:
            return TestRunInformation.getBrazenhead();