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 #107

Merged
merged 8 commits into from
Sep 14, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileAlreadyExistsException;
import java.util.*;

Expand Down Expand Up @@ -309,7 +310,7 @@ protected Void run () throws Exception {
Charset cs;

if (StringUtils.isEmpty(step.getCharset())) {
cs = Charset.forName("UTF-8"); //If it doesn't exist then something is broken in the jvm
cs = StandardCharsets.UTF_8; //If it doesn't exist then something is broken in the jvm
} else {
cs = Charset.forName(step.getCharset()); //Will throw stuff directly to the user
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.jar.Manifest;

Expand Down Expand Up @@ -69,7 +70,7 @@ protected SimpleManifest doRun() throws Exception {
}

private SimpleManifest parseText(String text) throws IOException {
Manifest manifest = new Manifest(new ByteArrayInputStream(text.getBytes("UTF-8")));
Manifest manifest = new Manifest(new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8)));
return new SimpleManifest(manifest);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import hudson.FilePath;
import java.io.FileNotFoundException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution;
Expand Down Expand Up @@ -68,7 +69,7 @@ protected Void run() throws Exception {
throw new FileNotFoundException(Messages.CSVStepExecution_fileIsDirectory(path.getRemote()));
}

try (OutputStreamWriter writer = new OutputStreamWriter(path.write(), "UTF-8")) {
try (OutputStreamWriter writer = new OutputStreamWriter(path.write(), StandardCharsets.UTF_8)) {
CSVFormat format = step.getFormat();
if (format == null) {
format = CSVFormat.DEFAULT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.FilePath;
import hudson.model.Descriptor;
import hudson.remoting.VirtualChannel;
import hudson.util.FormValidation;
import jenkins.MasterToSlaveFileCallable;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepContext;
Expand All @@ -14,15 +12,7 @@
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution;
import org.kohsuke.stapler.QueryParameter;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collections;
import java.util.Formatter;
import java.util.Locale;
import java.util.Set;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -74,7 +75,7 @@ protected Object doRun() throws Exception {
FilePath f = ws.child(step.getFile());
if (f.exists() && !f.isDirectory()) {
try (InputStream is = f.read()) {
json = JSONSerializer.toJSON(IOUtils.toString(is, "UTF-8"));
json = JSONSerializer.toJSON(IOUtils.toString(is, StandardCharsets.UTF_8));
}
} else if (f.isDirectory()) {
throw new IllegalArgumentException(Messages.JSONStepExecution_fileIsDirectory(f.getRemote()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import java.io.FileNotFoundException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;

/**
* Execution of {@link WriteJSONStep}.
Expand Down Expand Up @@ -61,7 +62,7 @@ protected Void run() throws Exception {
throw new FileNotFoundException(Messages.JSONStepExecution_fileIsDirectory(path.getRemote()));
}

try (OutputStreamWriter writer = new OutputStreamWriter(path.write(), "UTF-8")) {
try (OutputStreamWriter writer = new OutputStreamWriter(path.write(), StandardCharsets.UTF_8)) {
step.execute(writer);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public Map<String, String> invoke(File zipFile, VirtualChannel channel) throws I
}
PrintStream logger = listener.getLogger();
boolean doGlob = !StringUtils.isBlank(glob);
Map<String, String> strMap = new TreeMap<String, String>();
Map<String, String> strMap = new TreeMap<>();
try (ZipFile zip = new ZipFile(zipFile, Charset.forName(charset))) {
logger.println("Extracting from " + zipFile.getAbsolutePath());
Enumeration<? extends ZipEntry> entries = zip.entries();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected Void run() throws Exception {
int count = source.act(new ZipItFileCallable(destination, step.getGlob(), step.getExclude(), step.isOverwrite()));
listener.getLogger().println("Zipped " + count + " entries.");
if (step.isArchive()) {
Run build = getContext().get(Run.class);
Run<?, ?> build = getContext().get(Run.class);
if (build == null) {
throw new MissingContextVariableException(Run.class);
}
Expand All @@ -102,7 +102,7 @@ protected Void run() throws Exception {
throw new MissingContextVariableException(Launcher.class);
}
listener.getLogger().println("Archiving " + destination.getRemote());
Map<String, String> files = new HashMap<String, String>();
Map<String, String> files = new HashMap<>();
String s = step.getZipFile().replace('\\', '/');
files.put(s, s);
build.pickArtifactManager().archive(ws, launcher, new BuildListenerAdapter(listener), files);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import java.util.List;

import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;

public class CompareVersionsStepTest {
@Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@ public void readFileWithFormat() throws Exception {
j.assertBuildStatusSuccess(p.scheduleBuild2(0));
}

private String getCSV(char separator) throws IOException {
private String getCSV(char separator) {
String line0 = String.join(String.valueOf(separator), "key", "value", "attr");
String line1 = String.join(String.valueOf(separator), "a", "b", "c");
String line2 = String.join(String.valueOf(separator), "1", "2", "3");
return String.join("\n", line0, line1, line2);
}

private String getCSV() throws IOException {
private String getCSV() {
return getCSV(',');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void writeFile() throws Exception {
j.assertBuildStatusSuccess(p.scheduleBuild2(0));

// file exists by default so we check that should not be empty
assertThat(output.length(), greaterThan(0l));
assertThat(output.length(), greaterThan(0L));

String lines = new String(Files.readAllBytes(Paths.get(output.toURI())));
assertThat(lines.split("\r\n|\r|\n").length, equalTo(3));
Expand Down Expand Up @@ -114,7 +114,7 @@ public void writeFileWithHeader() throws Exception {
j.assertBuildStatusSuccess(p.scheduleBuild2(0));

// file exists by default so we check that should not be empty
assertThat(output.length(), greaterThan(0l));
assertThat(output.length(), greaterThan(0L));

String lines = new String(Files.readAllBytes(Paths.get(output.toURI())));
assertThat(lines.split("\r\n|\r|\n").length, equalTo(3));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.junit.Rule;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsSessionRule;

import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -77,7 +78,7 @@ public void smokes() throws Throwable {
SemaphoreStep.success("wait/1", null);
WorkflowRun b = r.jenkins.getItemByFullName("p", WorkflowJob.class).getBuildByNumber(1);
r.assertBuildStatus(Result.SUCCESS, r.waitForCompletion(b));
assertThat(r.getLog(b), stringContainsInOrder("got: first message second message", Functions.isWindows() ? "WS>rem" : "+ true"));
assertThat(JenkinsRule.getLog(b), stringContainsInOrder("got: first message second message", Functions.isWindows() ? "WS>rem" : "+ true"));

});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void setup() throws Exception {
@Test
public void configRoundTrip() throws Exception {
TouchStep step = new TouchStep("target/my.tag");
step.setTimestamp(10000l);
step.setTimestamp(10000L);

TouchStep step2 = new StepConfigTester(j).configRoundTrip(step);
j.assertEqualDataBoundBeans(step, step2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void readFileAndText() throws Exception {
j.assertLogContains(Messages.ReadJSONStepExecution_tooManyArguments("readJSON"), run);
}

private String getJSON() throws IOException {
private String getJSON() {
JSONArray tags = new JSONArray();
for (int i = 0; i < 3; i++) {
tags.add(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import static org.junit.Assert.assertNotNull;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand Down Expand Up @@ -116,7 +115,7 @@ public void writeFile() throws Exception {
j.assertBuildStatusSuccess(p.scheduleBuild2(0));

// file exists by default so we check that should not be empty
assertThat(output.length(), greaterThan(0l));
assertThat(output.length(), greaterThan(0L));

String jsonResult = new String(Files.readAllBytes(Paths.get(output.toURI())));
JSON json = JSONSerializer.toJSON(jsonResult);
Expand Down Expand Up @@ -187,7 +186,7 @@ public void checkCannotReturnTextAndFile() throws Exception {
j.assertLogContains(Messages.WriteJSONStepExecution_bothReturnTextAndFile("writeJSON"), run);
}

private String getJSON(int elements) throws IOException {
private String getJSON(int elements) {
JSONArray root = new JSONArray();
for (int i = 0; i < elements; i++) {
JSONObject jsonElement = new JSONObject();
Expand All @@ -197,4 +196,4 @@ private String getJSON(int elements) throws IOException {
return root.toString();
}

}
}