Browse Source

Refactor FileSnapshotTest to use NIO APIs

- use Path instead of File
- create test directories, files and output stream using Files methods
- delete unused list "files"

Change-Id: I8c5c601eca9f613efb5618d33b262277df92a06a
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
stable-5.1
Matthias Sohn 5 years ago
parent
commit
93144f1438
  1. 77
      org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/FileSnapshotTest.java
  2. 105
      org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/FileBasedConfigTest.java

77
org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/FileSnapshotTest.java

@ -45,15 +45,14 @@ package org.eclipse.jgit.internal.storage.file;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.FileUtils;
@ -65,27 +64,24 @@ import org.junit.Test;
public class FileSnapshotTest {
private List<File> files = new ArrayList<>();
private File trash;
private Path trash;
@Before
public void setUp() throws Exception {
trash = File.createTempFile("tmp_", "");
trash.delete();
assertTrue("mkdir " + trash, trash.mkdir());
trash = Files.createTempDirectory("tmp_");
}
@Before
@After
public void tearDown() throws Exception {
FileUtils.delete(trash, FileUtils.RECURSIVE | FileUtils.SKIP_MISSING);
FileUtils.delete(trash.toFile(),
FileUtils.RECURSIVE | FileUtils.SKIP_MISSING);
}
private static void waitNextTick(File f) throws IOException {
private static void waitNextTick(Path f) throws IOException {
Instant initialLastModified = FS.DETECTED.lastModifiedInstant(f);
do {
FS.DETECTED.setLastModified(f.toPath(), Instant.now());
FS.DETECTED.setLastModified(f, Instant.now());
} while (FS.DETECTED.lastModifiedInstant(f)
.equals(initialLastModified));
}
@ -97,12 +93,12 @@ public class FileSnapshotTest {
*/
@Test
public void testActuallyIsModifiedTrivial() throws Exception {
File f1 = createFile("simple");
Path f1 = createFile("simple");
waitNextTick(f1);
FileSnapshot save = FileSnapshot.save(f1);
FileSnapshot save = FileSnapshot.save(f1.toFile());
append(f1, (byte) 'x');
waitNextTick(f1);
assertTrue(save.isModified(f1));
assertTrue(save.isModified(f1.toFile()));
}
/**
@ -115,11 +111,11 @@ public class FileSnapshotTest {
*/
@Test
public void testNewFileWithWait() throws Exception {
File f1 = createFile("newfile");
Path f1 = createFile("newfile");
waitNextTick(f1);
FileSnapshot save = FileSnapshot.save(f1);
FileSnapshot save = FileSnapshot.save(f1.toFile());
Thread.sleep(1500);
assertTrue(save.isModified(f1));
assertTrue(save.isModified(f1.toFile()));
}
/**
@ -129,9 +125,9 @@ public class FileSnapshotTest {
*/
@Test
public void testNewFileNoWait() throws Exception {
File f1 = createFile("newfile");
FileSnapshot save = FileSnapshot.save(f1);
assertTrue(save.isModified(f1));
Path f1 = createFile("newfile");
FileSnapshot save = FileSnapshot.save(f1.toFile());
assertTrue(save.isModified(f1.toFile()));
}
/**
@ -145,19 +141,19 @@ public class FileSnapshotTest {
@Test
public void testSimulatePackfileReplacement() throws Exception {
Assume.assumeFalse(SystemReader.getInstance().isWindows());
File f1 = createFile("file"); // inode y
File f2 = createFile("fool"); // Guarantees new inode x
Path f1 = createFile("file"); // inode y
Path f2 = createFile("fool"); // Guarantees new inode x
// wait on f2 since this method resets lastModified of the file
// and leaves lastModified of f1 untouched
waitNextTick(f2);
waitNextTick(f2);
FileTime timestamp = Files.getLastModifiedTime(f1.toPath());
FileSnapshot save = FileSnapshot.save(f1);
Files.move(f2.toPath(), f1.toPath(), // Now "file" is inode x
FileTime timestamp = Files.getLastModifiedTime(f1);
FileSnapshot save = FileSnapshot.save(f1.toFile());
Files.move(f2, f1, // Now "file" is inode x
StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.ATOMIC_MOVE);
Files.setLastModifiedTime(f1.toPath(), timestamp);
assertTrue(save.isModified(f1));
Files.setLastModifiedTime(f1, timestamp);
assertTrue(save.isModified(f1.toFile()));
assertTrue("unexpected change of fileKey", save.wasFileKeyChanged());
assertFalse("unexpected size change", save.wasSizeChanged());
assertFalse("unexpected lastModified change",
@ -174,12 +170,12 @@ public class FileSnapshotTest {
*/
@Test
public void testFileSizeChanged() throws Exception {
File f = createFile("file");
FileTime timestamp = Files.getLastModifiedTime(f.toPath());
FileSnapshot save = FileSnapshot.save(f);
Path f = createFile("file");
FileTime timestamp = Files.getLastModifiedTime(f);
FileSnapshot save = FileSnapshot.save(f.toFile());
append(f, (byte) 'x');
Files.setLastModifiedTime(f.toPath(), timestamp);
assertTrue(save.isModified(f));
Files.setLastModifiedTime(f, timestamp);
assertTrue(save.isModified(f.toFile()));
assertTrue(save.wasSizeChanged());
}
@ -194,15 +190,14 @@ public class FileSnapshotTest {
assertTrue(fs2.equals(fs1));
}
private File createFile(String string) throws IOException {
trash.mkdirs();
File f = File.createTempFile(string, "tdat", trash);
files.add(f);
return f;
private Path createFile(String string) throws IOException {
Files.createDirectories(trash);
return Files.createTempFile(trash, string, "tdat");
}
private static void append(File f, byte b) throws IOException {
try (FileOutputStream os = new FileOutputStream(f, true)) {
private static void append(Path f, byte b) throws IOException {
try (OutputStream os = Files.newOutputStream(f,
StandardOpenOption.APPEND)) {
os.write(b);
}
}

105
org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/FileBasedConfigTest.java

@ -46,12 +46,13 @@ import static java.nio.charset.StandardCharsets.UTF_8;
import static org.eclipse.jgit.util.FileUtils.pathToString;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.util.FS;
@ -77,42 +78,43 @@ public class FileBasedConfigTest {
private static final String CONTENT2 = "[" + USER + "]\n\t" + NAME + " = "
+ BOB + "\n";
private File trash;
private Path trash;
@Before
public void setUp() throws Exception {
trash = File.createTempFile("tmp_", "");
trash.delete();
assertTrue("mkdir " + trash, trash.mkdir());
trash = Files.createTempDirectory("tmp_");
}
@After
public void tearDown() throws Exception {
FileUtils.delete(trash, FileUtils.RECURSIVE | FileUtils.SKIP_MISSING);
FileUtils.delete(trash.toFile(),
FileUtils.RECURSIVE | FileUtils.SKIP_MISSING);
}
@Test
public void testSystemEncoding() throws IOException, ConfigInvalidException {
final File file = createFile(CONTENT1.getBytes());
final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
final Path file = createFile(CONTENT1.getBytes());
final FileBasedConfig config = new FileBasedConfig(file.toFile(),
FS.DETECTED);
config.load();
assertEquals(ALICE, config.getString(USER, null, NAME));
config.setString(USER, null, NAME, BOB);
config.save();
assertArrayEquals(CONTENT2.getBytes(), IO.readFully(file));
assertArrayEquals(CONTENT2.getBytes(), IO.readFully(file.toFile()));
}
@Test
public void testUTF8withoutBOM() throws IOException, ConfigInvalidException {
final File file = createFile(CONTENT1.getBytes(UTF_8));
final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
final Path file = createFile(CONTENT1.getBytes(UTF_8));
final FileBasedConfig config = new FileBasedConfig(file.toFile(),
FS.DETECTED);
config.load();
assertEquals(ALICE, config.getString(USER, null, NAME));
config.setString(USER, null, NAME, BOB);
config.save();
assertArrayEquals(CONTENT2.getBytes(), IO.readFully(file));
assertArrayEquals(CONTENT2.getBytes(), IO.readFully(file.toFile()));
}
@Test
@ -123,8 +125,9 @@ public class FileBasedConfigTest {
bos1.write(0xBF);
bos1.write(CONTENT1.getBytes(UTF_8));
final File file = createFile(bos1.toByteArray());
final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
final Path file = createFile(bos1.toByteArray());
final FileBasedConfig config = new FileBasedConfig(file.toFile(),
FS.DETECTED);
config.load();
assertEquals(ALICE, config.getString(USER, null, NAME));
@ -136,7 +139,7 @@ public class FileBasedConfigTest {
bos2.write(0xBB);
bos2.write(0xBF);
bos2.write(CONTENT2.getBytes(UTF_8));
assertArrayEquals(bos2.toByteArray(), IO.readFully(file));
assertArrayEquals(bos2.toByteArray(), IO.readFully(file.toFile()));
}
@Test
@ -145,8 +148,9 @@ public class FileBasedConfigTest {
bos1.write(" \n\t".getBytes());
bos1.write(CONTENT1.getBytes());
final File file = createFile(bos1.toByteArray());
final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
final Path file = createFile(bos1.toByteArray());
final FileBasedConfig config = new FileBasedConfig(file.toFile(),
FS.DETECTED);
config.load();
assertEquals(ALICE, config.getString(USER, null, NAME));
@ -156,19 +160,20 @@ public class FileBasedConfigTest {
final ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
bos2.write(" \n\t".getBytes());
bos2.write(CONTENT2.getBytes());
assertArrayEquals(bos2.toByteArray(), IO.readFully(file));
assertArrayEquals(bos2.toByteArray(), IO.readFully(file.toFile()));
}
@Test
public void testIncludeAbsolute()
throws IOException, ConfigInvalidException {
final File includedFile = createFile(CONTENT1.getBytes());
final Path includedFile = createFile(CONTENT1.getBytes());
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write("[include]\npath=".getBytes());
bos.write(pathToString(includedFile).getBytes());
bos.write(pathToString(includedFile.toFile()).getBytes());
final File file = createFile(bos.toByteArray());
final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
final Path file = createFile(bos.toByteArray());
final FileBasedConfig config = new FileBasedConfig(file.toFile(),
FS.DETECTED);
config.load();
assertEquals(ALICE, config.getString(USER, null, NAME));
}
@ -176,13 +181,14 @@ public class FileBasedConfigTest {
@Test
public void testIncludeRelativeDot()
throws IOException, ConfigInvalidException {
final File includedFile = createFile(CONTENT1.getBytes(), "dir1");
final Path includedFile = createFile(CONTENT1.getBytes(), "dir1");
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write("[include]\npath=".getBytes());
bos.write(("./" + includedFile.getName()).getBytes());
bos.write(("./" + includedFile.getFileName()).getBytes());
final File file = createFile(bos.toByteArray(), "dir1");
final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
final Path file = createFile(bos.toByteArray(), "dir1");
final FileBasedConfig config = new FileBasedConfig(file.toFile(),
FS.DETECTED);
config.load();
assertEquals(ALICE, config.getString(USER, null, NAME));
}
@ -190,14 +196,15 @@ public class FileBasedConfigTest {
@Test
public void testIncludeRelativeDotDot()
throws IOException, ConfigInvalidException {
final File includedFile = createFile(CONTENT1.getBytes(), "dir1");
final Path includedFile = createFile(CONTENT1.getBytes(), "dir1");
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write("[include]\npath=".getBytes());
bos.write(("../" + includedFile.getParentFile().getName() + "/"
+ includedFile.getName()).getBytes());
bos.write(("../" + includedFile.getParent().getFileName() + "/"
+ includedFile.getFileName()).getBytes());
final File file = createFile(bos.toByteArray(), "dir2");
final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
final Path file = createFile(bos.toByteArray(), "dir2");
final FileBasedConfig config = new FileBasedConfig(file.toFile(),
FS.DETECTED);
config.load();
assertEquals(ALICE, config.getString(USER, null, NAME));
}
@ -205,13 +212,14 @@ public class FileBasedConfigTest {
@Test
public void testIncludeRelativeDotDotNotFound()
throws IOException, ConfigInvalidException {
final File includedFile = createFile(CONTENT1.getBytes());
final Path includedFile = createFile(CONTENT1.getBytes());
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write("[include]\npath=".getBytes());
bos.write(("../" + includedFile.getName()).getBytes());
bos.write(("../" + includedFile.getFileName()).getBytes());
final File file = createFile(bos.toByteArray());
final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
final Path file = createFile(bos.toByteArray());
final FileBasedConfig config = new FileBasedConfig(file.toFile(),
FS.DETECTED);
config.load();
assertEquals(null, config.getString(USER, null, NAME));
}
@ -219,30 +227,31 @@ public class FileBasedConfigTest {
@Test
public void testIncludeWithTilde()
throws IOException, ConfigInvalidException {
final File includedFile = createFile(CONTENT1.getBytes(), "home");
final Path includedFile = createFile(CONTENT1.getBytes(), "home");
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write("[include]\npath=".getBytes());
bos.write(("~/" + includedFile.getName()).getBytes());
bos.write(("~/" + includedFile.getFileName()).getBytes());
final File file = createFile(bos.toByteArray(), "repo");
final Path file = createFile(bos.toByteArray(), "repo");
final FS fs = FS.DETECTED.newInstance();
fs.setUserHome(includedFile.getParentFile());
fs.setUserHome(includedFile.getParent().toFile());
final FileBasedConfig config = new FileBasedConfig(file, fs);
final FileBasedConfig config = new FileBasedConfig(file.toFile(), fs);
config.load();
assertEquals(ALICE, config.getString(USER, null, NAME));
}
private File createFile(byte[] content) throws IOException {
private Path createFile(byte[] content) throws IOException {
return createFile(content, null);
}
private File createFile(byte[] content, String subdir) throws IOException {
File dir = subdir != null ? new File(trash, subdir) : trash;
dir.mkdirs();
private Path createFile(byte[] content, String subdir) throws IOException {
Path dir = subdir != null ? trash.resolve(subdir) : trash;
Files.createDirectories(dir);
File f = File.createTempFile(getClass().getName(), null, dir);
try (FileOutputStream os = new FileOutputStream(f, true)) {
Path f = Files.createTempFile(dir, getClass().getName(), null);
try (OutputStream os = Files.newOutputStream(f,
StandardOpenOption.APPEND)) {
os.write(content);
}
return f;

Loading…
Cancel
Save