Skip to content

Commit

Permalink
Add documents
Browse files Browse the repository at this point in the history
  • Loading branch information
XiongKezhi committed Dec 22, 2020
1 parent 7452bee commit edde6d8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,18 @@ public class BuildStatusChecksPublisherITest extends IntegrationTestWithJenkinsP
@TestExtension
public static final LoggingChecksPublisher.Factory PUBLISHER_FACTORY = new LoggingChecksPublisher.Factory();

/**
* Provide inject an implementation of {@link AbstractStatusChecksProperties} to control the checks.
*/
@TestExtension
public static final ChecksProperties PROPERTIES = new ChecksProperties();

/**
* Tests when the implementation of {@link AbstractStatusChecksProperties} is not applicable,
* a status checks should not be published.
*
* @throws IOException if failed getting log from {@link Run}
*/
@Test
public void shouldNotPublishStatusWhenNotApplicable() throws IOException {
PUBLISHER_FACTORY.setFormatter(details -> String.format(STATUS_TEMPLATE,
Expand All @@ -37,6 +46,11 @@ public void shouldNotPublishStatusWhenNotApplicable() throws IOException {
.doesNotContain(String.format(STATUS_TEMPLATE, "Test Status", "COMPLETED", "SUCCESS"));
}

/**
* Tests when status checks is skipped, a status checks should not be published.
*
* @throws IOException if failed getting log from {@link Run}
*/
@Test
public void shouldNotPublishStatusWhenSkipped() throws IOException {
PUBLISHER_FACTORY.setFormatter(details -> String.format(STATUS_TEMPLATE,
Expand All @@ -49,9 +63,14 @@ public void shouldNotPublishStatusWhenSkipped() throws IOException {
assertThat(JenkinsRule.getLog(buildSuccessfully(createFreeStyleProject())))
.doesNotContain(String.format(STATUS_TEMPLATE, "Test Status", "IN_PROGRESS", "NONE"))
.doesNotContain(String.format(STATUS_TEMPLATE, "Test Status", "COMPLETED", "SUCCESS"));

}

/**
* Tests when an implementation of {@link AbstractStatusChecksProperties} is applicable and not skipped,
* a status checks using the specified name should be published.
*
* @throws IOException if failed getting log from {@link Run}
*/
@Test
public void shouldPublishStatusWithProperties() throws IOException {
PUBLISHER_FACTORY.setFormatter(details -> String.format(STATUS_TEMPLATE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,36 @@

import java.util.Optional;

/**
* Implementation of {@link ChecksPublisher} for use in testing, that logs the checks details in user specified format.
*
* For example:
*
* <pre>
* public class ChecksPublishingTest extends IntegrationTestWithJenkinsPerTest {
*
* @TestExtension
* public static final LoggingChecksPublisher.Factory PUBLISHER_FACTORY = new LoggingChecksPublisher.Factory();
*
* @Test
* public void shouldLogChecks() {
* // ...Run a test job...
* Run<?, ?> run = buildSuccessfully(createFreeStyleProject());
*
* // ...Inspect logs...
* assertThat(JenkinsRule.getLog(run))
* .contains("...")
* .doesNotContain("...");
* }
* }
* </pre>
*
* An example of this can be found in {@link io.jenkins.plugins.checks.status.BuildStatusChecksPublisherITest}
*/
public class LoggingChecksPublisher extends ChecksPublisher {
/**
* Defines how to format a {@link ChecksDetails} to {@link String}.
*/
@FunctionalInterface
public interface Formatter {
String format(ChecksDetails details);
Expand All @@ -18,11 +47,20 @@ public interface Formatter {
private Formatter formatter = ChecksDetails::toString;
private TaskListener listener = TaskListener.NULL;

/**
* Logs the {@code details} using the {@link TaskListener}.
*
* @param details
* checks details that will be logged
*/
@Override
public void publish(final ChecksDetails details) {
listener.getLogger().print(formatter.format(details));
}

/**
* Implementation of {@link ChecksPublisherFactory} that returns a {@link LoggingChecksPublisher}.
*/
public static class Factory extends ChecksPublisherFactory {
private final LoggingChecksPublisher publisher = new LoggingChecksPublisher();

Expand Down

0 comments on commit edde6d8

Please sign in to comment.