From fcd5e310d54ede82ee48eeba89fbe12cd85c2c9e Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Mon, 24 May 2021 15:33:41 +0800 Subject: [PATCH 1/2] fix: Can select peer project if current project does not contain test framework --- .../test/plugin/util/TestGenerationUtils.java | 44 +++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/java-extension/com.microsoft.java.test.plugin/src/main/java/com/microsoft/java/test/plugin/util/TestGenerationUtils.java b/java-extension/com.microsoft.java.test.plugin/src/main/java/com/microsoft/java/test/plugin/util/TestGenerationUtils.java index 250cb823..5195dbd1 100644 --- a/java-extension/com.microsoft.java.test.plugin/src/main/java/com/microsoft/java/test/plugin/util/TestGenerationUtils.java +++ b/java-extension/com.microsoft.java.test.plugin/src/main/java/com/microsoft/java/test/plugin/util/TestGenerationUtils.java @@ -61,6 +61,7 @@ import org.eclipse.jdt.ls.core.internal.ChangeUtil; import org.eclipse.jdt.ls.core.internal.JDTUtils; import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; +import org.eclipse.jdt.ls.core.internal.ProjectUtils; import org.eclipse.jdt.ls.core.internal.handlers.CodeGenerationUtils; import org.eclipse.jdt.ls.core.internal.text.correction.SourceAssistProcessor; import org.eclipse.lsp4j.MessageType; @@ -180,14 +181,17 @@ public static WorkspaceEdit generateTests(List arguments, IProgressMonit */ private static WorkspaceEdit generateTestsFromSource(ICompilationUnit unit, ITypeBinding typeBinding, int cursorOffset) throws CoreException { - final IJavaProject javaProject = unit.getJavaProject(); + final IJavaProject javaProject = determineTestProject(unit); + if (javaProject == null) { + return null; + } final List testFrameworksInProject = TestKindProvider.getTestKindsFromCache(javaProject); final TestKind testKind = determineTestFramework(new HashSet<>(testFrameworksInProject)); if (testKind == null) { return null; } - final IClasspathEntry testEntry = getTestClasspathEntry(unit); + final IClasspathEntry testEntry = getTestClasspathEntry(javaProject, unit); if (testEntry == null) { JavaLanguageServerPlugin.getInstance().getClientConnection().showNotificationMessage(MessageType.Error, "Cannot find a valid classpath entry to generate tests."); @@ -352,8 +356,8 @@ private static String constructMethodStub(ICompilationUnit testUnit, TestKind te lineDelimiter, testUnit.getJavaProject().getOptions(true)); } - private static IClasspathEntry getTestClasspathEntry(ICompilationUnit unit) throws JavaModelException { - final IJavaProject javaProject = unit.getJavaProject(); + private static IClasspathEntry getTestClasspathEntry(IJavaProject javaProject, ICompilationUnit unit) + throws JavaModelException { // In most cases, this is the classpath entry used for testing, we first find the target entry by hard-code // to avoid go into the generated entries. IClasspathEntry testEntry = javaProject.getClasspathEntryFor( @@ -563,6 +567,38 @@ private static List determineMethodsToGenerate(List lifecycleAnn methodList, true /*pickMany*/); } + private static IJavaProject determineTestProject(ICompilationUnit unit) { + final IJavaProject javaProject = unit.getJavaProject(); + if (TestKindProvider.getTestKindsFromCache(javaProject).size() > 0) { + return javaProject; + } + + final IJavaProject[] javaProjects = ProjectUtils.getJavaProjects(); + final List