Browse Source

Explicitly specify charset when constructing FileWriter

Replace explicit construction of FileWriter with calls to the
utility method Files.newBufferedWriter, which allows to specify
the charset.

Also convert to try-with-resource.

Change-Id: I9fa3f612b9b2fc5ac12cd79d6e61ca181120dbf5
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
stable-5.2
David Pursehouse 6 years ago
parent
commit
fa61fd6f61
  1. 6
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DescribeCommandTest.java
  2. 13
      org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/FileRepositoryBuilderTest.java
  3. 22
      org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleWalkTest.java

6
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DescribeCommandTest.java

@ -42,13 +42,15 @@
*/ */
package org.eclipse.jgit.api; package org.eclipse.jgit.api;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -404,7 +406,7 @@ public class DescribeCommandTest extends RepositoryTestCase {
} }
private static void touch(File f, String contents) throws Exception { private static void touch(File f, String contents) throws Exception {
try (FileWriter w = new FileWriter(f)) { try (BufferedWriter w = Files.newBufferedWriter(f.toPath(), UTF_8)) {
w.write(contents); w.write(contents);
} }
} }

13
org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/FileRepositoryBuilderTest.java

@ -43,14 +43,16 @@
package org.eclipse.jgit.internal.storage.file; package org.eclipse.jgit.internal.storage.file;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase; import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
import org.eclipse.jgit.lib.ConfigConstants; import org.eclipse.jgit.lib.ConfigConstants;
@ -120,7 +122,8 @@ public class FileRepositoryBuilderTest extends LocalDiskRepositoryTestCase {
Repository repo1 = createWorkRepository(); Repository repo1 = createWorkRepository();
File dir = createTempDirectory("dir"); File dir = createTempDirectory("dir");
File dotGit = new File(dir, Constants.DOT_GIT); File dotGit = new File(dir, Constants.DOT_GIT);
try (FileWriter writer = new FileWriter(dotGit)) { try (BufferedWriter writer = Files.newBufferedWriter(dotGit.toPath(),
UTF_8)) {
writer.append("gitdir: " + repo1.getDirectory().getAbsolutePath()).close(); writer.append("gitdir: " + repo1.getDirectory().getAbsolutePath()).close();
FileRepositoryBuilder builder = new FileRepositoryBuilder(); FileRepositoryBuilder builder = new FileRepositoryBuilder();
@ -140,7 +143,8 @@ public class FileRepositoryBuilderTest extends LocalDiskRepositoryTestCase {
File dir = new File(repo1.getWorkTree(), "dir"); File dir = new File(repo1.getWorkTree(), "dir");
assertTrue(dir.mkdir()); assertTrue(dir.mkdir());
File dotGit = new File(dir, Constants.DOT_GIT); File dotGit = new File(dir, Constants.DOT_GIT);
try (FileWriter writer = new FileWriter(dotGit)) { try (BufferedWriter writer = Files.newBufferedWriter(dotGit.toPath(),
UTF_8)) {
writer.append("gitdir: ../" + Constants.DOT_GIT).close(); writer.append("gitdir: ../" + Constants.DOT_GIT).close();
FileRepositoryBuilder builder = new FileRepositoryBuilder(); FileRepositoryBuilder builder = new FileRepositoryBuilder();
@ -161,7 +165,8 @@ public class FileRepositoryBuilderTest extends LocalDiskRepositoryTestCase {
Repository repo1 = createWorkRepository(); Repository repo1 = createWorkRepository();
File dir = createTempDirectory("dir"); File dir = createTempDirectory("dir");
File dotGit = new File(dir, Constants.DOT_GIT); File dotGit = new File(dir, Constants.DOT_GIT);
try (FileWriter writer = new FileWriter(dotGit)) { try (BufferedWriter writer = Files.newBufferedWriter(dotGit.toPath(),
UTF_8)) {
writer.append( writer.append(
"gitdir: " + repo1.getDirectory().getAbsolutePath()).close(); "gitdir: " + repo1.getDirectory().getAbsolutePath()).close();
FileRepositoryBuilder builder = new FileRepositoryBuilder(); FileRepositoryBuilder builder = new FileRepositoryBuilder();

22
org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleWalkTest.java

@ -42,6 +42,7 @@
*/ */
package org.eclipse.jgit.submodule; package org.eclipse.jgit.submodule;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PATH; import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PATH;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_URL; import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_URL;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_SUBMODULE_SECTION; import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_SUBMODULE_SECTION;
@ -52,9 +53,10 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.Status; import org.eclipse.jgit.api.Status;
@ -155,10 +157,12 @@ public class SubmoduleWalkTest extends RepositoryTestCase {
if (!dotGit.getParentFile().exists()) if (!dotGit.getParentFile().exists())
dotGit.getParentFile().mkdirs(); dotGit.getParentFile().mkdirs();
File modulesGitDir = new File(db.getDirectory(), "modules" File modulesGitDir = new File(db.getDirectory(),
+ File.separatorChar + path); "modules" + File.separatorChar + path);
new FileWriter(dotGit).append( try (BufferedWriter fw = Files.newBufferedWriter(dotGit.toPath(),
"gitdir: " + modulesGitDir.getAbsolutePath()).close(); UTF_8)) {
fw.append("gitdir: " + modulesGitDir.getAbsolutePath());
}
FileRepositoryBuilder builder = new FileRepositoryBuilder(); FileRepositoryBuilder builder = new FileRepositoryBuilder();
builder.setWorkTree(new File(db.getWorkTree(), path)); builder.setWorkTree(new File(db.getWorkTree(), path));
builder.build().create(); builder.build().create();
@ -209,9 +213,11 @@ public class SubmoduleWalkTest extends RepositoryTestCase {
File modulesGitDir = new File(db.getDirectory(), "modules" File modulesGitDir = new File(db.getDirectory(), "modules"
+ File.separatorChar + path); + File.separatorChar + path);
new FileWriter(dotGit).append( try (BufferedWriter fw = Files.newBufferedWriter(dotGit.toPath(),
"gitdir: " + "../" + Constants.DOT_GIT + "/modules/" + path) UTF_8)) {
.close(); fw.append("gitdir: " + "../" + Constants.DOT_GIT + "/modules/"
+ path);
}
FileRepositoryBuilder builder = new FileRepositoryBuilder(); FileRepositoryBuilder builder = new FileRepositoryBuilder();
builder.setWorkTree(new File(db.getWorkTree(), path)); builder.setWorkTree(new File(db.getWorkTree(), path));
builder.build().create(); builder.build().create();

Loading…
Cancel
Save