From 87888c7d5fa72eae3b65ad34cf11034ac12237f2 Mon Sep 17 00:00:00 2001 From: Stefan Spieker Date: Wed, 27 Sep 2023 11:22:30 +0200 Subject: [PATCH] Use `isEmpty()` and simplified assertions in Test (#8473) * use isEmpty() and simplified assertions * use `assertThat` instead of `assertFalse` to get better messages in case of failure * Update test/src/test/java/lib/hudson/ListScmBrowsersTest.java Co-authored-by: James Nord * Update test/src/test/java/hudson/PluginManagerTest.java Co-authored-by: James Nord * Update test/src/test/java/hudson/PluginManagerTest.java Co-authored-by: James Nord * Update test/src/test/java/hudson/PluginManagerTest.java Co-authored-by: James Nord * Update test/src/test/java/hudson/model/HelpLinkTest.java Co-authored-by: James Nord * Update test/src/test/java/hudson/model/HelpLinkTest.java Co-authored-by: James Nord * Update test/src/test/java/lib/hudson/ListScmBrowsersTest.java Co-authored-by: James Nord * Update test/src/test/java/lib/hudson/ListScmBrowsersTest.java Co-authored-by: James Nord * spotless fix --------- Co-authored-by: James Nord --- test/src/test/java/hudson/PluginManagerTest.java | 7 +++++-- .../java/hudson/cli/ReloadConfigurationCommandTest.java | 2 +- .../java/hudson/model/FingerprintCleanupThreadTest.java | 2 +- test/src/test/java/hudson/model/HelpLinkTest.java | 6 ++++-- test/src/test/java/hudson/model/JobTest.java | 7 ++----- .../hudson/model/LabelLoadStatisticsQueueLengthTest.java | 5 ++--- .../hudson/node_monitors/ClockMonitorDescriptorTest.java | 2 +- test/src/test/java/lib/hudson/ListScmBrowsersTest.java | 5 ++++- 8 files changed, 20 insertions(+), 16 deletions(-) diff --git a/test/src/test/java/hudson/PluginManagerTest.java b/test/src/test/java/hudson/PluginManagerTest.java index 169b88993f38..507354b8e1b8 100644 --- a/test/src/test/java/hudson/PluginManagerTest.java +++ b/test/src/test/java/hudson/PluginManagerTest.java @@ -28,6 +28,9 @@ import static java.nio.file.attribute.PosixFilePermission.OWNER_READ; import static java.nio.file.attribute.PosixFilePermission.OWNER_WRITE; import static org.awaitility.Awaitility.await; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -488,7 +491,7 @@ private String callDependerValue() throws Exception { // Check that the basic API endpoint invocation works. assertEquals("ok", response.getString("status")); JSONArray data = response.getJSONArray("data"); - assertTrue(data.size() > 0); + assertThat(data, not(empty())); // Check that there was some data in the response and that the first entry // at least had some of the expected fields. @@ -564,7 +567,7 @@ private void dynamicLoadAndDisable(String plugin) throws IOException, Interrupte f.getInputByName("name").setValue(plugin.getAbsolutePath()); r.submit(f); - assertTrue(r.jenkins.getUpdateCenter().getJobs().size() > 0); + assertThat(r.jenkins.getUpdateCenter().getJobs(), not(empty())); // wait for all the download jobs to complete boolean done = true; diff --git a/test/src/test/java/hudson/cli/ReloadConfigurationCommandTest.java b/test/src/test/java/hudson/cli/ReloadConfigurationCommandTest.java index da82fb6534ba..afa742afb3ea 100644 --- a/test/src/test/java/hudson/cli/ReloadConfigurationCommandTest.java +++ b/test/src/test/java/hudson/cli/ReloadConfigurationCommandTest.java @@ -102,7 +102,7 @@ public void reloadSlaveConfig() throws Exception { } private void modifyNode(Node node) throws Exception { - replace(node.getNodeName().equals("") ? "config.xml" : String.format("nodes/%s/config.xml", node.getNodeName()), "oldLabel", "newLabel"); + replace(node.getNodeName().isEmpty() ? "config.xml" : String.format("nodes/%s/config.xml", node.getNodeName()), "oldLabel", "newLabel"); assertThat(node.getLabelString(), equalTo("oldLabel")); diff --git a/test/src/test/java/hudson/model/FingerprintCleanupThreadTest.java b/test/src/test/java/hudson/model/FingerprintCleanupThreadTest.java index 2b3e90613f27..4d87ac7b1bea 100644 --- a/test/src/test/java/hudson/model/FingerprintCleanupThreadTest.java +++ b/test/src/test/java/hudson/model/FingerprintCleanupThreadTest.java @@ -327,7 +327,7 @@ public void delete(String id) { @Override public boolean isReady() { - return storage.size() != 0; + return !storage.isEmpty(); } @Override diff --git a/test/src/test/java/hudson/model/HelpLinkTest.java b/test/src/test/java/hudson/model/HelpLinkTest.java index fc4b8bfc1e39..71e7de67aeae 100644 --- a/test/src/test/java/hudson/model/HelpLinkTest.java +++ b/test/src/test/java/hudson/model/HelpLinkTest.java @@ -1,6 +1,8 @@ package hudson.model; -import static org.junit.Assert.assertTrue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.not; import hudson.matrix.MatrixProject; import hudson.tasks.BuildStepDescriptor; @@ -94,7 +96,7 @@ private void clickAllHelpLinks(JenkinsRule.WebClient webClient, AbstractProject private void clickAllHelpLinks(HtmlPage p) throws Exception { List helpLinks = DomNodeUtil.selectNodes(p, "//a[@class='jenkins-help-button']"); - assertTrue(helpLinks.size() > 0); + assertThat(helpLinks, not(empty())); System.out.println("Clicking " + helpLinks.size() + " help links"); for (HtmlAnchor helpLink : (List) helpLinks) { diff --git a/test/src/test/java/hudson/model/JobTest.java b/test/src/test/java/hudson/model/JobTest.java index 8a219784d14b..e456760ec8ab 100644 --- a/test/src/test/java/hudson/model/JobTest.java +++ b/test/src/test/java/hudson/model/JobTest.java @@ -52,6 +52,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.Comparator; +import java.util.Objects; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; @@ -556,11 +557,7 @@ public void setVirtualName(String virtualName) { @NonNull @Override public String getNodeName() { - if (virtualName != null) { - return virtualName; - } else { - return super.getNodeName(); - } + return Objects.requireNonNullElseGet(virtualName, super::getNodeName); } } diff --git a/test/src/test/java/hudson/model/LabelLoadStatisticsQueueLengthTest.java b/test/src/test/java/hudson/model/LabelLoadStatisticsQueueLengthTest.java index d977a8ed64a2..d488758b1b8e 100644 --- a/test/src/test/java/hudson/model/LabelLoadStatisticsQueueLengthTest.java +++ b/test/src/test/java/hudson/model/LabelLoadStatisticsQueueLengthTest.java @@ -3,6 +3,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.greaterThan; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -203,9 +204,7 @@ private void maintainQueueAndForceRunOfLoadStatisticsUpdater( Thread.sleep(10); } - assertTrue( - "After waiting there are buildable items in the build queue.", - queue.getBuildableItems().size() > 0); + assertFalse("After waiting there are buildable items in the build queue.", queue.getBuildableItems().isEmpty()); // create a LoadStatisticsUpdater, and run it in order to update the // load stats for all the labels diff --git a/test/src/test/java/hudson/node_monitors/ClockMonitorDescriptorTest.java b/test/src/test/java/hudson/node_monitors/ClockMonitorDescriptorTest.java index 7a235a9dfa92..13896bf8af51 100644 --- a/test/src/test/java/hudson/node_monitors/ClockMonitorDescriptorTest.java +++ b/test/src/test/java/hudson/node_monitors/ClockMonitorDescriptorTest.java @@ -37,6 +37,6 @@ public void testClockMonitor() throws Exception { assertTrue(cd.abs() >= 0); assertTrue(cd.abs() < TimeUnit.SECONDS.toMillis(5)); assertFalse(cd.isDangerous()); - assertTrue("html output too short", cd.toHtml().length() > 0); + assertFalse("html output too short", cd.toHtml().isEmpty()); } } diff --git a/test/src/test/java/lib/hudson/ListScmBrowsersTest.java b/test/src/test/java/lib/hudson/ListScmBrowsersTest.java index 4b11c0ff07d2..942effdf47ff 100644 --- a/test/src/test/java/lib/hudson/ListScmBrowsersTest.java +++ b/test/src/test/java/lib/hudson/ListScmBrowsersTest.java @@ -1,5 +1,8 @@ package lib.hudson; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertTrue; import hudson.matrix.MatrixProject; @@ -38,7 +41,7 @@ public void selectBoxesUnique_MatrixProject() throws Exception { private void check(Item p) throws IOException, SAXException { HtmlPage page = j.createWebClient().getPage(p, "configure"); List selects = DomNodeUtil.selectNodes(page, "//select"); - assertTrue(selects.size() > 0); + assertThat(selects, not(empty())); for (HtmlSelect select : selects) { Set title = new HashSet<>(); for (HtmlOption o : select.getOptions()) {