From efbac53ec948a5c0f8205fc48a35ca6826d0b799 Mon Sep 17 00:00:00 2001 From: Charith De Silva Date: Thu, 7 Sep 2017 13:52:48 +1000 Subject: [PATCH] BAEL-973 Apache Commons Commons IO (#2262) * BAEL-973 Apache Commons Commons IO * BAEL-973 : fix up formatting * BAEL-973 : review comments. * BAEL-973 : review comments. * BAEL-973 : review comments. * BAEL-973 : review comments. * BAEL-973 : review comments. * BAEL-973 : review comments. --- .../com/baeldung/commons/io/FileMonitor.java | 43 +++++ .../commons/io/CommonsIOUnitTest.java | 151 ++++++++++++++++++ libraries/src/test/resources/ABC.txt | 1 + libraries/src/test/resources/aaa.txt | 1 + libraries/src/test/resources/fileTest.txt | 1 + libraries/src/test/resources/sample.txt | 2 + 6 files changed, 199 insertions(+) create mode 100644 libraries/src/main/java/com/baeldung/commons/io/FileMonitor.java create mode 100644 libraries/src/test/java/com/baeldung/commons/io/CommonsIOUnitTest.java create mode 100644 libraries/src/test/resources/ABC.txt create mode 100644 libraries/src/test/resources/aaa.txt create mode 100644 libraries/src/test/resources/fileTest.txt create mode 100644 libraries/src/test/resources/sample.txt diff --git a/libraries/src/main/java/com/baeldung/commons/io/FileMonitor.java b/libraries/src/main/java/com/baeldung/commons/io/FileMonitor.java new file mode 100644 index 0000000000..d42323a5df --- /dev/null +++ b/libraries/src/main/java/com/baeldung/commons/io/FileMonitor.java @@ -0,0 +1,43 @@ +package com.baeldung.commons.io; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.monitor.FileAlterationListener; +import org.apache.commons.io.monitor.FileAlterationListenerAdaptor; +import org.apache.commons.io.monitor.FileAlterationMonitor; +import org.apache.commons.io.monitor.FileAlterationObserver; + +import java.io.File; + +public class FileMonitor { + + public static void main(String[] args) throws Exception { + File folder = FileUtils.getTempDirectory(); + startFileMonitor(folder); + } + + /** + * @param folder + * @throws Exception + */ + public static void startFileMonitor(File folder) throws Exception { + FileAlterationObserver observer = new FileAlterationObserver(folder); + FileAlterationMonitor monitor = new FileAlterationMonitor(5000); + + FileAlterationListener fal = new FileAlterationListenerAdaptor() { + + @Override + public void onFileCreate(File file) { + // on create action + } + + @Override + public void onFileDelete(File file) { + // on delete action + } + }; + + observer.addListener(fal); + monitor.addObserver(observer); + monitor.start(); + } +} \ No newline at end of file diff --git a/libraries/src/test/java/com/baeldung/commons/io/CommonsIOUnitTest.java b/libraries/src/test/java/com/baeldung/commons/io/CommonsIOUnitTest.java new file mode 100644 index 0000000000..3c82c30d9b --- /dev/null +++ b/libraries/src/test/java/com/baeldung/commons/io/CommonsIOUnitTest.java @@ -0,0 +1,151 @@ +package com.baeldung.commons.io; + +import org.apache.commons.io.FileSystemUtils; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.FilenameUtils; +import org.apache.commons.io.IOCase; +import org.apache.commons.io.comparator.PathFileComparator; +import org.apache.commons.io.comparator.SizeFileComparator; +import org.apache.commons.io.filefilter.AndFileFilter; +import org.apache.commons.io.filefilter.NameFileFilter; +import org.apache.commons.io.filefilter.SuffixFileFilter; +import org.apache.commons.io.filefilter.WildcardFileFilter; +import org.apache.commons.io.input.TeeInputStream; +import org.apache.commons.io.output.TeeOutputStream; +import org.junit.Assert; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FilterOutputStream; +import java.io.IOException; +import java.nio.charset.Charset; + +public class CommonsIOUnitTest { + + @Test + public void whenCopyANDReadFileTesttxt_thenMatchExpectedData() + throws IOException { + + String expectedData = "Hello World from fileTest.txt!!!"; + + File file = FileUtils.getFile(getClass().getClassLoader() + .getResource("fileTest.txt") + .getPath()); + File tempDir = FileUtils.getTempDirectory(); + FileUtils.copyFileToDirectory(file, tempDir); + File newTempFile = FileUtils.getFile(tempDir, file.getName()); + String data = FileUtils.readFileToString(newTempFile, + Charset.defaultCharset()); + + Assert.assertEquals(expectedData, data.trim()); + } + + @Test + public void whenUsingFileNameUtils_thenshowdifferentFileOperations() + throws IOException { + + String path = getClass().getClassLoader() + .getResource("fileTest.txt") + .getPath(); + + String fullPath = FilenameUtils.getFullPath(path); + String extension = FilenameUtils.getExtension(path); + String baseName = FilenameUtils.getBaseName(path); + + System.out.println("full path" + fullPath); + System.out.println("Extension" + extension); + System.out.println("Base name" + baseName); + } + + @Test + public void whenUsingFileSystemUtils_thenDriveFreeSpace() + throws IOException { + + long freeSpace = FileSystemUtils.freeSpaceKb("/"); + } + + @SuppressWarnings("resource") + @Test + public void whenUsingTeeInputOutputStream_thenWriteto2OutputStreams() + throws IOException { + + final String str = "Hello World."; + ByteArrayInputStream inputStream = new ByteArrayInputStream(str.getBytes()); + ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream(); + ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream(); + + FilterOutputStream teeOutputStream = new TeeOutputStream(outputStream1,outputStream2); + new TeeInputStream(inputStream, teeOutputStream, true).read(new byte[str.length()]); + + Assert.assertEquals(str, String.valueOf(outputStream1)); + Assert.assertEquals(str, String.valueOf(outputStream2)); + } + + @Test + public void whenGetFilewithNameFileFilter_thenFindfileTesttxt() + throws IOException { + + final String testFile = "fileTest.txt"; + + String path = getClass().getClassLoader() + .getResource(testFile) + .getPath(); + File dir = FileUtils.getFile(FilenameUtils.getFullPath(path)); + + String[] possibleNames = { "NotThisOne", testFile }; + + Assert.assertEquals(testFile, + dir.list(new NameFileFilter(possibleNames, IOCase.INSENSITIVE))[0]); + } + + @Test + public void whenGetFilewith_ANDFileFilter_thenFindsampletxt() + throws IOException { + + String path = getClass().getClassLoader() + .getResource("fileTest.txt") + .getPath(); + File dir = FileUtils.getFile(FilenameUtils.getFullPath(path)); + + Assert.assertEquals("sample.txt", + dir.list(new AndFileFilter( + new WildcardFileFilter("*ple*", IOCase.INSENSITIVE), + new SuffixFileFilter("txt")))[0]); + } + + @Test + public void whenSortDirWithPathFileComparator_thenFirstFileaaatxt() + throws IOException { + + PathFileComparator pathFileComparator = new PathFileComparator( + IOCase.INSENSITIVE); + String path = FilenameUtils.getFullPath(getClass().getClassLoader() + .getResource("fileTest.txt") + .getPath()); + File dir = new File(path); + File[] files = dir.listFiles(); + + pathFileComparator.sort(files); + + Assert.assertEquals("aaa.txt", files[0].getName()); + } + + @Test + public void whenSizeFileComparator_thenLargerFile() + throws IOException { + + SizeFileComparator sizeFileComparator = new SizeFileComparator(); + File largerFile = FileUtils.getFile(getClass().getClassLoader() + .getResource("fileTest.txt") + .getPath()); + File smallerFile = FileUtils.getFile(getClass().getClassLoader() + .getResource("sample.txt") + .getPath()); + + int i = sizeFileComparator.compare(largerFile, smallerFile); + + Assert.assertTrue(i > 0); + } +} \ No newline at end of file diff --git a/libraries/src/test/resources/ABC.txt b/libraries/src/test/resources/ABC.txt new file mode 100644 index 0000000000..f78e42598c --- /dev/null +++ b/libraries/src/test/resources/ABC.txt @@ -0,0 +1 @@ +Hello World from ABC.txt!!! \ No newline at end of file diff --git a/libraries/src/test/resources/aaa.txt b/libraries/src/test/resources/aaa.txt new file mode 100644 index 0000000000..e5875f97d6 --- /dev/null +++ b/libraries/src/test/resources/aaa.txt @@ -0,0 +1 @@ +Hello World from aaa.txt!!! \ No newline at end of file diff --git a/libraries/src/test/resources/fileTest.txt b/libraries/src/test/resources/fileTest.txt new file mode 100644 index 0000000000..ce4bea208b --- /dev/null +++ b/libraries/src/test/resources/fileTest.txt @@ -0,0 +1 @@ +Hello World from fileTest.txt!!! \ No newline at end of file diff --git a/libraries/src/test/resources/sample.txt b/libraries/src/test/resources/sample.txt new file mode 100644 index 0000000000..20f137b416 --- /dev/null +++ b/libraries/src/test/resources/sample.txt @@ -0,0 +1,2 @@ +line 1 +a second line \ No newline at end of file