FileUtil { public static boolean isEmpty(final File file, final Charset charset) throws IOException { boolean empty = false; BufferedReader reader = null; boolean threw = true; try { reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset)); final String line = reader.readLine(); empty = line == null; threw = false; } finally { Closeables.close(reader, threw); } return empty; } private  FileUtil(); static boolean isEmpty(final File file, final Charset charset);  }
@Test public void isEmpty_withEmptyFile() throws Exception { final File tempEmptyFile = File.createTempFile("testfile", ".tmp"); tempEmptyFile.deleteOnExit(); final boolean isEmpty = FileUtil.isEmpty(tempEmptyFile, CHARSET); PowerMock.verifyAll(); assertThat(isEmpty).isTrue(); tempEmptyFile.delete(); }
@Test public void isEmpty_withFilledFile() throws Exception { final File tempEmptyFile = File.createTempFile("testfile", ".tmp"); tempEmptyFile.deleteOnExit(); final FileOutputStream writer = new FileOutputStream(tempEmptyFile); writer.write("test".getBytes()); writer.close(); final boolean isEmpty = FileUtil.isEmpty(tempEmptyFile, CHARSET); PowerMock.verifyAll(); assertThat(isEmpty).isFalse(); tempEmptyFile.delete(); }
@Test(expected = IOException.class) public void testNullCheckBeforeClosing() throws Exception { PowerMock.expectNiceNew(BufferedReader.class, EasyMock.anyObject(Reader.class)).andThrow(new IOException()); PowerMock.replayAll(); FileUtil.isEmpty(new File(VERSION_URL.toURI()), CHARSET); PowerMock.verifyAll(); }