|
| 1 | +package hudson.tasks.junit; |
| 2 | + |
| 3 | +import com.gargoylesoftware.htmlunit.html.HtmlPage; |
| 4 | +import com.gargoylesoftware.htmlunit.html.HtmlTable; |
| 5 | +import com.gargoylesoftware.htmlunit.html.HtmlTableCell; |
| 6 | +import hudson.Launcher; |
| 7 | +import hudson.model.AbstractBuild; |
| 8 | +import hudson.model.BuildListener; |
| 9 | +import hudson.model.FreeStyleBuild; |
| 10 | +import hudson.model.FreeStyleProject; |
| 11 | +import hudson.model.Result; |
| 12 | +import hudson.tasks.junit.rot13.Rot13Publisher; |
| 13 | +import hudson.tasks.test.helper.WebClientFactory; |
| 14 | +import org.apache.commons.lang3.tuple.Pair; |
| 15 | +import org.hamcrest.CoreMatchers; |
| 16 | +import org.junit.Before; |
| 17 | +import org.junit.Rule; |
| 18 | +import org.junit.Test; |
| 19 | +import org.jvnet.hudson.test.JenkinsRule; |
| 20 | +import org.jvnet.hudson.test.TestBuilder; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.List; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | + |
| 27 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 28 | +import static org.junit.Assert.assertEquals; |
| 29 | + |
| 30 | +/** |
| 31 | + * Verifies that TestDataPublishers can contribute custom columns to html tables in result pages. |
| 32 | + */ |
| 33 | +public class CustomColumnsTest { |
| 34 | + |
| 35 | + @Rule |
| 36 | + public JenkinsRule jenkins = new JenkinsRule(); |
| 37 | + |
| 38 | + private FreeStyleProject project; |
| 39 | + |
| 40 | + private static final String reportFileName = "junit-report-494.xml"; |
| 41 | + |
| 42 | + @Before |
| 43 | + public void setUp() throws Exception { |
| 44 | + project = jenkins.createFreeStyleProject("customcolumns"); |
| 45 | + JUnitResultArchiver archiver = new JUnitResultArchiver("*.xml"); |
| 46 | + archiver.setTestDataPublishers(Collections.singletonList(new Rot13Publisher())); |
| 47 | + archiver.setSkipPublishingChecks(true); |
| 48 | + project.getPublishersList().add(archiver); |
| 49 | + project.getBuildersList().add(new TestBuilder() { |
| 50 | + @Override |
| 51 | + public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) |
| 52 | + throws InterruptedException, IOException { |
| 53 | + build.getWorkspace().child(reportFileName).copyFrom(getClass().getResource(reportFileName)); |
| 54 | + return true; |
| 55 | + } |
| 56 | + }); |
| 57 | + FreeStyleBuild build = project.scheduleBuild2(0).get(5, TimeUnit.MINUTES); |
| 58 | + jenkins.assertBuildStatus(Result.UNSTABLE, build); |
| 59 | + } |
| 60 | + |
| 61 | + @SafeVarargs |
| 62 | + private void verifyThatTableContainsExpectedValues(String pathToPage, String tableId, String headerName, |
| 63 | + Pair<String, String>... rowValues) throws Exception { |
| 64 | + |
| 65 | + JenkinsRule.WebClient wc = WebClientFactory.createWebClientWithDisabledJavaScript(jenkins); |
| 66 | + HtmlPage projectPage = wc.getPage(project); |
| 67 | + jenkins.assertGoodStatus(projectPage); |
| 68 | + HtmlPage classReportPage = wc.getPage(project, pathToPage); |
| 69 | + jenkins.assertGoodStatus(classReportPage); |
| 70 | + |
| 71 | + HtmlTable testResultTable = (HtmlTable) classReportPage.getFirstByXPath("//table[@id='" + tableId + "']"); |
| 72 | + List<HtmlTableCell> headerRowCells = testResultTable.getHeader().getRows().get(0).getCells(); |
| 73 | + int numberOfColumns = headerRowCells.size(); |
| 74 | + assertEquals(headerName, headerRowCells.get(numberOfColumns - 1).asNormalizedText()); |
| 75 | + |
| 76 | + for (int x = 0; x < rowValues.length; x++) { |
| 77 | + List<HtmlTableCell> bodyRowCells = testResultTable.getBodies().get(0).getRows().get(x).getCells(); |
| 78 | + assertThat(bodyRowCells.get(0).asNormalizedText(), CoreMatchers.containsString(rowValues[x].getLeft())); |
| 79 | + assertEquals(rowValues[x].getRight(), bodyRowCells.get(numberOfColumns - 1).asNormalizedText()); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void verifyThatCustomColumnIsAddedToTheTestsTableOnTheClassResultPage() throws Exception { |
| 85 | + verifyThatTableContainsExpectedValues("/lastBuild/testReport/junit/io.jenkins.example/AnExampleTestClass/", |
| 86 | + "testresult", "ROT13 for cases on class page", Pair.of("testCaseA", "grfgPnfrN for case"), Pair.of( |
| 87 | + "testCaseZ", "grfgPnfrM for case")); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void verifyThatCustomColumnIsAddedToTheFailedTestsTableOnThePackageResultPage() throws Exception { |
| 92 | + verifyThatTableContainsExpectedValues("/lastBuild/testReport/junit/io.jenkins.example/", "failedtestresult", |
| 93 | + "ROT13 for failed cases on package page", Pair.of("testCaseA", "grfgPnfrN for case")); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void verifyThatCustomColumnIsAddedToTheClassesTableOnThePackageResultPage() throws Exception { |
| 98 | + verifyThatTableContainsExpectedValues("/lastBuild/testReport/junit/io.jenkins.example/", "testresult", |
| 99 | + "ROT13 for all classes on package page", Pair.of("AnExampleTestClass", "NaRknzcyrGrfgPynff for class")); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void verifyThatCustomColumnIsAddedToTheFailedTestsTableOnTheTestResultPage() throws Exception { |
| 104 | + verifyThatTableContainsExpectedValues("/lastBuild/testReport/", "failedtestresult", |
| 105 | + "ROT13 for failed cases on test page", Pair.of("testCaseA", "grfgPnfrN for case")); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void verifyThatCustomColumnIsAddedToTheClassesTableOnTheTestResultPage() throws Exception { |
| 110 | + verifyThatTableContainsExpectedValues("/lastBuild/testReport/", "testresult", |
| 111 | + "ROT13 for all packages on test page", Pair.of("io.jenkins.example", "vb.wraxvaf.rknzcyr for package")); |
| 112 | + } |
| 113 | +} |
0 commit comments