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 testWriteString() throws IOException { File temp = createTempFile(); Files.write(I18N, temp, Charsets.UTF_16LE); assertEquals(I18N, Files.toString(temp, Charsets.UTF_16LE)); }
public void testAppendString() throws IOException { File temp = createTempFile(); Files.append(I18N, temp, Charsets.UTF_16LE); assertEquals(I18N, Files.toString(temp, Charsets.UTF_16LE)); Files.append(I18N, temp, Charsets.UTF_16LE); assertEquals(I18N + I18N, Files.toString(temp, Charsets.UTF_16LE)); Files.append(I18N, temp, Charsets.UTF_16LE); assertEquals(I18N + I18N + I18N, Files.toString(temp, Charsets.UTF_16LE)); }
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 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 testCopyEqualFiles() throws IOException { File temp1 = createTempFile(); File temp2 = file(temp1.getPath()); assertEquals(temp1, temp2); Files.write(ASCII, temp1, Charsets.UTF_8); try { Files.copy(temp1, temp2); fail("Expected an IAE to be thrown but wasn't"); } catch (IllegalArgumentException expected) { } assertEquals(ASCII, Files.toString(temp1, Charsets.UTF_8)); }
public void testGetNameWithoutExtension() { assertEquals("", Files.getNameWithoutExtension(".txt")); assertEquals("blah", Files.getNameWithoutExtension("blah.txt")); assertEquals("blah.", Files.getNameWithoutExtension("blah..txt")); assertEquals(".blah", Files.getNameWithoutExtension(".blah.txt")); assertEquals("blah", Files.getNameWithoutExtension("/tmp/blah.txt")); assertEquals("blah.tar", Files.getNameWithoutExtension("blah.tar.gz")); assertEquals("", Files.getNameWithoutExtension("/")); assertEquals("", Files.getNameWithoutExtension(".")); assertEquals(".", Files.getNameWithoutExtension("..")); assertEquals("..", Files.getNameWithoutExtension("...")); assertEquals("blah", Files.getNameWithoutExtension("blah")); assertEquals("blah", Files.getNameWithoutExtension("blah.")); assertEquals(".blah", Files.getNameWithoutExtension(".blah.")); assertEquals("blah", Files.getNameWithoutExtension("/foo.bar/blah")); assertEquals("blah", Files.getNameWithoutExtension("/foo/.bar/blah")); }
public void testCopyIdenticalFiles() throws IOException { File temp1 = createTempFile(); Files.write(ASCII, temp1, Charsets.UTF_8); File temp2 = createTempFile(); Files.write(ASCII, temp2, Charsets.UTF_8); Files.copy(temp1, temp2); assertEquals(ASCII, Files.toString(temp1, Charsets.UTF_8)); }
public void testGetFileExtension() { assertEquals("txt", Files.getFileExtension(".txt")); assertEquals("txt", Files.getFileExtension("blah.txt")); assertEquals("txt", Files.getFileExtension("blah..txt")); assertEquals("txt", Files.getFileExtension(".blah.txt")); assertEquals("txt", Files.getFileExtension("/tmp/blah.txt")); assertEquals("gz", Files.getFileExtension("blah.tar.gz")); assertEquals("", Files.getFileExtension("/")); assertEquals("", Files.getFileExtension(".")); assertEquals("", Files.getFileExtension("..")); assertEquals("", Files.getFileExtension("...")); assertEquals("", Files.getFileExtension("blah")); assertEquals("", Files.getFileExtension("blah.")); assertEquals("", Files.getFileExtension(".blah.")); assertEquals("", Files.getFileExtension("/foo.bar/blah")); assertEquals("", Files.getFileExtension("/foo/.bar/blah")); }
public void testCopySameFile() throws IOException { File temp = createTempFile(); Files.write(ASCII, temp, Charsets.UTF_8); try { Files.copy(temp, temp); fail("Expected an IAE to be thrown but wasn't"); } catch (IllegalArgumentException expected) { } assertEquals(ASCII, Files.toString(temp, Charsets.UTF_8)); }
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 testTouchTime() throws IOException { File temp = createTempFile(); assertTrue(temp.exists()); temp.setLastModified(0); assertEquals(0, temp.lastModified()); Files.touch(temp); assertThat(temp.lastModified()).isNotEqualTo(0); }
public void testLineReading() throws IOException { File temp = createTempFile(); assertNull(Files.readFirstLine(temp, Charsets.UTF_8)); assertTrue(Files.readLines(temp, Charsets.UTF_8).isEmpty()); PrintWriter w = new PrintWriter(Files.newWriter(temp, Charsets.UTF_8)); w.println("hello"); w.println(""); w.println(" world "); w.println(""); w.close(); assertEquals("hello", Files.readFirstLine(temp, Charsets.UTF_8)); assertEquals( ImmutableList.of("hello", "", " world ", ""), Files.readLines(temp, Charsets.UTF_8)); assertTrue(temp.delete()); }
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 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 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 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 testCopyToOutputStream() throws IOException { File i18nFile = getTestFile("i18n.txt"); ByteArrayOutputStream out = new ByteArrayOutputStream(); Files.copy(i18nFile, out); assertEquals(I18N, out.toString("UTF-8")); }