KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > launcher > ResourceExtender


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.launcher;
12
13 import org.eclipse.core.expressions.PropertyTester;
14
15 import org.eclipse.core.resources.IResource;
16
17 import org.eclipse.jdt.core.ICompilationUnit;
18 import org.eclipse.jdt.core.IJavaElement;
19 import org.eclipse.jdt.core.IType;
20 import org.eclipse.jdt.core.JavaCore;
21 import org.eclipse.jdt.core.JavaModelException;
22 import org.eclipse.jdt.core.Signature;
23
24 import org.eclipse.jdt.internal.junit.util.TestSearchEngine;
25
26 /**
27  * ResourceExtender provides propertyTester(s) for IResource types
28  * for use in XML Expression Language syntax.
29  */

30 public class ResourceExtender extends PropertyTester {
31
32     private static final String JavaDoc PROPERTY_IS_TEST= "isTest"; //$NON-NLS-1$
33

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

37     public boolean test(Object JavaDoc receiver, String JavaDoc method, Object JavaDoc[] args, Object JavaDoc expectedValue) {
38         IResource resource= (IResource)receiver;
39         if (PROPERTY_IS_TEST.equals(method)) {
40             return isJUnitTest(resource);
41         }
42         throw new IllegalArgumentException JavaDoc("Method must be 'isTest' method"); //$NON-NLS-1$
43
}
44     
45     /*
46      * Return whether the target resource is a JUnit test.
47      */

48     private boolean isJUnitTest(IResource target) {
49         if (target != null) {
50             IJavaElement element = JavaCore.create(target);
51             if (element instanceof ICompilationUnit) {
52                 ICompilationUnit cu = (ICompilationUnit) element;
53                 IType mainType= cu.getType(Signature.getQualifier(cu.getElementName()));
54                 try {
55                     return TestSearchEngine.isTestOrTestSuite(mainType);
56                 } catch (JavaModelException e) {
57                     return false;
58                 }
59             }
60         }
61         return false;
62     }
63 }
64
Popular Tags