Skip to content

Migrate tests to JUnit5 #632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,26 @@
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneOffset;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import org.jvnet.localizer.Localizable;

public class BundleFileNameTest {
@WithJenkins
class BundleFileNameTest {

private static final Clock TEST_CLOCK = Clock.fixed(Instant.parse("2020-10-01T10:20:30Z"), ZoneOffset.UTC);

@Rule
public JenkinsRule j = new JenkinsRule();

@Test
public void testGenerate_Default() {
void testGenerate_Default(JenkinsRule j) {
assertThat(BundleFileName.generate(TEST_CLOCK, null), equalTo("support_2020-10-01_10.20.30.zip"));
}

@Ignore("Relies on SupportPlugin object which is not instantiated by TestPluginManager")
@Disabled("Relies on SupportPlugin object which is not instantiated by TestPluginManager")
@Test
public void testGenerate_WithSupportProvider() {
void testGenerate_WithSupportProvider(JenkinsRule j) {
assertThat(BundleFileName.generate(TEST_CLOCK, null), equalTo("amazing-support_2020-10-01_10.20.30.zip"));
}

Expand Down Expand Up @@ -61,13 +59,13 @@ public void printAboutJenkins(PrintWriter out) {}
}

@Test
public void testGenerate_WithQualifier() {
void testGenerate_WithQualifier(JenkinsRule j) {
assertThat(
BundleFileName.generate(TEST_CLOCK, "qualifier"), equalTo("support_qualifier_2020-10-01_10.20.30.zip"));
}

@Test
public void testGenerate_WithQualifierAndInstanceType() {
void testGenerate_WithQualifierAndInstanceType(JenkinsRule j) {
assertThat(
BundleFileName.generate(TEST_CLOCK, "qualifier"),
equalTo("support_qualifier_instance_type_2020-10-01_10.20.30.zip"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,33 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

public class BundleNamePrefixTest {
@WithJenkins
class BundleNamePrefixTest {

private static final String CURRENT_YEAR = new SimpleDateFormat("yyyy").format(new Date());

@Rule
public JenkinsRule j = new JenkinsRule();

@Test
public void checkOriginalBehaviour() {
void checkOriginalBehaviour(JenkinsRule j) {
assertThat(BundleFileName.generate(), startsWith("support_" + CURRENT_YEAR));
}

@Test
public void checkWithOneProvider() {
void checkWithOneProvider(JenkinsRule j) {
assertThat(BundleFileName.generate(), startsWith("support_pouet_" + CURRENT_YEAR));
}

@Test
public void tooManyProviders() {
void tooManyProviders(JenkinsRule j) {
assertThat(BundleFileName.generate(), startsWith("support_Zis_" + CURRENT_YEAR));
}

@Test
public void withSysProp() {
void withSysProp(JenkinsRule j) {
System.setProperty(BundleNameInstanceTypeProvider.SUPPORT_BUNDLE_NAMING_INSTANCE_SPEC_PROPERTY, "paf");
assertThat(BundleFileName.generate(), startsWith("support_paf_" + CURRENT_YEAR));
System.getProperties().remove(BundleNameInstanceTypeProvider.SUPPORT_BUNDLE_NAMING_INSTANCE_SPEC_PROPERTY);
Expand Down
22 changes: 10 additions & 12 deletions src/test/java/com/cloudbees/jenkins/support/CasCTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,27 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.cloudbees.jenkins.support.config.SupportAutomatedBundleConfiguration;
import com.cloudbees.jenkins.support.filter.ContentFilters;
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import org.junit.Rule;
import org.junit.Test;
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
import org.junit.jupiter.api.Test;

public class CasCTest {

@Rule
public JenkinsConfiguredWithCodeRule r = new JenkinsConfiguredWithCodeRule();
@WithJenkinsConfiguredWithCode
class CasCTest {

@Test
@ConfiguredWithCode("configuration-as-code.yaml")
public void assertConfiguredAsExpected() {
void assertConfiguredAsExpected(JenkinsConfiguredWithCodeRule r) {
assertTrue(
"JCasC should have configured support core to anonymize contents, but it didn't",
ContentFilters.get().isEnabled());
ContentFilters.get().isEnabled(),
"JCasC should have configured support core to anonymize contents, but it didn't");
assertTrue(
"JCasC should have configured support period bundle generation enabled, but it didn't",
SupportAutomatedBundleConfiguration.get().isEnabled());
SupportAutomatedBundleConfiguration.get().isEnabled(),
"JCasC should have configured support period bundle generation enabled, but it didn't");
assertThat(
"JCasC should have configured support period bundle generation period, but it didn't",
SupportAutomatedBundleConfiguration.get().getPeriod(),
Expand Down
65 changes: 29 additions & 36 deletions src/test/java/com/cloudbees/jenkins/support/CheckFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import com.cloudbees.jenkins.support.api.Component;
import com.cloudbees.jenkins.support.configfiles.AgentsConfigFile;
Expand Down Expand Up @@ -38,7 +39,6 @@
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays;
Expand All @@ -55,36 +55,29 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import jenkins.model.Jenkins;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.LoggerRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

public class CheckFilterTest {
@WithJenkins
class CheckFilterTest {
private static final Logger LOGGER = Logger.getLogger(CheckFilterTest.class.getName());

private static final String JOB_NAME = "thejob";
private static final String AGENT_NAME = "agent0"; // it's the name used by createOnlineSlave
private static final String VIEW_ALL_NEW_NAME = "all-view";
private static final String ENV_VAR = getFirstEnvVar();

@Rule
public TemporaryFolder temp = new TemporaryFolder();

@Rule
public JenkinsRule j = new JenkinsRule();

@Rule
public LoggerRule logging = new LoggerRule().record(AsyncResultCache.class, Level.FINER);
@TempDir
private File temp;

@Test
public void checkFilterTest() throws Exception {
void checkFilterTest(JenkinsRule j) throws Exception {
// Create the files to check
FileChecker checker = new FileChecker(j.jenkins);
// Create the objects needed for some contents to be included
QueueTaskFuture<FreeStyleBuild> build = createObjectsWithNames();
QueueTaskFuture<FreeStyleBuild> build = createObjectsWithNames(j);

// Reload the mappings, after the objects were created
ContentFilters.get().setEnabled(true);
Expand Down Expand Up @@ -123,7 +116,7 @@ public void checkFilterTest() throws Exception {
j.waitUntilNoActivity();
}

private QueueTaskFuture<FreeStyleBuild> createObjectsWithNames() throws Exception {
private static QueueTaskFuture<FreeStyleBuild> createObjectsWithNames(JenkinsRule j) throws Exception {
// For an environment variable
if (ENV_VAR != null) {
User.getOrCreateByIdOrFullName(ENV_VAR);
Expand Down Expand Up @@ -157,7 +150,7 @@ private QueueTaskFuture<FreeStyleBuild> createObjectsWithNames() throws Exceptio

private void assertComponent(List<Class<? extends Component>> componentClasses, FileChecker checker)
throws IOException {
File fileZip = new File(temp.getRoot(), "filteredBundle.zip");
File fileZip = new File(temp, "filteredBundle.zip");
Files.deleteIfExists(fileZip.toPath());

try (FileOutputStream zipOutputStream = new FileOutputStream(fileZip)) {
Expand Down Expand Up @@ -191,7 +184,7 @@ private void assertComponent(List<Class<? extends Component>> componentClasses,
}
}

private String getContentFromEntry(ZipFile zip, ZipEntry entry) throws IOException {
private static String getContentFromEntry(ZipFile zip, ZipEntry entry) throws IOException {
if (entry.getSize() == 0) {
return "";
}
Expand All @@ -209,7 +202,7 @@ private String getContentFromEntry(ZipFile zip, ZipEntry entry) throws IOExcepti
out.write(data, 0, currentByte);
}
out.flush();
content = new String(out.toByteArray(), StandardCharsets.UTF_8);
content = out.toString(StandardCharsets.UTF_8);
}
return content;
}
Expand All @@ -228,11 +221,11 @@ private static String getFirstEnvVar() {
}

private static class FileChecker {
private Set<FileToCheck> fileSet = new HashSet<>();
private Set<FileToCheck> unchecked = new HashSet<>();
private Set<String> words = new HashSet<>();
private final Set<FileToCheck> fileSet = new HashSet<>();
private final Set<FileToCheck> unchecked = new HashSet<>();
private final Set<String> words = new HashSet<>();

private FileChecker(Jenkins jenkins) throws UnknownHostException {
private FileChecker(Jenkins jenkins) {
fileSet.add(of("manifest.md", "about", false));
fileSet.add(of("about.md", "b", false));
// fileSet.add(of("items.md", "jobs", false));
Expand Down Expand Up @@ -347,22 +340,22 @@ private FileChecker(Jenkins jenkins) throws UnknownHostException {
unchecked.addAll(fileSet);
}

private String getUpdateCenterURL(Jenkins jenkins) {
private static String getUpdateCenterURL(Jenkins jenkins) {
if (jenkins.getUpdateCenter().getSiteList() != null
&& jenkins.getUpdateCenter().getSiteList().get(0) != null) {
return jenkins.getUpdateCenter().getSiteList().get(0).getUrl();
}
return null;
}

private String getInetAddress() {
private static String getInetAddress() {
try {
Enumeration<NetworkInterface> networkInterfaces = null;
networkInterfaces = NetworkInterface.getNetworkInterfaces();
if (networkInterfaces.hasMoreElements()) {
NetworkInterface ni = networkInterfaces.nextElement();
Enumeration<InetAddress> inetAddresses = ni.getInetAddresses();
if (inetAddresses != null && inetAddresses.hasMoreElements()) {
if (inetAddresses.hasMoreElements()) {
return inetAddresses.nextElement().toString();
}
}
Expand Down Expand Up @@ -409,15 +402,15 @@ private void check(String file, String content) {
if (content == null) {
fail(String.format("Error checking the file %s because its content was null", file));
} else {
Assert.assertTrue(
assertTrue(
content.toLowerCase(Locale.ENGLISH)
.contains(value.wordFiltered.toLowerCase(Locale.ENGLISH)),
String.format(
"The file '%s' should have the word '%s'. File content:\n\n----------\n%s\n%s----------\n\n",
file,
value.wordFiltered,
content.substring(0, Math.min(MAX_CONTENT_LENGTH, content.length())),
content.length() > MAX_CONTENT_LENGTH ? "...\n(content cut off)\n" : ""),
content.toLowerCase(Locale.ENGLISH)
.contains(value.wordFiltered.toLowerCase(Locale.ENGLISH)));
content.length() > MAX_CONTENT_LENGTH ? "...\n(content cut off)\n" : ""));
}
return;
}
Expand All @@ -427,9 +420,9 @@ private void check(String file, String content) {
}

private static class FileToCheck {
private String filePattern;
private String word;
private boolean fileIsFiltered;
private final String filePattern;
private final String word;
private final boolean fileIsFiltered;
private String wordFiltered;

private FileToCheck(String filePattern, String word, boolean fileIsFiltered) {
Expand Down
Loading
Loading