Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
Expand Down Expand Up @@ -48,6 +50,8 @@ public class TestNGLaunchConfigurationDelegate
extends AbstractJavaLaunchConfigurationDelegate {

private static final Version mimJvmVer = new Version("1.7.0"); //$NON-NLS-1$
private static final Pattern JAVA_VERSION =
Pattern.compile("(\\d+)(?:\\.(\\d+))?(?:\\.(\\d+))?"); //$NON-NLS-1$

/**
* Launch RemoteTestNG (except if we're in debug mode).
Expand Down Expand Up @@ -81,11 +85,14 @@ public void launch(ILaunchConfiguration configuration, String mode,
null, TestNGPluginConstants.LAUNCH_ERROR_JVM_VER_UNKNOWN);
}

int idx = jreVer.indexOf("_");
if (idx > 0) {
jreVer = jreVer.substring(0, idx);
Version vmVer = parseJavaVersion(jreVer);
if (vmVer == null) {
abort(
ResourceUtil.getFormattedString(
"TestNGLaunchConfigurationDelegate.error.unknownjre", //$NON-NLS-1$
new String[] { install.getName() }),
null, TestNGPluginConstants.LAUNCH_ERROR_JVM_VER_UNKNOWN);
}
Version vmVer = new Version(jreVer);
if (compareVersion(vmVer, mimJvmVer) < 0) {
abort(
ResourceUtil.getFormattedString(
Expand Down Expand Up @@ -429,6 +436,24 @@ private String getRunNameAttr(ILaunchConfiguration configuration) {
}
}

/**
* Parses the leading major[.minor[.micro]] of a Java version string. JEP 223
* allows an arbitrary number of numeric elements (e.g. "17.0.21.0.3") plus
* "_update", "-pre" and "+build" suffixes, none of which are valid OSGi
* version syntax.
*
* @return the version, or null if the string doesn't start with a number
*/
static Version parseJavaVersion(String javaVersion) {
Matcher m = JAVA_VERSION.matcher(javaVersion);
if (!m.lookingAt()) {
return null;
}
return new Version(Integer.parseInt(m.group(1)),
m.group(2) != null ? Integer.parseInt(m.group(2)) : 0,
m.group(3) != null ? Integer.parseInt(m.group(3)) : 0);
}

private static int compareVersion(Version v1, Version v2)
throws CoreException {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.testng.eclipse.launch;

import org.osgi.framework.Version;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestNGLaunchConfigurationDelegateTest {

@DataProvider(name = "javaVersions")
public Object[][] javaVersions() {
return new Object[][] {
// legacy format with update suffix (#583)
{ "1.8.0_201", new Version(1, 8, 0) },
{ "1.7.0", new Version(1, 7, 0) },
// JEP 223 with more than three numeric elements (#587)
{ "17.0.21.0.3", new Version(17, 0, 21) },
{ "21.0.13.0.3", new Version(21, 0, 13) },
{ "25.0.5.0.3", new Version(25, 0, 5) },
// short and suffixed forms
{ "21", new Version(21, 0, 0) },
{ "17.0", new Version(17, 0, 0) },
{ "21-ea", new Version(21, 0, 0) },
{ "11.0.2+9", new Version(11, 0, 2) },
{ "17.0.1-internal", new Version(17, 0, 1) },
};
}

@Test(dataProvider = "javaVersions")
public void parseJavaVersion(String input, Version expected) {
Assert.assertEquals(TestNGLaunchConfigurationDelegate.parseJavaVersion(input), expected);
}

@Test
public void parseJavaVersionRejectsNonNumeric() {
Assert.assertNull(TestNGLaunchConfigurationDelegate.parseJavaVersion("abc"));
Assert.assertNull(TestNGLaunchConfigurationDelegate.parseJavaVersion(""));
}

@Test
public void parsedVersionsCompareAgainstMinimum() {
Version min = new Version(1, 7, 0);
Assert.assertTrue(TestNGLaunchConfigurationDelegate.parseJavaVersion("17.0.21.0.3").compareTo(min) > 0);
Assert.assertTrue(TestNGLaunchConfigurationDelegate.parseJavaVersion("1.6.0_45").compareTo(min) < 0);
}
}
Loading