diff --git a/remote/src/main/java/org/testng/remote/AbstractRemoteTestNG.java b/remote/src/main/java/org/testng/remote/AbstractRemoteTestNG.java index e0141e1..8c74167 100644 --- a/remote/src/main/java/org/testng/remote/AbstractRemoteTestNG.java +++ b/remote/src/main/java/org/testng/remote/AbstractRemoteTestNG.java @@ -162,7 +162,7 @@ private IMessageSender getMessageSender() { } } - // fall back to original behivour + // fall back to original behaviour return m_serPort != null ? new SerializedMessageSender(m_host, m_serPort, m_ack) : new StringMessageSender(m_host, m_port); diff --git a/remote/src/main/java/org/testng/remote/RemoteArgs.java b/remote/src/main/java/org/testng/remote/RemoteArgs.java index 0a2139f..e316832 100644 --- a/remote/src/main/java/org/testng/remote/RemoteArgs.java +++ b/remote/src/main/java/org/testng/remote/RemoteArgs.java @@ -6,6 +6,14 @@ import com.beust.jcommander.Parameter; public class RemoteArgs { + public static final String DEBUG = "-serdebug"; + @Parameter(names = DEBUG, hidden = true, description = "Used to debug TestNG") + public Boolean debug = Boolean.FALSE; + + public static final String HOST = "-serhost"; + @Parameter(names = HOST, description = "The host", hidden = true) + public String host; + public static final String PORT = "-serport"; @Parameter(names = PORT, description = "The port for the serialization protocol") public Integer serPort; diff --git a/remote/src/main/java/org/testng/remote/RemoteTestNG.java b/remote/src/main/java/org/testng/remote/RemoteTestNG.java index 836768b..c6aee19 100644 --- a/remote/src/main/java/org/testng/remote/RemoteTestNG.java +++ b/remote/src/main/java/org/testng/remote/RemoteTestNG.java @@ -66,12 +66,26 @@ public static void main(String[] args) throws ParameterException { IRemoteTestNG remoteTestNg = factory.createRemoteTestNG(); remoteTestNg.dontExit(ra.dontExit); - if (cla.port != null && ra.serPort != null) { - throw new TestNGException("Can only specify one of " + CommandLineArgs.PORT - + " and " + RemoteArgs.PORT); + + boolean debug = ra.debug; + if (!debug) { + // use reflection below for backward compatibility of testng version < 7.10.0 + try { + Field debugField = CommandLineArgs.class.getDeclaredField("debug"); + Object d = debugField.get(cla); + if (d != null) { + if (Boolean.valueOf(d.toString())) { + debug = true; + } + } + } catch (NoSuchFieldException | IllegalAccessException e) { + if (isDebug()) { + e.printStackTrace(); + } + } } - m_debug = cla.debug; - remoteTestNg.setDebug(cla.debug); + m_debug = debug; + remoteTestNg.setDebug(debug); remoteTestNg.setAck(ra.ack); initAndRun(remoteTestNg, args, cla, ra); @@ -226,17 +240,45 @@ private static Version parseVersionFromManifest() throws Exception { private static void initAndRun(IRemoteTestNG remoteTestNg, String[] args, CommandLineArgs cla, RemoteArgs ra) { if (m_debug) { // In debug mode, override the port and the XML file to a fixed location - cla.port = Integer.parseInt(DEBUG_PORT); - ra.serPort = cla.port; + ra.serPort = Integer.parseInt(DEBUG_PORT); cla.suiteFiles = Arrays.asList(new String[] { DEBUG_SUITE_DIRECTORY + DEBUG_SUITE_FILE }); } remoteTestNg.configure(cla); - remoteTestNg.setHost(cla.host); + String host = ra.host; + if (host == null || host.isBlank()) { + // use reflection below for backward compatibility of testng version < 7.10.0 + try { + Field hostField = CommandLineArgs.class.getDeclaredField("host"); + Object h = hostField.get(cla); + if (h != null) { + host = (String) h; + } + } catch (NoSuchFieldException | IllegalAccessException e) { + if (isDebug()) { + e.printStackTrace(); + } + } + } + remoteTestNg.setHost(host); remoteTestNg.setSerPort(ra.serPort); remoteTestNg.setProtocol(ra.protocol); - remoteTestNg.setPort(cla.port); + + Integer port = null; + // use reflection below for backward compatibility of testng version < 7.10.0 + try { + Field portField = CommandLineArgs.class.getDeclaredField("port"); + Object p = portField.get(cla); + if (p != null) { + port = Integer.valueOf(p.toString()); + } + } catch (NoSuchFieldException | IllegalAccessException e) { + if (isDebug()) { + e.printStackTrace(); + } + } + remoteTestNg.setPort(port); if (isVerbose()) { StringBuilder sb = new StringBuilder("Invoked with "); for (String s : args) { @@ -280,8 +322,7 @@ static Version toVersion(String strVer) { } public static boolean isVerbose() { - boolean result = System.getProperty(PROPERTY_VERBOSE) != null || isDebug(); - return result; + return System.getProperty(PROPERTY_VERBOSE) != null || isDebug(); } public static boolean isDebug() { diff --git a/remote/src/test/java/test/remote/RemoteTest.java b/remote/src/test/java/test/remote/RemoteTest.java index 238e064..250735d 100644 --- a/remote/src/test/java/test/remote/RemoteTest.java +++ b/remote/src/test/java/test/remote/RemoteTest.java @@ -63,22 +63,19 @@ public void testString() { } private void launchRemoteTestNG(final String portArg, final int portValue, final String protocol) { - new Thread(new Runnable() { - @Override - public void run() { - List args = new ArrayList<>(); - args.add(portArg); - args.add(Integer.toString(portValue)); - args.add(RemoteArgs.VERSION); - args.add(getTestNGVersion()); - if (protocol != null) { - args.add(RemoteArgs.PROTOCOL); - args.add(protocol); - } - args.add("-dontexit"); - args.add(getPathToResource("testng-remote.xml")); - RemoteTestNG.main(args.toArray(new String[0])); - } + new Thread(() -> { + List args = new ArrayList<>(); + args.add(portArg); + args.add(Integer.toString(portValue)); + args.add(RemoteArgs.VERSION); + args.add(getTestNGVersion()); + if (protocol != null) { + args.add(RemoteArgs.PROTOCOL); + args.add(protocol); + } + args.add("-dontexit"); + args.add(getPathToResource("testng-remote.xml")); + RemoteTestNG.main(args.toArray(new String[0])); }).start(); }