Skip to content

Commit

Permalink
Fixed: JENKINS-19217 - Change class names to not contain package name…
Browse files Browse the repository at this point in the history
…s on result pages
  • Loading branch information
nullin committed Jun 29, 2014
1 parent e8153d0 commit 5f25af9
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 43 deletions.
3 changes: 2 additions & 1 deletion README
Expand Up @@ -16,8 +16,9 @@ graph and all details about which tests that failed are also presented.
Release Notes
-------

### Upcoming
### 1.7
* Fixed: JENKINS-23285 - All links on trend graph map are wrong on "testngreports" subpage
* Fixed: JENKINS-19217 - Change class names to not contain package names on result pages

### 1.6
* Fixed: JENKINS-19353 - Exception error message newlines are escaped
Expand Down
40 changes: 30 additions & 10 deletions src/main/java/hudson/plugins/testng/parser/ResultsParser.java
@@ -1,18 +1,33 @@
package hudson.plugins.testng.parser;

import hudson.FilePath;
import hudson.plugins.testng.results.*;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.*;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Logger;

import hudson.FilePath;
import hudson.plugins.testng.results.ClassResult;
import hudson.plugins.testng.results.MethodResult;
import hudson.plugins.testng.results.MethodResultException;
import hudson.plugins.testng.results.PackageResult;
import hudson.plugins.testng.results.TestNGResult;
import hudson.plugins.testng.results.TestNGTestResult;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

