KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > JUnitPropertyTester


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
30  * JUnitPropertyTester provides propertyTester(s) for IResource types
31  * for use in XML Expression Language syntax.
32  */

33 public class JUnitPropertyTester extends PropertyTester {
34
35     private static final String JavaDoc PROPERTY_IS_TEST= "isTest"; //$NON-NLS-1$
36

37     private static final String JavaDoc PROPERTY_CAN_LAUNCH_AS_JUNIT_TEST= "canLaunchAsJUnit"; //$NON-NLS-1$
38

39     /* (non-Javadoc)
40      * @see org.eclipse.jdt.internal.corext.refactoring.participants.properties.IPropertyEvaluator#test(java.lang.Object, java.lang.String, java.lang.String)
41      */

42     public boolean test(Object JavaDoc receiver, String JavaDoc property, Object JavaDoc[] args, Object JavaDoc expectedValue) {
43         if (!(receiver instanceof IAdaptable)) {
44             throw new IllegalArgumentException JavaDoc("Element must be of type 'IAdaptable', is " + receiver == null ? "null" : receiver.getClass().getName()); //$NON-NLS-1$ //$NON-NLS-2$
45
}
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 { // is IAdaptable
56
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 JavaDoc("Unknown test property '" + property + "'"); //$NON-NLS-1$ //$NON-NLS-2$
72
}
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; // can run, let test runner detect if there are tests
80
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     /*
91      * Return whether the target resource is a JUnit test.
92      */

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             // ignore, return false
110
}
111         return false;
112     }
113 }
114
Popular Tags