1 11 package org.eclipse.jdt.internal.junit; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.core.runtime.IAdaptable; 15 16 import org.eclipse.core.expressions.PropertyTester; 17 18 import org.eclipse.core.resources.IResource; 19 20 import org.eclipse.jdt.core.IClassFile; 21 import org.eclipse.jdt.core.ICompilationUnit; 22 import org.eclipse.jdt.core.IJavaElement; 23 import org.eclipse.jdt.core.IMember; 24 import org.eclipse.jdt.core.IType; 25 import org.eclipse.jdt.core.JavaCore; 26 27 import org.eclipse.jdt.internal.junit.util.TestSearchEngine; 28 29 33 public class JUnitPropertyTester extends PropertyTester { 34 35 private static final String PROPERTY_IS_TEST= "isTest"; 37 private static final String PROPERTY_CAN_LAUNCH_AS_JUNIT_TEST= "canLaunchAsJUnit"; 39 42 public boolean test(Object receiver, String property, Object [] args, Object expectedValue) { 43 if (!(receiver instanceof IAdaptable)) { 44 throw new IllegalArgumentException ("Element must be of type 'IAdaptable', is " + receiver == null ? "null" : receiver.getClass().getName()); } 46 47 IJavaElement element; 48 if (receiver instanceof IJavaElement) { 49 element= (IJavaElement) receiver; 50 } else if (receiver instanceof IResource) { 51 element = JavaCore.create((IResource) receiver); 52 if (element == null) { 53 return false; 54 } 55 } else { element= (IJavaElement) ((IAdaptable) receiver).getAdapter(IJavaElement.class); 57 if (element == null) { 58 IResource resource= (IResource) ((IAdaptable) receiver).getAdapter(IResource.class); 59 element = JavaCore.create(resource); 60 if (element == null) { 61 return false; 62 } 63 return false; 64 } 65 } 66 if (PROPERTY_IS_TEST.equals(property)) { 67 return isJUnitTest(element); 68 } else if (PROPERTY_CAN_LAUNCH_AS_JUNIT_TEST.equals(property)) { 69 return canLaunchAsJUnitTest(element); 70 } 71 throw new IllegalArgumentException ("Unknown test property '" + property + "'"); } 73 74 private boolean canLaunchAsJUnitTest(IJavaElement element) { 75 switch (element.getElementType()) { 76 case IJavaElement.JAVA_PROJECT: 77 case IJavaElement.PACKAGE_FRAGMENT_ROOT: 78 case IJavaElement.PACKAGE_FRAGMENT: 79 return true; case IJavaElement.COMPILATION_UNIT: 81 case IJavaElement.CLASS_FILE: 82 case IJavaElement.TYPE: 83 case IJavaElement.METHOD: 84 return isJUnitTest(element); 85 default: 86 return false; 87 } 88 } 89 90 93 private boolean isJUnitTest(IJavaElement element) { 94 try { 95 IType testType= null; 96 if (element instanceof ICompilationUnit) { 97 testType= (((ICompilationUnit) element)).findPrimaryType(); 98 } else if (element instanceof IClassFile) { 99 testType= (((IClassFile) element)).getType(); 100 } else if (element instanceof IType) { 101 testType= (IType) element; 102 } else if (element instanceof IMember) { 103 testType= ((IMember) element).getDeclaringType(); 104 } 105 if (testType != null && testType.exists()) { 106 return TestSearchEngine.isTestOrTestSuite(testType); 107 } 108 } catch (CoreException e) { 109 } 111 return false; 112 } 113 } 114 | Popular Tags |