diff --git a/org.eclipse.jdt.junit.core/.gitignore b/org.eclipse.jdt.junit.core/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/org.eclipse.jdt.junit.core/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/model/TestCaseElement.java b/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/model/TestCaseElement.java index c4e1dee..a44e440 100644 --- a/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/model/TestCaseElement.java +++ b/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/model/TestCaseElement.java @@ -7,7 +7,9 @@ * * Contributors: * IBM Corporation - initial API and implementation - * Xavier Coulon - https://bugs.eclipse.org/bugs/show_bug.cgi?id=102512 - [JUnit] test method name cut off before ( + * Xavier Coulon + * - [JUnit] test method name cut off before '(' - https://bugs.eclipse.org/bugs/show_bug.cgi?id=102512 + * - [JUnit] Add "Link with Editor" to JUnit view - https://bugs.eclipse.org/bugs/show_bug.cgi?id=372588 *******************************************************************************/ package org.eclipse.jdt.internal.junit.model; @@ -15,15 +17,47 @@ import org.eclipse.jdt.junit.model.ITestCaseElement; import org.eclipse.core.runtime.Assert; +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IMethod; +import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.JavaModelException; -public class TestCaseElement extends TestElement implements ITestCaseElement { +import org.eclipse.jdt.internal.junit.JUnitCorePlugin; + +/** + * + * @since 3.7 implements {@link IAdaptable} + */ +public class TestCaseElement extends TestElement implements ITestCaseElement, IAdaptable { + + private IMethod fJavaMethod= null; + + private boolean fJavaMethodResolved= false; private boolean fIgnored; public TestCaseElement(TestSuiteElement parent, String id, String testName) { super(parent, id, testName); Assert.isNotNull(parent); + } + + /** + * @return the name of the Java Method associated with this {@link TestCaseElement}, ie, it + * returns the valid java identifier part of the name (in particular, it removes the + * brackets suffix for Parameterized JUnit tests). + * + * + */ + private String getJavaTestMethodName() { + String testMethodName= getTestMethodName(); + for (int i= 0; i < testMethodName.length(); i++) { + if (!Character.isJavaIdentifierPart(testMethodName.charAt(i))) { + return testMethodName.substring(0, i); + } + } + return testMethodName; } /** @@ -41,6 +75,36 @@ if (index > 0) return testName.substring(0, index); return testName; + } + + /** + * Finds and returns the {@link IMethod} associated with this {@link TestCaseElement}. + * + * @return the corresponding Java method element or null if not found. + * @since 3.7 + */ + public IMethod getJavaMethod() { + if (!fJavaMethodResolved) { + try { + final IType type= getJavaType(); + if (type != null) { + final IMethod[] methods= type.getMethods(); + String testMethodName= getJavaTestMethodName(); + for (int i= 0; i < methods.length; i++) { + if (methods[i].getElementName().equals(testMethodName)) { + fJavaMethod= methods[i]; + return methods[i]; + } + } + } + return null; + } catch (JavaModelException e) { + JUnitCorePlugin.log(e); + } finally { + fJavaMethodResolved= true; + } + } + return fJavaMethod; } /** @@ -73,4 +137,19 @@ public String toString() { return "TestCase: " + getTestClassName() + "." + getTestMethodName() + " : " + super.toString(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } + + /** + * Provides support for converting this {@link TestCaseElement} into an {@link IMethod} when the + * given adapter class is {@link IJavaElement}. + * + * @param adapter the class in which this {@link TestCaseElement} should be adapted. + * @return an object in the request type, or null if it could not be adapted. + * @since 3.7 + */ + public Object getAdapter(Class adapter) { + if (adapter != null && adapter.equals(IJavaElement.class)) { + return getJavaMethod(); + } + return null; + } } diff --git a/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/model/TestElement.java b/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/model/TestElement.java index 0987059..a754db1 100644 --- a/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/model/TestElement.java +++ b/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/model/TestElement.java @@ -20,6 +20,11 @@ import org.eclipse.core.runtime.Assert; +import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.JavaModelException; + +import org.eclipse.jdt.internal.junit.JUnitCorePlugin; + public abstract class TestElement implements ITestElement { public final static class Status { @@ -175,6 +180,10 @@ private boolean fAssumptionFailed; + private IType fJavaType= null; + + private boolean fJavaTypeResolved= false; + /** * Running time in seconds. Contents depend on the current {@link #getProgressState()}: *