@Test public void testAppend2Files() throws IOException { File destination = new File(tempFolder.getRoot(), "merged"); File file1 = dataFolder.getFile("file1.txt"); File file2 = dataFolder.getFile("file2.txt"); LastFileUtils.appendFiles(destination, file1, file2); LastAssertions.assertFileEquals(dataFolder.getFile("file1-2.txt"), destination); }
@Test public void testAppend3Files_List() throws IOException { File destination = new File(tempFolder.getRoot(), "merged"); List<File> files = new ArrayList<File>(); files.add(dataFolder.getFile("file1.txt")); files.add(dataFolder.getFile("file2.txt")); files.add(dataFolder.getFile("file3.txt")); LastFileUtils.appendFiles(destination, files); LastAssertions.assertFileEquals(dataFolder.getFile("file1-2-3.txt"), destination); }
/** * Tests requesting a tail that is smaller than the file, that line breaks are preserved. * * @throws IOException */ @Test public void testTailLineBreaks() throws IOException { File file = dataFolder.getFile("3805bytes.log"); String tail = LastFileUtils.tail(file, 287); assertEquals( "2008-01-14 18:25:54,756 fm.last.citrine.jobs.syscommand.RollingFileSysCommandObserver.sysOut(RollingFileSysCommandObserver.java:72) version.bat\n2008-01-14 18:25:54,757 fm.last.citrine.jobs.syscommand.RollingFileSysCommandObserver.sysOut(RollingFileSysCommandObserver.java:72) version.sh", tail); }
@Test public void testWriteToFile() throws IOException { File inputFile = dataFolder.getFile("3805bytes.log"); File outputFile = new File(tempFolder.getRoot(), "copy.log"); InputStream inputStream = new FileInputStream(inputFile); LastFileUtils.writeToFile(inputStream, outputFile); assertEquals(FileUtils.readFileToString(inputFile), FileUtils.readFileToString(outputFile)); }
@Test(expected = NullPointerException.class) public void testMoveFileToDirectorySafely_NullDir_CreateDir() throws IOException { // first copy file from data folder to temp folder so it can be moved safely File originalFile = dataFolder.getFile("3805bytes.log"); FileUtils.copyFileToDirectory(originalFile, tempFolder.getRoot()); File inputFile = new File(tempFolder.getRoot(), originalFile.getName()); assertTrue(inputFile.getAbsolutePath() + " not found", inputFile.exists()); LastFileUtils.moveFileToDirectorySafely(inputFile, null, true); // null, create dir true }
@Test(expected = FileNotFoundException.class) public void testMoveFileToDirectorySafely_NoCreateDir() throws IOException { // first copy file from data folder to temp folder so it can be moved safely File originalFile = dataFolder.getFile("3805bytes.log"); FileUtils.copyFileToDirectory(originalFile, tempFolder.getRoot()); File inputFile = new File(tempFolder.getRoot(), originalFile.getName()); assertTrue(inputFile.getAbsolutePath() + " not found", inputFile.exists()); // now do the actual moving File newDir = new File(tempFolder.getRoot(), "FileUtilsTest"); assertFalse(newDir.exists()); // dir must not exist // copy file over to newdir, NOT creating dir if it doesn't exist LastFileUtils.moveFileToDirectorySafely(inputFile, newDir, false); }
@Test public void testMoveFileSafely() throws IOException { // first copy file from data folder to temp folder so it can be moved safely String filename = "3805bytes.log"; File originalFile = dataFolder.getFile(filename); FileUtils.copyFileToDirectory(originalFile, tempFolder.getRoot()); File inputFile = new File(tempFolder.getRoot(), filename); assertTrue(inputFile.getAbsolutePath() + " not found", inputFile.exists()); // now do the actual moving File movedFile = new File(tempFolder.getRoot(), "copy.log"); LastFileUtils.moveFileSafely(inputFile, movedFile); assertFalse(inputFile.getAbsolutePath() + " exists", inputFile.exists()); assertTrue(movedFile.getAbsolutePath() + " doesn't exist", movedFile.exists()); assertEquals(FileUtils.readFileToString(originalFile), FileUtils.readFileToString(movedFile)); }
@Test public void testMoveFileToDirectorySafely() throws IOException { // first copy file from data folder to temp folder so it can be moved safely File originalFile = dataFolder.getFile("3805bytes.log"); FileUtils.copyFileToDirectory(originalFile, tempFolder.getRoot()); File inputFile = new File(tempFolder.getRoot(), originalFile.getName()); assertTrue(inputFile.getAbsolutePath() + " not found", inputFile.exists()); // now do the actual moving File newDir = tempFolder.newFolder("FileUtilsTest"); assertTrue(newDir.exists()); // copy file over to newdir, not creating it if it doesn't exist LastFileUtils.moveFileToDirectorySafely(inputFile, newDir, false); assertFalse(inputFile.getAbsolutePath() + " exists", inputFile.exists()); File movedFile = new File(newDir, inputFile.getName()); assertTrue(movedFile.getAbsolutePath() + " doesn't exist", movedFile.exists()); assertEquals(FileUtils.readFileToString(originalFile), FileUtils.readFileToString(movedFile)); }
/** * Tests requesting a tail that is the same size as the file, should read in entire file. * * @throws IOException */ @Test public void testTailSameAsFile() throws IOException { File file = dataFolder.getFile("3805bytes.log"); String tail = LastFileUtils.tail(file, 3805); assertEquals(FileUtils.readFileToString(file), tail); }