Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
[JENKINS-31202] Updated tests for multibranch.
  • Loading branch information
benjaminfuchs committed Sep 2, 2016
1 parent 4074526 commit 4b0d422b7709f14745a5bfad90bc43eb686e69fe
Showing with 119 additions and 31 deletions.
  1. +102 −0 src/test/java/hudson/plugins/warnings/WarningsMultiBranchTest.java
  2. +17 −31 src/test/java/hudson/plugins/warnings/WarningsPublisherTest.java
@@ -0,0 +1,102 @@
package hudson.plugins.warnings;

import java.util.Collections;
import java.util.List;
import com.google.common.collect.Lists;
import jenkins.scm.api.SCMHead;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.WebAssert;
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject;
import com.cloudbees.hudson.plugins.folder.computed.FolderComputation;
import javax.annotation.Nonnull;
import jenkins.branch.BranchProperty;
import jenkins.branch.BranchSource;
import jenkins.branch.DefaultBranchPropertyStrategy;
import jenkins.plugins.git.GitSCMSource;
import jenkins.plugins.git.GitSampleRepoRule;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import hudson.model.Action;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.containsString;

/**
* Tests the class {@link WarningsPublisher}.
*
* @author Ulli Hafner
*/
public class WarningsMultiBranchTest {
private static final String SUFFIX_NAME = " Warnings";
private static final String PARSER = "Maven";
private static final String PATTERN = "Pattern";

@Rule public JenkinsRule jenkinsRule = new JenkinsRule();
@Rule public GitSampleRepoRule sampleRepo = new GitSampleRepoRule();

/**
* Verifies that the order of warnings is preserved.
*
* @see <a href="https://issues.jenkins-ci.org/browse/JENKINS-31202">Issue 31202</a>
*/
@Test
public void testIssue31202() throws Exception {
sampleRepo.init();
sampleRepo.write("Jenkinsfile", ""
+ "node {\n"
+ " checkout scm\n"
+ " step([$class: 'WarningsPublisher', parserConfigurations: [[parserName: '" + PARSER + "', pattern: '" + PATTERN + "']]])\n"
+ "}\n"
);
sampleRepo.git("add", "Jenkinsfile");
sampleRepo.git("commit", "--all", "--message=flow1");

WorkflowMultiBranchProject mp = jenkinsRule.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
WorkflowJob p = scheduleAndFindBranchProject(mp, "master");
assertEquals(new SCMHead("master"), SCMHead.HeadByItem.findHead(p));
assertEquals(1, mp.getItems().size());
jenkinsRule.waitUntilNoActivity();
WorkflowRun b1 = p.getLastBuild();
assertEquals(1, b1.getNumber());
sampleRepo.write("new", "empty");
sampleRepo.git("add", "new");
sampleRepo.git("commit", "--all", "--message=flow2");
p = scheduleAndFindBranchProject(mp, "master");
jenkinsRule.waitUntilNoActivity();
b1 = p.getLastBuild();
assertEquals(2, b1.getNumber());

HtmlPage page = jenkinsRule.createWebClient().getPage(p,"");
WebAssert.assertTextPresent(page, "Pipeline master");
WebAssert.assertTextPresent(page, PARSER);
WebAssert.assertTextPresent(page, "Enlarge");
WebAssert.assertTextPresent(page, "Configure");
}

public static @Nonnull WorkflowJob scheduleAndFindBranchProject(@Nonnull WorkflowMultiBranchProject mp, @Nonnull String name) throws Exception {
mp.scheduleBuild2(0).getFuture().get();
return findBranchProject(mp, name);
}

public static @Nonnull WorkflowJob findBranchProject(@Nonnull WorkflowMultiBranchProject mp, @Nonnull String name) throws Exception {
WorkflowJob p = mp.getItem(name);
showIndexing(mp);
if (p == null) {
fail(name + " project not found");
}
return p;
}

static void showIndexing(@Nonnull WorkflowMultiBranchProject mp) throws Exception {
FolderComputation<?> indexing = mp.getIndexing();
System.out.println("---%<--- " + indexing.getUrl());
indexing.writeWholeLogTo(System.out);
System.out.println("---%<--- ");
}
}


