Skip to content

Commit

Permalink
Use isEmpty() and simplified assertions in Test (#8473)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Update test/src/test/java/hudson/PluginManagerTest.java

Co-authored-by: James Nord <[email protected]>

* Update test/src/test/java/hudson/PluginManagerTest.java

Co-authored-by: James Nord <[email protected]>

* Update test/src/test/java/hudson/PluginManagerTest.java

Co-authored-by: James Nord <[email protected]>

* Update test/src/test/java/hudson/model/HelpLinkTest.java

Co-authored-by: James Nord <[email protected]>

* Update test/src/test/java/hudson/model/HelpLinkTest.java

Co-authored-by: James Nord <[email protected]>

* Update test/src/test/java/lib/hudson/ListScmBrowsersTest.java

Co-authored-by: James Nord <[email protected]>

* Update test/src/test/java/lib/hudson/ListScmBrowsersTest.java

Co-authored-by: James Nord <[email protected]>

* spotless fix

---------

Co-authored-by: James Nord <[email protected]>
  • Loading branch information
StefanSpieker and jtnord authored Sep 27, 2023
1 parent 105951c commit 87888c7
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 16 deletions.
7 changes: 5 additions & 2 deletions test/src/test/java/hudson/PluginManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public void delete(String id) {

@Override
public boolean isReady() {
return storage.size() != 0;
return !storage.isEmpty();
}

@Override
Expand Down
6 changes: 4 additions & 2 deletions test/src/test/java/hudson/model/HelpLinkTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<HtmlAnchor>) helpLinks) {
Expand Down
7 changes: 2 additions & 5 deletions test/src/test/java/hudson/model/JobTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
5 changes: 4 additions & 1 deletion test/src/test/java/lib/hudson/ListScmBrowsersTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<HtmlSelect> selects = DomNodeUtil.selectNodes(page, "//select");
assertTrue(selects.size() > 0);
assertThat(selects, not(empty()));
for (HtmlSelect select : selects) {
Set<String> title = new HashSet<>();
for (HtmlOption o : select.getOptions()) {
Expand Down

0 comments on commit 87888c7

Please sign in to comment.