Skip to content

Commit

Permalink
Bump Plugin Utilities API from 2.14.0 to 2.16.0 (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored May 5, 2022
1 parent a3bde2f commit c129796
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 152 deletions.
5 changes: 5 additions & 0 deletions checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suppressions PUBLIC "-//Checkstyle//DTD SuppressionFilter Configuration 1.0//EN" "https://checkstyle.org/dtds/suppressions_1_0.dtd">
<suppressions>
<suppress checks="RedundantModifierCheck" files="WithChecksStepITest.java" />
</suppressions>
35 changes: 34 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>

<!-- Jenkins Plug-in Dependencies Versions -->
<plugin-util-api.version>2.14.0</plugin-util-api.version>
<plugin-util-api.version>2.16.0</plugin-util-api.version>
<testcontainers.version>1.17.1</testcontainers.version>
</properties>

<licenses>
Expand Down Expand Up @@ -82,6 +83,31 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>ssh-credentials</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>ssh-slaves</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>

<!-- node -->
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand All @@ -104,6 +130,13 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<suppressionsLocation>${maven.multiModuleProjectDirectory}/checkstyle-suppressions.xml</suppressionsLocation>
</configuration>
</plugin>
<plugin>
<groupId>org.assertj</groupId>
<artifactId>assertj-assertions-generator-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ArchitectureTest {
static final ArchRule NO_JENKINS_INSTANCE_CALL = PluginArchitectureRules.NO_JENKINS_INSTANCE_CALL;

@ArchTest
static final ArchRule NO_PUBLIC_TEST_CLASSES = ((ClassesShouldConjunction) PluginArchitectureRules.NO_PUBLIC_TEST_CLASSES)
static final ArchRule NO_PUBLIC_TEST_CLASSES = ((ClassesShouldConjunction) ArchitectureRules.NO_PUBLIC_TEST_CLASSES)
.andShould().notBeAnnotatedWith(RunWith.class); // Allow for JUnit4 tests.

@ArchTest
Expand Down
165 changes: 88 additions & 77 deletions src/test/java/io/jenkins/plugins/checks/api/TruncatedStringTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package io.jenkins.plugins.checks.api;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand All @@ -15,152 +16,162 @@
* Test behavior of the {@link TruncatedString}.
*/
@SuppressWarnings({"VisibilityModifier", "MissingJavadocMethod"})
@RunWith(Parameterized.class)
@SuppressFBWarnings("NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR")
public class TruncatedStringTest {
class TruncatedStringTest {
private static final String MESSAGE = "Truncated"; // length 9

/**
* Human readable test name.
*/
@Parameterized.Parameter
public String testName;

/**
* Parameter for chunking on new lines (or not!).
*/
@Parameterized.Parameter(1)
public boolean chunkOnNewlines;

/**
* Parameter for chunking on chars (or not!).
*/
@Parameterized.Parameter(2)
public boolean chunkOnChars;

@Parameterized.Parameters(name = "{0}")
public static Object[][] parameters() {
return new Object[][]{
{"Chunks+Bytes", false, false},
{"Newlines+Bytes", true, false},
{"Chunks+Chars", false, true},
{"Newlines+Chars", true, true}
};
@SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD", justification = "Called by JUnit")
private static Stream<Arguments> parameters() {
return Stream.of(
Arguments.of(false, false),
Arguments.of(true, false),
Arguments.of(false, true),
Arguments.of(true, true));
}

private TruncatedString.Builder builder;

@Before
@BeforeEach
public void makeBuilder() {
this.builder = new TruncatedString.Builder()
.withTruncationText(MESSAGE);
if (chunkOnNewlines) {
builder.setChunkOnNewlines();
}
this.builder = new TruncatedString.Builder().withTruncationText(MESSAGE);
}

private String build(final int maxSize) {
private String build(final boolean chunkOnChars, final int maxSize) {
return chunkOnChars ? builder.build().buildByChars(maxSize) : builder.build().buildByBytes(maxSize);
}

private String buildRawString() {
return builder.build().toString();
}

@Test
public void shouldBuildStrings() {
@ParameterizedTest
@MethodSource("parameters")
public void shouldBuildStrings(final boolean chunkOnNewlines, final boolean chunkOnChars) {
if (chunkOnNewlines) {
builder.setChunkOnNewlines();
}
builder.addText("Hello\n");
assertThat(buildRawString()).isEqualTo("Hello\n");
assertThat(build(1000)).isEqualTo("Hello\n");
assertThat(build(chunkOnChars, 1000)).isEqualTo("Hello\n");
builder.addText(", world!");
assertThat(buildRawString()).isEqualTo("Hello\n, world!");
assertThat(build(1000)).isEqualTo("Hello\n, world!");
assertThat(build(chunkOnChars, 1000)).isEqualTo("Hello\n, world!");
}

@Test
public void shouldTruncateStrings() {
@ParameterizedTest
@MethodSource("parameters")
public void shouldTruncateStrings(final boolean chunkOnNewlines, final boolean chunkOnChars) {
if (chunkOnNewlines) {
builder.setChunkOnNewlines();
}
builder.addText("xxxxxxxxx\n"); // 10
assertThat(build(20)).isEqualTo("xxxxxxxxx\n");
assertThat(build(chunkOnChars, 20)).isEqualTo("xxxxxxxxx\n");
builder.addText("yyyy\n"); // 5, doesn't cause overflow
assertThat(build(20)).isEqualTo("xxxxxxxxx\nyyyy\n");
assertThat(build(chunkOnChars, 20)).isEqualTo("xxxxxxxxx\nyyyy\n");
builder.addText("zzzzzz\n"); // 7, does cause overflow
assertThat(build(20)).isEqualTo("xxxxxxxxx\nTruncated");
assertThat(build(chunkOnChars, 20)).isEqualTo("xxxxxxxxx\nTruncated");
}

@Test
public void shouldHandleEdgeCases() {
assertThat(build(10)).isEqualTo("");
@ParameterizedTest
@MethodSource("parameters")
public void shouldHandleEdgeCases(final boolean chunkOnNewlines, final boolean chunkOnChars) {
if (chunkOnNewlines) {
builder.setChunkOnNewlines();
}
assertThat(build(chunkOnChars, 10)).isEqualTo("");
assertThat(buildRawString()).isEqualTo("");
builder.addText("xxxxxxxxxxxxxx\n"); // 15
assertThat(build(10)).isEqualTo("Truncated");
assertThat(build(chunkOnChars, 10)).isEqualTo("Truncated");
assertThatThrownBy(() -> {
build(5);
build(chunkOnChars, 5);
}).isInstanceOf(IllegalArgumentException.class)
.hasMessage("Maximum length is less than truncation text.");
}

@Test
public void shouldHandleReversedChunking() {
@ParameterizedTest
@MethodSource("parameters")
public void shouldHandleReversedChunking(final boolean chunkOnNewlines, final boolean chunkOnChars) {
if (chunkOnNewlines) {
builder.setChunkOnNewlines();
}
builder.setTruncateStart();
builder.addText("zzzz\n"); // 5
assertThat(build(20)).isEqualTo("zzzz\n");
assertThat(build(chunkOnChars, 20)).isEqualTo("zzzz\n");
builder.addText("xxxx\n"); // 5, doesn't cause overflow
assertThat(build(20)).isEqualTo("zzzz\nxxxx\n");
assertThat(build(chunkOnChars, 20)).isEqualTo("zzzz\nxxxx\n");
builder.addText("cccc\n"); // 5, doesn't cause overflow
assertThat(build(20)).isEqualTo("zzzz\nxxxx\ncccc\n");
assertThat(build(chunkOnChars, 20)).isEqualTo("zzzz\nxxxx\ncccc\n");
builder.addText("aaaaaa\n"); // 7, does cause overflow
assertThat(build(20)).isEqualTo("Truncatedcccc\naaaaaa\n");
assertThat(build(chunkOnChars, 20)).isEqualTo("Truncatedcccc\naaaaaa\n");
}

@Test
public void shouldHandleEdgeCasesReversed() {
@ParameterizedTest
@MethodSource("parameters")
public void shouldHandleEdgeCasesReversed(final boolean chunkOnNewlines, final boolean chunkOnChars) {
if (chunkOnNewlines) {
builder.setChunkOnNewlines();
}
builder.setTruncateStart();
assertThat(build(10)).isEqualTo("");
assertThat(build(chunkOnChars, 10)).isEqualTo("");
assertThat(buildRawString()).isEqualTo("");
builder.addText("xxxxxxxxxxxxxx\n"); // 15
assertThat(build(10)).isEqualTo("Truncated");
assertThat(build(chunkOnChars, 10)).isEqualTo("Truncated");
assertThatThrownBy(() -> {
build(5);
build(chunkOnChars, 5);
}).isInstanceOf(IllegalArgumentException.class)
.hasMessage("Maximum length is less than truncation text.");
}

@Test
public void shouldChunkNewlinesDifferently() {
@ParameterizedTest
@MethodSource("parameters")
public void shouldChunkNewlinesDifferently(final boolean chunkOnNewlines, final boolean chunkOnChars) {
if (chunkOnNewlines) {
builder.setChunkOnNewlines();
}
builder.addText("xxxxxxxxxx"); // 10
builder.addText("yyyyyyyyyyy"); // 11
assertThat(build(20)).isEqualTo(chunkOnNewlines ? "Truncated" : "xxxxxxxxxxTruncated");
assertThat(build(chunkOnChars, 20)).isEqualTo(chunkOnNewlines ? "Truncated" : "xxxxxxxxxxTruncated");

makeBuilder();
if (chunkOnNewlines) {
builder.setChunkOnNewlines();
}
builder.addText("wwww\n"); // 5
builder.addText("xxxx\nyyyy\nzzzzz\n"); // 16
assertThat(build(20)).isEqualTo(chunkOnNewlines ? "wwww\nxxxx\nTruncated" : "wwww\nTruncated");
assertThat(build(chunkOnChars, 20)).isEqualTo(chunkOnNewlines ? "wwww\nxxxx\nTruncated" : "wwww\nTruncated");
}

@Test
public void shouldTruncateByBytesOrChars() {
@ParameterizedTest
@MethodSource("parameters")
public void shouldTruncateByBytesOrChars(final boolean chunkOnNewlines, final boolean chunkOnChars) {
if (chunkOnNewlines) {
builder.setChunkOnNewlines();
}
builder.addText("☃☃☃\n"); // 3 + 1
assertThat(buildRawString().length()).isEqualTo(4);
assertThat(buildRawString().getBytes(StandardCharsets.UTF_8).length).isEqualTo(10);
assertThat(build(20)).isEqualTo("☃☃☃\n");
assertThat(build(chunkOnChars, 20)).isEqualTo("☃☃☃\n");

builder.addText("🕴️🕴️\n"); // 2 + 1
assertThat(buildRawString().length()).isEqualTo(11);
assertThat(buildRawString().getBytes(StandardCharsets.UTF_8).length).isEqualTo(25);
assertThat(build(20)).isEqualTo(chunkOnChars ? "☃☃☃\n🕴️🕴️\n" : "☃☃☃\nTruncated");
assertThat(build(chunkOnChars, 20)).isEqualTo(chunkOnChars ? "☃☃☃\n🕴️🕴️\n" : "☃☃☃\nTruncated");
}

@Test
public void shouldHandleLongCharsInTruncationText() {
@ParameterizedTest
@MethodSource("parameters")
public void shouldHandleLongCharsInTruncationText(final boolean chunkOnNewlines, final boolean chunkOnChars) {
if (chunkOnNewlines) {
builder.setChunkOnNewlines();
}
String truncationText = "E_TOO_MUCH_☃";
assertThat(truncationText.length()).isEqualTo(12);
assertThat(truncationText.getBytes(StandardCharsets.UTF_8).length).isEqualTo(14);

builder.withTruncationText(truncationText);
builder.addText("xxxx\n"); // 5
builder.addText("x\n"); // 2
assertThat(build(20)).isEqualTo("xxxx\nx\n");
assertThat(build(chunkOnChars, 20)).isEqualTo("xxxx\nx\n");
builder.addText("xxxxxxxxxxxxxxx"); // 15
assertThat(build(20)).isEqualTo(chunkOnChars ? "xxxx\nx\nE_TOO_MUCH_☃" : "xxxx\nE_TOO_MUCH_☃");
assertThat(build(chunkOnChars, 20)).isEqualTo(chunkOnChars ? "xxxx\nx\nE_TOO_MUCH_☃" : "xxxx\nE_TOO_MUCH_☃");
}
}
Loading

0 comments on commit c129796

Please sign in to comment.