Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List packages and classes at server side #1240

Merged
merged 1 commit into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions java-extension/com.microsoft.java.test.plugin/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<command id="vscode.java.test.junit.argument" />
<command id="vscode.java.test.generateTests" />
<command id="vscode.java.test.findJavaProjects" />
<command id="vscode.java.test.findTestPackagesAndTypes" />
</delegateCommandHandler>
</extension>
</plugin>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017, 2019 Microsoft Corporation and others.
* Copyright (c) 2017-2021 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -29,6 +29,7 @@ public class TestDelegateCommandHandler implements IDelegateCommandHandler {
private static final String RESOLVE_JUNIT_ARGUMENT = "vscode.java.test.junit.argument";
private static final String GENERATE_TESTS = "vscode.java.test.generateTests";
private static final String FIND_JAVA_PROJECT = "vscode.java.test.findJavaProjects";
private static final String FIND_PACKAGES_AND_TYPES = "vscode.java.test.findTestPackagesAndTypes";

@Override
public Object executeCommand(String commandId, List<Object> arguments, IProgressMonitor monitor) throws Exception {
Expand All @@ -45,6 +46,8 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
return TestGenerationUtils.generateTests(arguments, monitor);
case FIND_JAVA_PROJECT:
return TestSearchUtils.findJavaProjects(arguments, monitor);
case FIND_PACKAGES_AND_TYPES:
return TestSearchUtils.findTestPackagesAndTypes(arguments, monitor);
default:
throw new UnsupportedOperationException(
String.format("Java test plugin doesn't support the command '%s'.", commandId));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018 Microsoft Corporation and others.
* Copyright (c) 2018-2021 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -11,16 +11,10 @@

package com.microsoft.java.test.plugin.searcher;

import com.microsoft.java.test.plugin.model.TestItem;
import com.microsoft.java.test.plugin.model.TestKind;
import com.microsoft.java.test.plugin.model.TestLevel;
import com.microsoft.java.test.plugin.util.TestFrameworkUtils;
import com.microsoft.java.test.plugin.util.TestItemUtils;

import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.IAnnotationBinding;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;

public abstract class BaseFrameworkSearcher implements TestFrameworkSearcher {
Expand All @@ -47,10 +41,4 @@ public boolean findAnnotation(IAnnotationBinding[] annotations, String[] annotat
}
return false;
}

@Override
public TestItem parseTestItem(IMethodBinding methodBinding) throws JavaModelException {
return TestItemUtils.constructTestItem((IMethod) methodBinding.getJavaElement(),
TestLevel.METHOD, this.getTestKind());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017 Microsoft Corporation and others.
* Copyright (c) 2017-2021 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -11,13 +11,11 @@

package com.microsoft.java.test.plugin.searcher;

import com.microsoft.java.test.plugin.model.TestItem;
import com.microsoft.java.test.plugin.model.TestKind;
import com.microsoft.java.test.plugin.model.TestLevel;
import com.microsoft.java.test.plugin.util.TestItemUtils;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
Expand All @@ -26,12 +24,9 @@
import org.eclipse.jdt.internal.junit.launcher.JUnit4TestFinder;
import org.eclipse.jdt.internal.junit.launcher.TestKindRegistry;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class JUnit4TestSearcher extends BaseFrameworkSearcher {

Expand Down Expand Up @@ -72,20 +67,14 @@ public boolean isTestClass(IType type) throws JavaModelException {
}

@Override
public TestItem[] findTestsInContainer(IJavaElement element, IProgressMonitor monitor) throws CoreException {
final Map<String, TestItem> result = new HashMap<>();
public Set<IType> findTestItemsInContainer(IJavaElement element, IProgressMonitor monitor) throws CoreException {
final Set<IType> types = new HashSet<>();
JUNIT4_TEST_FINDER.findTestsInContainer(element, types, monitor);
for (final IType type : types) {
final TestItem item = TestItemUtils.constructTestItem(type, TestLevel.CLASS, TestKind.JUnit);
item.setChildren(Arrays.stream(type.getMethods())
.map(m -> m.getJavaProject().getProject().getName() + "@" +
TestItemUtils.parseTestItemFullName(m, TestLevel.METHOD))
.collect(Collectors.toList())
);
result.put(item.getId(), item);
try {
JUNIT4_TEST_FINDER.findTestsInContainer(element, types, monitor);
} catch (OperationCanceledException e) {
return Collections.emptySet();
}

return result.values().toArray(new TestItem[0]);
return types;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017 Microsoft Corporation and others.
* Copyright (c) 2017-2021 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -11,14 +11,12 @@

package com.microsoft.java.test.plugin.searcher;

import com.microsoft.java.test.plugin.model.TestItem;
import com.microsoft.java.test.plugin.model.TestKind;
import com.microsoft.java.test.plugin.model.TestLevel;
import com.microsoft.java.test.plugin.util.TestFrameworkUtils;
import com.microsoft.java.test.plugin.util.TestItemUtils;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
Expand All @@ -29,12 +27,9 @@
import org.eclipse.jdt.internal.junit.launcher.JUnit5TestFinder;
import org.eclipse.jdt.internal.junit.launcher.TestKindRegistry;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class JUnit5TestSearcher extends BaseFrameworkSearcher {

Expand Down Expand Up @@ -74,24 +69,6 @@ public boolean isTestMethod(IMethodBinding methodBinding) {
return this.findAnnotation(methodBinding.getAnnotations(), this.getTestMethodAnnotations());
}

@Override
public TestItem parseTestItem(IMethodBinding methodBinding) throws JavaModelException {
final TestItem item = super.parseTestItem(methodBinding);

// deal with @DisplayName
for (final IAnnotationBinding annotation : methodBinding.getAnnotations()) {
if (annotation == null) {
continue;
}
if (matchesName(annotation.getAnnotationType(), DISPLAY_NAME_ANNOTATION_JUNIT5)) {
item.setDisplayName((String) annotation.getAllMemberValuePairs()[0].getValue());
break;
}
}

return item;
}

@Override
public boolean findAnnotation(IAnnotationBinding[] annotations, String[] annotationNames) {
for (final IAnnotationBinding annotation : annotations) {
Expand Down Expand Up @@ -119,24 +96,6 @@ public boolean isTestClass(IType type) throws JavaModelException {
return JUNIT5_TEST_FINDER.isTest(type);
}

@Override
public TestItem[] findTestsInContainer(IJavaElement element, IProgressMonitor monitor) throws CoreException {
final Map<String, TestItem> result = new HashMap<>();
final Set<IType> types = new HashSet<>();
JUNIT5_TEST_FINDER.findTestsInContainer(element, types, monitor);
for (final IType type : types) {
final TestItem item = TestItemUtils.constructTestItem(type, TestLevel.CLASS, TestKind.JUnit5);
item.setChildren(Arrays.stream(type.getMethods())
.map(m ->m.getJavaProject().getProject().getName() + "@" +
TestItemUtils.parseTestItemFullName(m, TestLevel.METHOD))
.collect(Collectors.toList())
);
result.put(item.getId(), item);
}

return result.values().toArray(new TestItem[0]);
}

private boolean matchesName(ITypeBinding annotationType, String annotationName) {
return TestFrameworkUtils.isEquivalentAnnotationType(annotationType, annotationName);
}
Expand All @@ -163,4 +122,15 @@ private boolean matchesNameInAnnotationHierarchy(IAnnotationBinding annotation,

return false;
}

@Override
public Set<IType> findTestItemsInContainer(IJavaElement element, IProgressMonitor monitor) throws CoreException {
final Set<IType> types = new HashSet<>();
try {
JUNIT5_TEST_FINDER.findTestsInContainer(element, types, monitor);
} catch (OperationCanceledException e) {
return Collections.emptySet();
}
return types;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017 Microsoft Corporation and others.
* Copyright (c) 2017-2021 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -11,7 +11,6 @@

package com.microsoft.java.test.plugin.searcher;

import com.microsoft.java.test.plugin.model.TestItem;
import com.microsoft.java.test.plugin.model.TestKind;

import org.eclipse.core.runtime.CoreException;
Expand All @@ -22,6 +21,8 @@
import org.eclipse.jdt.core.dom.IAnnotationBinding;
import org.eclipse.jdt.core.dom.IMethodBinding;

import java.util.Set;

public interface TestFrameworkSearcher {

TestKind getTestKind();
Expand All @@ -36,7 +37,5 @@ public interface TestFrameworkSearcher {

boolean findAnnotation(IAnnotationBinding[] annotations, String[] annotationNames);

TestItem parseTestItem(IMethodBinding methodBinding) throws JavaModelException;

TestItem[] findTestsInContainer(IJavaElement element, IProgressMonitor monitor) throws CoreException;
Set<IType> findTestItemsInContainer(IJavaElement element, IProgressMonitor monitor) throws CoreException;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018 Microsoft Corporation and others.
* Copyright (c) 2018-2021 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -11,14 +11,12 @@

package com.microsoft.java.test.plugin.searcher;

import com.microsoft.java.test.plugin.model.TestItem;
import com.microsoft.java.test.plugin.model.TestKind;
import com.microsoft.java.test.plugin.model.TestLevel;
import com.microsoft.java.test.plugin.util.TestItemUtils;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.IJavaElement;
Expand Down Expand Up @@ -49,13 +47,10 @@
import org.eclipse.jdt.core.search.SearchRequestor;
import org.eclipse.jdt.internal.junit.util.CoreTestSearchEngine;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class TestNGTestSearcher extends BaseFrameworkSearcher {

Expand Down Expand Up @@ -171,25 +166,6 @@ private boolean annotates(final IAnnotationBinding[] annotations, final String q
return false;
}

@Override
public TestItem[] findTestsInContainer(final IJavaElement element, final IProgressMonitor monitor)
throws CoreException {
final Map<String, TestItem> result = new HashMap<>();
final Set<IType> types = new HashSet<>();
findTestsInContainer(element, types, monitor);
for (final IType type : types) {
final TestItem item = TestItemUtils.constructTestItem(type, TestLevel.CLASS, TestKind.TestNG);
item.setChildren(
Arrays.stream(type.getMethods())
.map(m -> m.getJavaProject().getProject().getName() + "@" +
TestItemUtils.parseTestItemFullName(m, TestLevel.METHOD))
.collect(Collectors.toList()));
result.put(item.getId(), item);
}

return result.values().toArray(new TestItem[0]);
}

/*
* (non-Javadoc)
*
Expand Down Expand Up @@ -240,6 +216,17 @@ private void findTestsInContainer(final IJavaElement element, final Set result,
pm.done();
}
}

@Override
public Set<IType> findTestItemsInContainer(IJavaElement element, IProgressMonitor monitor) throws CoreException {
final Set<IType> types = new HashSet<>();
try {
this.findTestsInContainer(element, types, monitor);
} catch (OperationCanceledException e) {
return Collections.emptySet();
}
return types;
}
}

/*
Expand Down
Loading