--- /dev/null
+.classpath
+.project
+.settings
+bin
+target
--- /dev/null
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>de.laktatnebel.libs</groupId>
+ <artifactId>filelib</artifactId>
+ <version>2.1.1-SNAPSHOT</version>
+
+ <name>FileLib</name>
+
+ <parent>
+ <groupId>de.laktatnebel.maven</groupId>
+ <artifactId>laktatnebellib</artifactId>
+ <version>2.1.7</version>
+ </parent>
+
+ <dependencyManagement>
+ <dependencies>
+ <dependency>
+ <groupId>de.laktatnebel.libs</groupId>
+ <artifactId>utillib</artifactId>
+ <version>2.1.0</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+ </dependencyManagement>
+
+
+ <dependencies>
+ <dependency>
+ <groupId>de.laktatnebel.libs</groupId>
+ <artifactId>utillib</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+</project>
--- /dev/null
+package de.laktatnebel.libs.filelib;
+
+public enum DirectoryFilesSwitch {
+
+ DIRECTORIES, FILES, DIRECRTORIES_FILES;
+
+}
--- /dev/null
+/*\r
+ * Projekt:\r
+ * Dateiname: DirectoryManager.java\r
+ * Erzeugt: 07.01.2010\r
+ * Beschreibung:\r
+ * ***********************************************************************\r
+ * Modification History:\r
+ * ***********************************************************************\r
+ */\r
+package de.laktatnebel.libs.filelib;\r
+\r
+import de.laktatnebel.libs.filelib.exception.AbstractFileLibException;\r
+import de.laktatnebel.libs.filelib.exception.DirectoryManagerException;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.nio.file.FileVisitOption;\r
+import java.nio.file.FileVisitResult;\r
+import java.nio.file.Files;\r
+import java.nio.file.Path;\r
+import java.nio.file.SimpleFileVisitor;\r
+import java.nio.file.attribute.BasicFileAttributes;\r
+import java.util.ArrayList;\r
+import java.util.HashSet;\r
+import java.util.List;\r
+import java.util.Set;\r
+import java.util.regex.Matcher;\r
+import java.util.regex.Pattern;\r
+\r
+/**\r
+ * @author olgehrma\r
+ */\r
+public class DirectoryManager {\r
+\r
+ protected static class PatternMatcher {\r
+ /**\r
+ * \r
+ * @param mimePattern\r
+ * @param listOfFiles\r
+ * @param path\r
+ * @return\r
+ * @throws IOException\r
+ */\r
+ protected static boolean getNamePattern(final String namePattern, final List<Path> listOfFiles, final Path path)\r
+ throws IOException {\r
+ final String name = path.toFile().getAbsolutePath();\r
+ return getPattern(namePattern, listOfFiles, path, name);\r
+ }\r
+\r
+ /**\r
+ * \r
+ * @param mimePattern\r
+ * @param listOfFiles\r
+ * @param path\r
+ * @return\r
+ * @throws IOException\r
+ */\r
+ protected static boolean getMimePattern(final String mimePattern, final List<Path> listOfFiles, final Path path)\r
+ throws IOException {\r
+ final String mime = Files.probeContentType(path);\r
+ return getPattern(mimePattern, listOfFiles, path, mime);\r
+ }\r
+\r
+ /**\r
+ * \r
+ * @param patternToFind\r
+ * @param listOfFiles\r
+ * @param path\r
+ * @param candidate\r
+ * @return\r
+ */\r
+ private static boolean getPattern(final String patternToFind, final List<Path> listOfFiles, final Path path,\r
+ final String candidate) {\r
+ if (candidate == null) {\r
+ return true;\r
+ } else {\r
+ Pattern pattern = Pattern.compile(patternToFind);\r
+ Matcher matcher = pattern.matcher(candidate);\r
+ if (matcher.find()) {\r
+ return true;\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+ }\r
+\r
+ private static final String FIND_ALL_PATTERN = ".*?";\r
+\r
+ /**\r
+ * Liefert ArrayList<Path> von Files in einem Verzeichnis.\r
+ * \r
+ * @param pathName\r
+ * Pfadname\r
+ * @return List of Filenames\r
+ * @throws AbstractFileLibException\r
+ * DirectoryManagerException\r
+ */\r
+ public static List<Path> readPathAsListOfPath(final String pathName) throws AbstractFileLibException {\r
+ final File directorySources = new File(pathName);\r
+\r
+ if (!directorySources.isDirectory()) {\r
+ throw new DirectoryManagerException("FEHLER - Pfad ist kein Verzeichnis!");\r
+ }\r
+\r
+ String[] arrayOfFileNames = directorySources.list();\r
+\r
+ List<Path> listOfFiles = new ArrayList<Path>();\r
+\r
+ for (String fileName : arrayOfFileNames) {\r
+ listOfFiles.add(new File(fileName).toPath());\r
+ }\r
+\r
+ return listOfFiles;\r
+ }\r
+\r
+ public static void makeNewDirectory(final String targetDirectory) throws DirectoryManagerException {\r
+ if (targetDirectory != null) {\r
+ final File newPath = new File(targetDirectory);\r
+ newPath.mkdirs();\r
+ } else {\r
+ throw new DirectoryManagerException("\"targetDirectory\" ist nicht angegeben.");\r
+ }\r
+ }\r
+\r
+ private static List<Path> getPathListFromPath(final Path startPath, final String namePattern, final String mimePattern,\r
+ final DirectoryFilesSwitch directoryFilesSwitch, boolean recursivly) throws AbstractFileLibException {\r
+\r
+ if (namePattern == null || namePattern.isEmpty()) {\r
+ throw new DirectoryManagerException("Invalid parameter \"namePattern\": null or empty!");\r
+ }\r
+\r
+ final List<Path> listOfFiles = new ArrayList<Path>();\r
+\r
+ final Set<FileVisitOption> options = new HashSet<FileVisitOption>();\r
+ options.add(FileVisitOption.FOLLOW_LINKS);\r
+\r
+ int maxValue = 1;\r
+ if (recursivly) {\r
+ maxValue = Integer.MAX_VALUE;\r
+ }\r
+\r
+ try {\r
+\r
+ switch (directoryFilesSwitch) {\r
+ case DIRECTORIES:\r
+ Files.walkFileTree(startPath, options, maxValue, new SimpleFileVisitor<Path>() {\r
+ @Override\r
+ public FileVisitResult visitFileFailed(final Path path, final IOException ioe) {\r
+ return FileVisitResult.SKIP_SUBTREE;\r
+ }\r
+\r
+ @Override\r
+ public FileVisitResult preVisitDirectory(final Path path, final BasicFileAttributes attribs) {\r
+\r
+ try {\r
+ if (PatternMatcher.getNamePattern(namePattern, listOfFiles, path)) {\r
+ listOfFiles.add(path);\r
+ }\r
+ } catch (final IOException ioe) {\r
+ throw (RuntimeException) new RuntimeException().initCause(ioe);\r
+ }\r
+\r
+ return FileVisitResult.CONTINUE;\r
+ }\r
+\r
+ });\r
+ break;\r
+ case FILES:\r
+ Files.walkFileTree(startPath, options, maxValue, new SimpleFileVisitor<Path>() {\r
+ @Override\r
+ public FileVisitResult visitFileFailed(final Path path, final IOException ioe) {\r
+ return FileVisitResult.SKIP_SUBTREE;\r
+ }\r
+\r
+ @Override\r
+ public FileVisitResult visitFile(final Path path, final BasicFileAttributes attribs) {\r
+\r
+ try {\r
+ if (PatternMatcher.getNamePattern(namePattern, listOfFiles, path)\r
+ && PatternMatcher.getMimePattern(mimePattern, listOfFiles, path)) {\r
+ listOfFiles.add(path);\r
+ }\r
+ } catch (final IOException ioe) {\r
+ throw (RuntimeException) new RuntimeException().initCause(ioe);\r
+ }\r
+\r
+ return FileVisitResult.CONTINUE;\r
+ }\r
+\r
+ });\r
+ break;\r
+ case DIRECRTORIES_FILES:\r
+ Files.walkFileTree(startPath, options, maxValue, new SimpleFileVisitor<Path>() {\r
+ @Override\r
+ public FileVisitResult visitFileFailed(final Path path, final IOException ioe) {\r
+ return FileVisitResult.SKIP_SUBTREE;\r
+ }\r
+\r
+ @Override\r
+ public FileVisitResult preVisitDirectory(final Path path, final BasicFileAttributes attribs) {\r
+\r
+ try {\r
+ if (PatternMatcher.getNamePattern(namePattern, listOfFiles, path)) {\r
+ listOfFiles.add(path);\r
+ }\r
+ } catch (final IOException ioe) {\r
+ throw (RuntimeException) new RuntimeException().initCause(ioe);\r
+ }\r
+\r
+ return FileVisitResult.CONTINUE;\r
+ }\r
+\r
+ @Override\r
+ public FileVisitResult visitFile(final Path path, final BasicFileAttributes attribs) {\r
+\r
+ try {\r
+ if (PatternMatcher.getNamePattern(namePattern, listOfFiles, path)\r
+ && PatternMatcher.getMimePattern(mimePattern, listOfFiles, path)) {\r
+ listOfFiles.add(path);\r
+ }\r
+ } catch (final IOException ioe) {\r
+ throw (RuntimeException) new RuntimeException().initCause(ioe);\r
+ }\r
+\r
+ return FileVisitResult.CONTINUE;\r
+ }\r
+ });\r
+ break;\r
+ }\r
+\r
+ } catch (final IOException ioe) {\r
+ throw new DirectoryManagerException(ioe.getMessage(), ioe);\r
+ }\r
+\r
+ return listOfFiles;\r
+ }\r
+\r
+ /**\r
+ * Liefert Liste von Files und Verzeichnissen als ArrayList<java.nio.file.Path>\r
+ * \r
+ * @param startPath\r
+ * Path Startpunkt (Verzeichnis) der rekursiven Suche als java.nio.file.Path\r
+ * @param mimePattern\r
+ * String Filter-Pattern. Nur Namen von Files, die zum Pattern passen, werden genommen, null oder empty liefert\r
+ * Exception!\r
+ * @return ArrayList<java.nio.file.Path> Liste von File-Objeten\r
+ * @throws AbstractFileLibException\r
+ * DirectoryManagerException\r
+ */\r
+ public static List<Path> getPathListRecursivlyFromPath(final Path startPath, final String mimePattern)\r
+ throws AbstractFileLibException {\r
+\r
+ return DirectoryManager.getPathListFromPath(startPath, FIND_ALL_PATTERN, mimePattern, DirectoryFilesSwitch.FILES, true);\r
+ }\r
+\r
+ public static List<Path> getPathListNoneRecursivlyFromPath(final Path startPath, final String mimePattern)\r
+ throws AbstractFileLibException {\r
+\r
+ return DirectoryManager.getPathListFromPath(startPath, FIND_ALL_PATTERN, mimePattern, DirectoryFilesSwitch.FILES, false);\r
+ }\r
+\r
+ /**\r
+ * Liefert Liste von Files und Verzeichnissen als ArrayList<java.nio.file.Path>\r
+ * \r
+ * @param startPath\r
+ * Path Startpunkt (Verzeichnis) der rekursiven Suche als java.nio.file.Path\r
+ * @param mimePattern\r
+ * String Filter-Pattern. Nur Namen von Files, die zum Pattern passen, werden genommen, null oder empty liefert\r
+ * Exception!\r
+ * @return ArrayList<java.nio.file.Path> Liste von File-Objeten\r
+ * @throws AbstractFileLibException\r
+ * DirectoryManagerException\r
+ */\r
+ public static List<Path> getPathListRecursivlyFromPath(final Path startPath, final DirectoryFilesSwitch directoryFilesSwitch,\r
+ final String mimePattern) throws AbstractFileLibException {\r
+\r
+ return DirectoryManager.getPathListFromPath(startPath, FIND_ALL_PATTERN, mimePattern, directoryFilesSwitch, true);\r
+ }\r
+\r
+ public static List<Path> getPathListNoneRecursivlyFromPath(final Path startPath,\r
+ final DirectoryFilesSwitch directoryFilesSwitch, final String mimePattern) throws AbstractFileLibException {\r
+\r
+ return DirectoryManager.getPathListFromPath(startPath, FIND_ALL_PATTERN, mimePattern, directoryFilesSwitch, false);\r
+ }\r
+\r
+ public static List<Path> getPathListRecursivlyFromPath(final Path startPath, final DirectoryFilesSwitch directoryFilesSwitch)\r
+ throws AbstractFileLibException {\r
+ return DirectoryManager.getPathListFromPath(startPath, FIND_ALL_PATTERN, FIND_ALL_PATTERN, directoryFilesSwitch, true);\r
+ }\r
+\r
+ public static List<Path> getPathListNoneRecursivlyFromPath(final Path startPath, final DirectoryFilesSwitch directoryFilesSwitch)\r
+ throws AbstractFileLibException {\r
+ return DirectoryManager.getPathListFromPath(startPath, FIND_ALL_PATTERN, FIND_ALL_PATTERN, directoryFilesSwitch, false);\r
+ }\r
+\r
+ /**\r
+ * Liefert Liste von Files und Verzeichnissen als ArrayList<java.nio.file.Path>\r
+ * \r
+ * @param startPath\r
+ * Path Startpunkt (Verzeichnis) der rekursiven Suche als java.nio.file.Path\r
+ * @param mimePattern\r
+ * String Filter-Pattern. Nur Namen von Files, die zum Pattern passen, werden genommen, null oder empty liefert\r
+ * Exception!\r
+ * @return ArrayList<java.nio.file.Path> Liste von File-Objeten\r
+ * @throws AbstractFileLibException\r
+ * DirectoryManagerException\r
+ */\r
+ public static List<Path> getFilteredPathListRecursivlyFromPath(final Path startPath, final String namePattern,\r
+ final String mimePattern) throws AbstractFileLibException {\r
+\r
+ return DirectoryManager.getPathListFromPath(startPath, namePattern, mimePattern, DirectoryFilesSwitch.FILES, true);\r
+ }\r
+\r
+ public static List<Path> getFilteredPathListNoneRecursivlyFromPath(final Path startPath, final String namePattern,\r
+ final String mimePattern) throws AbstractFileLibException {\r
+\r
+ return DirectoryManager.getPathListFromPath(startPath, namePattern, mimePattern, DirectoryFilesSwitch.FILES, false);\r
+ }\r
+\r
+ /**\r
+ * Liefert Liste von Files und Verzeichnissen als ArrayList<java.nio.file.Path>\r
+ * \r
+ * @param startPath\r
+ * Path Startpunkt (Verzeichnis) der rekursiven Suche als java.nio.file.Path\r
+ * @param mimePattern\r
+ * String Filter-Pattern. Nur Namen von Files, die zum Pattern passen, werden genommen, null oder empty liefert\r
+ * Exception!\r
+ * @return ArrayList<java.nio.file.Path> Liste von File-Objeten\r
+ * @throws AbstractFileLibException\r
+ * DirectoryManagerException\r
+ */\r
+ public static List<Path> getFilteredPathListRecursivlyFromPath(final Path startPath,\r
+ final DirectoryFilesSwitch directoryFilesSwitch, final String namePattern, final String mimePattern)\r
+ throws AbstractFileLibException {\r
+\r
+ return DirectoryManager.getPathListFromPath(startPath, namePattern, mimePattern, directoryFilesSwitch, true);\r
+ }\r
+\r
+ public static List<Path> getFilteredPathListNoneRecursivlyFromPath(final Path startPath,\r
+ final DirectoryFilesSwitch directoryFilesSwitch, final String namePattern, final String mimePattern)\r
+ throws AbstractFileLibException {\r
+\r
+ return DirectoryManager.getPathListFromPath(startPath, namePattern, mimePattern, directoryFilesSwitch, false);\r
+ }\r
+\r
+ public static List<Path> getFilteredPathListRecursivlyFromPath(final Path startPath,\r
+ final DirectoryFilesSwitch directoryFilesSwitch, final String namePattern) throws AbstractFileLibException {\r
+ return DirectoryManager.getPathListFromPath(startPath, namePattern, FIND_ALL_PATTERN, directoryFilesSwitch, true);\r
+ }\r
+\r
+ public static List<Path> getFilteredPathListNoneRecursivlyFromPath(final Path startPath,\r
+ final DirectoryFilesSwitch directoryFilesSwitch, final String namePattern) throws AbstractFileLibException {\r
+ return DirectoryManager.getPathListFromPath(startPath, namePattern, FIND_ALL_PATTERN, directoryFilesSwitch, false);\r
+ }\r
+\r
+ public List<File> getFilesFromPathes(List<Path> pathes) {\r
+\r
+ List<File> files = new ArrayList<File>();\r
+ for (Path path : pathes) {\r
+ files.add(path.toFile());\r
+ }\r
+ return files;\r
+ }\r
+\r
+ public List<String> getFileNamesFromPathes(List<Path> pathes) throws AbstractFileLibException {\r
+\r
+ List<String> files = new ArrayList<String>();\r
+ try {\r
+ for (Path path : pathes) {\r
+ files.add(path.toFile().getCanonicalPath());\r
+ }\r
+ } catch (IOException ioe) {\r
+ throw new DirectoryManagerException(ioe.getMessage(), ioe);\r
+ }\r
+ return files;\r
+ }\r
+\r
+}\r
--- /dev/null
+package de.laktatnebel.libs.filelib;
+
+import de.laktatnebel.libs.filelib.exception.AbstractFileLibException;
+import de.laktatnebel.libs.filelib.exception.FileReaderException;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+public class FileReader {
+
+ /**
+ * Liefert File-Inhalt als String
+ *
+ * @param path
+ * File-Objet als java.nio.file.Path
+ * @return String Fileinhalt
+ * @throws AbstractFileLibException
+ * FileReaderException
+ */
+ public static String getFileAsString(final Path path) throws AbstractFileLibException {
+ String string = null;
+
+ try {
+ if (path == null) {
+ throw new FileReaderException("Parameter \"path\" is null!");
+ }
+ string = new String(Files.readAllBytes(path));
+ } catch (final IOException ioe) {
+ throw new FileReaderException(ioe.getMessage(), ioe);
+ }
+
+ return string;
+ }
+
+ /**
+ * Liefert File-Inhalt als String
+ *
+ * @param fileName
+ * Filename also String
+ * @return String Fileinhalt
+ * @throws AbstractFileLibException
+ * FileReaderException
+ */
+ public static String getFileAsString(final String fileName) throws AbstractFileLibException {
+
+ if (fileName == null) {
+ throw new FileReaderException("Parameter \"fileName\" is null!");
+ }
+ if (fileName.isEmpty()) {
+ throw new FileReaderException("Parameter \"fileName\" is empty!");
+ }
+
+ final File file = new File(fileName);
+
+ if (!file.exists()) {
+ throw new FileReaderException("File " + fileName + " does not exist!");
+ }
+
+ if (!file.isFile()) {
+ throw new FileReaderException("File " + fileName + " is not a File!");
+ }
+
+ return FileReader.getFileAsString(file.toPath());
+ }
+}
--- /dev/null
+package de.laktatnebel.libs.filelib.exception;
+
+public abstract class AbstractFileLibException extends Exception {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = -4226434342611199020L;
+
+ /**
+ * @param t
+ */
+ public AbstractFileLibException(final Throwable t) {
+ super(t);
+ }
+
+ /**
+ * @param message
+ * @param t
+ */
+ public AbstractFileLibException(final String message, final Throwable t) {
+ super(message, t);
+ }
+
+ /**
+ * @param message
+ */
+ public AbstractFileLibException(final String message) {
+ super(message);
+ }
+
+}
--- /dev/null
+package de.laktatnebel.libs.filelib.exception;\r
+\r
+public class DirectoryManagerException extends AbstractFileLibException {\r
+\r
+ /** long serialVersionUID field */\r
+ private static final long serialVersionUID = 2923278519379485647L;\r
+\r
+ /**\r
+ * @param t\r
+ */\r
+ public DirectoryManagerException(final Throwable t) {\r
+ super(t);\r
+ }\r
+\r
+ /**\r
+ * @param message\r
+ * @param t\r
+ */\r
+ public DirectoryManagerException(final String message, final Throwable t) {\r
+ super(message, t);\r
+ }\r
+\r
+ /**\r
+ * @param message\r
+ */\r
+ public DirectoryManagerException(final String message) {\r
+ super(message);\r
+ }\r
+\r
+}\r
--- /dev/null
+package de.laktatnebel.libs.filelib.exception;\r
+\r
+public class FileReaderException extends AbstractFileLibException {\r
+\r
+ /** long serialVersionUID field */\r
+ private static final long serialVersionUID = 2923278519379485647L;\r
+\r
+ /**\r
+ * @param t\r
+ */\r
+ public FileReaderException(final Throwable t) {\r
+ super(t);\r
+ }\r
+\r
+ /**\r
+ * @param message\r
+ * @param t\r
+ */\r
+ public FileReaderException(final String message, final Throwable t) {\r
+ super(message, t);\r
+ }\r
+\r
+ /**\r
+ * @param message\r
+ */\r
+ public FileReaderException(final String message) {\r
+ super(message);\r
+ }\r
+\r
+}\r
--- /dev/null
+package de.laktatnebel.libs.filelib;
+
+import de.laktatnebel.libs.filelib.DirectoryManager.PatternMatcher;
+import de.laktatnebel.libs.utillib.logging.StaticLogger;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+
+public class DirectoryManagerPatternMatcherTest {
+
+ private static final String PREFIX = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "subdir"
+ + File.separator;
+
+ private List<Path> expectedPathes;
+
+ @Before
+ public void prepare() {
+ expectedPathes = new ArrayList<Path>();
+ }
+
+ @Test
+ public void testPattern() {
+ DirectoryManager dm = new DirectoryManager();
+ String namePattern = null;
+ List<Path> listOfFiles = null;
+ Path path = null;
+ String mimePattern = null;
+ try {
+ PatternMatcher.getNamePattern(namePattern, listOfFiles, path);
+ PatternMatcher.getMimePattern(mimePattern, listOfFiles, path);
+ } catch (IOException e) {
+ // TODO Handle IOException
+ throw (RuntimeException) new RuntimeException().initCause(e);
+ }
+ }
+
+
+ /**
+ *
+ * @param pathListRecursivlyFromPath
+ * @throws IOException
+ */
+ private void getPathListAsLogging(List<Path> pathListRecursivlyFromPath) throws IOException {
+ for (Path path : pathListRecursivlyFromPath) {
+ StaticLogger.getLogger(DirectoryManagerPatternMatcherTest.class).debug(path.toFile().getCanonicalPath());
+ }
+ }
+
+ /**
+ *
+ * @param dirs
+ */
+ private void setExpectedPathes(String[] dirs) {
+ for (String dir : dirs) {
+ expectedPathes.add(new File(PREFIX + dir).toPath());
+ }
+ }
+}
--- /dev/null
+package de.laktatnebel.libs.filelib;
+
+import de.laktatnebel.libs.filelib.exception.DirectoryManagerException;
+import de.laktatnebel.libs.utillib.logging.StaticLogger;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+
+public class DirectoryManagerTest {
+
+ private static final String PREFIX = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "subdir"
+ + File.separator;
+
+ private List<Path> expectedPathes;
+
+ @Before
+ public void prepare() {
+ expectedPathes = new ArrayList<Path>();
+ }
+
+ @Test
+ public void testFindDirs() {
+
+ Path startPath = new File(PREFIX).toPath();
+ DirectoryFilesSwitch directoryFilesSwitch = DirectoryFilesSwitch.DIRECTORIES;
+
+ String[] dirs = { "", "1a", "1b", "1a" + File.separator + "2b", "1a" + File.separator + "2a",
+ "1a" + File.separator + "2a" + File.separator + "3a" };
+
+ setExpectedPathes(dirs);
+
+ try {
+ getPathListAsLogging(expectedPathes);
+ StaticLogger.getLogger(DirectoryManagerTest.class).debug("----");
+
+ List<Path> pathListRecursivlyFromPath = DirectoryManager.getPathListRecursivlyFromPath(startPath, directoryFilesSwitch);
+ getPathListAsLogging(pathListRecursivlyFromPath);
+
+ Assert.assertTrue(pathListRecursivlyFromPath.containsAll(expectedPathes));
+ Assert.assertTrue(expectedPathes.containsAll(pathListRecursivlyFromPath));
+
+ } catch (DirectoryManagerException dme) {
+ String message = dme.getMessage();
+ StaticLogger.getLogger(DirectoryManagerTest.class).debug(message);
+
+ Assert.fail();
+
+ } catch (Exception e) {
+ String message = e.getMessage();
+ StaticLogger.getLogger(DirectoryManagerTest.class).debug(message);
+
+ Assert.fail();
+
+ }
+
+ }
+
+ @Test
+ public void testFindDirsWithNamePattern() {
+
+ Path startPath = new File(PREFIX).toPath();
+ DirectoryFilesSwitch directoryFilesSwitch = DirectoryFilesSwitch.DIRECTORIES;
+
+ String[] dirs = { "1a" + File.separator + "2a", "1a" + File.separator + "2a" + File.separator + "3a" };
+
+ setExpectedPathes(dirs);
+
+ try {
+ getPathListAsLogging(expectedPathes);
+ StaticLogger.getLogger(DirectoryManagerTest.class).debug("----");
+
+ String namePattern = "2a";
+ List<Path> pathListRecursivlyFromPath = DirectoryManager.getFilteredPathListRecursivlyFromPath(startPath,
+ directoryFilesSwitch, namePattern);
+ getPathListAsLogging(pathListRecursivlyFromPath);
+
+ Assert.assertEquals(pathListRecursivlyFromPath.size(), dirs.length);
+ Assert.assertTrue(expectedPathes.containsAll(pathListRecursivlyFromPath));
+
+ } catch (DirectoryManagerException dme) {
+ String message = dme.getMessage();
+ StaticLogger.getLogger(DirectoryManagerTest.class).debug(message);
+
+ Assert.fail();
+
+ } catch (Exception e) {
+ String message = e.getMessage();
+ StaticLogger.getLogger(DirectoryManagerTest.class).debug(message);
+
+ Assert.fail();
+
+ }
+
+ }
+
+ @Test
+ public void testFindDirsNoneRecursivly() {
+
+ Path startPath = new File(PREFIX).toPath();
+ DirectoryFilesSwitch directoryFilesSwitch = DirectoryFilesSwitch.DIRECTORIES;
+
+ String[] dirs = { "", "1a", "1b", "1a" + File.separator + "2b", "1a" + File.separator + "2a",
+ "1a" + File.separator + "2a" + File.separator + "3a" };
+
+ setExpectedPathes(dirs);
+
+ try {
+ getPathListAsLogging(expectedPathes);
+ StaticLogger.getLogger(DirectoryManagerTest.class).debug("----");
+
+ List<Path> pathListRecursivlyFromPath = DirectoryManager.getPathListNoneRecursivlyFromPath(startPath,
+ directoryFilesSwitch);
+ getPathListAsLogging(pathListRecursivlyFromPath);
+
+ Assert.assertTrue(pathListRecursivlyFromPath.isEmpty());
+
+ } catch (DirectoryManagerException dme) {
+ String message = dme.getMessage();
+ StaticLogger.getLogger(DirectoryManagerTest.class).debug(message);
+
+ Assert.fail();
+
+ } catch (Exception e) {
+ String message = e.getMessage();
+ StaticLogger.getLogger(DirectoryManagerTest.class).debug(message);
+
+ Assert.fail();
+
+ }
+
+ }
+
+ @Test
+ public void testFindFilesNoneRecursivly() {
+
+ Path startPath = new File(PREFIX).toPath();
+ DirectoryFilesSwitch directoryFilesSwitch = DirectoryFilesSwitch.FILES;
+
+ String[] dirs = { "file.dat" };
+
+ setExpectedPathes(dirs);
+
+ try {
+ getPathListAsLogging(expectedPathes);
+ StaticLogger.getLogger(DirectoryManagerTest.class).debug("----");
+
+ List<Path> pathListRecursivlyFromPath = DirectoryManager.getPathListNoneRecursivlyFromPath(startPath,
+ directoryFilesSwitch);
+ getPathListAsLogging(pathListRecursivlyFromPath);
+
+ Assert.assertTrue(pathListRecursivlyFromPath.isEmpty());
+
+ } catch (DirectoryManagerException dme) {
+ String message = dme.getMessage();
+ StaticLogger.getLogger(DirectoryManagerTest.class).debug(message);
+
+ Assert.fail();
+
+ } catch (Exception e) {
+ String message = e.getMessage();
+ StaticLogger.getLogger(DirectoryManagerTest.class).debug(message);
+
+ Assert.fail();
+
+ }
+
+ }
+
+ /**
+ *
+ * @param pathListRecursivlyFromPath
+ * @throws IOException
+ */
+ private void getPathListAsLogging(List<Path> pathListRecursivlyFromPath) throws IOException {
+ for (Path path : pathListRecursivlyFromPath) {
+ StaticLogger.getLogger(DirectoryManagerTest.class).debug(path.toFile().getCanonicalPath());
+ }
+ }
+
+ /**
+ *
+ * @param dirs
+ */
+ private void setExpectedPathes(String[] dirs) {
+ for (String dir : dirs) {
+ expectedPathes.add(new File(PREFIX + dir).toPath());
+ }
+ }
+}
--- /dev/null
+package de.laktatnebel.libs.filelib;
+
+import de.laktatnebel.libs.filelib.exception.FileReaderException;
+import de.laktatnebel.libs.utillib.logging.StaticLogger;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.File;
+import java.nio.file.Path;
+
+public class FileReaderTest {
+
+ private final String expectedTrue = "Ich bin ein Testfile!";
+
+
+ @Test
+ public void testGetFileAsStringPathNull() {
+
+ final Path path = null;
+ String messageExpected = "Parameter \"path\" is null!";
+
+ StaticLogger.getLogger(FileReaderTest.class).debug("testGetFileAsStringPathNull()");
+
+ try {
+ FileReader.getFileAsString(path);
+ } catch (final FileReaderException fre) {
+ String message = fre.getMessage();
+ StaticLogger.getLogger(FileReaderTest.class).debug(message);
+
+ Assert.assertEquals(messageExpected, message);
+ } catch (final Exception e) {
+ StaticLogger.getLogger(FileReaderTest.class).debug(e.getMessage());
+
+ Assert.fail();
+ }
+ }
+
+ @Test
+ public void testGetFileAsStringFilenameNull() {
+
+ String fileName = null;
+ String messageExpected = "Parameter \"fileName\" is null!";
+
+ StaticLogger.getLogger(FileReaderTest.class).debug("testGetFileAsStringFilenameNull()");
+
+ try {
+ final String fileAsString = FileReader.getFileAsString(fileName);
+
+ Assert.assertEquals(expectedTrue, fileAsString);
+ } catch (final FileReaderException fre) {
+ String message = fre.getMessage();
+ StaticLogger.getLogger(FileReaderTest.class).debug(message);
+
+ Assert.assertEquals(messageExpected, message);
+ } catch (final Exception e) {
+ StaticLogger.getLogger(FileReaderTest.class).debug(e.getMessage());
+
+ Assert.fail();
+ }
+ }
+
+ @Test
+ public void testGetFileAsStringFilenameEmpty() {
+
+ String fileName = "";
+ String messageExpected = "Parameter \"fileName\" is empty!";
+
+ StaticLogger.getLogger(FileReaderTest.class).debug("testGetFileAsStringFilenameEmpty()");
+
+ try {
+ final String fileAsString = FileReader.getFileAsString(fileName);
+
+ Assert.assertEquals(expectedTrue, fileAsString);
+ } catch (final FileReaderException fre) {
+ String message = fre.getMessage();
+ StaticLogger.getLogger(FileReaderTest.class).debug(message);
+
+ Assert.assertEquals(messageExpected, message);
+ } catch (final Exception e) {
+ StaticLogger.getLogger(FileReaderTest.class).debug(e.getMessage());
+
+ Assert.fail();
+ }
+ }
+
+ @Test
+ public void testGetFileAsStringFileNotExits() {
+
+ String fileName = "filereadertestfile.nonexistant.dat";
+ String messageExpected = "File " + fileName + " does not exist!";
+
+ StaticLogger.getLogger(FileReaderTest.class).debug("testGetFileAsStringFileNotExits()");
+
+ try {
+ final String fileAsString = FileReader.getFileAsString(fileName);
+
+ Assert.assertEquals(expectedTrue, fileAsString);
+ } catch (final FileReaderException fre) {
+ String message = fre.getMessage();
+ StaticLogger.getLogger(FileReaderTest.class).debug(message);
+
+ Assert.assertEquals(messageExpected, message);
+ } catch (final Exception e) {
+ StaticLogger.getLogger(FileReaderTest.class).debug(e.getMessage());
+
+ Assert.fail();
+ }
+ }
+
+ @Test
+ public void testGetFileAsStringFileIsDirectory() {
+
+ String fileName = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "subdir";
+ String messageExpected = "File " + fileName + " is not a File!";
+
+ StaticLogger.getLogger(FileReaderTest.class).debug("testGetFileAsStringFileIsDirectory()");
+
+ try {
+
+ final String fileAsString = FileReader.getFileAsString(fileName);
+
+ Assert.assertEquals(expectedTrue, fileAsString);
+ } catch (final FileReaderException fre) {
+ String message = fre.getMessage();
+ StaticLogger.getLogger(FileReaderTest.class).debug(message);
+
+ Assert.assertEquals(messageExpected, message);
+ } catch (final Exception e) {
+ StaticLogger.getLogger(FileReaderTest.class).debug(e.getMessage());
+
+ Assert.fail();
+ }
+ }
+
+ @Test
+ public void testGetFileAsString() {
+
+ String fileName = "src" + File.separator + "test" + File.separator + "resources" + File.separator
+ + "filereadertestfile.dat";
+
+ StaticLogger.getLogger(FileReaderTest.class).debug("testGetFileAsStringFileIsDirectory()");
+
+ try {
+
+ final String fileAsString = FileReader.getFileAsString(fileName);
+
+ Assert.assertEquals(expectedTrue, fileAsString);
+ } catch (final Exception e) {
+ String message = e.getMessage();
+ StaticLogger.getLogger(FileReaderTest.class).debug(message);
+
+ Assert.fail();
+ }
+ }
+}
--- /dev/null
+Ich bin ein Testfile!
\ No newline at end of file
--- /dev/null
+<?xml version="1.0" encoding="UTF-8" ?>\r
+<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">\r
+\r
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">\r
+\r
+ <appender name="appender" class="org.apache.log4j.ConsoleAppender">\r
+ <layout class="org.apache.log4j.PatternLayout">\r
+ <param name="ConversionPattern" value="%d [%t] %p - %m%n" />\r
+ </layout>\r
+ </appender>\r
+\r
+ <root>\r
+ <priority value="trace" />\r
+ <appender-ref ref="appender" />\r
+ </root>\r
+\r
+</log4j:configuration>\r
--- /dev/null
+a
\ No newline at end of file
--- /dev/null
+a
\ No newline at end of file
--- /dev/null
+a
\ No newline at end of file
--- /dev/null
+a
\ No newline at end of file
--- /dev/null
+a
\ No newline at end of file
--- /dev/null
+a
\ No newline at end of file
--- /dev/null
+a
\ No newline at end of file
--- /dev/null
+a
\ No newline at end of file
--- /dev/null
+a
\ No newline at end of file
--- /dev/null
+a
\ No newline at end of file
--- /dev/null
+a
\ No newline at end of file
--- /dev/null
+a
\ No newline at end of file
--- /dev/null
+a
\ No newline at end of file