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.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.FileTime; import java.nio.file.attribute.FileTime;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jgit.util.FS; import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.FileUtils; import org.eclipse.jgit.util.FileUtils;
@ -65,27 +64,24 @@ import org.junit.Test;
public class FileSnapshotTest { public class FileSnapshotTest {
private List<File> files = new ArrayList<>(); private Path trash;
private File trash;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
trash = File.createTempFile("tmp_", ""); trash = Files.createTempDirectory("tmp_");
trash.delete();
assertTrue("mkdir " + trash, trash.mkdir());
} }
@Before @Before
@After @After
public void tearDown() throws Exception { 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); Instant initialLastModified = FS.DETECTED.lastModifiedInstant(f);
do { do {
FS.DETECTED.setLastModified(f.toPath(), Instant.now()); FS.DETECTED.setLastModified(f, Instant.now());
} while (FS.DETECTED.lastModifiedInstant(f) } while (FS.DETECTED.lastModifiedInstant(f)
.equals(initialLastModified)); .equals(initialLastModified));
} }
@ -97,12 +93,12 @@ public class FileSnapshotTest {
*/ */
@Test @Test
public void testActuallyIsModifiedTrivial() throws Exception { public void testActuallyIsModifiedTrivial() throws Exception {
File f1 = createFile("simple"); Path f1 = createFile("simple");
waitNextTick(f1); waitNextTick(f1);
FileSnapshot save = FileSnapshot.save(f1); FileSnapshot save = FileSnapshot.save(f1.toFile());
append(f1, (byte) 'x'); append(f1, (byte) 'x');
waitNextTick(f1); waitNextTick(f1);
assertTrue(save.isModified(f1)); assertTrue(save.isModified(f1.toFile()));
} }
/** /**
@ -115,11 +111,11 @@ public class FileSnapshotTest {
*/ */
@Test @Test
public void testNewFileWithWait() throws Exception { public void testNewFileWithWait() throws Exception {
File f1 = createFile("newfile"); Path f1 = createFile("newfile");
waitNextTick(f1); waitNextTick(f1);
FileSnapshot save = FileSnapshot.save(f1); FileSnapshot save = FileSnapshot.save(f1.toFile());
Thread.sleep(1500); Thread.sleep(1500);
assertTrue(save.isModified(f1)); assertTrue(save.isModified(f1.toFile()));
} }
/** /**
@ -129,9 +125,9 @@ public class FileSnapshotTest {
*/ */
@Test @Test
public void testNewFileNoWait() throws Exception { public void testNewFileNoWait() throws Exception {
File f1 = createFile("newfile"); Path f1 = createFile("newfile");
FileSnapshot save = FileSnapshot.save(f1); FileSnapshot save = FileSnapshot.save(f1.toFile());
assertTrue(save.isModified(f1)); assertTrue(save.isModified(f1.toFile()));
} }
/** /**
@ -145,19 +141,19 @@ public class FileSnapshotTest {
@Test @Test
public void testSimulatePackfileReplacement() throws Exception { public void testSimulatePackfileReplacement() throws Exception {
Assume.assumeFalse(SystemReader.getInstance().isWindows()); Assume.assumeFalse(SystemReader.getInstance().isWindows());
File f1 = createFile("file"); // inode y Path f1 = createFile("file"); // inode y
File f2 = createFile("fool"); // Guarantees new inode x Path f2 = createFile("fool"); // Guarantees new inode x
// wait on f2 since this method resets lastModified of the file // wait on f2 since this method resets lastModified of the file
// and leaves lastModified of f1 untouched // and leaves lastModified of f1 untouched
waitNextTick(f2); waitNextTick(f2);
waitNextTick(f2); waitNextTick(f2);
FileTime timestamp = Files.getLastModifiedTime(f1.toPath()); FileTime timestamp = Files.getLastModifiedTime(f1);
FileSnapshot save = FileSnapshot.save(f1); FileSnapshot save = FileSnapshot.save(f1.toFile());
Files.move(f2.toPath(), f1.toPath(), // Now "file" is inode x Files.move(f2, f1, // Now "file" is inode x
StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.ATOMIC_MOVE); StandardCopyOption.ATOMIC_MOVE);
Files.setLastModifiedTime(f1.toPath(), timestamp); Files.setLastModifiedTime(f1, timestamp);
assertTrue(save.isModified(f1)); assertTrue(save.isModified(f1.toFile()));
assertTrue("unexpected change of fileKey", save.wasFileKeyChanged()); assertTrue("unexpected change of fileKey", save.wasFileKeyChanged());
assertFalse("unexpected size change", save.wasSizeChanged()); assertFalse("unexpected size change", save.wasSizeChanged());
assertFalse("unexpected lastModified change", assertFalse("unexpected lastModified change",
@ -174,12 +170,12 @@ public class FileSnapshotTest {
*/ */
@Test @Test
public void testFileSizeChanged() throws Exception { public void testFileSizeChanged() throws Exception {
File f = createFile("file"); Path f = createFile("file");
FileTime timestamp = Files.getLastModifiedTime(f.toPath()); FileTime timestamp = Files.getLastModifiedTime(f);
FileSnapshot save = FileSnapshot.save(f); FileSnapshot save = FileSnapshot.save(f.toFile());
append(f, (byte) 'x'); append(f, (byte) 'x');
Files.setLastModifiedTime(f.toPath(), timestamp); Files.setLastModifiedTime(f, timestamp);
assertTrue(save.isModified(f)); assertTrue(save.isModified(f.toFile()));
assertTrue(save.wasSizeChanged()); assertTrue(save.wasSizeChanged());
} }
@ -194,15 +190,14 @@ public class FileSnapshotTest {
assertTrue(fs2.equals(fs1)); assertTrue(fs2.equals(fs1));
} }
private File createFile(String string) throws IOException { private Path createFile(String string) throws IOException {
trash.mkdirs(); Files.createDirectories(trash);
File f = File.createTempFile(string, "tdat", trash); return Files.createTempFile(trash, string, "tdat");
files.add(f);
return f;
} }
private static void append(File f, byte b) throws IOException { private static void append(Path f, byte b) throws IOException {
try (FileOutputStream os = new FileOutputStream(f, true)) { try (OutputStream os = Files.newOutputStream(f,
StandardOpenOption.APPEND)) {
os.write(b); 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.eclipse.jgit.util.FileUtils.pathToString;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; 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.errors.ConfigInvalidException;
import org.eclipse.jgit.util.FS; import org.eclipse.jgit.util.FS;
@ -77,42 +78,43 @@ public class FileBasedConfigTest {
private static final String CONTENT2 = "[" + USER + "]\n\t" + NAME + " = " private static final String CONTENT2 = "[" + USER + "]\n\t" + NAME + " = "
+ BOB + "\n"; + BOB + "\n";
private File trash; private Path trash;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
trash = File.createTempFile("tmp_", ""); trash = Files.createTempDirectory("tmp_");
trash.delete();
assertTrue("mkdir " + trash, trash.mkdir());
} }
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
FileUtils.delete(trash, FileUtils.RECURSIVE | FileUtils.SKIP_MISSING); FileUtils.delete(trash.toFile(),
FileUtils.RECURSIVE | FileUtils.SKIP_MISSING);
} }
@Test @Test
public void testSystemEncoding() throws IOException, ConfigInvalidException { public void testSystemEncoding() throws IOException, ConfigInvalidException {
final File file = createFile(CONTENT1.getBytes()); final Path file = createFile(CONTENT1.getBytes());
final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED); final FileBasedConfig config = new FileBasedConfig(file.toFile(),
FS.DETECTED);
config.load(); config.load();
assertEquals(ALICE, config.getString(USER, null, NAME)); assertEquals(ALICE, config.getString(USER, null, NAME));
config.setString(USER, null, NAME, BOB); config.setString(USER, null, NAME, BOB);
config.save(); config.save();
assertArrayEquals(CONTENT2.getBytes(), IO.readFully(file)); assertArrayEquals(CONTENT2.getBytes(), IO.readFully(file.toFile()));
} }
@Test @Test
public void testUTF8withoutBOM() throws IOException, ConfigInvalidException { public void testUTF8withoutBOM() throws IOException, ConfigInvalidException {
final File file = createFile(CONTENT1.getBytes(UTF_8)); final Path file = createFile(CONTENT1.getBytes(UTF_8));
final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED); final FileBasedConfig config = new FileBasedConfig(file.toFile(),
FS.DETECTED);
config.load(); config.load();
assertEquals(ALICE, config.getString(USER, null, NAME)); assertEquals(ALICE, config.getString(USER, null, NAME));
config.setString(USER, null, NAME, BOB); config.setString(USER, null, NAME, BOB);
config.save(); config.save();
assertArrayEquals(CONTENT2.getBytes(), IO.readFully(file)); assertArrayEquals(CONTENT2.getBytes(), IO.readFully(file.toFile()));
} }
@Test @Test
@ -123,8 +125,9 @@ public class FileBasedConfigTest {
bos1.write(0xBF); bos1.write(0xBF);
bos1.write(CONTENT1.getBytes(UTF_8)); bos1.write(CONTENT1.getBytes(UTF_8));
final File file = createFile(bos1.toByteArray()); final Path file = createFile(bos1.toByteArray());
final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED); final FileBasedConfig config = new FileBasedConfig(file.toFile(),
FS.DETECTED);
config.load(); config.load();
assertEquals(ALICE, config.getString(USER, null, NAME)); assertEquals(ALICE, config.getString(USER, null, NAME));
@ -136,7 +139,7 @@ public class FileBasedConfigTest {
bos2.write(0xBB); bos2.write(0xBB);
bos2.write(0xBF); bos2.write(0xBF);
bos2.write(CONTENT2.getBytes(UTF_8)); bos2.write(CONTENT2.getBytes(UTF_8));
assertArrayEquals(bos2.toByteArray(), IO.readFully(file)); assertArrayEquals(bos2.toByteArray(), IO.readFully(file.toFile()));
} }
@Test @Test
@ -145,8 +148,9 @@ public class FileBasedConfigTest {
bos1.write(" \n\t".getBytes()); bos1.write(" \n\t".getBytes());
bos1.write(CONTENT1.getBytes()); bos1.write(CONTENT1.getBytes());
final File file = createFile(bos1.toByteArray()); final Path file = createFile(bos1.toByteArray());
final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED); final FileBasedConfig config = new FileBasedConfig(file.toFile(),
FS.DETECTED);
config.load(); config.load();
assertEquals(ALICE, config.getString(USER, null, NAME)); assertEquals(ALICE, config.getString(USER, null, NAME));
@ -156,19 +160,20 @@ public class FileBasedConfigTest {
final ByteArrayOutputStream bos2 = new ByteArrayOutputStream(); final ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
bos2.write(" \n\t".getBytes()); bos2.write(" \n\t".getBytes());
bos2.write(CONTENT2.getBytes()); bos2.write(CONTENT2.getBytes());
assertArrayEquals(bos2.toByteArray(), IO.readFully(file)); assertArrayEquals(bos2.toByteArray(), IO.readFully(file.toFile()));
} }
@Test @Test
public void testIncludeAbsolute() public void testIncludeAbsolute()
throws IOException, ConfigInvalidException { throws IOException, ConfigInvalidException {
final File includedFile = createFile(CONTENT1.getBytes()); final Path includedFile = createFile(CONTENT1.getBytes());
final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write("[include]\npath=".getBytes()); bos.write("[include]\npath=".getBytes());
bos.write(pathToString(includedFile).getBytes()); bos.write(pathToString(includedFile.toFile()).getBytes());
final File file = createFile(bos.toByteArray()); final Path file = createFile(bos.toByteArray());
final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED); final FileBasedConfig config = new FileBasedConfig(file.toFile(),
FS.DETECTED);
config.load(); config.load();
assertEquals(ALICE, config.getString(USER, null, NAME)); assertEquals(ALICE, config.getString(USER, null, NAME));
} }
@ -176,13 +181,14 @@ public class FileBasedConfigTest {
@Test @Test
public void testIncludeRelativeDot() public void testIncludeRelativeDot()
throws IOException, ConfigInvalidException { throws IOException, ConfigInvalidException {
final File includedFile = createFile(CONTENT1.getBytes(), "dir1"); final Path includedFile = createFile(CONTENT1.getBytes(), "dir1");
final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write("[include]\npath=".getBytes()); bos.write("[include]\npath=".getBytes());
bos.write(("./" + includedFile.getName()).getBytes()); bos.write(("./" + includedFile.getFileName()).getBytes());
final File file = createFile(bos.toByteArray(), "dir1"); final Path file = createFile(bos.toByteArray(), "dir1");
final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED); final FileBasedConfig config = new FileBasedConfig(file.toFile(),
FS.DETECTED);
config.load(); config.load();
assertEquals(ALICE, config.getString(USER, null, NAME)); assertEquals(ALICE, config.getString(USER, null, NAME));
} }
@ -190,14 +196,15 @@ public class FileBasedConfigTest {
@Test @Test
public void testIncludeRelativeDotDot() public void testIncludeRelativeDotDot()
throws IOException, ConfigInvalidException { throws IOException, ConfigInvalidException {
final File includedFile = createFile(CONTENT1.getBytes(), "dir1"); final Path includedFile = createFile(CONTENT1.getBytes(), "dir1");
final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write("[include]\npath=".getBytes()); bos.write("[include]\npath=".getBytes());
bos.write(("../" + includedFile.getParentFile().getName() + "/" bos.write(("../" + includedFile.getParent().getFileName() + "/"
+ includedFile.getName()).getBytes()); + includedFile.getFileName()).getBytes());
final File file = createFile(bos.toByteArray(), "dir2"); final Path file = createFile(bos.toByteArray(), "dir2");
final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED); final FileBasedConfig config = new FileBasedConfig(file.toFile(),
FS.DETECTED);
config.load(); config.load();
assertEquals(ALICE, config.getString(USER, null, NAME)); assertEquals(ALICE, config.getString(USER, null, NAME));
} }
@ -205,13 +212,14 @@ public class FileBasedConfigTest {
@Test @Test
public void testIncludeRelativeDotDotNotFound() public void testIncludeRelativeDotDotNotFound()
throws IOException, ConfigInvalidException { throws IOException, ConfigInvalidException {
final File includedFile = createFile(CONTENT1.getBytes()); final Path includedFile = createFile(CONTENT1.getBytes());
final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write("[include]\npath=".getBytes()); bos.write("[include]\npath=".getBytes());
bos.write(("../" + includedFile.getName()).getBytes()); bos.write(("../" + includedFile.getFileName()).getBytes());
final File file = createFile(bos.toByteArray()); final Path file = createFile(bos.toByteArray());
final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED); final FileBasedConfig config = new FileBasedConfig(file.toFile(),
FS.DETECTED);
config.load(); config.load();
assertEquals(null, config.getString(USER, null, NAME)); assertEquals(null, config.getString(USER, null, NAME));
} }
@ -219,30 +227,31 @@ public class FileBasedConfigTest {
@Test @Test
public void testIncludeWithTilde() public void testIncludeWithTilde()
throws IOException, ConfigInvalidException { throws IOException, ConfigInvalidException {
final File includedFile = createFile(CONTENT1.getBytes(), "home"); final Path includedFile = createFile(CONTENT1.getBytes(), "home");
final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write("[include]\npath=".getBytes()); 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(); 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(); config.load();
assertEquals(ALICE, config.getString(USER, null, NAME)); 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); return createFile(content, null);
} }
private File createFile(byte[] content, String subdir) throws IOException { private Path createFile(byte[] content, String subdir) throws IOException {
File dir = subdir != null ? new File(trash, subdir) : trash; Path dir = subdir != null ? trash.resolve(subdir) : trash;
dir.mkdirs(); Files.createDirectories(dir);
File f = File.createTempFile(getClass().getName(), null, dir); Path f = Files.createTempFile(dir, getClass().getName(), null);
try (FileOutputStream os = new FileOutputStream(f, true)) { try (OutputStream os = Files.newOutputStream(f,
StandardOpenOption.APPEND)) {
os.write(content); os.write(content);
} }
return f; return f;

Loading…
Cancel
Save