Skip to content

Commit

Permalink
Merge pull request #47 from testng-team/f_debug_version_detect
Browse files Browse the repository at this point in the history
add more log debug log for diagnose the version detect issue
  • Loading branch information
missedone authored Jul 23, 2017
2 parents 2a9141c + 05560f1 commit 5dee584
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions remote/src/main/java/org/testng/remote/RemoteTestNG.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.URL;
import java.security.CodeSource;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map.Entry;
Expand Down Expand Up @@ -88,7 +89,9 @@ private static Version getTestNGVersion() {
strVer = getVersionFromClass();
return toVersion(strVer);
} catch (Exception e) {
if (isDebug()) {
p("failed to get TestNG version from class: " + e.getClass().getCanonicalName() + ": "
+ e.getMessage());
if (isVerbose()) {
e.printStackTrace();
}

Expand Down Expand Up @@ -132,6 +135,12 @@ private static Version getTestNGVersion() {
private static String getVersionFromClass() throws Exception {
@SuppressWarnings("rawtypes")
Class clazz = Class.forName("org.testng.internal.Version");
if (isDebug()) {
CodeSource codeSource = clazz.getProtectionDomain().getCodeSource();
if (codeSource != null) {
p("loaded class " + clazz.getCanonicalName() + " at " + codeSource.getLocation());
}
}
Field field = clazz.getDeclaredField("VERSION");
return (String) field.get(null);
}
Expand All @@ -154,16 +163,24 @@ private static Version parseVersionFromPom() throws Exception {

Enumeration<URL> resources = cl.getResources(
"META-INF/maven/org.testng/testng/pom.properties");
Version ver = null;
while (resources.hasMoreElements()) {
Properties props = new Properties();
try (InputStream in = resources.nextElement().openStream()) {
props.load(in);
URL url = resources.nextElement();
if (ver == null) {
Properties props = new Properties();
try (InputStream in = url.openStream()) {
props.load(in);
}

p("parsing TestNG version at " + url);
ver = toVersion(props.getProperty("version"));
}
else {
p("find more testng pom.properties but ignored: " + url);
}

return toVersion(props.getProperty("version"));
}

return null;
return ver;
}

/**
Expand All @@ -184,24 +201,28 @@ private static Version parseVersionFromManifest() throws Exception {

Enumeration<URL> resources = cl.getResources("META-INF/MANIFEST.MF");
while (resources.hasMoreElements()) {
Manifest mf = new Manifest(resources.nextElement().openStream());
URL url = resources.nextElement();
Manifest mf = new Manifest(url.openStream());
Attributes mainAttrs = mf.getMainAttributes();
if ("testng".equals(mainAttrs.getValue("Specification-Title"))) {
p("parsing TestNG version at " + url);
return toVersion(mainAttrs.getValue("Specification-Version"));
}

if ("org.testng".equals(mainAttrs.getValue("Bundle-SymbolicName"))) {
p("parsing TestNG version at " + url);
return toVersion(mainAttrs.getValue("Bundle-Version"));
}

if ("org.testng.TestNG".equals(mainAttrs.getValue("Main-Class"))) {
p("parsing TestNG version at " + url);
return toVersion(mainAttrs.getValue("Implementation-Version"));
}
}

return null;
}

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
Expand Down Expand Up @@ -241,9 +262,7 @@ private static void initAndRun(IRemoteTestNG remoteTestNg, String[] args, Comman
}

private static void p(String s) {
if (isVerbose()) {
System.out.println("[RemoteTestNG] " + s);
}
System.out.println("[RemoteTestNG] " + s);
}

static Version toVersion(String strVer) {
Expand Down

0 comments on commit 5dee584

Please sign in to comment.