Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[FIXED JENKINS-9637] Fixed the 404 when clicking the testResult on th…
…e Project Page when there is only an aggregated test result for a freestyle job. The name of the link is still wrong.
- Loading branch information
Stefan Wolf
committed
Aug 23, 2013
1 parent
964d9a0
commit 629be4a
Showing
10 changed files
with
397 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 178 additions & 0 deletions
178
test/src/test/java/hudson/tasks/test/AggregatedTestResultPublisherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
package hudson.tasks.test; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import hudson.model.AbstractBuild; | ||
import hudson.model.FreeStyleBuild; | ||
import hudson.model.FreeStyleProject; | ||
import hudson.model.Result; | ||
import hudson.tasks.BuildTrigger; | ||
import hudson.tasks.Fingerprinter; | ||
import hudson.tasks.Shell; | ||
import hudson.tasks.junit.JUnitResultArchiver; | ||
import hudson.tasks.test.helper.BuildPage; | ||
import hudson.tasks.test.helper.ProjectPage; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
import org.jvnet.hudson.test.TouchBuilder; | ||
import org.jvnet.hudson.test.recipes.LocalData; | ||
|
||
import java.util.List; | ||
|
||
import static org.hamcrest.Matchers.hasSize; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class AggregatedTestResultPublisherTest { | ||
public static final String TEST_PROJECT_NAME = "junit"; | ||
public static final String AGGREGATION_PROJECT_NAME = "aggregated"; | ||
@Rule | ||
public JenkinsRule j = new JenkinsRule(); | ||
private FreeStyleProject upstreamProject; | ||
private FreeStyleProject downstreamProject; | ||
|
||
private FreeStyleBuild build; | ||
private JenkinsRule.WebClient wc; | ||
private static final String[] singleContents = { | ||
"abcdef" | ||
}; | ||
private static final String[] singleFiles = { | ||
"test.txt" | ||
}; | ||
private BuildPage buildPage; | ||
private ProjectPage projectPage; | ||
|
||
@Before | ||
public void setup() { | ||
wc = j.createWebClient(); | ||
} | ||
|
||
@LocalData | ||
@Test | ||
public void aggregatedTestResultsOnly() throws Exception { | ||
createUpstreamProjectWithNoTests(); | ||
createDownstreamProjectWithTests(); | ||
|
||
buildAndSetupPageObjects(); | ||
|
||
projectPage.getLatestAggregatedTestReportLink() | ||
.assertHasLatestTestResultText() | ||
.assertHasTests() | ||
.follow().hasLinkToTestResultOfBuild(TEST_PROJECT_NAME, 1); | ||
projectPage.assertNoTestReportLink(); | ||
|
||
buildPage.getAggregatedTestReportLink() | ||
.assertHasAggregatedTestResultText() | ||
.assertHasTests() | ||
.follow().hasLinkToTestResultOfBuild(TEST_PROJECT_NAME, 1); | ||
buildPage.assertNoTestReportLink(); | ||
} | ||
|
||
@LocalData | ||
@Test | ||
public void testResultsOnly() throws Exception { | ||
createUpstreamProjectWithTests(); | ||
createDownstreamProjectWithNoTests(); | ||
|
||
buildAndSetupPageObjects(); | ||
|
||
projectPage.getLatestTestReportLink() | ||
.assertHasLatestTestResultText() | ||
.assertHasTests() | ||
.follow(); | ||
projectPage.assertNoAggregatedTestReportLink(); | ||
|
||
buildPage.getTestReportLink() | ||
.assertHasTestResultText() | ||
.assertHasTests() | ||
.follow(); | ||
buildPage.getAggregatedTestReportLink() | ||
.assertHasAggregatedTestResultText() | ||
.assertNoTests(); | ||
} | ||
|
||
@LocalData | ||
@Test | ||
public void testResultsAndAggregatedTestResults() throws Exception { | ||
createUpstreamProjectWithTests(); | ||
createDownstreamProjectWithTests(); | ||
|
||
buildAndSetupPageObjects(); | ||
|
||
projectPage.getLatestTestReportLink() | ||
.assertHasLatestTestResultText() | ||
.assertHasTests() | ||
.follow(); | ||
projectPage.assertNoAggregatedTestReportLink(); | ||
|
||
buildPage.getTestReportLink() | ||
.assertHasTestResultText() | ||
.assertHasTests() | ||
.follow(); | ||
buildPage.getAggregatedTestReportLink() | ||
.assertHasAggregatedTestResultText() | ||
.assertHasTests() | ||
.follow() | ||
.hasLinkToTestResultOfBuild(TEST_PROJECT_NAME, 1); | ||
} | ||
|
||
private void buildAndSetupPageObjects() throws Exception { | ||
buildOnce(); | ||
projectPage = new ProjectPage(wc.getPage(upstreamProject)); | ||
buildPage = new BuildPage(wc.getPage(build)); | ||
} | ||
|
||
private void buildOnce() throws Exception { | ||
build = j.buildAndAssertSuccess(upstreamProject); | ||
j.waitUntilNoActivity(); | ||
|
||
List<AbstractBuild<?, ?>> downstreamBuilds = ImmutableList.copyOf(build.getDownstreamBuilds(downstreamProject)); | ||
assertThat(downstreamBuilds, hasSize(1)); | ||
} | ||
|
||
|
||
private void createUpstreamProjectWithTests() throws Exception { | ||
createUpstreamProjectWithNoTests(); | ||
addJUnitResultArchiver(upstreamProject); | ||
} | ||
|
||
private void createUpstreamProjectWithNoTests() throws Exception { | ||
upstreamProject = j.createFreeStyleProject(AGGREGATION_PROJECT_NAME); | ||
addFingerprinterToProject(upstreamProject, singleContents, singleFiles); | ||
upstreamProject.setQuietPeriod(0); | ||
} | ||
|
||
private void createDownstreamProjectWithTests() throws Exception { | ||
createDownstreamProjectWithNoTests(); | ||
|
||
addJUnitResultArchiver(downstreamProject); | ||
j.jenkins.rebuildDependencyGraph(); | ||
} | ||
|
||
private void createDownstreamProjectWithNoTests() throws Exception { | ||
downstreamProject = j.createFreeStyleProject(TEST_PROJECT_NAME); | ||
downstreamProject.setQuietPeriod(0); | ||
addFingerprinterToProject(downstreamProject, singleContents, singleFiles); | ||
|
||
upstreamProject.getPublishersList().add(new BuildTrigger(ImmutableList.of(downstreamProject), Result.SUCCESS)); | ||
upstreamProject.getPublishersList().add(new AggregatedTestResultPublisher(TEST_PROJECT_NAME)); | ||
|
||
j.jenkins.rebuildDependencyGraph(); | ||
} | ||
|
||
private void addJUnitResultArchiver(FreeStyleProject project) { | ||
JUnitResultArchiver archiver = new JUnitResultArchiver("*.xml", false, null); | ||
project.getPublishersList().add(archiver); | ||
project.getBuildersList().add(new TouchBuilder()); | ||
} | ||
|
||
private void addFingerprinterToProject(FreeStyleProject project, String[] contents, String[] files) throws Exception { | ||
StringBuilder targets = new StringBuilder(); | ||
for (int i = 0; i < contents.length; i++) { | ||
project.getBuildersList().add(new Shell("echo " + contents[i] + " > " + files[i])); | ||
targets.append(files[i]).append(','); | ||
} | ||
|
||
project.getPublishersList().add(new Fingerprinter(targets.toString(), false)); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
test/src/test/java/hudson/tasks/test/helper/AbstractPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package hudson.tasks.test.helper; | ||
|
||
import com.gargoylesoftware.htmlunit.html.HtmlAnchor; | ||
import com.gargoylesoftware.htmlunit.html.HtmlPage; | ||
import org.xml.sax.SAXException; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
|
||
public abstract class AbstractPage { | ||
public static final String AGGREGATED_TEST_REPORT_URL = "aggregatedTestReport"; | ||
public static final String TEST_REPORT_URL = "testReport"; | ||
|
||
protected HtmlPage htmlPage; | ||
|
||
public AbstractPage(HtmlPage htmlPage) { | ||
this.htmlPage = htmlPage; | ||
} | ||
|
||
protected abstract String getHrefFromTestUrl(String testUrl); | ||
|
||
public HtmlAnchor getTestReportAnchor(String testUrl) throws IOException, SAXException { | ||
return htmlPage.getAnchorByHref(getHrefFromTestUrl(testUrl)); | ||
} | ||
public void assertNoLink(String url) { | ||
List<HtmlAnchor> anchors = htmlPage.getAnchors(); | ||
boolean found = false; | ||
String fullUrl = getHrefFromTestUrl(url); | ||
for (HtmlAnchor anchor : anchors) { | ||
if (fullUrl.equals(anchor.getHrefAttribute())) { | ||
found = true; | ||
break; | ||
} | ||
} | ||
assertFalse("Link to " + fullUrl + " found, but should not be present", found); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
test/src/test/java/hudson/tasks/test/helper/AbstractTestResultLink.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package hudson.tasks.test.helper; | ||
|
||
import com.gargoylesoftware.htmlunit.Page; | ||
import com.gargoylesoftware.htmlunit.html.HtmlAnchor; | ||
import com.gargoylesoftware.htmlunit.html.HtmlPage; | ||
|
||
import java.net.MalformedURLException; | ||
|
||
import static org.hamcrest.Matchers.containsString; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class AbstractTestResultLink<T extends AbstractTestResultLink<T>> { | ||
protected HtmlAnchor testResultLink; | ||
|
||
public AbstractTestResultLink(HtmlAnchor testResultLink) { | ||
this.testResultLink = testResultLink; | ||
} | ||
|
||
public String getResultText() { | ||
return testResultLink.getNextSibling().asText(); | ||
} | ||
public T assertNoTests() { | ||
assertThat(getResultText(), containsString("no tests")); | ||
return (T) this; | ||
} | ||
|
||
public T assertHasTests() { | ||
// Text is either "(no failures)" or "<n> failure(s)" | ||
assertThat(getResultText(), containsString("failure")); | ||
return (T) this; | ||
} | ||
|
||
public TestResultsPage follow() throws Exception { | ||
return new TestResultsPage((HtmlPage) testResultLink.openLinkInNewWindow()); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
test/src/test/java/hudson/tasks/test/helper/BuildPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package hudson.tasks.test.helper; | ||
|
||
import com.gargoylesoftware.htmlunit.html.HtmlAnchor; | ||
import com.gargoylesoftware.htmlunit.html.HtmlPage; | ||
import org.xml.sax.SAXException; | ||
|
||
import java.io.IOException; | ||
|
||
public class BuildPage extends AbstractPage { | ||
|
||
public BuildPage(HtmlPage htmlPage) { | ||
super(htmlPage); | ||
} | ||
|
||
@Override | ||
protected String getHrefFromTestUrl(String testUrl) { | ||
return testUrl + "/"; | ||
} | ||
|
||
|
||
public TestResultLink getAggregatedTestReportLink() throws IOException, SAXException { | ||
return new TestResultLink(getTestReportAnchor(AGGREGATED_TEST_REPORT_URL)); | ||
} | ||
|
||
public TestResultLink getTestReportLink() throws IOException, SAXException { | ||
return new TestResultLink(getTestReportAnchor(TEST_REPORT_URL)); | ||
} | ||
|
||
public void assertNoTestReportLink() { | ||
assertNoLink(TEST_REPORT_URL); | ||
} | ||
|
||
public void assertNoAggregatedTestReportLink() { | ||
assertNoLink(AGGREGATED_TEST_REPORT_URL); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
test/src/test/java/hudson/tasks/test/helper/LatestTestResultLink.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package hudson.tasks.test.helper; | ||
|
||
import static org.hamcrest.Matchers.containsString; | ||
import static org.junit.Assert.assertThat; | ||
import com.gargoylesoftware.htmlunit.html.HtmlAnchor; | ||
|
||
public class LatestTestResultLink extends AbstractTestResultLink<LatestTestResultLink> { | ||
|
||
public static final String LATEST_TEST_RESULT_STRING = "Latest Test Result"; | ||
public static final String LATEST_AGGREGATED_TEST_RESULT_STRING = "Latest Aggregated Test Result"; | ||
|
||
LatestTestResultLink(HtmlAnchor testResultLink) { | ||
super(testResultLink); | ||
} | ||
|
||
public LatestTestResultLink assertHasLatestTestResultText() { | ||
assertThat(testResultLink.getTextContent(), containsString(LATEST_TEST_RESULT_STRING)); | ||
return this; | ||
} | ||
|
||
public LatestTestResultLink assertHasLatestAggregatedTestResultText() { | ||
assertThat(testResultLink.getTextContent(), containsString(LATEST_AGGREGATED_TEST_RESULT_STRING)); | ||
return this; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
test/src/test/java/hudson/tasks/test/helper/ProjectPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package hudson.tasks.test.helper; | ||
|
||
import com.gargoylesoftware.htmlunit.html.HtmlPage; | ||
import org.xml.sax.SAXException; | ||
|
||
import java.io.IOException; | ||
|
||
public class ProjectPage extends AbstractPage { | ||
|
||
public ProjectPage(HtmlPage projectPage) { | ||
super(projectPage); | ||
} | ||
|
||
|
||
public LatestTestResultLink getLatestTestReportLink() throws IOException, SAXException { | ||
return new LatestTestResultLink(getTestReportAnchor(TEST_REPORT_URL)); | ||
} | ||
|
||
public LatestTestResultLink getLatestAggregatedTestReportLink() throws IOException, SAXException { | ||
return new LatestTestResultLink(getTestReportAnchor(AGGREGATED_TEST_REPORT_URL)); | ||
} | ||
|
||
protected String getHrefFromTestUrl(String testUrl) { | ||
return "lastCompletedBuild/" + testUrl + "/"; | ||
} | ||
|
||
public void assertNoTestReportLink() { | ||
assertNoLink(TEST_REPORT_URL); | ||
} | ||
|
||
public void assertNoAggregatedTestReportLink() { | ||
assertNoLink(AGGREGATED_TEST_REPORT_URL); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
test/src/test/java/hudson/tasks/test/helper/TestResultLink.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package hudson.tasks.test.helper; | ||
|
||
import com.gargoylesoftware.htmlunit.html.HtmlAnchor; | ||
|
||
import static org.hamcrest.Matchers.containsString; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class TestResultLink extends AbstractTestResultLink<TestResultLink> { | ||
|
||
public static final String TEST_RESULT_STRING = "Test Result"; | ||
public static final String AGGREGATED_TEST_RESULT_STRING = "Aggregated Test Result"; | ||
|
||
TestResultLink(HtmlAnchor testResultLink) { | ||
super(testResultLink); | ||
} | ||
|
||
public TestResultLink assertHasTestResultText() { | ||
assertThat(testResultLink.getTextContent(), containsString(TEST_RESULT_STRING)); | ||
return this; | ||
} | ||
|
||
public TestResultLink assertHasAggregatedTestResultText() { | ||
assertThat(testResultLink.getTextContent(), containsString(AGGREGATED_TEST_RESULT_STRING)); | ||
return this; | ||
} | ||
|
||
} |
Oops, something went wrong.