Skip to content
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

Miscellaneous code cleanup #1709

Merged
merged 19 commits into from
Sep 6, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -107,7 +106,7 @@ private WebDriver createWebDriver(TestCleaner cleaner, TestName testName) throws
setDriverPropertyIfMissing("geckodriver", GeckoDriverService.GECKO_DRIVER_EXE_PROPERTY);
GeckoDriverService.Builder builder = new GeckoDriverService.Builder();
if (display != null) {
builder.withEnvironment(Collections.singletonMap("DISPLAY", display));
builder.withEnvironment(Map.of("DISPLAY", display));
}
GeckoDriverService service = builder.build();
return new FirefoxDriver(service, buildFirefoxOptions(testName));
Expand All @@ -117,7 +116,7 @@ private WebDriver createWebDriver(TestCleaner cleaner, TestName testName) throws
case "chrome-container":
return createContainerWebDriver(cleaner, "selenium/standalone-chrome:4.24.0", new ChromeOptions());
case "chrome":
Map<String, String> prefs = new HashMap<String, String>();
Map<String, String> prefs = new HashMap<>();
prefs.put(LANGUAGE_SELECTOR, "en");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
Expand Down Expand Up @@ -283,7 +282,7 @@ private Proxy createSeleniumProxy(String testName) throws UnknownHostException {
// if we are running maven locally but the browser elsewhere (e.g. docker) using the "127.0.0.1"
// address will not work for the browser
String name = System.getenv("SELENIUM_PROXY_HOSTNAME");
InetAddress proxyAddr = null;
InetAddress proxyAddr;
if (name != null) {
proxyAddr = InetAddress.getByName(name);
} else {
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/org/jenkinsci/test/acceptance/Matchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static Matcher<WebDriver> hasContent(final String content) {
}

public static Matcher<WebDriver> hasContent(final Pattern pattern) {
return new Matcher<WebDriver>("Text matching %s", pattern) {
return new Matcher<>("Text matching %s", pattern) {
// text captured the time matchesSafely was executed
private String pageText;

Expand All @@ -59,7 +59,7 @@ public void describeMismatchSafely(WebDriver item, Description mismatchDescripti
* Matches that matches {@link WebDriver} when it has an element that matches to the given selector.
*/
public static Matcher<WebDriver> hasElement(final By selector) {
return new Matcher<WebDriver>("contains element that matches %s", selector) {
return new Matcher<>("contains element that matches %s", selector) {
@Override
public boolean matchesSafely(WebDriver item) {
try {
Expand All @@ -78,7 +78,7 @@ public void describeMismatchSafely(WebDriver item, Description d) {
}

public static Matcher<WebDriver> hasURL(final URL url) {
return new Matcher<WebDriver>("URL matching %s", url) {
return new Matcher<>("URL matching %s", url) {
@Override
public boolean matchesSafely(WebDriver item) {
return item.getCurrentUrl().equals(url.toString());
Expand All @@ -95,7 +95,7 @@ public void describeMismatchSafely(WebDriver item, Description mismatchDescripti
* For asserting that a {@link PageObject}'s top page has an action of the given name.
*/
public static Matcher<PageObject> hasAction(final String displayName) {
return new Matcher<PageObject>("contains action titled %s", displayName) {
return new Matcher<>("contains action titled %s", displayName) {
@Override
public boolean matchesSafely(PageObject po) {
try {
Expand Down Expand Up @@ -147,7 +147,7 @@ public static org.hamcrest.Matcher<String> containsString(final String format, f
* Matches if a string contains a portion that matches to the regular expression.
*/
public static Matcher<String> containsRegexp(final Pattern re) {
return new Matcher<String>("Matches regexp %s", re.toString()) {
return new Matcher<>("Matches regexp %s", re.toString()) {
@Override
public boolean matchesSafely(String item) {
return re.matcher(item).find();
Expand All @@ -156,7 +156,7 @@ public boolean matchesSafely(String item) {
}

public static Matcher<PageObject> pageObjectExists() {
return new Matcher<PageObject>("Page object exists") {
return new Matcher<>("Page object exists") {
private @CheckForNull HttpURLConnection conn; // Store for later defect localization

@Override
Expand All @@ -178,7 +178,7 @@ public boolean matchesSafely(PageObject item) {
}

public static Matcher<PageObject> pageObjectDoesNotExist() {
return new Matcher<PageObject>("Page object does not exist") {
return new Matcher<>("Page object does not exist") {
private @CheckForNull HttpURLConnection conn; // Store for later defect localization

@Override
Expand All @@ -199,7 +199,7 @@ public boolean matchesSafely(PageObject item) {
}

public static Matcher<Jenkins> hasLoggedInUser(final String user) {
return new Matcher<Jenkins>("has logged in user %s", user) {
return new Matcher<>("has logged in user %s", user) {
@Override
public boolean matchesSafely(final Jenkins jenkins) {
final User currentUser = jenkins.getCurrentUser();
Expand All @@ -217,7 +217,7 @@ public void describeMismatchSafely(final Jenkins item, final Description desc) {
}

public static Matcher<Login> loggedInAs(final String user) {
return new Matcher<Login>("has logged in user %s", user) {
return new Matcher<>("has logged in user %s", user) {
@Override
public boolean matchesSafely(final Login login) {
final User currentUser = login.getJenkins().getCurrentUser();
Expand All @@ -235,7 +235,7 @@ public void describeMismatchSafely(final Login item, final Description desc) {
}

public static Matcher<Login> hasInvalidLoginInformation() {
return new Matcher<Login>("has invalid login information message") {
return new Matcher<>("has invalid login information message") {
@Override
public boolean matchesSafely(final Login login) {
try {
Expand All @@ -254,7 +254,7 @@ public void describeMismatchSafely(final Login item, final Description desc) {
}

public static Matcher<User> isMemberOf(final String group) {
return new Matcher<User>(" is member of group %s", group) {
return new Matcher<>(" is member of group %s", group) {
@Override
public boolean matchesSafely(final User user) {
user.open();
Expand All @@ -279,7 +279,7 @@ public void describeMismatchSafely(final User item, final Description desc) {
}

public static Matcher<User> fullNameIs(final String fullName) {
return new Matcher<User>(" full name is %s", fullName) {
return new Matcher<>(" full name is %s", fullName) {
@Override
public boolean matchesSafely(final User user) {
if (user.fullName() != null) {
Expand All @@ -296,7 +296,7 @@ public void describeMismatchSafely(final User item, final Description desc) {
}

public static Matcher<User> mailAddressIs(final String mail) {
return new Matcher<User>(" mail address is %s", mail) {
return new Matcher<>(" mail address is %s", mail) {
@Override
public boolean matchesSafely(final User user) {
if (user.mail() != null) {
Expand All @@ -313,7 +313,7 @@ public void describeMismatchSafely(final User item, final Description desc) {
}

public static Matcher<File> existingFile() {
return new Matcher<File>("an existing file") {
return new Matcher<>("an existing file") {
@Override
public boolean matchesSafely(final File item) {
return item.exists() && item.isFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
Expand Down Expand Up @@ -37,7 +36,7 @@ protected static List<String> envVarOpts(String jenkins_opts) {
if (getenv == null) {
return Collections.emptyList();
}
return Arrays.asList(getenv.split("\\s+"));
return List.of(getenv.split("\\s+"));
}

public void addJavaOpt(String javaOpt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ public HttpResponse<String> createRepo(String repoName, String token) throws Run
.header("PRIVATE-TOKEN", token)
.POST(HttpRequest.BodyPublishers.ofString("{ \"name\": \"" + repoName + "\" }"))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response;
return client.send(request, HttpResponse.BodyHandlers.ofString());
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import javax.xml.rpc.ServiceException;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -96,7 +95,7 @@ private void connect() throws IOException, ServiceException {

public List<RemoteComment> getComments(String ticket) throws IOException, ServiceException {
connect();
return Arrays.asList(svc.getComments(token, ticket));
return List.of(svc.getComments(token, ticket));
}

public JiraSoapService getSvc() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
import org.codehaus.plexus.util.FileUtils;
import org.jenkinsci.test.acceptance.guice.TestName;
import org.jenkinsci.test.acceptance.guice.TestScope;
Expand Down Expand Up @@ -103,8 +105,8 @@ protected void succeeded(Description description) {
@Override
public void failed(Throwable e, Description description) {
if (dir.exists()) {
try {
Files.walk(dir.toPath()).forEach(p -> {
try (Stream<Path> stream = Files.walk(dir.toPath())) {
stream.forEach(p -> {
if (Files.isRegularFile(p)) {
// https://wiki.jenkins-ci.org/display/JENKINS/JUnit+Attachments+Plugin#JUnitAttachmentsPlugin-ByprintingoutthefilenameinaformatthatJenkinswillunderstand
System.out.printf((JUNIT_ATTACHMENT) + "%n", p.toAbsolutePath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

import com.google.inject.Inject;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.jenkinsci.test.acceptance.guice.World;
import org.junit.Assume;
Expand Down Expand Up @@ -70,17 +70,17 @@ public abstract static class Filter {
public abstract String whySkip(Statement base, Description description);

public static <T extends Annotation> Set<T> getAnnotations(Description description, Class<T> type) {
Set<T> annotations = new HashSet<T>();
Set<T> annotations = new HashSet<>();
annotations.add(description.getAnnotation(type));
annotations.add(description.getTestClass().getAnnotation(type));
annotations.remove(null);
return annotations;
}

public static Set<Annotation> getAnnotations(Description description) {
Set<Annotation> annotations = new HashSet<Annotation>();
Set<Annotation> annotations = new HashSet<>();
annotations.addAll(description.getAnnotations());
annotations.addAll(Arrays.asList(description.getTestClass().getAnnotations()));
annotations.addAll(List.of(description.getTestClass().getAnnotations()));
return annotations;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @author Ullrich Hafner
*/
public class JUnitProgressReporter extends RunListener {
private final Set<String> results = new CopyOnWriteArraySet<String>();
private final Set<String> results = new CopyOnWriteArraySet<>();

@Override
public void testStarted(final Description description) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private Statement decorateWithRules(Statement body) {
collectGlobalRules(rules);

// Make sure Jenkins is started between -1 and 0
rules.computeIfAbsent(0, k -> new LinkedHashSet<TestRule>());
rules.computeIfAbsent(0, k -> new LinkedHashSet<>());
rules.get(0).add(jenkinsBoot(rules));

for (Set<TestRule> rulesGroup : rules.values()) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jenkinsci/test/acceptance/junit/Wait.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public Wait<Subject> withMessage(String pattern, Object... args) {

// For convenience as we have quite a lot of Callables historically
public <Return> Return until(final Callable<Return> isTrue) {
return super.until(new Function<Subject, Return>() {
return super.until(new Function<>() {
@Override
public Return apply(Subject input) {
try {
Expand Down Expand Up @@ -153,7 +153,7 @@ public String diagnose(Throwable lastException, String message) {
}

public <Return> Return until(final Wait.Predicate<Return> isTrue) {
Function<Subject, Return> fun = new Function<Subject, Return>() {
Function<Subject, Return> fun = new Function<>() {
@Override
public Return apply(Subject input) {
try {
Expand Down Expand Up @@ -183,7 +183,7 @@ public String toString() {
protected RuntimeException timeoutException(String message, Throwable lastException) {
if (predicate != null) {
String diagnosis = predicate.diagnose(lastException, message);
if (diagnosis != null && !diagnosis.equals("")) {
if (diagnosis != null && !diagnosis.isEmpty()) {
message += ". " + diagnosis;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.SystemUtils;
import org.jenkinsci.test.acceptance.controller.JenkinsController;
import org.jenkinsci.test.acceptance.controller.LocalController;
Expand Down Expand Up @@ -76,7 +77,7 @@ private void check(WithOS withos, Class<?> testCase) {
String errorMsg =
"Test and Jenkins instance must be running on any of the following operating systems: "
+ Arrays.toString(withos.os());
if (!Arrays.asList(withos.os()).contains(currentOS())) {
if (!List.of(withos.os()).contains(currentOS())) {
throw new AssumptionViolatedException(errorMsg);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private void installPlugins(List<PluginSpec> install) {
LOGGER.info("All required plugins already installed.");
} else {
LOGGER.info("Installing plugins for test: " + install);
PluginSpec[] installList = install.toArray(new PluginSpec[install.size()]);
PluginSpec[] installList = install.toArray(new PluginSpec[0]);
try {
//noinspection deprecation
pm.installPlugins(installList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public boolean hasLogged(Pattern pattern) {
public List<String> getEvents() {
try {
List<String> events = new ArrayList<>();
for (String line : (List<String>) IOUtils.readLines(url.openStream(), StandardCharsets.UTF_8)) {
for (String line : IOUtils.readLines(url.openStream(), StandardCharsets.UTF_8)) {
Matcher m = LOG_PATTERN.matcher(line);
m.find();
events.add(m.group(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public ConfigFileProvider(Folder f) {
public <T extends ProvidedFile> T addFile(Class<T> type) {
visit(url("selectProvider"));

WebElement radio = findCaption(type, new Finder<WebElement>() {
WebElement radio = findCaption(type, new Finder<>() {
@Override
protected WebElement find(String caption) {
return outer.find(by.radioButton(caption));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected CredentialsPage createCredentialsPage(Boolean userCredentials) {
}

protected CredentialsPage createCredentialsPage(Boolean userCredentials, String domain) {
CredentialsPage cp = null;
CredentialsPage cp;
if (userCredentials) {
cp = new CredentialsPage(jenkins, domain, CREATED_USER);
// Make sure we are not going to have a 404, we got some issues like that
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jenkinsci.test.acceptance.plugins.dashboard_view.controls;

import java.util.Arrays;
import java.util.List;
import org.jenkinsci.test.acceptance.po.Control;
import org.jenkinsci.test.acceptance.po.PageAreaImpl;
Expand Down Expand Up @@ -56,7 +55,7 @@ public void removeAll() {
By xpath = By.xpath("//div[@name='columns' and not(contains(.,'" + Column.LAST_STABLE.getText()
+ "'))]//button[@title='Delete']");
List<WebElement> columns = control(form).resolve().findElements(xpath);
Arrays.stream(columns.toArray(new WebElement[columns.size()])).forEach(WebElement::click);
columns.forEach(WebElement::click);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void setIncludeRegex(String regex) {
*
* @author peter-mueller
*/
public static enum StatusFilter {
public enum StatusFilter {
/**
* Only show enabled jobs.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jenkinsci.test.acceptance.plugins.dashboard_view.read;

import java.util.Arrays;
import java.util.List;
import org.jenkinsci.test.acceptance.po.PageAreaImpl;
import org.jenkinsci.test.acceptance.po.PageObject;
Expand Down Expand Up @@ -34,6 +33,6 @@ public BreadCrumbs(PageObject context, String path) {
public List<String> getBreadCrumbs() {
final WebElement webElement = find(nameCrumb);
final String[] crumbs = webElement.getText().split("\n");
return Arrays.asList(crumbs);
return List.of(crumbs);
}
}
Loading
Loading