@Test public void sizeOf_is_zero_on_empty_files() throws IOException { File file = temporaryFolder.newFile(); assertThat(FileUtils2.sizeOf(file.toPath())).isEqualTo(0); }
@Test public void sizeOf_sums_sizes_of_all_files_in_directory() throws IOException { File dir = temporaryFolder.newFolder(); File child = new File(dir, "child.txt"); File grandChild1 = new File(dir, "grand/child1.txt"); File grandChild2 = new File(dir, "grand/child2.txt"); FileUtils.write(child, "foo", UTF_8); FileUtils.write(grandChild1, "bar", UTF_8); FileUtils.write(grandChild2, "baz", UTF_8); long childSize = FileUtils2.sizeOf(child.toPath()); assertThat(childSize).isPositive(); long grandChild1Size = FileUtils2.sizeOf(grandChild1.toPath()); assertThat(grandChild1Size).isPositive(); long grandChild2Size = FileUtils2.sizeOf(grandChild2.toPath()); assertThat(grandChild2Size).isPositive(); assertThat(FileUtils2.sizeOf(dir.toPath())) .isEqualTo(childSize + grandChild1Size + grandChild2Size); // sanity check by comparing commons-io assertThat(FileUtils2.sizeOf(dir.toPath())) .isEqualTo(FileUtils.sizeOfDirectory(dir)); }
@Test public void sizeOf_ignores_size_of_non_regular_files() throws IOException { assumeTrue(SystemUtils.IS_OS_UNIX); File outside = temporaryFolder.newFile(); FileUtils.write(outside, "outside!!!", UTF_8); File dir = temporaryFolder.newFolder(); File child = new File(dir, "child1.txt"); FileUtils.write(child, "inside!!!", UTF_8); File symlink = new File(dir, "child2.txt"); Files.createSymbolicLink(symlink.toPath(), outside.toPath()); assertThat(FileUtils2.sizeOf(dir.toPath())) .isPositive() .isEqualTo(FileUtils2.sizeOf(child.toPath())); }
@Test public void sizeOf_throws_IOE_if_path_does_not_exist() throws IOException { Path path = temporaryFolder.newFile().toPath(); Files.delete(path); expectedException.expect(IOException.class); FileUtils2.sizeOf(path); }
@Override public void execute(ComputationStep.Context context) { try (DbSession dbSession = dbClient.openSession(false)) { Optional<CeTaskInputDao.DataStream> opt = dbClient.ceTaskInputDao().selectData(dbSession, task.getUuid()); if (opt.isPresent()) { File unzippedDir = tempFolder.newDir(); try (CeTaskInputDao.DataStream reportStream = opt.get(); InputStream zipStream = new BufferedInputStream(reportStream.getInputStream())) { ZipUtils.unzip(zipStream, unzippedDir); } catch (IOException e) { throw new IllegalStateException("Fail to extract report " + task.getUuid() + " from database", e); } reportDirectoryHolder.setDirectory(unzippedDir); if (LOGGER.isDebugEnabled()) { // size is not added to context statistics because computation // can take time. It's enabled only if log level is DEBUG. try { String dirSize = FileUtils.byteCountToDisplaySize(FileUtils2.sizeOf(unzippedDir.toPath())); LOGGER.debug("Analysis report is {} uncompressed", dirSize); } catch (IOException e) { LOGGER.warn("Fail to compute size of directory " + unzippedDir, e); } } } else { throw MessageException.of("Analysis report " + task.getUuid() + " is missing in database"); } } }