From f1dfac53de5ac9c7dc5002010f5bb091cf88927f Mon Sep 17 00:00:00 2001 From: Zsombor Date: Thu, 28 Nov 2024 00:12:43 +0100 Subject: [PATCH] Fix clicking on a ParametrizedTest (#1735) * Fix clicking on a ParametrizedTest When we click on the 'grouping' element for a ParametrizedTest, the code tries to find the method without the parameter types, but the search actually tries to find a parameter-less method with the same name. * Fix test result navigation in 'Generic Test view' * Fix \0 handling in the test parameters Previously if the variable contained a '\0' the rest of the string was not displayed, with this fix, the rest of the string shows up too * Version bump(s) for 4.35 stream --------- Co-authored-by: Eclipse JDT Bot --- .../internal/junit/model/TestRunSession.java | 12 +++---- .../META-INF/MANIFEST.MF | 2 +- org.eclipse.jdt.junit.runtime/pom.xml | 2 +- .../junit/runner/RemoteTestRunner.java | 32 ++++++----------- .../jdt/internal/junit/ui/TestViewer.java | 4 +-- .../feature.xml | 2 +- .../pom.xml | 2 +- .../META-INF/MANIFEST.MF | 2 +- org.eclipse.jdt.ui.unittest.junit/pom.xml | 2 +- .../launcher/JUnitRemoteTestRunnerClient.java | 2 +- .../junit/ui/JUnitTestViewSupport.java | 34 +++++++++++++------ 11 files changed, 50 insertions(+), 46 deletions(-) diff --git a/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/model/TestRunSession.java b/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/model/TestRunSession.java index 31dcdf5cf41..46bc7f88b97 100644 --- a/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/model/TestRunSession.java +++ b/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/model/TestRunSession.java @@ -482,7 +482,7 @@ private TestElement addTreeEntry(String treeEntry) { int index0= treeEntry.indexOf(','); String id= treeEntry.substring(0, index0); - StringBuffer testNameBuffer= new StringBuffer(100); + StringBuilder testNameBuffer= new StringBuilder(100); int index1= scanTestName(treeEntry, index0 + 1, testNameBuffer); String testName= testNameBuffer.toString().trim(); @@ -493,11 +493,11 @@ private TestElement addTreeEntry(String treeEntry) { boolean isDynamicTest; String parentId; String displayName; - StringBuffer displayNameBuffer= new StringBuffer(100); + StringBuilder displayNameBuffer= new StringBuilder(100); String[] parameterTypes; - StringBuffer parameterTypesBuffer= new StringBuffer(200); + StringBuilder parameterTypesBuffer= new StringBuilder(200); String uniqueId; - StringBuffer uniqueIdBuffer= new StringBuffer(200); + StringBuilder uniqueIdBuffer= new StringBuilder(200); int index3= treeEntry.indexOf(',', index2 + 1); if (index3 == -1) { testCount= Integer.parseInt(treeEntry.substring(index2 + 1)); @@ -519,7 +519,7 @@ private TestElement addTreeEntry(String treeEntry) { } int index6= scanTestName(treeEntry, index5 + 1, displayNameBuffer); - displayName= displayNameBuffer.toString().trim(); + displayName= displayNameBuffer.toString().replace('\0', ' ').trim(); if (displayName.equals(testName)) { displayName= null; } @@ -592,7 +592,7 @@ public TestElement createTestElement(TestSuiteElement parent, String id, String * * @return the index of the next ',' */ - private int scanTestName(String s, int start, StringBuffer testName) { + private int scanTestName(String s, int start, StringBuilder testName) { boolean inQuote= false; int i= start; for (; i < s.length(); i++) { diff --git a/org.eclipse.jdt.junit.runtime/META-INF/MANIFEST.MF b/org.eclipse.jdt.junit.runtime/META-INF/MANIFEST.MF index 982475a7b40..79e47d95874 100644 --- a/org.eclipse.jdt.junit.runtime/META-INF/MANIFEST.MF +++ b/org.eclipse.jdt.junit.runtime/META-INF/MANIFEST.MF @@ -3,7 +3,7 @@ Automatic-Module-Name: org.eclipse.jdt.junit.runtime Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.jdt.junit.runtime;singleton:=true -Bundle-Version: 3.7.500.qualifier +Bundle-Version: 3.7.600.qualifier Bundle-Vendor: %providerName Bundle-Localization: plugin Export-Package: diff --git a/org.eclipse.jdt.junit.runtime/pom.xml b/org.eclipse.jdt.junit.runtime/pom.xml index d1add87a0e4..9dd501ade2e 100644 --- a/org.eclipse.jdt.junit.runtime/pom.xml +++ b/org.eclipse.jdt.junit.runtime/pom.xml @@ -18,7 +18,7 @@ org.eclipse.jdt org.eclipse.jdt.junit.runtime - 3.7.500-SNAPSHOT + 3.7.600-SNAPSHOT eclipse-plugin diff --git a/org.eclipse.jdt.junit.runtime/src/org/eclipse/jdt/internal/junit/runner/RemoteTestRunner.java b/org.eclipse.jdt.junit.runtime/src/org/eclipse/jdt/internal/junit/runner/RemoteTestRunner.java index 8e9b6390cd7..d0429e9ce2e 100644 --- a/org.eclipse.jdt.junit.runtime/src/org/eclipse/jdt/internal/junit/runner/RemoteTestRunner.java +++ b/org.eclipse.jdt.junit.runtime/src/org/eclipse/jdt/internal/junit/runner/RemoteTestRunner.java @@ -386,15 +386,19 @@ public void setLoader(ITestLoader newInstance) { fLoader = newInstance; } - private void readPackageNames(String pkgNameFile) throws IOException { - try(BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(new File(pkgNameFile)), StandardCharsets.UTF_8))) { + private String[] readLines(String fileName) throws IOException { + try(BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName)), StandardCharsets.UTF_8))) { String line; Vector list= new Vector<>(); while ((line= br.readLine()) != null) { list.add(line); } - fPackageNames= list.toArray(new String[list.size()]); + return list.toArray(new String[list.size()]); } + } + + private void readPackageNames(String pkgNameFile) throws IOException { + fPackageNames= readLines(pkgNameFile); if (fDebugMode) { System.out.println("Packages:"); //$NON-NLS-1$ for (String fPackageName : fPackageNames) { @@ -404,14 +408,7 @@ private void readPackageNames(String pkgNameFile) throws IOException { } private void readTestNames(String testNameFile) throws IOException { - try(BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(new File(testNameFile)), StandardCharsets.UTF_8))) { - String line; - Vector list= new Vector<>(); - while ((line= br.readLine()) != null) { - list.add(line); - } - fTestClassNames= list.toArray(new String[list.size()]); - } + fTestClassNames= readLines(testNameFile); if (fDebugMode) { System.out.println("Tests:"); //$NON-NLS-1$ for (String fTestClassName : fTestClassNames) { @@ -421,14 +418,7 @@ private void readTestNames(String testNameFile) throws IOException { } private void readFailureNames(String testFailureFile) throws IOException { - try(BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(new File(testFailureFile)), StandardCharsets.UTF_8))) { - String line; - Vector list= new Vector<>(); - while ((line= br.readLine()) != null) { - list.add(line); - } - fFailureNames= list.toArray(new String[list.size()]); - } + fFailureNames = readLines(testFailureFile); if (fDebugMode) { System.out.println("Failures:"); //$NON-NLS-1$ for (String fFailureName : fFailureNames) { @@ -510,7 +500,7 @@ protected void notifyListenersOfTestEnd(TestExecution execution, * @param testName individual method to be run * @param execution executor */ - public void runTests(String[] testClassNames, String testName, TestExecution execution) { + private void runTests(String[] testClassNames, String testName, TestExecution execution) { ITestReference[] suites= fLoader.loadTests(loadClasses(testClassNames), testName, fFailureNames, fPackageNames, fIncludeExcludeTags, fUniqueId, this); // count all testMethods and inform ITestRunListeners @@ -754,7 +744,7 @@ public void flush() { fWriter.flush(); } - public void runTests(TestExecution execution) { + private void runTests(TestExecution execution) { runTests(fTestClassNames, fTestName, execution); } diff --git a/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/TestViewer.java b/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/TestViewer.java index a982151d7b3..00c347c3133 100644 --- a/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/TestViewer.java +++ b/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/TestViewer.java @@ -450,14 +450,14 @@ private OpenTestAction getOpenTestAction(TestSuiteElement testSuite) { if (children.length > 0 && children[0] instanceof TestCaseElement tce && tce.isDynamicTest()) { // a group of parameterized tests - return new OpenTestAction(fTestRunnerPart, (TestCaseElement) children[0], null); + return new OpenTestAction(fTestRunnerPart, tce, tce.getParameterTypes()); } if (children.length == 0) { // check if we have applied the workaround for: https://github.com/eclipse-jdt/eclipse.jdt.ui/issues/945 TestCaseElement child= testSuite.getSingleDynamicChild(); if (child != null) { // a parameterized test that ran only one test - return new OpenTestAction(fTestRunnerPart, child, null); + return new OpenTestAction(fTestRunnerPart, child, child.getParameterTypes()); } } diff --git a/org.eclipse.jdt.ui.unittest.junit.feature/feature.xml b/org.eclipse.jdt.ui.unittest.junit.feature/feature.xml index 1a6a1340db1..4a4243c800b 100644 --- a/org.eclipse.jdt.ui.unittest.junit.feature/feature.xml +++ b/org.eclipse.jdt.ui.unittest.junit.feature/feature.xml @@ -2,7 +2,7 @@ diff --git a/org.eclipse.jdt.ui.unittest.junit.feature/pom.xml b/org.eclipse.jdt.ui.unittest.junit.feature/pom.xml index 0c3e3c88625..1a12ee4c9bc 100644 --- a/org.eclipse.jdt.ui.unittest.junit.feature/pom.xml +++ b/org.eclipse.jdt.ui.unittest.junit.feature/pom.xml @@ -20,7 +20,7 @@ org.eclipse.jdt.feature org.eclipse.jdt.ui.unittest.junit.feature - 1.1.500-SNAPSHOT + 1.1.600-SNAPSHOT eclipse-feature diff --git a/org.eclipse.jdt.ui.unittest.junit/META-INF/MANIFEST.MF b/org.eclipse.jdt.ui.unittest.junit/META-INF/MANIFEST.MF index becd8104bb4..b81e2f2cd8c 100644 --- a/org.eclipse.jdt.ui.unittest.junit/META-INF/MANIFEST.MF +++ b/org.eclipse.jdt.ui.unittest.junit/META-INF/MANIFEST.MF @@ -3,7 +3,7 @@ Automatic-Module-Name: org.eclipse.jdt.ui.unittest.junit Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.jdt.ui.unittest.junit;singleton:=true -Bundle-Version: 1.1.500.qualifier +Bundle-Version: 1.1.600.qualifier Bundle-Activator: org.eclipse.jdt.ui.unittest.junit.JUnitTestPlugin Bundle-ActivationPolicy: lazy Bundle-Vendor: %providerName diff --git a/org.eclipse.jdt.ui.unittest.junit/pom.xml b/org.eclipse.jdt.ui.unittest.junit/pom.xml index 05ed1f45a55..2ec0580f391 100644 --- a/org.eclipse.jdt.ui.unittest.junit/pom.xml +++ b/org.eclipse.jdt.ui.unittest.junit/pom.xml @@ -18,6 +18,6 @@ org.eclipse.jdt org.eclipse.jdt.ui.unittest.junit - 1.1.500-SNAPSHOT + 1.1.600-SNAPSHOT eclipse-plugin diff --git a/org.eclipse.jdt.ui.unittest.junit/src/org/eclipse/jdt/ui/unittest/junit/launcher/JUnitRemoteTestRunnerClient.java b/org.eclipse.jdt.ui.unittest.junit/src/org/eclipse/jdt/ui/unittest/junit/launcher/JUnitRemoteTestRunnerClient.java index 11d69d7c5c8..0a36a100a42 100644 --- a/org.eclipse.jdt.ui.unittest.junit/src/org/eclipse/jdt/ui/unittest/junit/launcher/JUnitRemoteTestRunnerClient.java +++ b/org.eclipse.jdt.ui.unittest.junit/src/org/eclipse/jdt/ui/unittest/junit/launcher/JUnitRemoteTestRunnerClient.java @@ -424,7 +424,7 @@ private void notifyTestTreeEntry(final String treeEntry) { } int index6 = scanTestName(fixedTreeEntry, index5 + 1, displayNameBuffer); - displayName = displayNameBuffer.toString().trim(); + displayName = displayNameBuffer.toString().replace('\0', ' ').trim(); if (displayName.equals(testName)) { displayName = null; } diff --git a/org.eclipse.jdt.ui.unittest.junit/src/org/eclipse/jdt/ui/unittest/junit/ui/JUnitTestViewSupport.java b/org.eclipse.jdt.ui.unittest.junit/src/org/eclipse/jdt/ui/unittest/junit/ui/JUnitTestViewSupport.java index d1b19eeab64..1da731fdb95 100644 --- a/org.eclipse.jdt.ui.unittest.junit/src/org/eclipse/jdt/ui/unittest/junit/ui/JUnitTestViewSupport.java +++ b/org.eclipse.jdt.ui.unittest.junit/src/org/eclipse/jdt/ui/unittest/junit/ui/JUnitTestViewSupport.java @@ -89,7 +89,7 @@ public IAction getOpenTestAction(Shell shell, ITestSuiteElement testSuite) { int index = testName.indexOf('('); // test factory method if (index > 0) { - return new OpenTestAction(shell, testSuite.getTestName(), testName.substring(0, index), + return new OpenTestAction(shell, getClassName(testSuite), testName.substring(0, index), getParameterTypes(testSuite), true, testSuite.getTestRunSession()); } @@ -259,20 +259,34 @@ public ITestRunnerClient newTestRunnerClient(ITestRunSession session) { * @return a parameter type array */ private String[] getParameterTypes(ITestElement test) { - String testName = test.getDisplayName(); + final String testName = test.getDisplayName(); if (testName != null) { - int index = testName.lastIndexOf("method:"); //$NON-NLS-1$ + final int index = testName.lastIndexOf("method:"); //$NON-NLS-1$ if (index != -1) { - index = testName.indexOf('(', index); - if (index > 0) { - int closeIndex = testName.indexOf(')', index); - if (closeIndex > 0) { - String params = testName.substring(index + 1, closeIndex); - return params.split(","); //$NON-NLS-1$ - } + String[] result = extractParameters(testName, index); + if (result != null) { + return result; } } } + final String testUniqData = test.getData(); + if (testUniqData != null) { + final int testTemplateIdx = testUniqData.indexOf("test-template:"); //$NON-NLS-1$ + if (testTemplateIdx >= 0) { + return extractParameters(testUniqData, testTemplateIdx); + } + } + return null; + } + + private String[] extractParameters(String testDescription, int startIndex) { + final int index = testDescription.indexOf('(', startIndex); + if (index > 0) { + final int closeIndex = testDescription.indexOf(')', index); + if (closeIndex > 0) { + return testDescription.substring(index + 1, closeIndex).split(","); //$NON-NLS-1$ + } + } return null; }