Reduce usage of FileUtils (#6756)

This commit is contained in:
Basil Crow authored and GitHub committed 2022-07-04 06:51:26 -07:00
1 parent 43862dfcfc
commit 23ab4fa0a6
45 files changed
+254 -218

No files matched your search

+2 -2
View File
@@ -38,8 +38,8 @@ import hudson.util.StreamTaskListener;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Files;
import jenkins.security.MasterToSlaveCallable;
import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@@ -69,7 +69,7 @@ public class LauncherTest {
assertTrue("Join did not finish promptly. The completion time (" + terminationTime + "ms) is longer than expected 15s", terminationTime < 15000);
channels.french.call(new NoopCallable()); // this only returns after the other side of the channel has finished executing cancellation
Thread.sleep(2000); // more delay to make sure it's gone
assertNull("process should be gone", ProcessTree.get().get(Integer.parseInt(FileUtils.readFileToString(tmp, Charset.defaultCharset()).trim())));
assertNull("process should be gone", ProcessTree.get().get(Integer.parseInt(Files.readString(tmp.toPath(), Charset.defaultCharset()).trim())));
// Manual version of test: set up instance w/ one agent. Now in script console
// new hudson.FilePath(new java.io.File("/tmp")).createLauncher(new hudson.util.StreamTaskListener(System.err)).
@@ -44,7 +44,6 @@ import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.jvnet.hudson.test.Issue;
@@ -149,13 +148,13 @@ public class PluginManagerTest {
"Plugin-Developers: ";
private File createHpiWithManifest() throws IOException {
String manifestPath = "META-INF/MANIFEST.MF";
new File("META-INF").mkdir();
FileUtils.write(new File(tmp.toFile(), manifestPath), SAMPLE_MANIFEST_FILE, StandardCharsets.UTF_8);
Path metaInf = tmp.resolve("META-INF");
Files.createDirectory(metaInf);
Files.writeString(metaInf.resolve("MANIFEST.MF"), SAMPLE_MANIFEST_FILE, StandardCharsets.UTF_8);
final File f = new File(tmp.toFile(), "my.hpi");
try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(f.toPath()))) {
ZipEntry e = new ZipEntry(manifestPath);
ZipEntry e = new ZipEntry("META-INF/MANIFEST.MF");
out.putNextEntry(e);
byte[] data = SAMPLE_MANIFEST_FILE.getBytes(StandardCharsets.UTF_8);
out.write(data, 0, data.length);
+1 -2
View File
@@ -58,7 +58,6 @@ import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.Assert;
@@ -612,7 +611,7 @@ public class UtilTest {
File aa = new File(a, "aa");
aa.mkdirs();
File aaTxt = new File(aa, "aa.txt");
FileUtils.write(aaTxt, "aa", StandardCharsets.US_ASCII, false);
Files.writeString(aaTxt.toPath(), "aa", StandardCharsets.US_ASCII);
File b = new File(root, "b");
b.mkdir();
@@ -22,7 +22,6 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
@@ -69,7 +68,7 @@ public class AtomicFileWriterTest {
@Before
public void setUp() throws IOException {
af = tmp.newFile();
FileUtils.writeStringToFile(af, PREVIOUS, Charset.defaultCharset());
Files.writeString(af.toPath(), PREVIOUS, Charset.defaultCharset());
afw = new AtomicFileWriter(af.toPath(), Charset.defaultCharset());
}
@@ -119,7 +118,7 @@ public class AtomicFileWriterTest {
// Then
assertEquals(expectedContent.length() + 3, Files.size(af.toPath()));
assertEquals(expectedContent + "hey", FileUtils.readFileToString(af, Charset.defaultCharset()));
assertEquals(expectedContent + "hey", Files.readString(af.toPath(), Charset.defaultCharset()));
}
@Test
@@ -132,14 +131,14 @@ public class AtomicFileWriterTest {
// Then
assertTrue(Files.notExists(afw.getTemporaryPath()));
assertEquals(PREVIOUS, FileUtils.readFileToString(af, Charset.defaultCharset()));
assertEquals(PREVIOUS, Files.readString(af.toPath(), Charset.defaultCharset()));
}
@Test
public void indexOutOfBoundsLeavesOriginalUntouched() throws Exception {
// Given
assertThrows(IndexOutOfBoundsException.class, () -> afw.write(expectedContent, 0, expectedContent.length() + 10));
assertEquals(PREVIOUS, FileUtils.readFileToString(af, Charset.defaultCharset()));
assertEquals(PREVIOUS, Files.readString(af.toPath(), Charset.defaultCharset()));
}
@Test
@@ -8,8 +8,8 @@ import java.io.File;
import java.io.IOException;
import java.nio.channels.ClosedChannelException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -55,6 +55,6 @@ public class FileChannelWriterTest {
private void assertContent(String string) throws IOException {
assertThat(FileUtils.readFileToString(file, StandardCharsets.UTF_8), equalTo(string));
assertThat(Files.readString(file.toPath(), StandardCharsets.UTF_8), equalTo(string));
}
}
@@ -10,13 +10,13 @@ import hudson.model.TaskListener;
import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.List;
import java.util.regex.Pattern;
import javax.crypto.Cipher;
import jenkins.security.ConfidentialStoreRule;
import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@@ -50,11 +50,11 @@ public class SecretRewriterTest {
private String roundtrip(String before) throws Exception {
SecretRewriter sr = new SecretRewriter();
File f = File.createTempFile("test", "xml", tmp.getRoot());
FileUtils.write(f, before, Charset.defaultCharset());
sr.rewrite(f);
Path path = Files.createTempFile(tmp.getRoot().toPath(), "test", "xml");
Files.writeString(path, before, Charset.defaultCharset());
sr.rewrite(path.toFile());
//assert after.replaceAll(System.getProperty("line.separator"), "\n").trim()==f.text.replaceAll(System.getProperty("line.separator"), "\n").trim()
return FileUtils.readFileToString(f, Charset.defaultCharset()).replaceAll(System.getProperty("line.separator"), "\n").trim();
return Files.readString(path, Charset.defaultCharset()).replaceAll(System.getProperty("line.separator"), "\n").trim();
}
@SuppressWarnings("deprecation")
@@ -83,16 +83,16 @@ public class SecretRewriterTest {
// set up some directories with stuff
File t = tmp.newFolder("t");
List<String> dirs = Arrays.asList("a", "b", "c", "c/d", "c/d/e");
List<String> dirs = List.of("a", "b", "c", "c/d", "c/d/e");
for (String p : dirs) {
File d = new File(t, p);
d.mkdir();
FileUtils.write(new File(d, "foo.xml"), payload, Charset.defaultCharset());
Files.writeString(d.toPath().resolve("foo.xml"), payload, Charset.defaultCharset());
}
// stuff outside
File t2 = tmp.newFolder("t2");
FileUtils.write(new File(t2, "foo.xml"), payload, Charset.defaultCharset());
Files.writeString(t2.toPath().resolve("foo.xml"), payload, Charset.defaultCharset());
// some recursions as well as valid symlinks
new FilePath(t).child("c/symlink").symlinkTo("..", st);
@@ -102,11 +102,11 @@ public class SecretRewriterTest {
assertEquals(6, sw.rewriteRecursive(t, st));
for (String p : dirs) {
assertTrue(MSG_PATTERN.matcher(FileUtils.readFileToString(new File(t, p + "/foo.xml"), Charset.defaultCharset()).trim()).matches());
assertTrue(MSG_PATTERN.matcher(Files.readString(new File(t, p + "/foo.xml").toPath(), Charset.defaultCharset()).trim()).matches());
}
// t2 is only reachable by following a symlink. this should be covered, too
assertTrue(MSG_PATTERN.matcher(FileUtils.readFileToString(new File(t2, "foo.xml"), Charset.defaultCharset()).trim()).matches());
assertTrue(MSG_PATTERN.matcher(Files.readString(new File(t2, "foo.xml").toPath(), Charset.defaultCharset()).trim()).matches());
}
}
@@ -6,6 +6,7 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.junit.Rule;
@@ -31,7 +32,7 @@ public class TextFileTest {
@Test
public void shortHead() throws Exception {
File f = tmp.newFile();
FileUtils.write(f, "hello", Charset.defaultCharset());
Files.writeString(f.toPath(), "hello", Charset.defaultCharset());
TextFile t = new TextFile(f);
assertEquals("hello", t.head(35));
@@ -41,7 +42,7 @@ public class TextFileTest {
public void tail() throws Exception {
File f = tmp.newFile();
FileUtils.copyURLToFile(getClass().getResource("ascii.txt"), f);
String whole = FileUtils.readFileToString(f, Charset.defaultCharset());
String whole = Files.readString(f.toPath(), Charset.defaultCharset());
TextFile t = new TextFile(f);
String tailStr = whole.substring(whole.length() - 34);
assertEquals(tailStr, t.fastTail(tailStr.length()));
@@ -50,7 +51,7 @@ public class TextFileTest {
@Test
public void shortTail() throws Exception {
File f = tmp.newFile();
FileUtils.write(f, "hello", Charset.defaultCharset());
Files.writeString(f.toPath(), "hello", Charset.defaultCharset());
TextFile t = new TextFile(f);
assertEquals("hello", t.fastTail(35));
@@ -36,6 +36,8 @@ import hudson.util.StreamTaskListener;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.TimeZone;
import java.util.TreeMap;
@@ -309,7 +311,9 @@ public class RunIdMigratorTest {
// TODO test sane recovery from various error conditions
private void write(String file, String text) throws Exception {
FileUtils.write(new File(dir, file), text, Charset.defaultCharset());
Path path = new File(dir, file).toPath();
Files.createDirectories(path.getParent());
Files.writeString(path, text, Charset.defaultCharset());
}
private void link(String symlink, String dest) throws Exception {
@@ -329,7 +333,7 @@ public class RunIdMigratorTest {
if (symlink != null) {
notation = "→" + symlink;
} else if (kid.isFile()) {
notation = "'" + FileUtils.readFileToString(kid, Charset.defaultCharset()) + "'";
notation = "'" + Files.readString(kid.toPath(), Charset.defaultCharset()) + "'";
} else if (kid.isDirectory()) {
notation = summarize(kid);
} else {
@@ -27,7 +27,7 @@ package jenkins.model.lazy;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import org.apache.commons.io.FileUtils;
import java.nio.file.Files;
/**
* @author Kohsuke Kawaguchi
@@ -44,7 +44,7 @@ public class FakeMap extends AbstractLazyLoadRunMap<Build> {
@Override
protected Build retrieve(File dir) throws IOException {
String n = FileUtils.readFileToString(new File(dir, "n"), Charset.defaultCharset()).trim();
String n = Files.readString(dir.toPath().resolve("n"), Charset.defaultCharset()).trim();
//new Exception("loading #" + n).printStackTrace();
return new Build(Integer.parseInt(n));
}
@@ -27,6 +27,7 @@ package jenkins.model.lazy;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import org.apache.commons.io.FileUtils;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
@@ -49,8 +50,8 @@ public class FakeMapBuilder implements TestRule {
public FakeMapBuilder add(int n) throws IOException {
File build = new File(dir, Integer.toString(n));
FileUtils.writeStringToFile(new File(build, "n"), Integer.toString(n), StandardCharsets.US_ASCII);
build.mkdir();
Files.createDirectory(build.toPath());
Files.writeString(build.toPath().resolve("n"), Integer.toString(n), StandardCharsets.US_ASCII);
return this;
}
@@ -1,17 +1,16 @@
package jenkins.security;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import hudson.FilePath;
import hudson.Functions;
import java.io.File;
import java.nio.charset.MalformedInputException;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.FileUtils;
import java.nio.file.Files;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@@ -37,7 +36,7 @@ public class DefaultConfidentialStoreTest {
assertTrue(new File(tmp, "test").exists());
assertTrue(new File(tmp, "master.key").exists());
assertThat(FileUtils.readFileToString(new File(tmp, "test"), StandardCharsets.UTF_8), not(containsString("Hello"))); // the data shouldn't be a plain text, obviously
assertThrows(MalformedInputException.class, () -> Files.readString(tmp.toPath().resolve("test"), StandardCharsets.UTF_8)); // the data shouldn't be a plain text, obviously
if (!Functions.isWindows()) {
assertEquals(0700, new FilePath(tmp).mode() & 0777); // should be read only
@@ -41,6 +41,7 @@ import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
@@ -51,7 +52,6 @@ import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.stream.Collectors;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -203,10 +203,10 @@ public class ApiTokenStatsTest {
{ // replace the ID_1 with ID_2 in the file
XmlFile statsFile = ApiTokenStats.getConfigFile(tmp.getRoot());
String content = FileUtils.readFileToString(statsFile.getFile(), Charset.defaultCharset());
String content = Files.readString(statsFile.getFile().toPath(), Charset.defaultCharset());
// now there are multiple times the same id in the file with different stats
String newContentWithDuplicatedId = content.replace(ID_1, ID_2).replace(ID_3, ID_2);
FileUtils.write(statsFile.getFile(), newContentWithDuplicatedId, Charset.defaultCharset());
Files.writeString(statsFile.getFile().toPath(), newContentWithDuplicatedId, Charset.defaultCharset());
}
{
@@ -86,10 +86,10 @@ public class VirtualFileTest {
@Test public void outsideSymlinks() throws Exception {
assumeFalse(Functions.isWindows());
File ws = tmp.newFolder("ws");
FileUtils.write(new File(ws, "safe"), "safe", StandardCharsets.US_ASCII, false);
Files.writeString(ws.toPath().resolve("safe"), "safe", StandardCharsets.US_ASCII);
Util.createSymlink(ws, "safe", "supported", TaskListener.NULL);
File other = tmp.newFolder("other");
FileUtils.write(new File(other, "secret"), "s3cr3t", StandardCharsets.US_ASCII, false);
Files.writeString(other.toPath().resolve("secret"), "s3cr3t", StandardCharsets.US_ASCII);
Util.createSymlink(ws, "../other/secret", "hack", TaskListener.NULL);
VirtualFile root = VirtualFile.forFile(ws);
VirtualFile supported = root.child("supported");
@@ -759,19 +759,19 @@ public class VirtualFileTest {
File aaa = new File(aa, "aaa");
aaa.mkdirs();
File aaTxt = new File(aa, "aa.txt");
FileUtils.write(aaTxt, "aa", StandardCharsets.US_ASCII, false);
Files.writeString(aaTxt.toPath(), "aa", StandardCharsets.US_ASCII);
File ab = new File(a, "ab");
ab.mkdirs();
File abTxt = new File(ab, "ab.txt");
FileUtils.write(abTxt, "ab", StandardCharsets.US_ASCII, false);
Files.writeString(abTxt.toPath(), "ab", StandardCharsets.US_ASCII);
File b = new File(root, "b");
File ba = new File(b, "ba");
ba.mkdirs();
File baTxt = new File(ba, "ba.txt");
FileUtils.write(baTxt, "ba", StandardCharsets.US_ASCII, false);
Files.writeString(baTxt.toPath(), "ba", StandardCharsets.US_ASCII);
File _a = new File(b, "_a");
new FilePath(_a).symlinkTo(a.getAbsolutePath(), TaskListener.NULL);
@@ -1041,7 +1041,7 @@ public class VirtualFileTest {
public void testLength_FileVF() throws IOException {
File ws = tmp.newFolder("ws");
String childString = "child";
FileUtils.write(new File(ws, childString), childString);
Files.writeString(ws.toPath().resolve(childString), childString, StandardCharsets.US_ASCII);
VirtualFile child = VirtualFile.forFile(ws).child(childString);
assertThat(child.length(), is((long) childString.length()));
}
@@ -1120,7 +1120,7 @@ public class VirtualFileTest {
// which generally does nothing.
File ws = tmp.newFolder("ws");
String childString = "child";
FileUtils.write(new File(ws, childString), childString);
Files.writeString(ws.toPath().resolve(childString), childString, StandardCharsets.US_ASCII);
VirtualFile child = new VirtualFileMinimalImplementation(ws).child(childString);
String fileContents = IOUtils.toString(child.open());
assertThat(childString, is(fileContents));
@@ -1134,7 +1134,7 @@ public class VirtualFileTest {
// which generally does nothing.
File ws = tmp.newFolder("ws");
String childString = "child";
FileUtils.write(new File(ws, childString), childString);
Files.writeString(ws.toPath().resolve(childString), childString, StandardCharsets.US_ASCII);
String linkString = "link";
Util.createSymlink(ws, childString, linkString, TaskListener.NULL);
@@ -1148,7 +1148,7 @@ public class VirtualFileTest {
public void testOpenNoFollowLinks_NoFollowsLink_FileVF() throws Exception {
File ws = tmp.newFolder("ws");
String childString = "child";
FileUtils.write(new File(ws, childString), childString);
Files.writeString(ws.toPath().resolve(childString), childString, StandardCharsets.US_ASCII);
String linkString = "link";
Util.createSymlink(ws, childString, linkString, TaskListener.NULL);
@@ -1164,7 +1164,7 @@ public class VirtualFileTest {
String symlinkName = "symlink";
Util.createSymlink(tmp.getRoot(), ws.getName(), symlinkName, null);
String childString = "child";
FileUtils.write(new File(ws, childString), childString);
Files.writeString(ws.toPath().resolve(childString), childString, StandardCharsets.US_ASCII);
File childThroughSymlink = new File(tmp.getRoot(), "/" + symlinkName + "/" + childString);
VirtualFile child = rootVirtualFile.child(symlinkName).child(childString);
assertThrows("Should have not followed links", IOException.class, () -> child.open(true));
@@ -1177,7 +1177,7 @@ public class VirtualFileTest {
String symlinkName = "symlink";
Util.createSymlink(tmp.getRoot(), ws.getName(), symlinkName, null);
String childString = "child";
FileUtils.write(new File(ws, childString), childString);
Files.writeString(ws.toPath().resolve(childString), childString, StandardCharsets.US_ASCII);
VirtualFile rootVirtualPath = VirtualFile.forFilePath(new FilePath(tmp.getRoot()));
VirtualFile childVirtualPath = rootVirtualPath.child(symlinkName).child(childString);
assertThrows("Should have not followed links", IOException.class, () -> childVirtualPath.open(true));
@@ -1188,7 +1188,7 @@ public class VirtualFileTest {
public void testOpenNoFollowLinks_NoFollowsLink_FilePathVF() throws Exception {
File ws = tmp.newFolder("ws");
String childString = "child";
FileUtils.write(new File(ws, childString), childString);
Files.writeString(ws.toPath().resolve(childString), childString, StandardCharsets.US_ASCII);
String linkString = "link";
Util.createSymlink(ws, childString, linkString, TaskListener.NULL);
@@ -1236,7 +1236,7 @@ public class VirtualFileTest {
public void testLength_FilePathVF() throws IOException {
File ws = tmp.newFolder("ws");
String childString = "child";
FileUtils.write(new File(ws, childString), childString);
Files.writeString(ws.toPath().resolve(childString), childString, StandardCharsets.US_ASCII);
VirtualFile child = VirtualFile.forFilePath(new FilePath(ws)).child(childString);
assertThat(child.length(), is((long) childString.length()));
}
@@ -1493,7 +1493,7 @@ public class VirtualFileTest {
String externalFolderName = "external";
File externalFile = tmp.newFolder(externalFolderName);
String childString = "child";
FileUtils.write(new File(externalFile, childString), childString);
Files.writeString(externalFile.toPath().resolve(childString), childString, StandardCharsets.US_ASCII);
Util.createSymlink(ws, "../" + externalFolderName, "invalidSymlink", TaskListener.NULL);
return ws;
}