### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.junit diff --git src/org/eclipse/jdt/internal/junit/ui/JUnitMessages.java src/org/eclipse/jdt/internal/junit/ui/JUnitMessages.java index 7e80c1f..ccf4d97 100644 --- src/org/eclipse/jdt/internal/junit/ui/JUnitMessages.java +++ src/org/eclipse/jdt/internal/junit/ui/JUnitMessages.java @@ -218,6 +218,8 @@ public static String OpenEditorAction_message_cannotopen; public static String OpenTestAction_error_methodNoFound; public static String OpenTestAction_error_title; + public static String OpenTestAction_title; + public static String OpenTestAction_select_element; public static String RerunAction_label_debug; public static String RerunAction_label_run; public static String RerunAction_label_rerun; diff --git src/org/eclipse/jdt/internal/junit/ui/JUnitMessages.properties src/org/eclipse/jdt/internal/junit/ui/JUnitMessages.properties index 9e5333b..9bb07b2 100644 --- src/org/eclipse/jdt/internal/junit/ui/JUnitMessages.properties +++ src/org/eclipse/jdt/internal/junit/ui/JUnitMessages.properties @@ -64,6 +64,8 @@ OpenTestAction_error_title=Go To Test OpenTestAction_error_methodNoFound=Method ''{0}'' not found. Opening the test class. +OpenTestAction_title=Go to Test +OpenTestAction_select_element=&Select or enter the element to open: TestRunnerViewPart_jobName=Update JUnit diff --git src/org/eclipse/jdt/internal/junit/ui/OpenTestAction.java src/org/eclipse/jdt/internal/junit/ui/OpenTestAction.java index 4d7e7ad..c3e8598 100644 --- src/org/eclipse/jdt/internal/junit/ui/OpenTestAction.java +++ src/org/eclipse/jdt/internal/junit/ui/OpenTestAction.java @@ -7,9 +7,12 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Andreas Schmid, service@aaschmid.de - Locate test method even if it contains parameters - http://bugs.eclipse.org/343935 *******************************************************************************/ package org.eclipse.jdt.internal.junit.ui; +import java.util.ArrayList; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -24,6 +27,7 @@ import org.eclipse.ui.texteditor.ITextEditor; +import org.eclipse.jdt.core.IAnnotation; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.IMethod; @@ -39,6 +43,8 @@ import org.eclipse.jdt.internal.junit.Messages; import org.eclipse.jdt.internal.junit.model.TestCaseElement; import org.eclipse.jdt.internal.junit.model.TestElement; + +import org.eclipse.jdt.internal.ui.actions.SelectionConverter; /** * Open a class on a Test method. @@ -106,35 +112,72 @@ if (fMethodName == null) return type; - IMethod method= findMethod(type); - if (method == null) { + List methods= findMethod(type); + if (methods.isEmpty()) { ITypeHierarchy typeHierarchy= type.newSupertypeHierarchy(null); IType[] supertypes= typeHierarchy.getAllSuperclasses(type); for (IType supertype : supertypes) { - method= findMethod(supertype); - if (method != null) + methods= findMethod(supertype); + if (!methods.isEmpty()) break; } } - if (method == null) { + + IMethod method = null; + if (methods.isEmpty()) { String title= JUnitMessages.OpenTestAction_error_title; String message= Messages.format(JUnitMessages.OpenTestAction_error_methodNoFound, BasicElementLabels.getJavaElementName(fMethodName)); MessageDialog.openInformation(getShell(), title, message); - return type; + + } else if (methods.size() > 1) { + IMethod[] elements= methods.toArray(new IMethod[methods.size()]); + String title= JUnitMessages.OpenTestAction_title; + String message= JUnitMessages.OpenTestAction_select_element; + + method= (IMethod)SelectionConverter.selectJavaElement(elements, getShell(), title, message); + + } else { + method= methods.get(0); } + if (method == null) + return type; fMethod= method; return method; } - private IMethod findMethod(IType type) { + private List findMethod(IType type) { IStatus status= JavaConventionsUtil.validateMethodName(fMethodName, type); if (! status.isOK()) return null; + + List result= new ArrayList(); + IMethod method= type.getMethod(fMethodName, new String[0]); if (method != null && method.exists()) - return method; - return null; + result.add(method); + + else { + // search just by name, if method not found yet (for custom runner with test methods having parameters) + try { + for (IMethod method2 : type.getMethods()) { + String methodName= method2.getElementName(); + IAnnotation methodAnnotation= method2.getAnnotation("Test"); + + // JUnit3 test method starts with "test" or JUnit4 test method is annotated with "@Test" + if (!(methodName.startsWith("test") || (methodAnnotation != null && methodAnnotation.exists()))) { + continue; + } + + if (fMethodName.equals(methodName)) + result.add(method2); + + } + } catch (JavaModelException e) { + // if type does not exist or if an exception occurs while accessing its resource => ignore (no method found) + } + } + return result; } @Override