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

AndroidJUnitPropertyTester

public class AndroidJUnitPropertyTester extends org.eclipse.core.expressions.PropertyTester
A {@link PropertyTester} that checks if selected elements can be run as Android JUnit tests.

Based on org.eclipse.jdt.internal.junit.JUnitPropertyTester. The only substantial difference in this implementation is source folders cannot be run as Android JUnit.

Fields Summary
private static final String
PROPERTY_IS_TEST
private static final String
PROPERTY_CAN_LAUNCH_AS_JUNIT_TEST
Constructors Summary
Methods Summary
private booleancanLaunchAsJUnitTest(org.eclipse.jdt.core.IJavaElement element)

        try {
            switch (element.getElementType()) {
                case IJavaElement.JAVA_PROJECT:
                    return true; // can run, let JDT detect if there are tests
                case IJavaElement.PACKAGE_FRAGMENT_ROOT:
                    return false; // not supported by Android test runner
                case IJavaElement.PACKAGE_FRAGMENT:
                    return ((IPackageFragment) element).hasChildren(); 
                case IJavaElement.COMPILATION_UNIT:
                case IJavaElement.CLASS_FILE:
                case IJavaElement.TYPE:
                case IJavaElement.METHOD:
                    return isJUnitTest(element);
                default:
                    return false;
            }
        } catch (JavaModelException e) {
            return false;
        }
    
private booleanisJUnitTest(org.eclipse.jdt.core.IJavaElement element)
Return whether the target resource is a JUnit test.

        try {
            IType testType = null;
            if (element instanceof ICompilationUnit) {
                testType = (((ICompilationUnit) element)).findPrimaryType();
            } else if (element instanceof IClassFile) {
                testType = (((IClassFile) element)).getType();
            } else if (element instanceof IType) {
                testType = (IType) element;
            } else if (element instanceof IMember) {
                testType = ((IMember) element).getDeclaringType();
            }
            if (testType != null && testType.exists()) {
                return TestSearchEngine.isTestOrTestSuite(testType);
            }
        } catch (CoreException e) {
            // ignore, return false
        }
        return false;
    
public booleantest(java.lang.Object receiver, java.lang.String property, java.lang.Object[] args, java.lang.Object expectedValue)

 //$NON-NLS-1$

    /* (non-Javadoc)
     * @see org.eclipse.jdt.internal.corext.refactoring.participants.properties.IPropertyEvaluator#test(java.lang.Object, java.lang.String, java.lang.String)
     */
              
        if (!(receiver instanceof IAdaptable)) {
            final String elementName = (receiver == null ? "null" : //$NON-NLS-1$
                receiver.getClass().getName());
            throw new IllegalArgumentException(
                    String.format("Element must be of type IAdaptable, is %s", //$NON-NLS-1$
                            elementName));
        }

        IJavaElement element;
        if (receiver instanceof IJavaElement) {
            element = (IJavaElement) receiver;
        } else if (receiver instanceof IResource) {
            element = JavaCore.create((IResource) receiver);
            if (element == null) {
                return false;
            }
        } else { // is IAdaptable
            element= (IJavaElement) ((IAdaptable) receiver).getAdapter(IJavaElement.class);
            if (element == null) {
                IResource resource = (IResource) ((IAdaptable) receiver).getAdapter(
                        IResource.class);
                element = JavaCore.create(resource);
                if (element == null) {
                    return false;
                }
            }
        }
        if (PROPERTY_IS_TEST.equals(property)) { 
            return isJUnitTest(element);
        } else if (PROPERTY_CAN_LAUNCH_AS_JUNIT_TEST.equals(property)) {
            return canLaunchAsJUnitTest(element);
        }
        throw new IllegalArgumentException(
                String.format("Unknown test property '%s'", property)); //$NON-NLS-1$