public void testCreateParentDirs_multipleParentsNeeded() throws IOException { File file = file(getTempDir(), "grandparent", "parent", "nonexistent.file"); File parent = file.getParentFile(); File grandparent = parent.getParentFile(); assertFalse(grandparent.exists()); Files.createParentDirs(file); assertTrue(parent.exists()); }
public void testPredicates() throws IOException { File asciiFile = getTestFile("ascii.txt"); File dir = asciiFile.getParentFile(); assertTrue(Files.isDirectory().apply(dir)); assertFalse(Files.isFile().apply(dir)); assertFalse(Files.isDirectory().apply(asciiFile)); assertTrue(Files.isFile().apply(asciiFile)); }
public void testCreateParentDirs_oneParentNeeded() throws IOException { File file = file(getTempDir(), "parent", "nonexistent.file"); File parent = file.getParentFile(); assertFalse(parent.exists()); try { Files.createParentDirs(file); assertTrue(parent.exists()); } finally { assertTrue(parent.delete()); } }
private void moveHelper(boolean success, File from, File to) throws IOException { try { Files.move(from, to); if (success) { assertFalse(from.exists()); assertTrue(to.exists()); } else { fail("expected exception"); } } catch (IOException possiblyExpected) { if (success) { throw possiblyExpected; } } }
public void testTouch() throws IOException { File temp = createTempFile(); assertTrue(temp.exists()); assertTrue(temp.delete()); assertFalse(temp.exists()); Files.touch(temp); assertTrue(temp.exists()); Files.touch(temp); assertTrue(temp.exists()); try { Files.touch( new File(temp.getPath()) { @Override public boolean setLastModified(long t) { return false; } private static final long serialVersionUID = 0; }); fail("expected exception"); } catch (IOException expected) { } }
public void testMap_readWrite_creates() throws IOException { // Test data int size = 1024; byte[] expectedBytes = newPreFilledByteArray(1024); // Setup File file = createTempFile(); boolean deleted = file.delete(); assertTrue(deleted); assertFalse(file.exists()); // Test MappedByteBuffer map = Files.map(file, MapMode.READ_WRITE, size); map.put(expectedBytes); // Verify assertTrue(file.exists()); assertTrue(file.isFile()); assertEquals(size, file.length()); byte[] actualBytes = Files.toByteArray(file); assertTrue(Arrays.equals(expectedBytes, actualBytes)); }
public void testEqual() throws IOException { File asciiFile = getTestFile("ascii.txt"); File i18nFile = getTestFile("i18n.txt"); assertFalse(Files.equal(asciiFile, i18nFile)); assertTrue(Files.equal(asciiFile, asciiFile)); File temp = createTempFile(); Files.copy(asciiFile, temp); assertTrue(Files.equal(asciiFile, temp)); Files.copy(i18nFile, temp); assertTrue(Files.equal(i18nFile, temp)); Files.copy(asciiFile, temp); RandomAccessFile rf = new RandomAccessFile(temp, "rw"); rf.writeByte(0); rf.close(); assertEquals(asciiFile.length(), temp.length()); assertFalse(Files.equal(asciiFile, temp)); assertTrue(Files.asByteSource(asciiFile).contentEquals(Files.asByteSource(asciiFile))); // 0-length files have special treatment (/proc, etc.) assertTrue(Files.equal(asciiFile, new BadLengthFile(asciiFile, 0))); }
public void testCreateParentDirs_multipleParentsNeeded() throws IOException { File file = file(getTempDir(), "grandparent", "parent", "nonexistent.file"); File parent = file.getParentFile(); File grandparent = parent.getParentFile(); assertFalse(grandparent.exists()); Files.createParentDirs(file); assertTrue(parent.exists()); }
public void testPredicates() throws IOException { File asciiFile = getTestFile("ascii.txt"); File dir = asciiFile.getParentFile(); assertTrue(Files.isDirectory().apply(dir)); assertFalse(Files.isFile().apply(dir)); assertFalse(Files.isDirectory().apply(asciiFile)); assertTrue(Files.isFile().apply(asciiFile)); }
public void testCreateParentDirs_oneParentNeeded() throws IOException { File file = file(getTempDir(), "parent", "nonexistent.file"); File parent = file.getParentFile(); assertFalse(parent.exists()); try { Files.createParentDirs(file); assertTrue(parent.exists()); } finally { assertTrue(parent.delete()); } }
private void moveHelper(boolean success, File from, File to) throws IOException { try { Files.move(from, to); if (success) { assertFalse(from.exists()); assertTrue(to.exists()); } else { fail("expected exception"); } } catch (IOException possiblyExpected) { if (success) { throw possiblyExpected; } } }
public void testTouch() throws IOException { File temp = createTempFile(); assertTrue(temp.exists()); assertTrue(temp.delete()); assertFalse(temp.exists()); Files.touch(temp); assertTrue(temp.exists()); Files.touch(temp); assertTrue(temp.exists()); try { Files.touch(new File(temp.getPath()) { @Override public boolean setLastModified(long t) { return false; } private static final long serialVersionUID = 0; }); fail("expected exception"); } catch (IOException expected) { } }
public void testMap_readWrite_creates() throws IOException { // Test data int size = 1024; byte[] expectedBytes = newPreFilledByteArray(1024); // Setup File file = createTempFile(); boolean deleted = file.delete(); assertTrue(deleted); assertFalse(file.exists()); // Test MappedByteBuffer map = Files.map(file, MapMode.READ_WRITE, size); map.put(expectedBytes); // Verify assertTrue(file.exists()); assertTrue(file.isFile()); assertEquals(size, file.length()); byte[] actualBytes = Files.toByteArray(file); assertTrue(Arrays.equals(expectedBytes, actualBytes)); }
public void testEqual() throws IOException { File asciiFile = getTestFile("ascii.txt"); File i18nFile = getTestFile("i18n.txt"); assertFalse(Files.equal(asciiFile, i18nFile)); assertTrue(Files.equal(asciiFile, asciiFile)); File temp = createTempFile(); Files.copy(asciiFile, temp); assertTrue(Files.equal(asciiFile, temp)); Files.copy(i18nFile, temp); assertTrue(Files.equal(i18nFile, temp)); Files.copy(asciiFile, temp); RandomAccessFile rf = new RandomAccessFile(temp, "rw"); rf.writeByte(0); rf.close(); assertEquals(asciiFile.length(), temp.length()); assertFalse(Files.equal(asciiFile, temp)); assertTrue(Files.asByteSource(asciiFile) .contentEquals(Files.asByteSource(asciiFile))); // 0-length files have special treatment (/proc, etc.) assertTrue(Files.equal(asciiFile, new BadLengthFile(asciiFile, 0))); }