From fa8442409d5888a4173071db5005c9d7de6a5293 Mon Sep 17 00:00:00 2001 From: "Ole B. Rosentreter" Date: Fri, 27 Sep 2024 17:15:58 +0200 Subject: [PATCH 1/1] Initial project version --- .gitignore | 5 + pom.xml | 34 ++ .../libs/filelib/DirectoryFilesSwitch.java | 7 + .../libs/filelib/DirectoryManager.java | 378 ++++++++++++++++++ .../laktatnebel/libs/filelib/FileReader.java | 67 ++++ .../exception/AbstractFileLibException.java | 32 ++ .../exception/DirectoryManagerException.java | 30 ++ .../exception/FileReaderException.java | 30 ++ .../DirectoryManagerPatternMatcherTest.java | 64 +++ .../libs/filelib/DirectoryManagerTest.java | 196 +++++++++ .../libs/filelib/FileReaderTest.java | 156 ++++++++ src/test/resources/filereadertestfile.dat | 1 + src/test/resources/log4j.xml | 17 + .../resources/subdir/1a/2a/3a/file100.dat | 1 + .../resources/subdir/1a/2a/3a/file101.dat | 1 + src/test/resources/subdir/1a/2a/file10.dat | 1 + src/test/resources/subdir/1a/2a/file11.dat | 1 + src/test/resources/subdir/1a/2a/file12.dat | 1 + src/test/resources/subdir/1a/2a/file13.dat | 1 + src/test/resources/subdir/1a/2b/file20.dat | 1 + src/test/resources/subdir/1a/2b/file21.dat | 1 + src/test/resources/subdir/1a/file1.dat | 1 + src/test/resources/subdir/1a/file2.dat | 1 + src/test/resources/subdir/1a/file3.dat | 1 + src/test/resources/subdir/1b/file1.dat | 1 + src/test/resources/subdir/1b/img1.jpg | Bin 0 -> 1538 bytes src/test/resources/subdir/1b/img2.jpg | Bin 0 -> 1538 bytes src/test/resources/subdir/file.dat | 1 + 28 files changed, 1030 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/de/laktatnebel/libs/filelib/DirectoryFilesSwitch.java create mode 100644 src/main/java/de/laktatnebel/libs/filelib/DirectoryManager.java create mode 100644 src/main/java/de/laktatnebel/libs/filelib/FileReader.java create mode 100644 src/main/java/de/laktatnebel/libs/filelib/exception/AbstractFileLibException.java create mode 100644 src/main/java/de/laktatnebel/libs/filelib/exception/DirectoryManagerException.java create mode 100644 src/main/java/de/laktatnebel/libs/filelib/exception/FileReaderException.java create mode 100644 src/test/java/de/laktatnebel/libs/filelib/DirectoryManagerPatternMatcherTest.java create mode 100644 src/test/java/de/laktatnebel/libs/filelib/DirectoryManagerTest.java create mode 100644 src/test/java/de/laktatnebel/libs/filelib/FileReaderTest.java create mode 100644 src/test/resources/filereadertestfile.dat create mode 100644 src/test/resources/log4j.xml create mode 100644 src/test/resources/subdir/1a/2a/3a/file100.dat create mode 100644 src/test/resources/subdir/1a/2a/3a/file101.dat create mode 100644 src/test/resources/subdir/1a/2a/file10.dat create mode 100644 src/test/resources/subdir/1a/2a/file11.dat create mode 100644 src/test/resources/subdir/1a/2a/file12.dat create mode 100644 src/test/resources/subdir/1a/2a/file13.dat create mode 100644 src/test/resources/subdir/1a/2b/file20.dat create mode 100644 src/test/resources/subdir/1a/2b/file21.dat create mode 100644 src/test/resources/subdir/1a/file1.dat create mode 100644 src/test/resources/subdir/1a/file2.dat create mode 100644 src/test/resources/subdir/1a/file3.dat create mode 100644 src/test/resources/subdir/1b/file1.dat create mode 100755 src/test/resources/subdir/1b/img1.jpg create mode 100755 src/test/resources/subdir/1b/img2.jpg create mode 100644 src/test/resources/subdir/file.dat diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9aac7d9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.classpath +.project +.settings +bin +target diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..40fcdb8 --- /dev/null +++ b/pom.xml @@ -0,0 +1,34 @@ + + 4.0.0 + de.laktatnebel.libs + filelib + 2.1.1-SNAPSHOT + + FileLib + + + de.laktatnebel.maven + laktatnebellib + 2.1.7 + + + + + + de.laktatnebel.libs + utillib + 2.1.0 + test + + + + + + + + de.laktatnebel.libs + utillib + test + + + diff --git a/src/main/java/de/laktatnebel/libs/filelib/DirectoryFilesSwitch.java b/src/main/java/de/laktatnebel/libs/filelib/DirectoryFilesSwitch.java new file mode 100644 index 0000000..a056808 --- /dev/null +++ b/src/main/java/de/laktatnebel/libs/filelib/DirectoryFilesSwitch.java @@ -0,0 +1,7 @@ +package de.laktatnebel.libs.filelib; + +public enum DirectoryFilesSwitch { + + DIRECTORIES, FILES, DIRECRTORIES_FILES; + +} diff --git a/src/main/java/de/laktatnebel/libs/filelib/DirectoryManager.java b/src/main/java/de/laktatnebel/libs/filelib/DirectoryManager.java new file mode 100644 index 0000000..6eaf67b --- /dev/null +++ b/src/main/java/de/laktatnebel/libs/filelib/DirectoryManager.java @@ -0,0 +1,378 @@ +/* + * Projekt: + * Dateiname: DirectoryManager.java + * Erzeugt: 07.01.2010 + * Beschreibung: + * *********************************************************************** + * Modification History: + * *********************************************************************** + */ +package de.laktatnebel.libs.filelib; + +import de.laktatnebel.libs.filelib.exception.AbstractFileLibException; +import de.laktatnebel.libs.filelib.exception.DirectoryManagerException; + +import java.io.File; +import java.io.IOException; +import java.nio.file.FileVisitOption; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * @author olgehrma + */ +public class DirectoryManager { + + protected static class PatternMatcher { + /** + * + * @param mimePattern + * @param listOfFiles + * @param path + * @return + * @throws IOException + */ + protected static boolean getNamePattern(final String namePattern, final List listOfFiles, final Path path) + throws IOException { + final String name = path.toFile().getAbsolutePath(); + return getPattern(namePattern, listOfFiles, path, name); + } + + /** + * + * @param mimePattern + * @param listOfFiles + * @param path + * @return + * @throws IOException + */ + protected static boolean getMimePattern(final String mimePattern, final List listOfFiles, final Path path) + throws IOException { + final String mime = Files.probeContentType(path); + return getPattern(mimePattern, listOfFiles, path, mime); + } + + /** + * + * @param patternToFind + * @param listOfFiles + * @param path + * @param candidate + * @return + */ + private static boolean getPattern(final String patternToFind, final List listOfFiles, final Path path, + final String candidate) { + if (candidate == null) { + return true; + } else { + Pattern pattern = Pattern.compile(patternToFind); + Matcher matcher = pattern.matcher(candidate); + if (matcher.find()) { + return true; + } + } + return false; + } + } + + private static final String FIND_ALL_PATTERN = ".*?"; + + /** + * Liefert ArrayList von Files in einem Verzeichnis. + * + * @param pathName + * Pfadname + * @return List of Filenames + * @throws AbstractFileLibException + * DirectoryManagerException + */ + public static List readPathAsListOfPath(final String pathName) throws AbstractFileLibException { + final File directorySources = new File(pathName); + + if (!directorySources.isDirectory()) { + throw new DirectoryManagerException("FEHLER - Pfad ist kein Verzeichnis!"); + } + + String[] arrayOfFileNames = directorySources.list(); + + List listOfFiles = new ArrayList(); + + for (String fileName : arrayOfFileNames) { + listOfFiles.add(new File(fileName).toPath()); + } + + return listOfFiles; + } + + public static void makeNewDirectory(final String targetDirectory) throws DirectoryManagerException { + if (targetDirectory != null) { + final File newPath = new File(targetDirectory); + newPath.mkdirs(); + } else { + throw new DirectoryManagerException("\"targetDirectory\" ist nicht angegeben."); + } + } + + private static List getPathListFromPath(final Path startPath, final String namePattern, final String mimePattern, + final DirectoryFilesSwitch directoryFilesSwitch, boolean recursivly) throws AbstractFileLibException { + + if (namePattern == null || namePattern.isEmpty()) { + throw new DirectoryManagerException("Invalid parameter \"namePattern\": null or empty!"); + } + + final List listOfFiles = new ArrayList(); + + final Set options = new HashSet(); + options.add(FileVisitOption.FOLLOW_LINKS); + + int maxValue = 1; + if (recursivly) { + maxValue = Integer.MAX_VALUE; + } + + try { + + switch (directoryFilesSwitch) { + case DIRECTORIES: + Files.walkFileTree(startPath, options, maxValue, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFileFailed(final Path path, final IOException ioe) { + return FileVisitResult.SKIP_SUBTREE; + } + + @Override + public FileVisitResult preVisitDirectory(final Path path, final BasicFileAttributes attribs) { + + try { + if (PatternMatcher.getNamePattern(namePattern, listOfFiles, path)) { + listOfFiles.add(path); + } + } catch (final IOException ioe) { + throw (RuntimeException) new RuntimeException().initCause(ioe); + } + + return FileVisitResult.CONTINUE; + } + + }); + break; + case FILES: + Files.walkFileTree(startPath, options, maxValue, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFileFailed(final Path path, final IOException ioe) { + return FileVisitResult.SKIP_SUBTREE; + } + + @Override + public FileVisitResult visitFile(final Path path, final BasicFileAttributes attribs) { + + try { + if (PatternMatcher.getNamePattern(namePattern, listOfFiles, path) + && PatternMatcher.getMimePattern(mimePattern, listOfFiles, path)) { + listOfFiles.add(path); + } + } catch (final IOException ioe) { + throw (RuntimeException) new RuntimeException().initCause(ioe); + } + + return FileVisitResult.CONTINUE; + } + + }); + break; + case DIRECRTORIES_FILES: + Files.walkFileTree(startPath, options, maxValue, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFileFailed(final Path path, final IOException ioe) { + return FileVisitResult.SKIP_SUBTREE; + } + + @Override + public FileVisitResult preVisitDirectory(final Path path, final BasicFileAttributes attribs) { + + try { + if (PatternMatcher.getNamePattern(namePattern, listOfFiles, path)) { + listOfFiles.add(path); + } + } catch (final IOException ioe) { + throw (RuntimeException) new RuntimeException().initCause(ioe); + } + + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFile(final Path path, final BasicFileAttributes attribs) { + + try { + if (PatternMatcher.getNamePattern(namePattern, listOfFiles, path) + && PatternMatcher.getMimePattern(mimePattern, listOfFiles, path)) { + listOfFiles.add(path); + } + } catch (final IOException ioe) { + throw (RuntimeException) new RuntimeException().initCause(ioe); + } + + return FileVisitResult.CONTINUE; + } + }); + break; + } + + } catch (final IOException ioe) { + throw new DirectoryManagerException(ioe.getMessage(), ioe); + } + + return listOfFiles; + } + + /** + * Liefert Liste von Files und Verzeichnissen als ArrayList + * + * @param startPath + * Path Startpunkt (Verzeichnis) der rekursiven Suche als java.nio.file.Path + * @param mimePattern + * String Filter-Pattern. Nur Namen von Files, die zum Pattern passen, werden genommen, null oder empty liefert + * Exception! + * @return ArrayList Liste von File-Objeten + * @throws AbstractFileLibException + * DirectoryManagerException + */ + public static List getPathListRecursivlyFromPath(final Path startPath, final String mimePattern) + throws AbstractFileLibException { + + return DirectoryManager.getPathListFromPath(startPath, FIND_ALL_PATTERN, mimePattern, DirectoryFilesSwitch.FILES, true); + } + + public static List getPathListNoneRecursivlyFromPath(final Path startPath, final String mimePattern) + throws AbstractFileLibException { + + return DirectoryManager.getPathListFromPath(startPath, FIND_ALL_PATTERN, mimePattern, DirectoryFilesSwitch.FILES, false); + } + + /** + * Liefert Liste von Files und Verzeichnissen als ArrayList + * + * @param startPath + * Path Startpunkt (Verzeichnis) der rekursiven Suche als java.nio.file.Path + * @param mimePattern + * String Filter-Pattern. Nur Namen von Files, die zum Pattern passen, werden genommen, null oder empty liefert + * Exception! + * @return ArrayList Liste von File-Objeten + * @throws AbstractFileLibException + * DirectoryManagerException + */ + public static List getPathListRecursivlyFromPath(final Path startPath, final DirectoryFilesSwitch directoryFilesSwitch, + final String mimePattern) throws AbstractFileLibException { + + return DirectoryManager.getPathListFromPath(startPath, FIND_ALL_PATTERN, mimePattern, directoryFilesSwitch, true); + } + + public static List getPathListNoneRecursivlyFromPath(final Path startPath, + final DirectoryFilesSwitch directoryFilesSwitch, final String mimePattern) throws AbstractFileLibException { + + return DirectoryManager.getPathListFromPath(startPath, FIND_ALL_PATTERN, mimePattern, directoryFilesSwitch, false); + } + + public static List getPathListRecursivlyFromPath(final Path startPath, final DirectoryFilesSwitch directoryFilesSwitch) + throws AbstractFileLibException { + return DirectoryManager.getPathListFromPath(startPath, FIND_ALL_PATTERN, FIND_ALL_PATTERN, directoryFilesSwitch, true); + } + + public static List getPathListNoneRecursivlyFromPath(final Path startPath, final DirectoryFilesSwitch directoryFilesSwitch) + throws AbstractFileLibException { + return DirectoryManager.getPathListFromPath(startPath, FIND_ALL_PATTERN, FIND_ALL_PATTERN, directoryFilesSwitch, false); + } + + /** + * Liefert Liste von Files und Verzeichnissen als ArrayList + * + * @param startPath + * Path Startpunkt (Verzeichnis) der rekursiven Suche als java.nio.file.Path + * @param mimePattern + * String Filter-Pattern. Nur Namen von Files, die zum Pattern passen, werden genommen, null oder empty liefert + * Exception! + * @return ArrayList Liste von File-Objeten + * @throws AbstractFileLibException + * DirectoryManagerException + */ + public static List getFilteredPathListRecursivlyFromPath(final Path startPath, final String namePattern, + final String mimePattern) throws AbstractFileLibException { + + return DirectoryManager.getPathListFromPath(startPath, namePattern, mimePattern, DirectoryFilesSwitch.FILES, true); + } + + public static List getFilteredPathListNoneRecursivlyFromPath(final Path startPath, final String namePattern, + final String mimePattern) throws AbstractFileLibException { + + return DirectoryManager.getPathListFromPath(startPath, namePattern, mimePattern, DirectoryFilesSwitch.FILES, false); + } + + /** + * Liefert Liste von Files und Verzeichnissen als ArrayList + * + * @param startPath + * Path Startpunkt (Verzeichnis) der rekursiven Suche als java.nio.file.Path + * @param mimePattern + * String Filter-Pattern. Nur Namen von Files, die zum Pattern passen, werden genommen, null oder empty liefert + * Exception! + * @return ArrayList Liste von File-Objeten + * @throws AbstractFileLibException + * DirectoryManagerException + */ + public static List getFilteredPathListRecursivlyFromPath(final Path startPath, + final DirectoryFilesSwitch directoryFilesSwitch, final String namePattern, final String mimePattern) + throws AbstractFileLibException { + + return DirectoryManager.getPathListFromPath(startPath, namePattern, mimePattern, directoryFilesSwitch, true); + } + + public static List getFilteredPathListNoneRecursivlyFromPath(final Path startPath, + final DirectoryFilesSwitch directoryFilesSwitch, final String namePattern, final String mimePattern) + throws AbstractFileLibException { + + return DirectoryManager.getPathListFromPath(startPath, namePattern, mimePattern, directoryFilesSwitch, false); + } + + public static List getFilteredPathListRecursivlyFromPath(final Path startPath, + final DirectoryFilesSwitch directoryFilesSwitch, final String namePattern) throws AbstractFileLibException { + return DirectoryManager.getPathListFromPath(startPath, namePattern, FIND_ALL_PATTERN, directoryFilesSwitch, true); + } + + public static List getFilteredPathListNoneRecursivlyFromPath(final Path startPath, + final DirectoryFilesSwitch directoryFilesSwitch, final String namePattern) throws AbstractFileLibException { + return DirectoryManager.getPathListFromPath(startPath, namePattern, FIND_ALL_PATTERN, directoryFilesSwitch, false); + } + + public List getFilesFromPathes(List pathes) { + + List files = new ArrayList(); + for (Path path : pathes) { + files.add(path.toFile()); + } + return files; + } + + public List getFileNamesFromPathes(List pathes) throws AbstractFileLibException { + + List files = new ArrayList(); + try { + for (Path path : pathes) { + files.add(path.toFile().getCanonicalPath()); + } + } catch (IOException ioe) { + throw new DirectoryManagerException(ioe.getMessage(), ioe); + } + return files; + } + +} diff --git a/src/main/java/de/laktatnebel/libs/filelib/FileReader.java b/src/main/java/de/laktatnebel/libs/filelib/FileReader.java new file mode 100644 index 0000000..9958b2b --- /dev/null +++ b/src/main/java/de/laktatnebel/libs/filelib/FileReader.java @@ -0,0 +1,67 @@ +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()); + } +} diff --git a/src/main/java/de/laktatnebel/libs/filelib/exception/AbstractFileLibException.java b/src/main/java/de/laktatnebel/libs/filelib/exception/AbstractFileLibException.java new file mode 100644 index 0000000..0fb98b2 --- /dev/null +++ b/src/main/java/de/laktatnebel/libs/filelib/exception/AbstractFileLibException.java @@ -0,0 +1,32 @@ +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); + } + +} diff --git a/src/main/java/de/laktatnebel/libs/filelib/exception/DirectoryManagerException.java b/src/main/java/de/laktatnebel/libs/filelib/exception/DirectoryManagerException.java new file mode 100644 index 0000000..0496a4b --- /dev/null +++ b/src/main/java/de/laktatnebel/libs/filelib/exception/DirectoryManagerException.java @@ -0,0 +1,30 @@ +package de.laktatnebel.libs.filelib.exception; + +public class DirectoryManagerException extends AbstractFileLibException { + + /** long serialVersionUID field */ + private static final long serialVersionUID = 2923278519379485647L; + + /** + * @param t + */ + public DirectoryManagerException(final Throwable t) { + super(t); + } + + /** + * @param message + * @param t + */ + public DirectoryManagerException(final String message, final Throwable t) { + super(message, t); + } + + /** + * @param message + */ + public DirectoryManagerException(final String message) { + super(message); + } + +} diff --git a/src/main/java/de/laktatnebel/libs/filelib/exception/FileReaderException.java b/src/main/java/de/laktatnebel/libs/filelib/exception/FileReaderException.java new file mode 100644 index 0000000..ff30c3e --- /dev/null +++ b/src/main/java/de/laktatnebel/libs/filelib/exception/FileReaderException.java @@ -0,0 +1,30 @@ +package de.laktatnebel.libs.filelib.exception; + +public class FileReaderException extends AbstractFileLibException { + + /** long serialVersionUID field */ + private static final long serialVersionUID = 2923278519379485647L; + + /** + * @param t + */ + public FileReaderException(final Throwable t) { + super(t); + } + + /** + * @param message + * @param t + */ + public FileReaderException(final String message, final Throwable t) { + super(message, t); + } + + /** + * @param message + */ + public FileReaderException(final String message) { + super(message); + } + +} diff --git a/src/test/java/de/laktatnebel/libs/filelib/DirectoryManagerPatternMatcherTest.java b/src/test/java/de/laktatnebel/libs/filelib/DirectoryManagerPatternMatcherTest.java new file mode 100644 index 0000000..6a58c63 --- /dev/null +++ b/src/test/java/de/laktatnebel/libs/filelib/DirectoryManagerPatternMatcherTest.java @@ -0,0 +1,64 @@ +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 expectedPathes; + + @Before + public void prepare() { + expectedPathes = new ArrayList(); + } + + @Test + public void testPattern() { + DirectoryManager dm = new DirectoryManager(); + String namePattern = null; + List 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 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()); + } + } +} diff --git a/src/test/java/de/laktatnebel/libs/filelib/DirectoryManagerTest.java b/src/test/java/de/laktatnebel/libs/filelib/DirectoryManagerTest.java new file mode 100644 index 0000000..33a1bbb --- /dev/null +++ b/src/test/java/de/laktatnebel/libs/filelib/DirectoryManagerTest.java @@ -0,0 +1,196 @@ +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 expectedPathes; + + @Before + public void prepare() { + expectedPathes = new ArrayList(); + } + + @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 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 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 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 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 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()); + } + } +} diff --git a/src/test/java/de/laktatnebel/libs/filelib/FileReaderTest.java b/src/test/java/de/laktatnebel/libs/filelib/FileReaderTest.java new file mode 100644 index 0000000..db3a26c --- /dev/null +++ b/src/test/java/de/laktatnebel/libs/filelib/FileReaderTest.java @@ -0,0 +1,156 @@ +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(); + } + } +} diff --git a/src/test/resources/filereadertestfile.dat b/src/test/resources/filereadertestfile.dat new file mode 100644 index 0000000..441e2a4 --- /dev/null +++ b/src/test/resources/filereadertestfile.dat @@ -0,0 +1 @@ +Ich bin ein Testfile! \ No newline at end of file diff --git a/src/test/resources/log4j.xml b/src/test/resources/log4j.xml new file mode 100644 index 0000000..f04bbed --- /dev/null +++ b/src/test/resources/log4j.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/subdir/1a/2a/3a/file100.dat b/src/test/resources/subdir/1a/2a/3a/file100.dat new file mode 100644 index 0000000..2e65efe --- /dev/null +++ b/src/test/resources/subdir/1a/2a/3a/file100.dat @@ -0,0 +1 @@ +a \ No newline at end of file diff --git a/src/test/resources/subdir/1a/2a/3a/file101.dat b/src/test/resources/subdir/1a/2a/3a/file101.dat new file mode 100644 index 0000000..2e65efe --- /dev/null +++ b/src/test/resources/subdir/1a/2a/3a/file101.dat @@ -0,0 +1 @@ +a \ No newline at end of file diff --git a/src/test/resources/subdir/1a/2a/file10.dat b/src/test/resources/subdir/1a/2a/file10.dat new file mode 100644 index 0000000..2e65efe --- /dev/null +++ b/src/test/resources/subdir/1a/2a/file10.dat @@ -0,0 +1 @@ +a \ No newline at end of file diff --git a/src/test/resources/subdir/1a/2a/file11.dat b/src/test/resources/subdir/1a/2a/file11.dat new file mode 100644 index 0000000..2e65efe --- /dev/null +++ b/src/test/resources/subdir/1a/2a/file11.dat @@ -0,0 +1 @@ +a \ No newline at end of file diff --git a/src/test/resources/subdir/1a/2a/file12.dat b/src/test/resources/subdir/1a/2a/file12.dat new file mode 100644 index 0000000..2e65efe --- /dev/null +++ b/src/test/resources/subdir/1a/2a/file12.dat @@ -0,0 +1 @@ +a \ No newline at end of file diff --git a/src/test/resources/subdir/1a/2a/file13.dat b/src/test/resources/subdir/1a/2a/file13.dat new file mode 100644 index 0000000..2e65efe --- /dev/null +++ b/src/test/resources/subdir/1a/2a/file13.dat @@ -0,0 +1 @@ +a \ No newline at end of file diff --git a/src/test/resources/subdir/1a/2b/file20.dat b/src/test/resources/subdir/1a/2b/file20.dat new file mode 100644 index 0000000..2e65efe --- /dev/null +++ b/src/test/resources/subdir/1a/2b/file20.dat @@ -0,0 +1 @@ +a \ No newline at end of file diff --git a/src/test/resources/subdir/1a/2b/file21.dat b/src/test/resources/subdir/1a/2b/file21.dat new file mode 100644 index 0000000..2e65efe --- /dev/null +++ b/src/test/resources/subdir/1a/2b/file21.dat @@ -0,0 +1 @@ +a \ No newline at end of file diff --git a/src/test/resources/subdir/1a/file1.dat b/src/test/resources/subdir/1a/file1.dat new file mode 100644 index 0000000..2e65efe --- /dev/null +++ b/src/test/resources/subdir/1a/file1.dat @@ -0,0 +1 @@ +a \ No newline at end of file diff --git a/src/test/resources/subdir/1a/file2.dat b/src/test/resources/subdir/1a/file2.dat new file mode 100644 index 0000000..2e65efe --- /dev/null +++ b/src/test/resources/subdir/1a/file2.dat @@ -0,0 +1 @@ +a \ No newline at end of file diff --git a/src/test/resources/subdir/1a/file3.dat b/src/test/resources/subdir/1a/file3.dat new file mode 100644 index 0000000..2e65efe --- /dev/null +++ b/src/test/resources/subdir/1a/file3.dat @@ -0,0 +1 @@ +a \ No newline at end of file diff --git a/src/test/resources/subdir/1b/file1.dat b/src/test/resources/subdir/1b/file1.dat new file mode 100644 index 0000000..2e65efe --- /dev/null +++ b/src/test/resources/subdir/1b/file1.dat @@ -0,0 +1 @@ +a \ No newline at end of file diff --git a/src/test/resources/subdir/1b/img1.jpg b/src/test/resources/subdir/1b/img1.jpg new file mode 100755 index 0000000000000000000000000000000000000000..9f6c523fc3315be2cba573bfdc72bbdaacc1fe0e GIT binary patch literal 1538 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