FileDocCategorySizeDatePackage
AndroidJUnitLaunchConfigDelegate.javaAPI DocAndroid 1.5 API10644Wed May 06 22:41:10 BST 2009com.android.ide.eclipse.adt.launch.junit

AndroidJUnitLaunchConfigDelegate

public class AndroidJUnitLaunchConfigDelegate extends com.android.ide.eclipse.adt.launch.LaunchConfigDelegate
Run configuration that can execute JUnit tests on an Android platform.

Will deploy apps on target Android platform by reusing functionality from ADT LaunchConfigDelegate, and then run JUnits tests by reusing functionality from JDT JUnitLaunchConfigDelegate.

Fields Summary
static final String
ATTR_INSTR_NAME
Launch config attribute that stores instrumentation runner.
private static final String
EMPTY_STRING
Constructors Summary
Methods Summary
protected voiddoLaunch(org.eclipse.debug.core.ILaunchConfiguration configuration, java.lang.String mode, org.eclipse.core.runtime.IProgressMonitor monitor, org.eclipse.core.resources.IProject project, com.android.ide.eclipse.adt.launch.AndroidLaunch androidLaunch, com.android.ide.eclipse.adt.launch.AndroidLaunchConfiguration config, com.android.ide.eclipse.adt.launch.AndroidLaunchController controller, org.eclipse.core.resources.IFile applicationPackage, com.android.ide.eclipse.common.project.AndroidManifestParser manifestParser)

 //$NON-NLS-1$

    
           
                  
               
                

        String runner = getRunner(project, configuration, manifestParser);
        if (runner == null) {
            AdtPlugin.displayError("Android Launch",
                    "An instrumention test runner is not specified!");
            androidLaunch.stopLaunch();
            return;
        }
        // get the target app's package
        String targetAppPackage = getTargetPackage(manifestParser, runner); 
        if (targetAppPackage == null) {
            AdtPlugin.displayError("Android Launch",
                    String.format("A target package for instrumention test runner %1$s could not be found!", 
                    runner));
            androidLaunch.stopLaunch();
            return; 
        }
        String testAppPackage = manifestParser.getPackage();
        AndroidJUnitLaunchInfo junitLaunchInfo = new AndroidJUnitLaunchInfo(project, 
                testAppPackage, runner);
        junitLaunchInfo.setTestClass(getTestClass(configuration));
        junitLaunchInfo.setTestPackage(getTestPackage(configuration));
        junitLaunchInfo.setTestMethod(getTestMethod(configuration));
        junitLaunchInfo.setLaunch(androidLaunch);
        IAndroidLaunchAction junitLaunch = new AndroidJUnitLaunchAction(junitLaunchInfo);
        
        controller.launch(project, mode, applicationPackage, testAppPackage, targetAppPackage,
                manifestParser.getDebuggable(), manifestParser.getApiLevelRequirement(),
                junitLaunch, config, androidLaunch, monitor);
    
private java.lang.StringgetRunner(org.eclipse.core.resources.IProject project, org.eclipse.debug.core.ILaunchConfiguration configuration, com.android.ide.eclipse.common.project.AndroidManifestParser manifestParser)
Gets a instrumentation runner for the launch.

If a runner is stored in the given configuration, will return that. Otherwise, will try to find the first valid runner for the project. If a runner can still not be found, will return null.

param
project the {@link IProject} for the app
param
configuration the {@link ILaunchConfiguration} for the launch
param
manifestParser the {@link AndroidManifestParser} for the project
return
null if no instrumentation runner can be found, otherwise return the fully qualified runner name.

        try {
            String runner = getRunnerFromConfig(configuration);
            if (runner != null) {
                return runner;
            }
            final InstrumentationRunnerValidator instrFinder = new InstrumentationRunnerValidator(
                    BaseProjectHelper.getJavaProject(project), manifestParser);
            runner = instrFinder.getValidInstrumentationTestRunner();
            if (runner != null) {
                AdtPlugin.printErrorToConsole(project,
                        String.format("Warning: No instrumentation runner found for the launch, " +
                                "using %1$s", runner));
                return runner;
            }
            AdtPlugin.printErrorToConsole(project,
                    String.format("ERROR: Application does not specify a %1$s instrumentation or does not declare uses-library %2$s",
                    AndroidConstants.CLASS_INSTRUMENTATION_RUNNER, 
                    AndroidConstants.LIBRARY_TEST_RUNNER));
            return null;
        } catch (CoreException e) {
            AdtPlugin.log(e, "Error when retrieving instrumentation info"); //$NON-NLS-1$           
        }
        return null;

    
private java.lang.StringgetRunnerFromConfig(org.eclipse.debug.core.ILaunchConfiguration configuration)

        return getStringLaunchAttribute(ATTR_INSTR_NAME, configuration);
    
private java.lang.StringgetStringLaunchAttribute(java.lang.String attributeName, org.eclipse.debug.core.ILaunchConfiguration configuration)
Helper method to retrieve a string attribute from the launch configuration

param
attributeName name of the launch attribute
param
configuration the {@link ILaunchConfiguration} to retrieve the attribute from
return
the attribute's value. null if not found.

        try {
            String attrValue = configuration.getAttribute(attributeName, EMPTY_STRING);
            if (attrValue.length() < 1) {
                return null;
            }
            return attrValue;
        } catch (CoreException e) {
            AdtPlugin.log(e, String.format("Error when retrieving launch info %1$s",  //$NON-NLS-1$
                    attributeName));
        }
        return null;
    
private java.lang.StringgetTargetPackage(com.android.ide.eclipse.common.project.AndroidManifestParser manifestParser, java.lang.String runner)
Get the target Android application's package for the given instrumentation runner, or null if it could not be found.

param
manifestParser the {@link AndroidManifestParser} for the test project
param
runner the instrumentation runner class name
return
the target package or null

        for (Instrumentation instr : manifestParser.getInstrumentations()) {
            if (instr.getName().equals(runner)) {
                return instr.getTargetPackage();
            }
        }
        return null;
    
private java.lang.StringgetTestClass(org.eclipse.debug.core.ILaunchConfiguration configuration)
Returns the test class stored in the launch configuration.

param
configuration the {@link ILaunchConfiguration} to retrieve the test class info from
return
the test class. null if not specified.

        return getStringLaunchAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME,
                configuration);
    
private java.lang.StringgetTestMethod(org.eclipse.debug.core.ILaunchConfiguration configuration)
Returns the test method stored in the launch configuration.

param
configuration the {@link ILaunchConfiguration} to retrieve the test method info from
return
the test method. null if not specified.

        return getStringLaunchAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_METHOD_NAME,
                configuration);
    
private java.lang.StringgetTestPackage(org.eclipse.debug.core.ILaunchConfiguration configuration)
Returns the test package stored in the launch configuration, or null if not specified.

param
configuration the {@link ILaunchConfiguration} to retrieve the test package info from
return
the test package or null.

        // try to retrieve a package name from the JUnit container attribute
        String containerHandle = getStringLaunchAttribute(
                JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, configuration);
        if (containerHandle != null && containerHandle.length() > 0) {
            IJavaElement element = JavaCore.create(containerHandle);
            // containerHandle could be a IProject, check if its a java package
            if (element.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
                return element.getElementName();
            }
        }
        return null;
    
static voidsetJUnitDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy config)
Helper method to set JUnit-related attributes expected by JDT JUnit runner

param
config the launch configuration to modify

        // set the test runner to JUnit3 to placate JDT JUnit runner logic
        config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_RUNNER_KIND, 
                TestKindRegistry.JUNIT3_TEST_KIND_ID);