public void testCopyToOutputStream() throws IOException { File i18nFile = getTestFile("i18n.txt"); ByteArrayOutputStream out = new ByteArrayOutputStream(); Files.copy(i18nFile, out); assertEquals(I18N, out.toString("UTF-8")); }
public void testCopyToAppendable() throws IOException { File i18nFile = getTestFile("i18n.txt"); StringBuilder sb = new StringBuilder(); Files.copy(i18nFile, Charsets.UTF_8, sb); assertEquals(I18N, sb.toString()); }
public void testToString() throws IOException { File asciiFile = getTestFile("ascii.txt"); File i18nFile = getTestFile("i18n.txt"); assertEquals(ASCII, Files.toString(asciiFile, Charsets.US_ASCII)); assertEquals(I18N, Files.toString(i18nFile, Charsets.UTF_8)); assertThat(Files.toString(i18nFile, Charsets.US_ASCII)).isNotEqualTo(I18N); }
public void testCopyFile() throws IOException { File i18nFile = getTestFile("i18n.txt"); File temp = createTempFile(); Files.copy(i18nFile, temp); assertEquals(I18N, Files.toString(temp, Charsets.UTF_8)); }
public void testHash() throws IOException { File asciiFile = getTestFile("ascii.txt"); File i18nFile = getTestFile("i18n.txt"); String init = "d41d8cd98f00b204e9800998ecf8427e"; assertEquals(init, Hashing.md5().newHasher().hash().toString()); String asciiHash = "e5df5a39f2b8cb71b24e1d8038f93131"; assertEquals(asciiHash, Files.hash(asciiFile, Hashing.md5()).toString()); String i18nHash = "7fa826962ce2079c8334cd4ebf33aea4"; assertEquals(i18nHash, Files.hash(i18nFile, Hashing.md5()).toString()); }
public void testToByteArray() throws IOException { File asciiFile = getTestFile("ascii.txt"); File i18nFile = getTestFile("i18n.txt"); assertTrue(Arrays.equals(ASCII.getBytes(Charsets.US_ASCII), Files.toByteArray(asciiFile))); assertTrue(Arrays.equals(I18N.getBytes(Charsets.UTF_8), Files.toByteArray(i18nFile))); assertTrue(Arrays.equals(I18N.getBytes(Charsets.UTF_8), Files.asByteSource(i18nFile).read())); }
public void testReadBytes_returnFalse() throws IOException { ByteProcessor<byte[]> processor = new ByteProcessor<byte[]>() { private final ByteArrayOutputStream out = new ByteArrayOutputStream(); @Override public boolean processBytes(byte[] buffer, int offset, int length) throws IOException { if (length > 0) { out.write(buffer, offset, 1); return false; } else { return true; } } @Override public byte[] getResult() { return out.toByteArray(); } }; File asciiFile = getTestFile("ascii.txt"); byte[] result = Files.readBytes(asciiFile, processor); assertEquals(1, result.length); }
public void testCreateParentDirs_nonDirectoryParentExists() throws IOException { File parent = getTestFile("ascii.txt"); assertTrue(parent.isFile()); File file = file(parent, "foo"); try { Files.createParentDirs(file); fail(); } catch (IOException expected) { } }
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 testMove() throws IOException { File i18nFile = getTestFile("i18n.txt"); File temp1 = createTempFile(); File temp2 = createTempFile(); Files.copy(i18nFile, temp1); moveHelper(true, temp1, temp2); assertTrue(Files.equal(temp2, i18nFile)); }
public void testReadBytes() throws IOException { ByteProcessor<byte[]> processor = new ByteProcessor<byte[]>() { private final ByteArrayOutputStream out = new ByteArrayOutputStream(); @Override public boolean processBytes(byte[] buffer, int offset, int length) throws IOException { if (length >= 0) { out.write(buffer, offset, length); } return true; } @Override public byte[] getResult() { return out.toByteArray(); } }; File asciiFile = getTestFile("ascii.txt"); byte[] result = Files.readBytes(asciiFile, processor); assertEquals(Bytes.asList(Files.toByteArray(asciiFile)), Bytes.asList(result)); }
public void testRoundTripSources() throws Exception { File asciiFile = getTestFile("ascii.txt"); ByteSource byteSource = Files.asByteSource(asciiFile); assertSame(byteSource, byteSource.asCharSource(Charsets.UTF_8).asByteSource(Charsets.UTF_8)); }
public void testMoveViaCopy() throws IOException { File i18nFile = getTestFile("i18n.txt"); File temp1 = createTempFile(); File temp2 = createTempFile(); Files.copy(i18nFile, temp1); moveHelper(true, new UnmovableFile(temp1, false, true), temp2); assertTrue(Files.equal(temp2, i18nFile)); }
public void testNewReader() throws IOException { File asciiFile = getTestFile("ascii.txt"); try { Files.newReader(asciiFile, null); fail("expected exception"); } catch (NullPointerException expected) { } try { Files.newReader(null, Charsets.UTF_8); fail("expected exception"); } catch (NullPointerException expected) { } BufferedReader r = Files.newReader(asciiFile, Charsets.US_ASCII); try { assertEquals(ASCII, r.readLine()); } finally { r.close(); } }
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 testNewWriter() throws IOException { File temp = createTempFile(); try { Files.newWriter(temp, null); fail("expected exception"); } catch (NullPointerException expected) { } try { Files.newWriter(null, Charsets.UTF_8); fail("expected exception"); } catch (NullPointerException expected) { } BufferedWriter w = Files.newWriter(temp, Charsets.UTF_8); try { w.write(I18N); } finally { w.close(); } File i18nFile = getTestFile("i18n.txt"); assertTrue(Files.equal(i18nFile, temp)); }
public void testMoveFailures() throws IOException { File temp1 = createTempFile(); File temp2 = createTempFile(); moveHelper(false, new UnmovableFile(temp1, false, false), temp2); moveHelper( false, new UnmovableFile(temp1, false, false), new UnmovableFile(temp2, true, false)); try { File asciiFile = getTestFile("ascii.txt"); moveHelper(false, asciiFile, asciiFile); fail("expected exception"); } catch (IllegalArgumentException expected) { } }
public void testHash() throws IOException { File asciiFile = getTestFile("ascii.txt"); File i18nFile = getTestFile("i18n.txt"); String init = "d41d8cd98f00b204e9800998ecf8427e"; assertEquals(init, Hashing.md5().newHasher().hash().toString()); String asciiHash = "e5df5a39f2b8cb71b24e1d8038f93131"; assertEquals(asciiHash, Files.hash(asciiFile, Hashing.md5()).toString()); String i18nHash = "7fa826962ce2079c8334cd4ebf33aea4"; assertEquals(i18nHash, Files.hash(i18nFile, Hashing.md5()).toString()); }
public void testMove() throws IOException { File i18nFile = getTestFile("i18n.txt"); File temp1 = createTempFile(); File temp2 = createTempFile(); Files.copy(i18nFile, temp1); moveHelper(true, temp1, temp2); assertTrue(Files.equal(temp2, i18nFile)); }
public void testRoundTripSources() throws Exception { File asciiFile = getTestFile("ascii.txt"); ByteSource byteSource = Files.asByteSource(asciiFile); assertSame(byteSource, byteSource.asCharSource(Charsets.UTF_8).asByteSource(Charsets.UTF_8)); }