From 2feae9cc9b34c72224d7e150cbad7c8db2474a61 Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Fri, 31 May 2019 14:36:18 +0800 Subject: [PATCH] refactor: Remove the Test Level: NESTED_CLASS (#717) --- .../java/test/plugin/model/TestLevel.java | 3 -- .../searcher/BaseFrameworkSearcher.java | 2 +- .../java/test/plugin/util/TestItemUtils.java | 10 ----- .../test/plugin/util/TestSearchUtils.java | 41 ++++--------------- src/codeLensProvider.ts | 3 +- src/explorer/testExplorer.ts | 1 - src/protocols.ts | 3 +- src/runners/junit5Runner/JUnit5Runner.ts | 2 +- 8 files changed, 13 insertions(+), 52 deletions(-) diff --git a/java-extension/com.microsoft.java.test.plugin/src/main/java/com/microsoft/java/test/plugin/model/TestLevel.java b/java-extension/com.microsoft.java.test.plugin/src/main/java/com/microsoft/java/test/plugin/model/TestLevel.java index 92cd0f1e..1d45f6b1 100644 --- a/java-extension/com.microsoft.java.test.plugin/src/main/java/com/microsoft/java/test/plugin/model/TestLevel.java +++ b/java-extension/com.microsoft.java.test.plugin/src/main/java/com/microsoft/java/test/plugin/model/TestLevel.java @@ -27,8 +27,5 @@ public enum TestLevel { CLASS, @SerializedName("4") - NESTED_CLASS, - - @SerializedName("5") METHOD; } diff --git a/java-extension/com.microsoft.java.test.plugin/src/main/java/com/microsoft/java/test/plugin/searcher/BaseFrameworkSearcher.java b/java-extension/com.microsoft.java.test.plugin/src/main/java/com/microsoft/java/test/plugin/searcher/BaseFrameworkSearcher.java index 1eedf580..9215ec1c 100644 --- a/java-extension/com.microsoft.java.test.plugin/src/main/java/com/microsoft/java/test/plugin/searcher/BaseFrameworkSearcher.java +++ b/java-extension/com.microsoft.java.test.plugin/src/main/java/com/microsoft/java/test/plugin/searcher/BaseFrameworkSearcher.java @@ -81,6 +81,6 @@ public TestItem parseTestItem(IMethod method) throws JavaModelException { @Override public TestItem parseTestItem(IType type) throws JavaModelException { - return TestItemUtils.constructTestItem(type, TestItemUtils.getTestLevelForIType(type), this.getTestKind()); + return TestItemUtils.constructTestItem(type, TestLevel.CLASS, this.getTestKind()); } } diff --git a/java-extension/com.microsoft.java.test.plugin/src/main/java/com/microsoft/java/test/plugin/util/TestItemUtils.java b/java-extension/com.microsoft.java.test.plugin/src/main/java/com/microsoft/java/test/plugin/util/TestItemUtils.java index 4e23202e..f176a7ed 100644 --- a/java-extension/com.microsoft.java.test.plugin/src/main/java/com/microsoft/java/test/plugin/util/TestItemUtils.java +++ b/java-extension/com.microsoft.java.test.plugin/src/main/java/com/microsoft/java/test/plugin/util/TestItemUtils.java @@ -15,7 +15,6 @@ import com.microsoft.java.test.plugin.model.TestKind; import com.microsoft.java.test.plugin.model.TestLevel; -import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IMethod; import org.eclipse.jdt.core.IPackageFragment; @@ -55,14 +54,6 @@ public static TestItem constructTestItem(IJavaElement element, TestLevel level, return new TestItem(displayName, fullName, uri, projectName, Collections.emptyList(), range, level, kind); } - public static TestLevel getTestLevelForIType(IType type) { - if (type.getParent() instanceof ICompilationUnit) { - return TestLevel.CLASS; - } else { - return TestLevel.NESTED_CLASS; - } - } - public static Range parseTestItemRange(IJavaElement element) throws JavaModelException { if (element instanceof ISourceReference) { final ISourceRange range = ((ISourceReference) element).getNameRange(); @@ -74,7 +65,6 @@ public static Range parseTestItemRange(IJavaElement element) throws JavaModelExc private static String parseTestItemFullName(IJavaElement element, TestLevel level) { switch (level) { case CLASS: - case NESTED_CLASS: final IType type = (IType) element; return type.getFullyQualifiedName(); case METHOD: diff --git a/java-extension/com.microsoft.java.test.plugin/src/main/java/com/microsoft/java/test/plugin/util/TestSearchUtils.java b/java-extension/com.microsoft.java.test.plugin/src/main/java/com/microsoft/java/test/plugin/util/TestSearchUtils.java index 7e44c0b1..cbd88686 100644 --- a/java-extension/com.microsoft.java.test.plugin/src/main/java/com/microsoft/java/test/plugin/util/TestSearchUtils.java +++ b/java-extension/com.microsoft.java.test.plugin/src/main/java/com/microsoft/java/test/plugin/util/TestSearchUtils.java @@ -103,7 +103,7 @@ public static List searchCodeLens(List arguments, IProgressMon } }).filter(Objects::nonNull).collect(Collectors.toList()); if (testMethodList.size() > 0) { - final TestItem parent = TestItemUtils.constructTestItem(type, TestItemUtils.getTestLevelForIType(type)); + final TestItem parent = TestItemUtils.constructTestItem(type, TestLevel.CLASS); parent.setChildren(testMethodList); // Assume the kinds of all methods are the same. parent.setKind(testMethodList.get(0).getKind()); @@ -112,8 +112,7 @@ public static List searchCodeLens(List arguments, IProgressMon } // Class annotated by @RunWith should be considered as a Suite even it has no test method children if (TestFrameworkUtils.hasAnnotation(type, JUnit4TestSearcher.RUN_WITH)) { - resultList.add(TestItemUtils.constructTestItem(type, TestItemUtils.getTestLevelForIType(type), - TestKind.JUnit)); + resultList.add(TestItemUtils.constructTestItem(type, TestLevel.CLASS, TestKind.JUnit)); } } @@ -150,9 +149,6 @@ public static List searchTestItems(List arguments, IProgressMo case CLASS: searchInClass(resultList, params); break; - case NESTED_CLASS: - searchInNestedClass(resultList, params); - break; default: break; } @@ -210,8 +206,7 @@ public void acceptSearchMatch(SearchMatch match) throws CoreException { if (classItem != null) { classItem.addChild(methodItem); } else { - final TestItem newClassItem = TestItemUtils.constructTestItem(type, - TestItemUtils.getTestLevelForIType(type)); + final TestItem newClassItem = TestItemUtils.constructTestItem(type, TestLevel.CLASS); newClassItem.addChild(methodItem); classMap.put(type.getFullyQualifiedName(), newClassItem); } @@ -315,7 +310,6 @@ private static IJavaSearchScope createSearchScope(SearchTestItemParams params) return SearchEngine.createJavaSearchScope(new IJavaElement[] { packageElement }, IJavaSearchScope.SOURCES); case CLASS: - case NESTED_CLASS: final ICompilationUnit compilationUnit = JDTUtils.resolveCompilationUnit(params.getUri()); final IType[] types = compilationUnit.getAllTypes(); for (final IType type : types) { @@ -395,10 +389,14 @@ private static IPackageFragment resolvePackage(String uriString, String fullName private static void searchInClass(List resultList, SearchTestItemParams params) throws JavaModelException { final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getUri()); - for (final IType type : unit.getTypes()) { + for (final IType type : unit.getAllTypes()) { if (type.getFullyQualifiedName().equals(params.getFullName())) { for (final IType innerType : type.getTypes()) { - resultList.add(TestItemUtils.constructTestItem(innerType, TestLevel.NESTED_CLASS)); + resultList.add(TestItemUtils.constructTestItem(innerType, TestLevel.CLASS)); + } + + if (!isTestableClass(type)) { + continue; } for (final IMethod method : type.getMethods()) { final TestItem item = TestFrameworkUtils.resoveTestItemForMethod(method); @@ -410,27 +408,6 @@ private static void searchInClass(List resultList, SearchTestItemParam } } - private static void searchInNestedClass(List resultList, SearchTestItemParams params) - throws JavaModelException { - final ICompilationUnit compilationUnit = JDTUtils.resolveCompilationUnit(params.getUri()); - for (final IType type : compilationUnit.getAllTypes()) { - if (type.getFullyQualifiedName().equals(params.getFullName())) { - resultList.addAll(searchTestMethodsOfType(type)); - } - } - } - - private static List searchTestMethodsOfType(IType type) throws JavaModelException { - final List results = new LinkedList<>(); - for (final IMethod method : type.getMethods()) { - final TestItem item = TestFrameworkUtils.resoveTestItemForMethod(method); - if (item != null) { - results.add(item); - } - } - return results; - } - private static boolean isTestableClass(IType type) throws JavaModelException { final int flags = type.getFlags(); if (Flags.isInterface(flags) || Flags.isAbstract(flags)) { diff --git a/src/codeLensProvider.ts b/src/codeLensProvider.ts index 246139e6..7c7c33c2 100644 --- a/src/codeLensProvider.ts +++ b/src/codeLensProvider.ts @@ -88,7 +88,6 @@ class TestCodeLensProvider implements CodeLensProvider { } break; case TestLevel.Class: - case TestLevel.NestedClass: if (!test.children) { break; } @@ -147,7 +146,7 @@ class TestCodeLensProvider implements CodeLensProvider { if (item.level === TestLevel.Method) { return 1; } - if (item.level === TestLevel.Class || item.level === TestLevel.NestedClass) { + if (item.level === TestLevel.Class) { if (item.children) { return item.children.length; } diff --git a/src/explorer/testExplorer.ts b/src/explorer/testExplorer.ts index 2006ac97..4284dcac 100644 --- a/src/explorer/testExplorer.ts +++ b/src/explorer/testExplorer.ts @@ -84,7 +84,6 @@ export class TestExplorer implements TreeDataProvider { light: this._context.asAbsolutePath(path.join('resources', 'media', 'light', 'method.svg')), }; case TestLevel.Class: - case TestLevel.NestedClass: return { dark: this._context.asAbsolutePath(path.join('resources', 'media', 'dark', 'class.svg')), light: this._context.asAbsolutePath(path.join('resources', 'media', 'light', 'class.svg')), diff --git a/src/protocols.ts b/src/protocols.ts index ceebd737..0d25e018 100644 --- a/src/protocols.ts +++ b/src/protocols.ts @@ -35,8 +35,7 @@ export enum TestLevel { Folder = 1, Package = 2, Class = 3, - NestedClass = 4, - Method = 5, + Method = 4, } export enum TestKind { diff --git a/src/runners/junit5Runner/JUnit5Runner.ts b/src/runners/junit5Runner/JUnit5Runner.ts index 7cde579a..0e2bf723 100644 --- a/src/runners/junit5Runner/JUnit5Runner.ts +++ b/src/runners/junit5Runner/JUnit5Runner.ts @@ -18,7 +18,7 @@ export class JUnit5Runner extends BaseRunner { private constructParamsForTests(): string[] { const params: string[] = []; for (const test of this.tests) { - if (test.level === TestLevel.Class || test.level === TestLevel.NestedClass) { + if (test.level === TestLevel.Class) { params.push('-c', test.fullName); } else if (test.level === TestLevel.Method) { params.push('-m', `${test.fullName}(${test.paramTypes.join(',')})`);