@@ -2,7 +2,6 @@

import java.util.Collections;
import java.util.List;
import com.google.common.collect.Lists;

import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
@@ -13,31 +12,35 @@
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import hudson.model.Action;

import com.google.common.collect.Lists;

import static org.junit.Assert.*;

import hudson.model.Action;

/**
* Tests the class {@link WarningsPublisher}.
*
* @author Ulli Hafner
*/
public class WarningsPublisherTest {
private static final String SUFFIX_NAME = " Warnings";
private static final String SECOND = "PyLint";
private static final String FIRST = "Maven";
private static final String SECOND = "JSLint";
private static final String PATTERN = "Pattern";

@Rule public JenkinsRule jenkinsRule = new JenkinsRule();
@Rule
public JenkinsRule jenkinsRule = new JenkinsRule();

/**
/**
* Verifies that the order of warnings is preserved.
*
* @see <a href="http://issues.jenkins-ci.org/browse/JENKINS-14615">Issue 14615</a>
*/
@Test
public void testIssue14615Console() throws Exception {
String flow = "node {\n"
String flow = "node {\n"
+ " step([$class: 'WarningsPublisher', consoleParsers: [[parserName: '" + FIRST + "']]])\n"
+ " step([$class: 'WarningsPublisher', consoleParsers: [[parserName: '" + SECOND + "']]])\n"
+ "}\n";
@@ -51,7 +54,7 @@ public void testIssue14615Console() throws Exception {
checkOrder(expected, ordered);
}

/**
/**
* Verifies that the order of warnings is preserved.
*
* @see <a href="http://issues.jenkins-ci.org/browse/JENKINS-14615">Issue 14615</a>
@@ -75,7 +78,7 @@ public void testIssue14615File() throws Exception {
private List<Action> getListOfActions(String flow) throws Exception {
WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "p");
job.setDefinition(new CpsFlowDefinition(flow));
jenkinsRule.assertBuildStatusSuccess(job.scheduleBuild2(0));
job.scheduleBuild2(0);
jenkinsRule.waitUntilNoActivity();

return Lists.newArrayList(job.getLastBuild().getAllActions());
@@ -88,38 +91,21 @@ private List<String> createExpectedResult() {
return expected;
}

/*
* INFO ordered looks like this:
* - Cause
* - null
* - Git Build Data
* - No Tags
* - null
* - Maven Warnings
* - Compiler Warnings
* - JSLint Warnings
* - Compiler Warnings
* - Replay
* - null
* - Pipeline Steps
* - null
*/
private void checkOrder(final List<String> expected, final List<Action> ordered) {
int numberOfActions = (expected.size() * 2) + 4;
assertEquals("Wrong number of actions.", numberOfActions, ordered.size());

for (int position = 0; position < ordered.size(); position++) {
System.out.println(ordered.get(position).getDisplayName());
System.out.println(ordered.get(position).getDisplayName());
}

assertEquals("Wrong number of actions.", 8, ordered.size());

for (int position = 0; position < expected.size(); position++) {
assertPosition(ordered, expected, position);
assertPosition(ordered, expected, position);
}
}

private void assertPosition(final List<Action> ordered, final List<String> expected, final int position) {
int orderedPosition = (position * 2) + 5; // 0 => 5, 1 => 7, 2 => 9, ...
assertEquals("Wrong action at position " + position, expected.get(position), ordered.get(orderedPosition).getDisplayName());
int orderedPosition = (position * 2) + 5;
assertEquals("Wrong action at position " + position, expected.get(position), ordered.get(orderedPosition).getDisplayName());
}
}

0 comments on commit 4b0d422

Please sign in to comment.