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

InstrumentationRunnerValidator

public class InstrumentationRunnerValidator extends Object
Provides validation for Android instrumentation test runner

Fields Summary
private final org.eclipse.jdt.core.IJavaProject
mJavaProject
private String[]
mInstrumentationNames
private boolean
mHasRunnerLibrary
static final String
INSTRUMENTATION_OK
Constructors Summary
InstrumentationRunnerValidator(org.eclipse.jdt.core.IJavaProject javaProject)
Initializes the InstrumentationRunnerValidator.

param
javaProject the {@link IJavaProject} for the Android project to validate


                        
      
        mJavaProject = javaProject;
        try {
            AndroidManifestParser manifestParser = AndroidManifestParser.parse(javaProject, 
                    null /* errorListener */, true /* gatherData */, false /* markErrors */);
            init(manifestParser);
        } catch (CoreException e) {
            AdtPlugin.printErrorToConsole(javaProject.getProject(), "ERROR: Failed to parse %1$s",
                    AndroidConstants.FN_ANDROID_MANIFEST);
        }
    
InstrumentationRunnerValidator(org.eclipse.core.resources.IProject project)
Initializes the InstrumentationRunnerValidator.

param
project the {@link IProject} for the Android project to validate
throws
CoreException if a fatal error occurred in initialization

        this(BaseProjectHelper.getJavaProject(project));
    
InstrumentationRunnerValidator(org.eclipse.jdt.core.IJavaProject javaProject, com.android.ide.eclipse.common.project.AndroidManifestParser manifestParser)
Initializes the InstrumentationRunnerValidator with an existing {@link AndroidManifestParser}

param
javaProject the {@link IJavaProject} for the Android project to validate
param
manifestParser the {@link AndroidManifestParser} for the Android project

        mJavaProject = javaProject;
        init(manifestParser);
    
Methods Summary
java.lang.String[]getInstrumentationNames()
Return the set of instrumentation names for the Android project.

return
null

        return mInstrumentationNames;
    
java.lang.StringgetValidInstrumentationTestRunner()
Helper method to get the first instrumentation that can be used as a test runner.

return
fully qualified instrumentation class name. null if no valid instrumentation can be found.

        for (String instrumentation : getInstrumentationNames()) {
            if (validateInstrumentationRunner(instrumentation) == INSTRUMENTATION_OK) {
                return instrumentation;
            }
        }
        return null;
    
private booleanhasTestRunnerLibrary(com.android.ide.eclipse.common.project.AndroidManifestParser manifestParser)
Helper method to determine if given manifest has a AndroidConstants.LIBRARY_TEST_RUNNER library reference

param
manifestParser the {@link AndroidManifestParser} to search
return
true if test runner library found, false otherwise

       for (String lib : manifestParser.getUsesLibraries()) {
           if (lib.equals(AndroidConstants.LIBRARY_TEST_RUNNER)) {
               return true;
           }
       }
       return false;
    
private voidinit(com.android.ide.eclipse.common.project.AndroidManifestParser manifestParser)

        Instrumentation[] instrumentations = manifestParser.getInstrumentations();
        mInstrumentationNames = new String[instrumentations.length];
        for (int i = 0; i < instrumentations.length; i++) {
            mInstrumentationNames[i] = instrumentations[i].getName();
        }
        mHasRunnerLibrary = hasTestRunnerLibrary(manifestParser);
    
java.lang.StringvalidateInstrumentationRunner(java.lang.String instrumentation)
Helper method to determine if specified instrumentation can be used as a test runner

param
instrumentation the instrumentation class name to validate. Assumes this instrumentation is one of {@link #getInstrumentationNames()}
return
INSTRUMENTATION_OK if valid, otherwise returns error message

        if (!mHasRunnerLibrary) {
            return String.format("The application does not declare uses-library %1$s", 
                    AndroidConstants.LIBRARY_TEST_RUNNER);
        }
        // check if this instrumentation is the standard test runner
        if (!instrumentation.equals(AndroidConstants.CLASS_INSTRUMENTATION_RUNNER)) {
            // check if it extends the standard test runner
            String result = BaseProjectHelper.testClassForManifest(mJavaProject,
                    instrumentation, AndroidConstants.CLASS_INSTRUMENTATION_RUNNER, true);
             if (result != BaseProjectHelper.TEST_CLASS_OK) {
                return String.format("The instrumentation runner must be of type %s", 
                        AndroidConstants.CLASS_INSTRUMENTATION_RUNNER);
             }
        }
        return INSTRUMENTATION_OK;