-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test for PublicBuildStatusAction.java (#163)
* add test for PublicBuildStatusAction.java * delete testDoText * Use web client instead of mocks to call method Web client is provided by JenkinsRule and avoids mocking in the tests. Use a single JenkinsRule for the entire test class so that we don't pay the JenkinsRule startup cost for every test. JenkinsRule runs slowly on Windows, so it is better to use a class level JenkinsRule when tests are independent of each other. Keep the tests independent of each other by defining the test job name based on the name of the test method. Use hamcrest assertions for easier diagnosis and simpler reading of the assertions. Format the source file with `mvn spotless:apply` so that the file is consistent with the source formatting in the rest of the plugin. * Use a real implementation of isWindows() --------- Co-authored-by: Mark Waite <[email protected]>
- Loading branch information
1 parent
127c05e
commit 221c5b4
Showing
1 changed file
with
130 additions
and
0 deletions.
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
src/test/java/org/jenkinsci/plugins/badge/actions/PublicBuildStatusActionTest.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,130 @@ | ||
package org.jenkinsci.plugins.badge.actions; | ||
|
||
import static org.hamcrest.CoreMatchers.containsString; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.not; | ||
import static org.hamcrest.CoreMatchers.nullValue; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import hudson.model.FreeStyleProject; | ||
import hudson.model.Run; | ||
import hudson.tasks.BatchFile; | ||
import hudson.tasks.Shell; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import org.junit.Before; | ||
import org.junit.ClassRule; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TestName; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
public class PublicBuildStatusActionTest { | ||
|
||
// JenkinsRule startup cost is high on Windows | ||
// Use a ClassRule to create one JenkinsRule used by all tests | ||
@ClassRule public static JenkinsRule j = new JenkinsRule(); | ||
|
||
@Rule public TestName name = new TestName(); | ||
|
||
private static final String SUCCESS_MARKER = "fill=\"#44cc11\""; | ||
private static final String NOT_RUN_MARKER = "fill=\"#9f9f9f\""; | ||
|
||
private FreeStyleProject job; | ||
private String jobStatusUrl; | ||
private JenkinsRule.WebClient webClient; | ||
|
||
@Before | ||
public void createJob() throws IOException { | ||
// Give each job a name based on the name of the test method | ||
// Simplifies debugging and failure diagnosis | ||
// Also avoids any caching from reusing job name | ||
job = j.createFreeStyleProject("job-" + name.getMethodName()); | ||
// Assure the job can pass on Windows and Unix | ||
job.getBuildersList() | ||
.add( | ||
isWindows() | ||
? new BatchFile("echo hello from a batch file") | ||
: new Shell("echo hello from a shell")); | ||
String statusUrl = j.getURL().toString() + "buildStatus/icon"; | ||
jobStatusUrl = statusUrl + "?job=" + job.getName(); | ||
} | ||
|
||
@Before | ||
public void createWebClient() { | ||
webClient = j.createWebClient(); | ||
} | ||
|
||
@Test | ||
public void testDoIconJobBefore() throws Exception { | ||
// Check job status icon is "not run" before job runs | ||
JenkinsRule.JSONWebResponse json = webClient.getJSON(jobStatusUrl); | ||
String result = json.getContentAsString(); | ||
assertThat(result, containsString("<svg ")); | ||
assertThat(result, not(containsString(SUCCESS_MARKER))); | ||
assertThat(result, containsString(NOT_RUN_MARKER)); | ||
} | ||
|
||
@Test | ||
public void testDoIconBuildBefore() throws Exception { | ||
String buildStatusUrl = jobStatusUrl + "&build=123"; | ||
|
||
// Check build status icon is "not run" before job runs | ||
JenkinsRule.JSONWebResponse json = webClient.getJSON(buildStatusUrl); | ||
String result = json.getContentAsString(); | ||
assertThat(result, containsString("<svg ")); | ||
assertThat(result, not(containsString(SUCCESS_MARKER))); | ||
assertThat(result, containsString(NOT_RUN_MARKER)); | ||
} | ||
|
||
@Test | ||
public void testDoIconJobAfter() throws Exception { | ||
// Run the job, assert that it was successful | ||
Run<?, ?> build = job.scheduleBuild2(0).get(); | ||
j.assertBuildStatusSuccess(build); | ||
|
||
// Check job status icon is correct after job runs successfully | ||
JenkinsRule.JSONWebResponse json = webClient.getJSON(jobStatusUrl); | ||
String result = json.getContentAsString(); | ||
assertThat(result, containsString("<svg ")); | ||
assertThat(result, containsString(SUCCESS_MARKER)); | ||
assertThat(result, not(containsString(NOT_RUN_MARKER))); | ||
} | ||
|
||
@Test | ||
public void testDoIconBuildAfter() throws Exception { | ||
// Run the job, assert that it was successful | ||
Run<?, ?> build = job.scheduleBuild2(0).get(); | ||
j.assertBuildStatusSuccess(build); | ||
|
||
// Check build status icon is correct after job runs successfully | ||
String buildStatusUrl = jobStatusUrl + "&build=" + build.getNumber(); | ||
JenkinsRule.JSONWebResponse json = webClient.getJSON(buildStatusUrl); | ||
String result = json.getContentAsString(); | ||
assertThat(result, containsString("<svg ")); | ||
assertThat(result, containsString(SUCCESS_MARKER)); | ||
assertThat(result, not(containsString(NOT_RUN_MARKER))); | ||
} | ||
|
||
@Test | ||
public void testGetUrlName() throws IOException { | ||
PublicBuildStatusAction action = new PublicBuildStatusAction(); | ||
assertThat(action.getUrlName(), is("buildStatus")); | ||
} | ||
|
||
@Test | ||
public void testGetIconFileName() throws IOException { | ||
PublicBuildStatusAction action = new PublicBuildStatusAction(); | ||
assertThat(action.getIconFileName(), is(nullValue())); | ||
} | ||
|
||
@Test | ||
public void testGetDisplayName() throws IOException { | ||
PublicBuildStatusAction action = new PublicBuildStatusAction(); | ||
assertThat(action.getDisplayName(), is(nullValue())); | ||
} | ||
|
||
private boolean isWindows() { | ||
return File.pathSeparatorChar == ';'; | ||
} | ||
} |