From a6ff453fe54eb3a0e36fc1a635c9fde86d7f0f7e Mon Sep 17 00:00:00 2001 From: RoiSoleil Date: Thu, 28 Dec 2023 22:03:33 +0100 Subject: [PATCH] Test detection in code mining is not reliable #213 --- .../moreunit/codemining/JumpCodeMining.java | 56 ++++++------------- .../preferences/PreferenceConstants.java | 3 +- .../org/moreunit/preferences/Preferences.java | 1 + 3 files changed, 20 insertions(+), 40 deletions(-) diff --git a/org.moreunit.plugin/src/org/moreunit/codemining/JumpCodeMining.java b/org.moreunit.plugin/src/org/moreunit/codemining/JumpCodeMining.java index 79848c3c..49ea7a1d 100644 --- a/org.moreunit.plugin/src/org/moreunit/codemining/JumpCodeMining.java +++ b/org.moreunit.plugin/src/org/moreunit/codemining/JumpCodeMining.java @@ -6,11 +6,9 @@ import java.util.function.Consumer; import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.jdt.core.IClasspathEntry; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IMember; import org.eclipse.jdt.core.IMethod; -import org.eclipse.jdt.core.IPackageFragmentRoot; import org.eclipse.jdt.core.ISourceRange; import org.eclipse.jdt.core.ISourceReference; import org.eclipse.jdt.core.IType; @@ -58,22 +56,25 @@ private static int getLineNumber(IJavaElement element, IDocument document) throw @Override protected CompletableFuture doResolve(ITextViewer viewer, IProgressMonitor monitor) { - String testOrTested = isTest() ? "tested" : "test"; return CompletableFuture.runAsync(() -> { + IMember member = (IMember) element; + TypeFacade typeFacade = TypeFacade.createFacade(member.getCompilationUnit()); + String testOrTested = typeFacade instanceof TestCaseTypeFacade ? "tested" : "test"; if(element instanceof IType) { - MethodSearchMode searchMode = Preferences.getInstance().getMethodSearchMode(element.getJavaProject()); - - TypeFacade typeFacade = TypeFacade.createFacade(((IMember) element).getCompilationUnit()); - - CorrespondingMemberRequest request = newCorrespondingMemberRequest() // - .withExpectedResultType(MemberType.TYPE_OR_METHOD) // - .withCurrentMethod(element instanceof IMethod method ? method : null) // - .methodSearchMode(searchMode) // - .build(); - - IMember memberToJump = typeFacade.getOneCorrespondingMember(request); - if(memberToJump != null) + boolean jumpable = false; + if(typeFacade instanceof ClassTypeFacade) + { + ClassTypeFacade classTypeFacade = (ClassTypeFacade) typeFacade; + jumpable = classTypeFacade.hasTestCase(); + } + else if(typeFacade instanceof TestCaseTypeFacade) + { + TestCaseTypeFacade testCaseTypeFacade = (TestCaseTypeFacade) typeFacade; + IType correspondingClassUnderTest = testCaseTypeFacade.getCorrespondingClassUnderTest(); + jumpable = correspondingClassUnderTest != null; + } + if(jumpable) { setLabel("Jump to " + testOrTested + " class"); } @@ -85,17 +86,11 @@ protected CompletableFuture doResolve(ITextViewer viewer, IProgressMonitor else if(element instanceof IMethod) { IMethod method = (IMethod) element; - TestAnnotationMode testAnnotationMode = Preferences.forProject(method.getJavaProject()).getTestAnnotationMode(); - if(testAnnotationMode == TestAnnotationMode.OFF) - { - return; - } - TypeFacade typeFacade = TypeFacade.createFacade(((IMember) element).getCompilationUnit()); boolean jumpable = false; if(typeFacade instanceof ClassTypeFacade) { ClassTypeFacade classTypeFacade = (ClassTypeFacade) typeFacade; - jumpable = ! (classTypeFacade.getCorrespondingTestMethods(method, testAnnotationMode.getMethodSearchMode()).isEmpty()); + jumpable = ! (classTypeFacade.getCorrespondingTestMethods(method, TestAnnotationMode.BY_CALL_AND_BY_NAME.getMethodSearchMode()).isEmpty()); } else if(typeFacade instanceof TestCaseTypeFacade) { @@ -118,23 +113,6 @@ else if(typeFacade instanceof TestCaseTypeFacade) }); } - private boolean isTest() - { - IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); - if(packageFragmentRoot != null) - { - try - { - IClasspathEntry resolvedClasspathEntry = packageFragmentRoot.getResolvedClasspathEntry(); - return resolvedClasspathEntry != null && resolvedClasspathEntry.isTest(); - } - catch (JavaModelException e) - { - } - } - return false; - } - @Override public Consumer getAction() { diff --git a/org.moreunit.plugin/src/org/moreunit/preferences/PreferenceConstants.java b/org.moreunit.plugin/src/org/moreunit/preferences/PreferenceConstants.java index 70de21ec..c27989e4 100644 --- a/org.moreunit.plugin/src/org/moreunit/preferences/PreferenceConstants.java +++ b/org.moreunit.plugin/src/org/moreunit/preferences/PreferenceConstants.java @@ -46,6 +46,7 @@ public interface PreferenceConstants String DEFAULT_TEST_CLASS_NAME_TEMPLATE = TestFileNamePattern.SRC_FILE_VARIABLE + "(Test|IT)"; String DEFAULT_TEST_METHOD_DEFAULT_CONTENT = "throw new RuntimeException(\"not yet implemented\");"; String DEFAULT_TEST_ANNOTATION_MODE = "OFF"; + boolean DEFAULT_ENABLE_MOREUNIT_CODEMINING = true; String TEXT_GENERAL_SETTINGS = "General settings for your unit tests (they can then be refined for each project):"; String TEXT_TEST_SUPERCLASS = "Test superclass:"; @@ -63,7 +64,7 @@ public interface PreferenceConstants String TEXT_EXTENDED_TEST_METHOD_SEARCH = "Enable test method search \"by call\""; String TEXT_ENABLE_TEST_METHOD_SEARCH_BY_NAME = "Enable test method search \"by name\""; String TEXT_GENERATE_COMMENTS_FOR_TEST_METHOD = "Generate comments for test methods"; - String TEXT_ENABLE_MOREUNIT_CODEMINING = "Enable MoreUnit Code Mining (Beta)"; + String TEXT_ENABLE_MOREUNIT_CODEMINING = "Enable MoreUnit Code Mining"; String TEXT_ANNOTATION_MODE = "Annotate tested methods"; String TEST_ANNOTATION_MODE_DISABLED = "Disabled"; String TEST_ANNOTATION_MODE_BY_NAME = "Search by method name"; diff --git a/org.moreunit.plugin/src/org/moreunit/preferences/Preferences.java b/org.moreunit.plugin/src/org/moreunit/preferences/Preferences.java index fa1c03b8..4f3e04f6 100644 --- a/org.moreunit.plugin/src/org/moreunit/preferences/Preferences.java +++ b/org.moreunit.plugin/src/org/moreunit/preferences/Preferences.java @@ -79,6 +79,7 @@ protected static final IPreferenceStore initStore(IPreferenceStore store) store.setDefault(PreferenceConstants.TEST_CLASS_NAME_TEMPLATE, PreferenceConstants.DEFAULT_TEST_CLASS_NAME_TEMPLATE); store.setDefault(PreferenceConstants.TEST_METHOD_DEFAULT_CONTENT, PreferenceConstants.DEFAULT_TEST_METHOD_DEFAULT_CONTENT); store.setDefault(PreferenceConstants.TEST_ANNOTATION_MODE, PreferenceConstants.DEFAULT_TEST_ANNOTATION_MODE); + store.setDefault(PreferenceConstants.ENABLE_MOREUNIT_CODE_MINING, PreferenceConstants.DEFAULT_ENABLE_MOREUNIT_CODEMINING); return store; }