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 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 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 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) { } }
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 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 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 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 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 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 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 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) { } }
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 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 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 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) { } }