/**
* Parses TestNG result XMLs generated using org.testng.reporters.XmlReporter
* into objects that are then used to display results in Jenkins.
Expand Down Expand Up @@ -411,7 +426,7 @@ private void startTestMethod(String name,
currentMethod = new MethodResult(name, status, description, duration,
startedAtDate, isConfig, currentTestRunId, currentTest.getName(),
currentSuite, testInstanceName);
List<String> groups = methodGroupMap.get(currentClass.getName() + "|" + name);
List<String> groups = methodGroupMap.get(currentClass.getCanonicalName() + "|" + name);
if (groups != null) {
currentMethod.setGroups(groups);
}
Expand All @@ -428,10 +443,15 @@ private void finishTestMethod()

private void startClass(String name)
{
int idx = name.lastIndexOf('.');
String simpleName = idx == -1 ?
name : name.substring(idx + 1, name.length());
String pkgName = idx == -1 ?
PackageResult.NO_PKG_NAME : name.substring(0, idx);
if (classResultMap.containsKey(name)) {
currentClass = classResultMap.get(name);
} else {
currentClass = new ClassResult(name);
currentClass = new ClassResult(pkgName, simpleName);
classResultMap.put(name, currentClass);
}
currentMethodList = new ArrayList<MethodResult>();
Expand Down
24 changes: 19 additions & 5 deletions src/main/java/hudson/plugins/testng/results/ClassResult.java
Expand Up @@ -16,6 +16,7 @@
@SuppressWarnings("serial")
public class ClassResult extends BaseResult {

private String pkgName; //name of package containing this class
private List<MethodResult> testMethodList = new ArrayList<MethodResult>();

//cache
Expand All @@ -27,8 +28,21 @@ public class ClassResult extends BaseResult {
private transient int skip;
private transient int pass;

public ClassResult(String name) {
public ClassResult(String pkgName, String name) {
super(name);
this.pkgName = pkgName;
}

public String getPkgName() {
return pkgName;
}

public String getCanonicalName() {
if (PackageResult.NO_PKG_NAME.equals(pkgName)) {
return getName();
} else {
return pkgName + "." + getName();
}
}

/**
Expand Down Expand Up @@ -123,10 +137,10 @@ public void tally() {
}
this.duration += methodResult.getDuration();
methodResult.setParent(this);
/*
* Setup testUuids to ensure that methods with same names can be
* reached using unique urls
*/
/*
* Setup testUuids to ensure that methods with same names can be
* reached using unique urls
*/
String methodName = methodResult.getName();
if (methodInstanceMap.containsKey(methodName)) {
int currIdx = methodInstanceMap.get(methodName);
Expand Down
40 changes: 38 additions & 2 deletions src/main/java/hudson/plugins/testng/results/PackageResult.java
@@ -1,18 +1,26 @@
package hudson.plugins.testng.results;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import hudson.model.AbstractBuild;
import hudson.plugins.testng.util.FormatUtil;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.bind.JavaScriptMethod;
import org.kohsuke.stapler.export.Exported;

import java.util.*;

/**
* Handles package level results
*/
@SuppressWarnings("serial")
public class PackageResult extends BaseResult {

public static final String NO_PKG_NAME = "No Package";
//TODO: switch to using a Map instead of List
//list of all classes run from this package
private List<ClassResult> classList = new ArrayList<ClassResult>();
Expand Down Expand Up @@ -202,4 +210,32 @@ public boolean hasChildren() {
return classList != null && !classList.isEmpty();
}

/**
* {@inheritDoc}
*
* Overriding so that we can be backward compatible with shared links. We changed name
* for classes to be simple name instead of canonical.
*
* TODO: Added this in release 1.7. Delete this method in one of the next few release.
*
* @param token
* @param req
* @param rsp
* @return
*/
@Override
public Object getDynamic(String token, StaplerRequest req, StaplerResponse rsp) {
if (token.indexOf('.') == -1) {
return super.getDynamic(token, req, rsp);
}
if (this.classList != null) {
for (ClassResult classResult : this.classList) {
if (token.equals(classResult.getPkgName() + "." + classResult.getName())) {
return classResult;
}
}
}
return null;
}

}
Expand Up @@ -196,13 +196,7 @@ public void tally() {
packageMap.clear();
for (TestNGTestResult _test : testList) {
for (ClassResult _class : _test.getClassList()) {
String pkg = _class.getName();
int lastDot = pkg.lastIndexOf('.');
if (lastDot == -1) {
pkg = "No Package";
} else {
pkg = pkg.substring(0, lastDot);
}
String pkg = _class.getPkgName();
if (packageMap.containsKey(pkg)) {
List<ClassResult> classResults = packageMap.get(pkg).getChildren();
if (!classResults.contains(_class)) {
Expand Down
Expand Up @@ -67,12 +67,12 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
//run build
FreeStyleBuild build = p.scheduleBuild2(0).get();
TestNGResult testngResult = (TestNGResult) build.getTestResultAction().getResult();
TestResult classResult = testngResult.findCorrespondingResult(PluginImpl.URL + "/precheckins/precheckins.LegacyOps");
TestResult classResult = testngResult.findCorrespondingResult(PluginImpl.URL + "/precheckins/LegacyOps");
Map<String, GroupedTestRun> testRunMap = ((ClassResult)classResult).getTestRunMap();

//Get page
String urlPrefix = build.getUrl() + PluginImpl.URL;
HtmlPage page = createWebClient().goTo(urlPrefix + "/precheckins/precheckins.LegacyOps/");
HtmlPage page = createWebClient().goTo(urlPrefix + "/precheckins/LegacyOps/");

List<HtmlElement> elements = page.selectNodes("//div[starts-with(@id, 'run-')]/span[@id='run-info']");

Expand Down Expand Up @@ -169,12 +169,12 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
//run build
FreeStyleBuild build = p.scheduleBuild2(0).get();
TestNGResult testngResult = (TestNGResult) build.getTestResultAction().getResult();
TestResult classResult = testngResult.findCorrespondingResult(PluginImpl.URL + "/test/test.CommandLineTest");
TestResult classResult = testngResult.findCorrespondingResult(PluginImpl.URL + "/test/CommandLineTest");
Map<String, GroupedTestRun> testRunMap = ((ClassResult)classResult).getTestRunMap();

//Get page
String urlPrefix = build.getUrl() + PluginImpl.URL;
HtmlPage page = createWebClient().goTo(urlPrefix + "/test/test.CommandLineTest");
HtmlPage page = createWebClient().goTo(urlPrefix + "/test/CommandLineTest");

List<HtmlElement> elements = page.selectNodes("//div[starts-with(@id, 'run-')]/table[@id='config']");
// there are no configuration methods
Expand Down
28 changes: 14 additions & 14 deletions src/test/java/hudson/plugins/testng/results/MethodResultTest.java
Expand Up @@ -51,7 +51,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
Assert.assertEquals(Result.UNSTABLE, build.getResult());

//Compare output
String methodUrl = build.getUrl() + PluginImpl.URL + "/gov.nasa.jpl/gov.nasa.jpl.FoobarTests/b";
String methodUrl = build.getUrl() + PluginImpl.URL + "/gov.nasa.jpl/FoobarTests/b";
HtmlPage page = createWebClient().goTo(methodUrl);
HtmlElement expMsg = page.getElementById("exp-msg");
String contents = expMsg.getTextContent();
Expand Down Expand Up @@ -80,7 +80,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
FreeStyleBuild build = p.scheduleBuild2(0).get();

//Compare output
String methodUrl = build.getUrl() + PluginImpl.URL + "/gov.nasa.jpl/gov.nasa.jpl.FoobarTests/b";
String methodUrl = build.getUrl() + PluginImpl.URL + "/gov.nasa.jpl/FoobarTests/b";
HtmlPage page = createWebClient().goTo(methodUrl);
HtmlElement expMsg = page.getElementById("exp-msg");
String contents = expMsg.getTextContent();
Expand Down Expand Up @@ -109,7 +109,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
FreeStyleBuild build = p.scheduleBuild2(0).get();

//Compare output
String methodUrl = build.getUrl() + PluginImpl.URL + "/com.test/com.test.UploadTest/uploadFile";
String methodUrl = build.getUrl() + PluginImpl.URL + "/com.test/UploadTest/uploadFile";
HtmlPage page = createWebClient().goTo(methodUrl);
HtmlElement description = page.getElementById("description");
String contents = description.getTextContent();
Expand Down Expand Up @@ -139,7 +139,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
FreeStyleBuild build = p.scheduleBuild2(0).get();

//Compare output
String methodUrl = build.getUrl() + PluginImpl.URL + "/com.test/com.test.UploadTest/uploadFile";
String methodUrl = build.getUrl() + PluginImpl.URL + "/com.test/UploadTest/uploadFile";
HtmlPage page = createWebClient().goTo(methodUrl);
HtmlElement description = page.getElementById("description");
String contents = description.getTextContent();
Expand Down Expand Up @@ -177,7 +177,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
FreeStyleBuild build = p.scheduleBuild2(0).get();

//Compare output
String methodUrl = build.getUrl() + PluginImpl.URL + "/com.fakepkg.test/com.fakepkg.test.FoobarTests/test";
String methodUrl = build.getUrl() + PluginImpl.URL + "/com.fakepkg.test/FoobarTests/test";
HtmlPage page = createWebClient().goTo(methodUrl);
HtmlElement description = page.getElementById("description");
assertEquals(2, description.getElementsByTagName("br").size());
Expand Down Expand Up @@ -209,7 +209,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,

//Compare output
String methodUrl = build.getUrl() + PluginImpl.URL
+ "/org.example.test/org.example.test.ExampleIntegrationTest/FirstTest";
+ "/org.example.test/ExampleIntegrationTest/FirstTest";
HtmlPage page = createWebClient().goTo(methodUrl);
HtmlElement reporterOutput = page.getElementById("reporter-output");
String contents = reporterOutput.getTextContent();
Expand Down Expand Up @@ -243,16 +243,16 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
FreeStyleBuild build = p.scheduleBuild2(0).get();
TestNGResult testngResult = (TestNGResult) build.getTestResultAction().getResult();
TestResult methodResult = testngResult.findCorrespondingResult(
PluginImpl.URL + "/test/test.Test1/includedGroups_1");
PluginImpl.URL + "/test/Test1/includedGroups_1");

//Compare output for a method
String urlPrefix = build.getUrl() + PluginImpl.URL;
HtmlPage page = createWebClient().goTo(urlPrefix + "/test/test.Test1/includedGroups_1/");
HtmlPage page = createWebClient().goTo(urlPrefix + "/test/Test1/includedGroups_1/");
HtmlElement element = page.getElementById("parent");
String contents = element.getTextContent();
//information about class and time taken
assertStringContains(contents, "test.Test1");
assertTrue(element.getAttribute("href").endsWith(urlPrefix + "/test/test.Test1"));
assertStringContains(contents, "Test1");
assertTrue(element.getAttribute("href").endsWith(urlPrefix + "/test/Test1"));

//duration string
assertStringContains(page.getElementById("report").getTextContent(), methodResult.getDurationString());
Expand Down Expand Up @@ -286,7 +286,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,

//method run using two parameters
page = createWebClient().goTo(urlPrefix
+ "/test.dataprovider/test.dataprovider.Sample1Test/verifyNames_1/");
+ "/test.dataprovider/Sample1Test/verifyNames_1/");
element = page.getElementById("params");
contents = element.getTextContent();
//information about class and time taken
Expand Down Expand Up @@ -323,7 +323,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
//Compare output for a dp method that failed
String urlPrefix = build.getUrl() + PluginImpl.URL;
WebClient wc = createWebClient();
HtmlPage page = wc.goTo(urlPrefix + "/org.jenkins/org.jenkins.TestDataProvider/test/");
HtmlPage page = wc.goTo(urlPrefix + "/org.jenkins/TestDataProvider/test/");

//method status information
HtmlElement element = page.getElementById("status");
Expand Down Expand Up @@ -351,7 +351,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
"org.jenkins.TestDataProvider.test(TestDataProvider.java:15)");

//compare output for a dp method that passed
page = wc.goTo(urlPrefix + "/org.jenkins/org.jenkins.TestDataProvider/test_2/");
page = wc.goTo(urlPrefix + "/org.jenkins/TestDataProvider/test_2/");

//method status information
element = page.getElementById("status");
Expand Down Expand Up @@ -400,7 +400,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
//Compare output for a dp method that failed
String urlPrefix = build.getUrl() + PluginImpl.URL;
WebClient wc = createWebClient();
HtmlPage page = wc.goTo(urlPrefix + "/testng.instancename/testng.instancename.MyITestFactoryTest/factoryTest1/");
HtmlPage page = wc.goTo(urlPrefix + "/testng.instancename/MyITestFactoryTest/factoryTest1/");

//method instance name information
HtmlElement element = page.getElementById("inst-name");
Expand Down

0 comments on commit 5f25af9

Please sign in to comment.