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 testWriteString() throws IOException { File temp = createTempFile(); Files.write(I18N, temp, Charsets.UTF_16LE); assertEquals(I18N, Files.toString(temp, Charsets.UTF_16LE)); }
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 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 testMap() throws IOException { // Test data int size = 1024; byte[] bytes = newPreFilledByteArray(size); // Setup File file = createTempFile(); Files.write(bytes, file); // Test MappedByteBuffer actual = Files.map(file); // Verify ByteBuffer expected = ByteBuffer.wrap(bytes); assertTrue("ByteBuffers should be equal.", expected.equals(actual)); }
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 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 testMap_readWrite_max_value_plus_1() throws IOException { // Setup File file = createTempFile(); // Test try { Files.map(file, MapMode.READ_WRITE, (long) Integer.MAX_VALUE + 1); fail("Should throw when size exceeds Integer.MAX_VALUE"); } catch (IllegalArgumentException expected) { } }
public void testWriteBytes() throws IOException { File temp = createTempFile(); byte[] data = newPreFilledByteArray(2000); Files.write(data, temp); assertTrue(Arrays.equals(data, Files.toByteArray(temp))); try { Files.write(null, temp); fail("expected exception"); } catch (NullPointerException expected) { } }
public void testMap_noSuchFile() throws IOException { // Setup File file = createTempFile(); boolean deleted = file.delete(); assertTrue(deleted); // Test try { Files.map(file); fail("Should have thrown FileNotFoundException."); } catch (FileNotFoundException expected) { } }
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 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 testMap_readWrite() throws IOException { // Test data int size = 1024; byte[] expectedBytes = new byte[size]; byte[] bytes = newPreFilledByteArray(1024); // Setup File file = createTempFile(); Files.write(bytes, file); Random random = new Random(); random.nextBytes(expectedBytes); // Test MappedByteBuffer map = Files.map(file, MapMode.READ_WRITE); map.put(expectedBytes); // Verify byte[] actualBytes = Files.toByteArray(file); assertTrue(Arrays.equals(expectedBytes, actualBytes)); }
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 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 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 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 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))); }