KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > ui > JavaTypeExtender


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ui;
12
13 import org.eclipse.core.runtime.IAdaptable;
14
15 import org.eclipse.core.expressions.PropertyTester;
16
17 import org.eclipse.jdt.core.IClassFile;
18 import org.eclipse.jdt.core.ICompilationUnit;
19 import org.eclipse.jdt.core.IJavaElement;
20 import org.eclipse.jdt.core.IMember;
21 import org.eclipse.jdt.core.IType;
22 import org.eclipse.jdt.core.JavaModelException;
23 import org.eclipse.jdt.core.Signature;
24
25 import org.eclipse.jdt.internal.junit.util.TestSearchEngine;
26
27 /**
28  * Contributes an "isTest" property for ITypes.
29  */

30 public class JavaTypeExtender extends PropertyTester {
31     private static final String JavaDoc PROPERTY_IS_Test= "isTest"; //$NON-NLS-1$
32
/**
33      * @inheritDoc
34      */

35     public boolean test(Object JavaDoc receiver, String JavaDoc method, Object JavaDoc[] args, Object JavaDoc expectedValue) {
36         IJavaElement javaElement = null;
37         if (receiver instanceof IAdaptable) {
38             javaElement = (IJavaElement) ((IAdaptable)receiver).getAdapter(IJavaElement.class);
39         }
40         if (javaElement != null) {
41             if (!javaElement.exists()) {
42                 return false;
43             }
44         }
45         if (javaElement != null) {
46             if (PROPERTY_IS_Test.equals(method)) {
47                 return isTest(javaElement);
48             }
49         }
50         return false;
51     }
52
53     private boolean isTest(IJavaElement element) {
54         try {
55             IType testType = null;
56             if (element instanceof ICompilationUnit) {
57                 ICompilationUnit cu = (ICompilationUnit) element;
58                 testType= cu.getType(Signature.getQualifier(cu.getElementName()));
59             } else if (element instanceof IClassFile) {
60                     testType = ((IClassFile)element).getType();
61             } else if (element instanceof IType) {
62                 testType = (IType) element;
63             } else if (element instanceof IMember) {
64                 testType = ((IMember)element).getDeclaringType();
65             }
66             if (testType != null && testType.exists() && TestSearchEngine.isTestOrTestSuite(testType)) {
67                 return true;
68             }
69         } catch (JavaModelException e) {
70         }
71         return false;
72     }
73 }
74
Popular Tags