From 53d5c386b72bbbe5eff1fac3be9c8a504bb42d27 Mon Sep 17 00:00:00 2001 From: "Ole B. Rosentreter" Date: Fri, 27 Sep 2024 17:15:45 +0200 Subject: [PATCH 1/1] Initial project version --- .gitignore | 5 + pom.xml | 44 ++++ .../AbstractCollectionConsoleOutput.java | 27 +++ .../CollectionConsoleOutputList.java | 46 ++++ .../CollectionConsoleOutputMap.java | 47 ++++ .../CollectionConsoleOutputSet.java | 41 ++++ .../collection/CollectionOutputList.java | 86 +++++++ .../collection/CollectionOutputMap.java | 51 ++++ .../collection/CollectionOutputSet.java | 57 +++++ .../utillib/collection/CollectionSort.java | 50 ++++ .../libs/utillib/collection/ListComp.java | 169 +++++++++++++ .../libs/utillib/constants/NullVal.java | 11 + .../exception/AbstractUtilLibException.java | 36 +++ .../exception/CollectionOutputException.java | 36 +++ .../utillib/exception/ListCompException.java | 28 +++ .../libs/utillib/logging/Loglevel.java | 34 +++ .../libs/utillib/logging/StaticLogger.java | 46 ++++ .../AbstractCollectionOutputTest.java | 222 ++++++++++++++++++ .../CollectionConsoleOutputListTest.java | 69 ++++++ .../CollectionConsoleOutputMapTest.java | 56 +++++ .../CollectionConsoleOutputSetTest.java | 69 ++++++ .../collection/CollectionOutputListTest.java | 89 +++++++ .../collection/CollectionOutputMapTest.java | 105 +++++++++ .../collection/CollectionOutputSetTest.java | 93 ++++++++ .../collection/CollectionSortTest.java | 110 +++++++++ .../libs/utillib/collection/ListCompTest.java | 214 +++++++++++++++++ src/test/resources/log4j.xml | 17 ++ 27 files changed, 1858 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/de/laktatnebel/libs/utillib/collection/AbstractCollectionConsoleOutput.java create mode 100644 src/main/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputList.java create mode 100644 src/main/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputMap.java create mode 100644 src/main/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputSet.java create mode 100644 src/main/java/de/laktatnebel/libs/utillib/collection/CollectionOutputList.java create mode 100644 src/main/java/de/laktatnebel/libs/utillib/collection/CollectionOutputMap.java create mode 100644 src/main/java/de/laktatnebel/libs/utillib/collection/CollectionOutputSet.java create mode 100755 src/main/java/de/laktatnebel/libs/utillib/collection/CollectionSort.java create mode 100644 src/main/java/de/laktatnebel/libs/utillib/collection/ListComp.java create mode 100644 src/main/java/de/laktatnebel/libs/utillib/constants/NullVal.java create mode 100644 src/main/java/de/laktatnebel/libs/utillib/exception/AbstractUtilLibException.java create mode 100644 src/main/java/de/laktatnebel/libs/utillib/exception/CollectionOutputException.java create mode 100644 src/main/java/de/laktatnebel/libs/utillib/exception/ListCompException.java create mode 100644 src/main/java/de/laktatnebel/libs/utillib/logging/Loglevel.java create mode 100644 src/main/java/de/laktatnebel/libs/utillib/logging/StaticLogger.java create mode 100644 src/test/java/de/laktatnebel/libs/utillib/collection/AbstractCollectionOutputTest.java create mode 100644 src/test/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputListTest.java create mode 100644 src/test/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputMapTest.java create mode 100644 src/test/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputSetTest.java create mode 100644 src/test/java/de/laktatnebel/libs/utillib/collection/CollectionOutputListTest.java create mode 100644 src/test/java/de/laktatnebel/libs/utillib/collection/CollectionOutputMapTest.java create mode 100644 src/test/java/de/laktatnebel/libs/utillib/collection/CollectionOutputSetTest.java create mode 100755 src/test/java/de/laktatnebel/libs/utillib/collection/CollectionSortTest.java create mode 100755 src/test/java/de/laktatnebel/libs/utillib/collection/ListCompTest.java create mode 100644 src/test/resources/log4j.xml 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..75b7244 --- /dev/null +++ b/pom.xml @@ -0,0 +1,44 @@ + + 4.0.0 + de.laktatnebel.libs + utillib + 2.1.1-SNAPSHOT + + UtilLib + + + de.laktatnebel.maven + laktatnebellib + 2.1.7 + + + + 1.6.4 + + + + + + org.slf4j + slf4j-api + ${slf4j.version} + + + org.slf4j + slf4j-log4j12 + ${slf4j.version} + + + + + + + org.slf4j + slf4j-api + + + org.slf4j + slf4j-log4j12 + + + diff --git a/src/main/java/de/laktatnebel/libs/utillib/collection/AbstractCollectionConsoleOutput.java b/src/main/java/de/laktatnebel/libs/utillib/collection/AbstractCollectionConsoleOutput.java new file mode 100644 index 0000000..ca88dc2 --- /dev/null +++ b/src/main/java/de/laktatnebel/libs/utillib/collection/AbstractCollectionConsoleOutput.java @@ -0,0 +1,27 @@ +package de.laktatnebel.libs.utillib.collection; + +import org.slf4j.Logger; + +import de.laktatnebel.libs.utillib.logging.StaticLogger; + +/** +* @author oleb + * + */ +public abstract class AbstractCollectionConsoleOutput { + + /** + * + * @param array Object[] Object-Array + */ + protected static void getArrayLoop(Object[] array) { + Logger logger = StaticLogger.getLogger(CollectionConsoleOutputList.class); + + for (Object obj : array) { + if (obj != null) { + StaticLogger.log(logger, obj.toString()); + } + } + } + +} diff --git a/src/main/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputList.java b/src/main/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputList.java new file mode 100644 index 0000000..dca3b79 --- /dev/null +++ b/src/main/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputList.java @@ -0,0 +1,46 @@ +package de.laktatnebel.libs.utillib.collection; + +import java.util.List; + +import de.laktatnebel.libs.utillib.exception.CollectionOutputException; + +/** + * + * @author oleb + */ +public class CollectionConsoleOutputList extends + AbstractCollectionConsoleOutput { + + + /** + * @param list List + * @throws CollectionOutputException + * void + */ + public static void getStringListToConsole(final List list) + throws CollectionOutputException { + if (list == null) { + throw new CollectionOutputException("Parameter \"list\" null!"); + } + + final Object[] array = list.toArray(); + + AbstractCollectionConsoleOutput.getArrayLoop(array); + } + + + /** + * @param list List list + * @throws CollectionOutputException + */ + public static void getListToConsole(final List list) + throws CollectionOutputException { + if (list == null) { + throw new CollectionOutputException("Parameter \"list\" null!"); + } + + final Object[] array = list.toArray(); + + AbstractCollectionConsoleOutput.getArrayLoop(array); + } +} diff --git a/src/main/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputMap.java b/src/main/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputMap.java new file mode 100644 index 0000000..ca37fb7 --- /dev/null +++ b/src/main/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputMap.java @@ -0,0 +1,47 @@ +package de.laktatnebel.libs.utillib.collection; + +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.slf4j.Logger; + +import de.laktatnebel.libs.utillib.constants.NullVal; +import de.laktatnebel.libs.utillib.exception.CollectionOutputException; +import de.laktatnebel.libs.utillib.logging.StaticLogger; + +/** + * +* @author oleb + */ +public class CollectionConsoleOutputMap extends AbstractCollectionConsoleOutput { + + /** + * @param map Map + * @param keysOnly boolean + * @param delim String + * @throws CollectionOutputException CollectionOutputException + */ + public static void getMapToStringToConsole(final Map map, final boolean keysOnly, final String delim) throws CollectionOutputException { + if (map == null) { + throw new CollectionOutputException("Parameter \"map\" null!"); + } + + if (keysOnly) { + + CollectionConsoleOutputSet.getSetToConsole(map.keySet()); + } else { + final Set entrySet = map.entrySet(); + + final Logger logger = StaticLogger.getLogger(CollectionConsoleOutputMap.class); + + for (final Object entryObj : entrySet) { + + Entry entry = (Entry) entryObj; + final Object key = entry.getKey(); + final Object value = entry.getValue(); + StaticLogger.log(logger, (key == null ? NullVal.STRING : key.toString()) + (delim == null ? NullVal.STRING : delim) + (value == null ? NullVal.STRING : value.toString())); + } + } + } +} diff --git a/src/main/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputSet.java b/src/main/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputSet.java new file mode 100644 index 0000000..d500eea --- /dev/null +++ b/src/main/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputSet.java @@ -0,0 +1,41 @@ +package de.laktatnebel.libs.utillib.collection; + +import java.util.Set; + +import de.laktatnebel.libs.utillib.exception.CollectionOutputException; + +/** + * +* @author oleb + */ +public class CollectionConsoleOutputSet extends AbstractCollectionConsoleOutput { + + /** + * + * @param set Set + * @throws CollectionOutputException CollectionOutputException + */ + public static void getStringSetToConsole(final Set set) throws CollectionOutputException { + if (set == null) { + throw new CollectionOutputException("Parameter \"set\" null!"); + } + + final Object[] array = set.toArray(); + + AbstractCollectionConsoleOutput.getArrayLoop(array); + } + + /** + * + * @param set Set + * @throws CollectionOutputException CollectionOutputException + */ + public static void getSetToConsole(final Set set) throws CollectionOutputException { + if (set == null) { + throw new CollectionOutputException("Parameter \"set\" null!"); + } + final Object[] array = set.toArray(); + + AbstractCollectionConsoleOutput.getArrayLoop(array); + } +} diff --git a/src/main/java/de/laktatnebel/libs/utillib/collection/CollectionOutputList.java b/src/main/java/de/laktatnebel/libs/utillib/collection/CollectionOutputList.java new file mode 100644 index 0000000..42d1ef7 --- /dev/null +++ b/src/main/java/de/laktatnebel/libs/utillib/collection/CollectionOutputList.java @@ -0,0 +1,86 @@ +package de.laktatnebel.libs.utillib.collection; + +import java.util.Iterator; +import java.util.List; + +import de.laktatnebel.libs.utillib.exception.CollectionOutputException; + +/** + * +* @author oleb + */ +public class CollectionOutputList { + + /** + * +* @param list List + * @return StringBuffer + * @throws CollectionOutputException CollectionOutputException + */ + public static StringBuffer getStringListToString(List list) throws CollectionOutputException { + if (list == null) { + throw new CollectionOutputException("Parameter \"list\" null!"); + } + + StringBuffer sb = new StringBuffer(); + + for (String string : list) { + sb.append(string); + sb.append("\n"); + } + + return sb; + } + + /** + * +* @param list List + * @return StringBuffer + * @throws CollectionOutputException CollectionOutputException + */ + public static StringBuffer getListToString(List list) throws CollectionOutputException { + if (list == null) { + throw new CollectionOutputException("Parameter \"list\" null!"); + } + + StringBuffer sb = new StringBuffer(); + + for (Object obj : list) { + if (obj != null) { + sb.append(obj.toString()); + } + sb.append("\n"); + } + + return sb; + } + + /** + * @param stringList + * @return + * String + */ + public static String getStringListFromList(final List stringList) { + return getStringListFromList(stringList, " "); + } + + /** + * @param stringList + * @param seperatorString + * @return + * String + */ + public static String getStringListFromList(final List stringList, String seperatorString) { + if (stringList == null || stringList.isEmpty()) { + return null; + } + + Iterator iterator = stringList.iterator(); + String returnString = ""; + while (iterator.hasNext()) { + returnString += iterator.next() + seperatorString; + } + + return returnString; + } +} diff --git a/src/main/java/de/laktatnebel/libs/utillib/collection/CollectionOutputMap.java b/src/main/java/de/laktatnebel/libs/utillib/collection/CollectionOutputMap.java new file mode 100644 index 0000000..bd1e73b --- /dev/null +++ b/src/main/java/de/laktatnebel/libs/utillib/collection/CollectionOutputMap.java @@ -0,0 +1,51 @@ + +package de.laktatnebel.libs.utillib.collection; + +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import de.laktatnebel.libs.utillib.constants.NullVal; +import de.laktatnebel.libs.utillib.exception.CollectionOutputException; + +/** + * +* @author oleb + */ +public class CollectionOutputMap { + + /** + * @param map Map + * @param keysOnly boolean + * @param delim String + * @return StringBuffer + * @throws CollectionOutputException CollectionOutputException + */ + public static StringBuffer getMapToString(Map map, boolean keysOnly, String delim) throws CollectionOutputException { + if (map == null) { + throw new CollectionOutputException("Parameter \"map\" null!"); + } + + StringBuffer sb = new StringBuffer(); + + if (keysOnly) { + + return CollectionOutputSet.getSetToString(map.keySet()); + } else { + Set entrySet = map.entrySet(); + + for (Object entryObj : entrySet) { + + Entry entry = (Entry) entryObj; + + sb.append(entry.getKey()); + sb.append(delim == null ? NullVal.STRING : delim); + sb.append(entry.getValue()); + + sb.append("\n"); + } + } + + return sb; + } +} diff --git a/src/main/java/de/laktatnebel/libs/utillib/collection/CollectionOutputSet.java b/src/main/java/de/laktatnebel/libs/utillib/collection/CollectionOutputSet.java new file mode 100644 index 0000000..26afe50 --- /dev/null +++ b/src/main/java/de/laktatnebel/libs/utillib/collection/CollectionOutputSet.java @@ -0,0 +1,57 @@ + +package de.laktatnebel.libs.utillib.collection; + +import java.util.Set; + +import de.laktatnebel.libs.utillib.exception.CollectionOutputException; + +/** + * +* @author oleb + */ +public class CollectionOutputSet { + + /** + * + * @param set Set set + * @return StringBuffer + * @throws CollectionOutputException CollectionOutputException + */ + public static StringBuffer getStringSetToString(Set set) throws CollectionOutputException { + if (set == null) { + throw new CollectionOutputException("Parameter \"set\" null!"); + } + + StringBuffer sb = new StringBuffer(); + + for (String string : set) { + sb.append(string); + sb.append("\n"); + } + + return sb; + } + + /** + * + * @param set Set set + * @return StringBuffer + * @throws CollectionOutputException CollectionOutputException + */ + public static StringBuffer getSetToString(Set set) throws CollectionOutputException { + if (set == null) { + throw new CollectionOutputException("Parameter \"set\" null!"); + } + + StringBuffer sb = new StringBuffer(); + + for (Object obj : set) { + if (obj != null) { + sb.append(obj.toString()); + } + sb.append("\n"); + } + + return sb; + } +} diff --git a/src/main/java/de/laktatnebel/libs/utillib/collection/CollectionSort.java b/src/main/java/de/laktatnebel/libs/utillib/collection/CollectionSort.java new file mode 100755 index 0000000..3bdecd9 --- /dev/null +++ b/src/main/java/de/laktatnebel/libs/utillib/collection/CollectionSort.java @@ -0,0 +1,50 @@ +package de.laktatnebel.libs.utillib.collection; + +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; + +import de.laktatnebel.libs.utillib.exception.CollectionOutputException; + +/** + * @author oleb + * + */ +public class CollectionSort { + + /** + * @param + * @param + * @param map + * @return + * @throws CollectionOutputException + */ + public static Map mapSort(Map map) throws CollectionOutputException { + + if (map.containsKey(null)) { + throw new CollectionOutputException("Ungleiche Typen in KeySet von Parameter \"map\"!"); + } + + Map sortedMap = new TreeMap(map); + + return sortedMap; + } + + + /** + * @param set + * @return + * @throws CollectionOutputException + */ + public static Set setSort(Set set) throws CollectionOutputException { + + if (set.contains(null)) { + throw new CollectionOutputException("Ungleiche Typen im Set von Parameter \"set\"!"); + } + + Set sortedSet = new TreeSet(set); + + return sortedSet; + } +} diff --git a/src/main/java/de/laktatnebel/libs/utillib/collection/ListComp.java b/src/main/java/de/laktatnebel/libs/utillib/collection/ListComp.java new file mode 100644 index 0000000..55184fc --- /dev/null +++ b/src/main/java/de/laktatnebel/libs/utillib/collection/ListComp.java @@ -0,0 +1,169 @@ +package de.laktatnebel.libs.utillib.collection; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.List; + +import de.laktatnebel.libs.utillib.exception.AbstractUtilLibException; +import de.laktatnebel.libs.utillib.exception.ListCompException; +import de.laktatnebel.libs.utillib.logging.StaticLogger; + +/** + * @author oleb + * + */ +public class ListComp { + + /** + * @author oleb + * + */ + enum TypeOfSet { + IN_FIRST_ONLY, IN_LAST_ONLY, IN_BOTH + } + + /** + * + * @param args String[] + * @throws Exception + */ + public static void main(String[] args) throws Exception { + + StaticLogger.getLogger(ListComp.class).info("START"); + + String fileOne = args[0]; + String fileTwo = args[1]; + String fileBoth = args[0]+args[1]; + + if (fileOne == null || fileTwo == null) { + throw new ListCompException("Es fehlen Eingebaparameter!"); + } + + StaticLogger.getLogger(ListComp.class).info("Liste 1:\t" + fileOne); + StaticLogger.getLogger(ListComp.class).info("Liste 2:\t" + fileTwo); + + ArrayList listOne = readFile(fileOne); + ArrayList listTwo = readFile(fileTwo); + + StringBuffer onlyInListOne = getStringBufferFromCompare(listOne, + listTwo, TypeOfSet.IN_FIRST_ONLY); + + StringBuffer onlyInListTwo = getStringBufferFromCompare(listOne, + listTwo, TypeOfSet.IN_LAST_ONLY); + + StringBuffer inBothLists = getStringBufferFromCompare(listOne, + listTwo, TypeOfSet.IN_BOTH); + + + writeFile(onlyInListOne, fileOne); + writeFile(onlyInListTwo, fileTwo); + writeFile(inBothLists, fileBoth); + + } + + /** + * @param listOne ArrayList + * @param listTwo ArrayList + * @param typeOfSet typeOfSet + * @return StringBuffer + * @throws AbstractUtilLibException AbstractUtilLibException + */ + @SuppressWarnings("unchecked") + private static StringBuffer getStringBufferFromCompare( + ArrayList listOne, ArrayList listTwo, TypeOfSet typeOfSet) + throws AbstractUtilLibException { + List onlyInListOne = new ArrayList(); + onlyInListOne = (List) compare(listOne, listTwo, typeOfSet); + StringBuffer listToString = CollectionOutputList.getListToString(onlyInListOne); + return listToString; + } + + /** + * @param sb StringBuffer + * @param fileName String + * @throws ListCompException + */ + private static void writeFile(StringBuffer sb, String fileName) + throws ListCompException { + + PrintWriter printWriter = null; + String deltaFileName = fileName + ".delta"; + try { + printWriter = new PrintWriter(new FileWriter(deltaFileName)); + + String string = sb.toString(); + printWriter.print(string.substring(0, string.length() - 1)); + } catch (IOException ioe) { + throw new ListCompException("Fehler beim Schreiben von \"" + + deltaFileName + "\"", ioe); + } finally { + if (printWriter != null) { + printWriter.close(); + } + } + StaticLogger.getLogger(ListComp.class).info( + "File \"" + deltaFileName + "\" geschrieben."); + } + + /** + * + * @param fileName String + * @throws ListCompException + */ + private static ArrayList readFile(String fileName) + throws ListCompException { + ArrayList lines = new ArrayList(); + BufferedReader in = null; + try { + in = new BufferedReader(new FileReader(fileName)); + String zeile = null; + while ((zeile = in.readLine()) != null) { + lines.add(zeile.trim()); + } + } catch (IOException ioe) { + throw new ListCompException("Fehler beim Einlesen von \"" + + fileName + "\"", ioe); + } finally { + try { + in.close(); + } catch (IOException ioe) { + throw new ListCompException("Fehler beim Schliessen von \"" + + fileName + "\"", ioe); + } + } + StaticLogger.getLogger(ListComp.class).info( + lines.size() + " gelesene Zeilen in File \"" + fileName + "\""); + return lines; + } + + /** + * @param first + * @param last + * @param typeOfSet + * @return + */ + public static List compare(List first, List last, TypeOfSet typeOfSet) { + + List same = new ArrayList(); + + for (Object element : first) { + if (last.contains(element)) { + same.add(element); + } + } + + if (typeOfSet.equals(ListComp.TypeOfSet.IN_FIRST_ONLY)) { + first.removeAll(same); + return (List) first; + } else if (typeOfSet.equals(ListComp.TypeOfSet.IN_LAST_ONLY)) { + last.removeAll(same); + return (List) last; + } + + return same; + } +} diff --git a/src/main/java/de/laktatnebel/libs/utillib/constants/NullVal.java b/src/main/java/de/laktatnebel/libs/utillib/constants/NullVal.java new file mode 100644 index 0000000..090c172 --- /dev/null +++ b/src/main/java/de/laktatnebel/libs/utillib/constants/NullVal.java @@ -0,0 +1,11 @@ +package de.laktatnebel.libs.utillib.constants; + +/** +* @author oleb + * + */ +public final class NullVal { + + public static final String STRING = ""; + +} diff --git a/src/main/java/de/laktatnebel/libs/utillib/exception/AbstractUtilLibException.java b/src/main/java/de/laktatnebel/libs/utillib/exception/AbstractUtilLibException.java new file mode 100644 index 0000000..3ec732c --- /dev/null +++ b/src/main/java/de/laktatnebel/libs/utillib/exception/AbstractUtilLibException.java @@ -0,0 +1,36 @@ +package de.laktatnebel.libs.utillib.exception; + +/** +* @author oleb + * + */ +public abstract class AbstractUtilLibException extends Exception { + + /** + * + */ + private static final long serialVersionUID = -4226434342611199020L; + + /** + * @param t Throwable + */ + public AbstractUtilLibException(final Throwable t) { + super(t); + } + + /** + * @param message String + * @param t Throwable + */ + public AbstractUtilLibException(final String message, final Throwable t) { + super(message, t); + } + + /** + * @param message String + */ + public AbstractUtilLibException(final String message) { + super(message); + } + +} diff --git a/src/main/java/de/laktatnebel/libs/utillib/exception/CollectionOutputException.java b/src/main/java/de/laktatnebel/libs/utillib/exception/CollectionOutputException.java new file mode 100644 index 0000000..f4042e7 --- /dev/null +++ b/src/main/java/de/laktatnebel/libs/utillib/exception/CollectionOutputException.java @@ -0,0 +1,36 @@ +package de.laktatnebel.libs.utillib.exception; + +import de.laktatnebel.libs.utillib.exception.AbstractUtilLibException; + +/** +* @author oleb + * + */ +public class CollectionOutputException extends AbstractUtilLibException { + + /** long serialVersionUID field */ + private static final long serialVersionUID = 2923278519379485647L; + + /** + * @param t Throwable + */ + public CollectionOutputException(final Throwable t) { + super(t); + } + + /** + * @param message String + * @param t Throwable + */ + public CollectionOutputException(final String message, final Throwable t) { + super(message, t); + } + + /** + * @param message String + */ + public CollectionOutputException(final String message) { + super(message); + } + +} diff --git a/src/main/java/de/laktatnebel/libs/utillib/exception/ListCompException.java b/src/main/java/de/laktatnebel/libs/utillib/exception/ListCompException.java new file mode 100644 index 0000000..5c05bec --- /dev/null +++ b/src/main/java/de/laktatnebel/libs/utillib/exception/ListCompException.java @@ -0,0 +1,28 @@ +package de.laktatnebel.libs.utillib.exception; + + +/** +* @author oleb + * + */ +public class ListCompException extends Exception { + + /** long serialVersionUID field */ + private static final long serialVersionUID = -6086330098400406481L; + + /** + * @param message String + */ + public ListCompException(String message) { + super(message); + } + + /** + * @param message String + * @param t Throwable + */ + public ListCompException(String message, Throwable t) { + super(message, t); + } + +} diff --git a/src/main/java/de/laktatnebel/libs/utillib/logging/Loglevel.java b/src/main/java/de/laktatnebel/libs/utillib/logging/Loglevel.java new file mode 100644 index 0000000..968399a --- /dev/null +++ b/src/main/java/de/laktatnebel/libs/utillib/logging/Loglevel.java @@ -0,0 +1,34 @@ +package de.laktatnebel.libs.utillib.logging; + +/** +* @author oleb + * + */ +public enum Loglevel { + + /** + * + */ + ALL, /** + * + */ + TRACE, /** + * + */ + DEBUG, /** + * + */ + INFO, /** + * + */ + WARN, /** + * + */ + ERROR, /** + * + */ + FATAL, /** + * + */ + OFF; +} diff --git a/src/main/java/de/laktatnebel/libs/utillib/logging/StaticLogger.java b/src/main/java/de/laktatnebel/libs/utillib/logging/StaticLogger.java new file mode 100644 index 0000000..4f53301 --- /dev/null +++ b/src/main/java/de/laktatnebel/libs/utillib/logging/StaticLogger.java @@ -0,0 +1,46 @@ +package de.laktatnebel.libs.utillib.logging; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** +* @author oleb + * + */ +public class StaticLogger { + /** Logger logger field */ + private static Logger logger = LoggerFactory.getLogger(StaticLogger.class); + + { + logger = null; + } + + /** + * @param clazz Class + * @return Logger + */ + public static Logger getLogger(Class clazz) { + if (StaticLogger.logger == null) { + StaticLogger.logger = LoggerFactory.getLogger(clazz); + } + return StaticLogger.logger; + } + /** + * + * @param logger Logger + * @param string String + */ + public static void log(Logger logger, String string) { + if (logger.isTraceEnabled()) { + logger.trace(string); + } else if (logger.isDebugEnabled()) { + logger.debug(string); + } else if (logger.isInfoEnabled()) { + logger.info(string); + } else if (logger.isWarnEnabled()) { + logger.warn(string); + } else if (logger.isErrorEnabled()) { + logger.error(string); + } + } +} diff --git a/src/test/java/de/laktatnebel/libs/utillib/collection/AbstractCollectionOutputTest.java b/src/test/java/de/laktatnebel/libs/utillib/collection/AbstractCollectionOutputTest.java new file mode 100644 index 0000000..f6e05c6 --- /dev/null +++ b/src/test/java/de/laktatnebel/libs/utillib/collection/AbstractCollectionOutputTest.java @@ -0,0 +1,222 @@ +package de.laktatnebel.libs.utillib.collection; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; + +import de.laktatnebel.libs.utillib.logging.StaticLogger; + +/** +* @author oleb + * + */ +public abstract class AbstractCollectionOutputTest { + + protected static final String PARAMETER_MAP_NULL = "Parameter \"map\" null!"; + protected static final String PARAMETER_MAP_KEYS_NOT_EQUAL_TYPE = "Ungleiche Typen in KeySet von Parameter \"map\"!"; + protected static final String PARAMETER_MAP_KEYS_NULL = "Parameter \"map\" has keys == null!"; + protected static final String PARAMETER_SET_NULL = "Parameter \"set\" null!"; + protected static final String PARAMETER_SET_KEYS_NOT_EQUAL_TYPE = "Ungleiche Typen im Set von Parameter \"set\"!"; + protected static final String PARAMETER_LIST_NULL = "Parameter \"list\" null!"; + protected static final String JAVA_LANG_STRING = "java.lang.String"; + protected static final String MUST_FAIL = "Must fail!"; + protected static final String MUST_NOT_FAIL = "Must not fail!"; + + protected static List testObjectList = new ArrayList(); + protected static Map testObjectMap = new HashMap(); + protected static Map testObjectMapFloat = new HashMap(); + protected static Map testObjectMapString = new HashMap(); + protected static Set testObjectSet = new HashSet(); + protected static Set testObjectSetFloat = new HashSet(); + protected static List testStringList = new ArrayList(); + protected static Set testStringSet = new HashSet(); + + @BeforeClass + public static void fillData() { + testStringList.add("A"); + testStringList.add("B"); + testStringList.add("D"); + testStringList.add("H"); + testStringList.add(new String()); + testStringList.add(null); + testStringList.add(""); + + testObjectList.add(new String("_")); + testObjectList.add(new Integer(5)); + testObjectList.add(new Boolean(false)); + testObjectList.add(null); + testObjectList.add(new Object()); + + testStringSet.add("A"); + testStringSet.add("B"); + testStringSet.add("D"); + testStringSet.add("H"); + testStringSet.add(new String()); + testStringSet.add(null); + testStringSet.add(""); + + testObjectSet.add(new String("_")); + testObjectSet.add(new Integer(5)); + testObjectSet.add(new Boolean(false)); + testObjectSet.add(null); + testObjectSet.add(new Object()); + + testObjectSetFloat.add(new Float(-18.3)); + testObjectSetFloat.add(new Float(5)); + testObjectSetFloat.add(new Float(-0.13)); + testObjectSetFloat.add(new Float(5)); + testObjectSetFloat.add(new Float(0)); + testObjectSetFloat.add(new Float(100.4)); + + testObjectMap.put(new Integer(3), null); + testObjectMap.put(null, null); + testObjectMap.put(null, "Value"); + testObjectMap.put(new Boolean(false), "false"); + testObjectMap.put("Key", Boolean.TRUE); + testObjectMap.put(new Object(), "Object"); + + testObjectMapFloat.put(new Float(-18.3f), null); + testObjectMapFloat.put(new Float(5f), null); + testObjectMapFloat.put(new Float(-0.13f), "Value"); + testObjectMapFloat.put(new Float(5f), "false"); + testObjectMapFloat.put(new Float(0f), Boolean.TRUE); + testObjectMapFloat.put(new Float(100.4f), "Object"); + + testObjectMapString.put("Hund", new Integer(3)); + testObjectMapString.put("Elefant", new Boolean(false)); + testObjectMapString.put("Pferd", "Value"); + testObjectMapString.put("Maus", null); + testObjectMapString.put("Katze", Boolean.TRUE); + testObjectMapString.put("Fisch", "Object"); + } + + @AfterClass + public static void killData() { + testStringList.clear(); + testObjectList.clear(); + testStringSet.clear(); + testObjectSet.clear(); + testObjectSetFloat.clear(); + testObjectMap.clear(); + testObjectMapFloat.clear(); + testObjectMapString.clear(); + } + + /** + * + * @return Map + */ + protected Map removeKeysNullFromMap() { + final Map testObjectMapSpecial = new HashMap(); + + final Set keySet = testObjectMap.keySet(); + for (final Object key : keySet) { + if (key != null) { + testObjectMapSpecial.put(key, testObjectMap.get(key)); + } + } + return testObjectMapSpecial; + } + + /** + * + * @return Set + */ + protected Set removeValuesNullFromSet() { + final Set testObjectSetSpecial = new HashSet(); + + for (final Object key : testObjectSet) { + if (key != null) { + testObjectSetSpecial.add(key); + } + } + return testObjectSetSpecial; + } + + /** + * + * @return List removeValuesNullFromList() { + final List testObjectListSpecial = new ArrayList(); + + for (final Object element : testObjectSet) { + if (element != null) { + testObjectListSpecial.add(element); + } + } + return testObjectListSpecial; + } + + /** + * + * @return Set + */ + protected Set removeValuesNoStringFromSet() { + final Set testObjectSetSpecial = new HashSet(); + + for (final Object key : testStringSet) { + if ((key != null) && (key.getClass().getCanonicalName() == JAVA_LANG_STRING)) { + testObjectSetSpecial.add((String) key); + } + } + return testObjectSetSpecial; + } + + /** + * + * @return List + */ + protected List removeValuesNoStringFromList() { + final List testObjectListSpecial = new ArrayList(); + + for (final Object element : testObjectMapString.keySet()) { + if ((element != null) && (element.getClass().getCanonicalName() == JAVA_LANG_STRING)) { + testObjectListSpecial.add((String) element); + } + } + return testObjectListSpecial; + } + + /** + * @param clazz Class + * @param expected int + * @param sb StringBuffer + */ + protected void assertStringLength(final Class clazz, final int expected, final StringBuffer sb) { + final int length = sb.length(); + StaticLogger.getLogger(clazz).debug("Length:\t" + length); + Assert.assertEquals(expected, length); + } + + /** + * @param clazz Class + * @param expected int + * @param delta int + * @param sb StringBuffer + */ + protected void assertStringLengthDelta(final Class clazz, final int expected, final int delta, final StringBuffer sb) { + final int length = sb.length(); + StaticLogger.getLogger(clazz).debug("Length:\t" + length); + Assert.assertTrue((length == expected) || (length == (expected + delta)) || (length == (expected - delta))); + } + + /** + * @param expectedStringArray String[] + * @param string String + */ + protected void assertStringContains(final String[] expectedStringArray, final String string) { + + for (final String element : expectedStringArray) { + Assert.assertTrue(string.contains(element)); + } + } + +} diff --git a/src/test/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputListTest.java b/src/test/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputListTest.java new file mode 100644 index 0000000..f9c2b67 --- /dev/null +++ b/src/test/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputListTest.java @@ -0,0 +1,69 @@ +package de.laktatnebel.libs.utillib.collection; + +import org.junit.Assert; +import org.junit.Test; + +import de.laktatnebel.libs.utillib.exception.AbstractUtilLibException; +import de.laktatnebel.libs.utillib.logging.StaticLogger; + +/** +* @author oleb + * + */ +public class CollectionConsoleOutputListTest extends AbstractCollectionOutputTest { + + private Class clazz = CollectionConsoleOutputListTest.class; + + @Test + public void testGetListToConsoleNull() { + StaticLogger.getLogger(clazz).debug(new Throwable().getStackTrace()[0].getClassName() + "." + new Throwable().getStackTrace()[0].getMethodName() + "() running."); + + try { + CollectionConsoleOutputList.getListToConsole(null); + + Assert.fail(MUST_FAIL); + } catch (final AbstractUtilLibException aule) { + StaticLogger.getLogger(clazz).debug(aule.getMessage()); + } + } + + @Test + public void testGetStringListToConsoleNull() { + StaticLogger.getLogger(clazz).debug(new Throwable().getStackTrace()[0].getClassName() + "." + new Throwable().getStackTrace()[0].getMethodName() + "() running."); + + try { + CollectionConsoleOutputList.getStringListToConsole(null); + + Assert.fail(MUST_FAIL); + } catch (final AbstractUtilLibException aule) { + StaticLogger.getLogger(clazz).debug(aule.getMessage()); + } + } + + @Test + public void testGetListToConsole() { + StaticLogger.getLogger(clazz).debug(new Throwable().getStackTrace()[0].getClassName() + "." + new Throwable().getStackTrace()[0].getMethodName() + "() running."); + + try { + CollectionConsoleOutputList.getListToConsole(AbstractCollectionOutputTest.testObjectList); + + } catch (final AbstractUtilLibException aule) { + StaticLogger.getLogger(clazz).debug(aule.getMessage()); + Assert.fail(MUST_NOT_FAIL); + } + } + + @Test + public void testGetStringListToConsole() { + StaticLogger.getLogger(clazz).debug(new Throwable().getStackTrace()[0].getClassName() + "." + new Throwable().getStackTrace()[0].getMethodName() + "() running."); + + try { + CollectionConsoleOutputList.getStringListToConsole(AbstractCollectionOutputTest.testStringList); + + } catch (final AbstractUtilLibException aule) { + StaticLogger.getLogger(clazz).debug(aule.getMessage()); + Assert.fail(MUST_NOT_FAIL); + } + } + +} diff --git a/src/test/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputMapTest.java b/src/test/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputMapTest.java new file mode 100644 index 0000000..82742eb --- /dev/null +++ b/src/test/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputMapTest.java @@ -0,0 +1,56 @@ +package de.laktatnebel.libs.utillib.collection; + +import org.junit.Assert; +import org.junit.Test; + +import de.laktatnebel.libs.utillib.exception.AbstractUtilLibException; +import de.laktatnebel.libs.utillib.logging.StaticLogger; + +/** +* @author oleb + * + */ +public class CollectionConsoleOutputMapTest extends AbstractCollectionOutputTest { + + private Class clazz = CollectionConsoleOutputMapTest.class; + + @Test + public void testGetMapToConsoleNull() { + StaticLogger.getLogger(clazz).debug(new Throwable().getStackTrace()[0].getClassName() + "." + new Throwable().getStackTrace()[0].getMethodName() + "() running."); + + try { + CollectionConsoleOutputMap.getMapToStringToConsole(null, false, "--"); + + Assert.fail(MUST_FAIL); + } catch (AbstractUtilLibException aule) { + StaticLogger.getLogger(clazz).debug(aule.getMessage()); + } + } + + @Test + public void testGetMapToConsole() { + StaticLogger.getLogger(clazz).debug(new Throwable().getStackTrace()[0].getClassName() + "." + new Throwable().getStackTrace()[0].getMethodName() + "() running."); + + try { + CollectionConsoleOutputMap.getMapToStringToConsole(testObjectMap, false, "<-->"); + + } catch (AbstractUtilLibException aule) { + StaticLogger.getLogger(clazz).debug(aule.getMessage()); + Assert.fail(MUST_NOT_FAIL); + } + } + + @Test + public void testGetMapToConsoleKeysOnly() { + StaticLogger.getLogger(clazz).debug(new Throwable().getStackTrace()[0].getClassName() + "." + new Throwable().getStackTrace()[0].getMethodName() + "() running."); + + try { + CollectionConsoleOutputMap.getMapToStringToConsole(testObjectMap, true, null); + + } catch (AbstractUtilLibException aule) { + StaticLogger.getLogger(clazz).debug(aule.getMessage()); + Assert.fail(MUST_NOT_FAIL); + } + } + +} diff --git a/src/test/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputSetTest.java b/src/test/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputSetTest.java new file mode 100644 index 0000000..461a6f0 --- /dev/null +++ b/src/test/java/de/laktatnebel/libs/utillib/collection/CollectionConsoleOutputSetTest.java @@ -0,0 +1,69 @@ +package de.laktatnebel.libs.utillib.collection; + +import org.junit.Assert; +import org.junit.Test; + +import de.laktatnebel.libs.utillib.exception.AbstractUtilLibException; +import de.laktatnebel.libs.utillib.logging.StaticLogger; + +/** +* @author oleb + * + */ +public class CollectionConsoleOutputSetTest extends AbstractCollectionOutputTest { + + private Class clazz = CollectionConsoleOutputSetTest.class; + + @Test + public void testGetSetToConsoleNull() { + StaticLogger.getLogger(clazz).debug(new Throwable().getStackTrace()[0].getClassName() + "." + new Throwable().getStackTrace()[0].getMethodName() + "() running."); + + try { + CollectionConsoleOutputSet.getSetToConsole(null); + + Assert.fail(MUST_FAIL); + } catch (final AbstractUtilLibException aule) { + StaticLogger.getLogger(clazz).debug(aule.getMessage()); + } + } + + @Test + public void testGetStringSetToConsoleNull() { + StaticLogger.getLogger(clazz).debug(new Throwable().getStackTrace()[0].getClassName() + "." + new Throwable().getStackTrace()[0].getMethodName() + "() running."); + + try { + CollectionConsoleOutputSet.getStringSetToConsole(null); + + Assert.fail(MUST_FAIL); + } catch (final AbstractUtilLibException aule) { + StaticLogger.getLogger(clazz).debug(aule.getMessage()); + } + } + + @Test + public void testGetSetToConsole() { + StaticLogger.getLogger(clazz).debug(new Throwable().getStackTrace()[0].getClassName() + "." + new Throwable().getStackTrace()[0].getMethodName() + "() running."); + + try { + CollectionConsoleOutputSet.getSetToConsole(AbstractCollectionOutputTest.testObjectSet); + + } catch (final AbstractUtilLibException aule) { + StaticLogger.getLogger(clazz).debug(aule.getMessage()); + Assert.fail(MUST_NOT_FAIL); + } + } + + @Test + public void testGetStringSetToConsole() { + StaticLogger.getLogger(clazz).debug(new Throwable().getStackTrace()[0].getClassName() + "." + new Throwable().getStackTrace()[0].getMethodName() + "() running."); + + try { + CollectionConsoleOutputSet.getStringSetToConsole(AbstractCollectionOutputTest.testStringSet); + + } catch (final AbstractUtilLibException aule) { + StaticLogger.getLogger(clazz).debug(aule.getMessage()); + Assert.fail(MUST_NOT_FAIL); + } + } + +} diff --git a/src/test/java/de/laktatnebel/libs/utillib/collection/CollectionOutputListTest.java b/src/test/java/de/laktatnebel/libs/utillib/collection/CollectionOutputListTest.java new file mode 100644 index 0000000..216dce6 --- /dev/null +++ b/src/test/java/de/laktatnebel/libs/utillib/collection/CollectionOutputListTest.java @@ -0,0 +1,89 @@ +package de.laktatnebel.libs.utillib.collection; + +import org.junit.Assert; +import org.junit.Test; + +import de.laktatnebel.libs.utillib.exception.AbstractUtilLibException; +import de.laktatnebel.libs.utillib.logging.StaticLogger; + +/** +* @author oleb + * + */ +public class CollectionOutputListTest extends AbstractCollectionOutputTest { + + private Class clazz = CollectionOutputListTest.class; + + @Test + public void testGetStringListToString() { + StaticLogger.getLogger(clazz).debug(new Throwable().getStackTrace()[0].getClassName() + "." + new Throwable().getStackTrace()[0].getMethodName() + "() running."); + + int expectedStringLength = 15; + + StringBuffer sb = new StringBuffer(); + + try { + sb = CollectionOutputList.getStringListToString(AbstractCollectionOutputTest.testStringList); + } catch (final AbstractUtilLibException aule) { + StaticLogger.getLogger(clazz).debug(aule.getMessage()); + Assert.fail(); + } + + assertStringLength(clazz, expectedStringLength, sb); + + StaticLogger.getLogger(clazz).debug("toString:\t" + sb.toString()); + Assert.assertEquals("A\nB\nD\nH\n\nnull\n\n", sb.toString()); + } + + @Test + public void testGetListToString() { + StaticLogger.getLogger(clazz).debug(new Throwable().getStackTrace()[0].getClassName() + "." + new Throwable().getStackTrace()[0].getMethodName() + "() running."); + + int expectedStringLength = 36; + int expectedStringLengthDelta = 1; + String expectedStringArray [] = {"5", "false", "_", "java.lang.Object@"}; + + StringBuffer sb = new StringBuffer(); + + try { + sb = CollectionOutputList.getListToString(AbstractCollectionOutputTest.testObjectList); + } catch (final AbstractUtilLibException aule) { + StaticLogger.getLogger(clazz).debug(aule.getMessage()); + Assert.fail(); + } + + assertStringLengthDelta(clazz, expectedStringLength, expectedStringLengthDelta, sb); + + final String string = sb.toString(); + StaticLogger.getLogger(clazz).debug("toString:\t" + string); + assertStringContains(expectedStringArray, string); + } + + @Test + public void testGetStringListToStringNull() { + StaticLogger.getLogger(clazz).debug(new Throwable().getStackTrace()[0].getClassName() + "." + new Throwable().getStackTrace()[0].getMethodName() + "() running."); + + try { + CollectionOutputList.getStringListToString(null); + Assert.fail(MUST_FAIL); + } catch (final AbstractUtilLibException aule) { + final String message = aule.getMessage(); + StaticLogger.getLogger(clazz).debug(message); + Assert.assertEquals(PARAMETER_LIST_NULL, message); + } + } + + @Test + public void testGetListToStringNull() { + StaticLogger.getLogger(clazz).debug(new Throwable().getStackTrace()[0].getClassName() + "." + new Throwable().getStackTrace()[0].getMethodName() + "() running."); + + try { + CollectionOutputList.getListToString(null); + Assert.fail(MUST_FAIL); + } catch (final AbstractUtilLibException aule) { + final String message = aule.getMessage(); + StaticLogger.getLogger(clazz).debug(message); + Assert.assertEquals(PARAMETER_LIST_NULL, message); + } + } +} diff --git a/src/test/java/de/laktatnebel/libs/utillib/collection/CollectionOutputMapTest.java b/src/test/java/de/laktatnebel/libs/utillib/collection/CollectionOutputMapTest.java new file mode 100644 index 0000000..42a2680 --- /dev/null +++ b/src/test/java/de/laktatnebel/libs/utillib/collection/CollectionOutputMapTest.java @@ -0,0 +1,105 @@ +package de.laktatnebel.libs.utillib.collection; + +import org.junit.Assert; +import org.junit.Test; + +import de.laktatnebel.libs.utillib.exception.AbstractUtilLibException; +import de.laktatnebel.libs.utillib.logging.StaticLogger; + +/** +* @author oleb + * + */ +public class CollectionOutputMapTest extends AbstractCollectionOutputTest { + + private Class clazz = CollectionOutputMapTest.class; + + @Test + public void testGetMapToStringKeysOnly() { + StaticLogger.getLogger(CollectionConsoleOutputSetTest.class).debug(new Throwable().getStackTrace()[0].getClassName() + "." + new Throwable().getStackTrace()[0].getMethodName() + "() running."); + + int expectedStringLength = 38; + int expectedStringLengthDelta = 1; + String expectedStringArray [] = {"3", "false", "Key", "java.lang.Object@"}; + + StringBuffer sb = new StringBuffer(); + + try { + sb = CollectionOutputMap.getMapToString(AbstractCollectionOutputTest.testObjectMap, true, "-"); + } catch (final AbstractUtilLibException aule) { + StaticLogger.getLogger(clazz).debug(aule.getMessage()); + Assert.fail(); + } + + assertStringLengthDelta(clazz, expectedStringLength, expectedStringLengthDelta, sb); + + final String string = sb.toString(); + StaticLogger.getLogger(clazz).debug("toString:\t" + string); + + assertStringContains(expectedStringArray, string); + } + + @Test + public void testGetMapToString() { + StaticLogger.getLogger(CollectionConsoleOutputSetTest.class).debug(new Throwable().getStackTrace()[0].getClassName() + "." + new Throwable().getStackTrace()[0].getMethodName() + "() running."); + + int expectedStringLength = 71; + int expectedStringLengthDelta = 1; + String expectedStringArray [] = {"3", "false", "Key", "java.lang.Object@"}; + + StringBuffer sb = new StringBuffer(); + + try { + sb = CollectionOutputMap.getMapToString(AbstractCollectionOutputTest.testObjectMap, false, "-"); + } catch (final AbstractUtilLibException aule) { + StaticLogger.getLogger(clazz).debug(aule.getMessage()); + Assert.fail(); + } + + assertStringLengthDelta(clazz, expectedStringLength, expectedStringLengthDelta, sb); + + final String string = sb.toString(); + StaticLogger.getLogger(clazz).debug("toString:\t" + string); + + assertStringContains(expectedStringArray, string); + } + + @Test + public void testGetMapToStringDelimNull() { + StaticLogger.getLogger(CollectionConsoleOutputSetTest.class).debug(new Throwable().getStackTrace()[0].getClassName() + "." + new Throwable().getStackTrace()[0].getMethodName() + "() running."); + + int expectedStringLength = 66; + int expectedStringLengthDelta = 1; + String expectedStringArray [] = {"3", "false", "Key", "java.lang.Object@"}; + + StringBuffer sb = new StringBuffer(); + + try { + sb = CollectionOutputMap.getMapToString(AbstractCollectionOutputTest.testObjectMap, false, null); + } catch (final AbstractUtilLibException aule) { + StaticLogger.getLogger(clazz).debug(aule.getMessage()); + Assert.fail(); + } + + assertStringLengthDelta(clazz, expectedStringLength, expectedStringLengthDelta, sb); + + final String string = sb.toString(); + StaticLogger.getLogger(clazz).debug("toString:\t" + string); + + assertStringContains(expectedStringArray, string); + } + + @Test + public void testGetMapToStringNull() { + StaticLogger.getLogger(CollectionConsoleOutputSetTest.class).debug(new Throwable().getStackTrace()[0].getClassName() + "." + new Throwable().getStackTrace()[0].getMethodName() + "() running."); + + try { + CollectionOutputMap.getMapToString(null, true, "-"); + Assert.fail(); + } catch (final AbstractUtilLibException aule) { + final String message = aule.getMessage(); + StaticLogger.getLogger(clazz).debug(message); + Assert.assertEquals(PARAMETER_MAP_NULL, message); + } + } +} diff --git a/src/test/java/de/laktatnebel/libs/utillib/collection/CollectionOutputSetTest.java b/src/test/java/de/laktatnebel/libs/utillib/collection/CollectionOutputSetTest.java new file mode 100644 index 0000000..c465504 --- /dev/null +++ b/src/test/java/de/laktatnebel/libs/utillib/collection/CollectionOutputSetTest.java @@ -0,0 +1,93 @@ +package de.laktatnebel.libs.utillib.collection; + +import org.junit.Assert; +import org.junit.Test; + +import de.laktatnebel.libs.utillib.exception.AbstractUtilLibException; +import de.laktatnebel.libs.utillib.logging.StaticLogger; + +/** +* @author oleb + * + */ +public class CollectionOutputSetTest extends AbstractCollectionOutputTest { + + private Class clazz = CollectionOutputSetTest.class; + + @Test + public void testGetStringSetToString() { + StaticLogger.getLogger(clazz).debug(new Throwable().getStackTrace()[0].getClassName() + "." + new Throwable().getStackTrace()[0].getMethodName() + "() running."); + + int expectedStringLength = 14; + String expectedStringArray [] = {"A", "B", "D", "H", "null"}; + + StringBuffer sb = new StringBuffer(); + + try { + sb = CollectionOutputSet.getStringSetToString(AbstractCollectionOutputTest.testStringSet); + } catch (final AbstractUtilLibException aule) { + StaticLogger.getLogger(clazz).debug(aule.getMessage()); + Assert.fail(); + } + + assertStringLength(clazz, expectedStringLength, sb); + + final String string = sb.toString(); + StaticLogger.getLogger(clazz).debug("toString:\t" + string); + + assertStringContains(expectedStringArray, string); + } + + @Test + public void testGetSetToString() { + StaticLogger.getLogger(clazz).debug(new Throwable().getStackTrace()[0].getClassName() + "." + new Throwable().getStackTrace()[0].getMethodName() + "() running."); + + int expectedStringLength = 36; + int expectedStringLengthDelta = 1; + String expectedStringArray [] = {"5", "false", "_", "java.lang.Object@"}; + + StringBuffer sb = new StringBuffer(); + + try { + sb = CollectionOutputSet.getSetToString(AbstractCollectionOutputTest.testObjectSet); + } catch (final AbstractUtilLibException aule) { + StaticLogger.getLogger(clazz).debug(aule.getMessage()); + Assert.fail(); + } + + assertStringLengthDelta(clazz, expectedStringLength, expectedStringLengthDelta, sb); + + final String string = sb.toString(); + StaticLogger.getLogger(clazz).debug("toString:\t" + string); + + assertStringContains(expectedStringArray, string); + } + + @Test + public void testGetStringSetToStringNull() { + StaticLogger.getLogger(clazz).debug(new Throwable().getStackTrace()[0].getClassName() + "." + new Throwable().getStackTrace()[0].getMethodName() + "() running."); + + try { + CollectionOutputSet.getStringSetToString(null); + Assert.fail(MUST_FAIL); + } catch (final AbstractUtilLibException aule) { + final String message = aule.getMessage(); + StaticLogger.getLogger(clazz).debug(message); + Assert.assertEquals(PARAMETER_SET_NULL, message); + } + } + + @Test + public void testGetSetToStringNull() { + StaticLogger.getLogger(clazz).debug(new Throwable().getStackTrace()[0].getClassName() + "." + new Throwable().getStackTrace()[0].getMethodName() + "() running."); + + try { + CollectionOutputSet.getSetToString(null); + Assert.fail(MUST_FAIL); + } catch (final AbstractUtilLibException aule) { + final String message = aule.getMessage(); + StaticLogger.getLogger(clazz).debug(message); + Assert.assertEquals(PARAMETER_SET_NULL, message); + } + } +} diff --git a/src/test/java/de/laktatnebel/libs/utillib/collection/CollectionSortTest.java b/src/test/java/de/laktatnebel/libs/utillib/collection/CollectionSortTest.java new file mode 100755 index 0000000..7c8a171 --- /dev/null +++ b/src/test/java/de/laktatnebel/libs/utillib/collection/CollectionSortTest.java @@ -0,0 +1,110 @@ +package de.laktatnebel.libs.utillib.collection; + +import java.util.Map; +import java.util.Set; + +import org.junit.Assert; +import org.junit.Test; + +import de.laktatnebel.libs.utillib.exception.CollectionOutputException; +import de.laktatnebel.libs.utillib.logging.StaticLogger; + +public class CollectionSortTest extends AbstractCollectionOutputTest { + + private Class clazz = CollectionSortTest.class; + + /** + * + */ + @Test + public void testSetSort() { + StaticLogger.getLogger(clazz).debug( + new Throwable().getStackTrace()[0].getClassName() + "." + + new Throwable().getStackTrace()[0].getMethodName() + + "() running."); + + StringBuffer setToString = null; + try { + Set setSort = CollectionSort + .setSort(AbstractCollectionOutputTest.testObjectSetFloat); + + setToString = CollectionOutputSet.getSetToString(setSort); + } catch (CollectionOutputException coe) { + StaticLogger.getLogger(clazz).debug(coe.getMessage()); + Assert.fail(); + } + + StaticLogger.getLogger(clazz).debug(setToString.toString()); + Assert.assertEquals("-18.3\n-0.13\n0.0\n5.0\n100.4\n", + setToString.toString()); + } + + /** + * + */ + @Test + public void testSetSortWithNullKey() { + StaticLogger.getLogger(clazz).debug( + new Throwable().getStackTrace()[0].getClassName() + "." + + new Throwable().getStackTrace()[0].getMethodName() + + "() running."); + + try { + CollectionSort.setSort(AbstractCollectionOutputTest.testObjectSet); + + Assert.fail(MUST_FAIL); + } catch (CollectionOutputException coe) { + final String message = coe.getMessage(); + StaticLogger.getLogger(clazz).debug(message); + Assert.assertEquals(PARAMETER_SET_KEYS_NOT_EQUAL_TYPE, message); + } + } + + /** + * + */ + @Test + public void testMapSort() { + StaticLogger.getLogger(clazz).debug( + new Throwable().getStackTrace()[0].getClassName() + "." + + new Throwable().getStackTrace()[0].getMethodName() + + "() running."); + + StringBuffer mapToString = null; + try { + Map mapSort = CollectionSort + .mapSort(AbstractCollectionOutputTest.testObjectMapFloat); + + mapToString = CollectionOutputMap.getMapToString(mapSort, true, + null); + } catch (CollectionOutputException coe) { + StaticLogger.getLogger(clazz).debug(coe.getMessage()); + Assert.fail(); + } + + StaticLogger.getLogger(clazz).debug(mapToString.toString()); + Assert.assertEquals("-18.3\n-0.13\n0.0\n5.0\n100.4\n", + mapToString.toString()); + } + + /** + * + */ + @Test + public void testMapSortWithNullKey() { + StaticLogger.getLogger(clazz).debug( + new Throwable().getStackTrace()[0].getClassName() + "." + + new Throwable().getStackTrace()[0].getMethodName() + + "() running."); + + try { + CollectionSort.mapSort(AbstractCollectionOutputTest.testObjectMap); + + Assert.fail(MUST_FAIL); + } catch (CollectionOutputException coe) { + final String message = coe.getMessage(); + StaticLogger.getLogger(clazz).debug(message); + Assert.assertEquals(PARAMETER_MAP_KEYS_NOT_EQUAL_TYPE, message); + } + } +} diff --git a/src/test/java/de/laktatnebel/libs/utillib/collection/ListCompTest.java b/src/test/java/de/laktatnebel/libs/utillib/collection/ListCompTest.java new file mode 100755 index 0000000..21e8dc6 --- /dev/null +++ b/src/test/java/de/laktatnebel/libs/utillib/collection/ListCompTest.java @@ -0,0 +1,214 @@ +package de.laktatnebel.libs.utillib.collection; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** +* @author oleb + * + */ +public class ListCompTest { + + private static List primzahlen = new ArrayList(); + private static List fibonacci = new ArrayList(); + private static List gleich = new ArrayList(); + private static List negativ = new ArrayList(); + + List result = null; + Object[] resultExpected; + int expected; + + @Before + public void fillData() { + primzahlen.add(new Integer(19)); + primzahlen.add(new Integer(2)); + primzahlen.add(new Integer(5)); + primzahlen.add(new Integer(11)); + primzahlen.add(new Integer(23)); + primzahlen.add(new Integer(13)); + primzahlen.add(new Integer(3)); + primzahlen.add(new Integer(7)); + primzahlen.add(new Integer(17)); + + fibonacci.add(new Integer(1)); + fibonacci.add(new Integer(21)); + fibonacci.add(new Integer(2)); + fibonacci.add(new Integer(55)); + fibonacci.add(new Integer(5)); + fibonacci.add(new Integer(1)); + fibonacci.add(new Integer(8)); + fibonacci.add(new Integer(34)); + fibonacci.add(new Integer(13)); + fibonacci.add(new Integer(3)); + + gleich.add(new Integer(2)); + gleich.add(new Integer(3)); + gleich.add(new Integer(5)); + + negativ.add(new Integer(-1)); + negativ.add(new Integer(-18)); + negativ.add(new Integer(-12)); + } + + @After + public void killData() { + primzahlen.clear(); + fibonacci.clear(); + gleich.clear(); + negativ.clear(); + } + + @Test + public void testTeilmengeFirst1() { + + result = ListComp.compare(gleich, fibonacci, ListComp.TypeOfSet.IN_FIRST_ONLY); + expected = 0; + Assert.assertEquals("Erwartet: " + expected, expected, result.size()); + + resultExpected = new Integer[]{}; + assertArrayElements(resultExpected, result); + } + + @Test + public void testTeilmengeFirst2() { + + result = ListComp.compare(primzahlen, gleich, ListComp.TypeOfSet.IN_FIRST_ONLY); + expected = 6; + Assert.assertEquals("Erwartet: " + expected, expected, result.size()); + + resultExpected = new Integer[]{7,11,17,19,23}; + assertArrayElements(resultExpected, result); + } + + @Test + public void testTeilmengeLast1() { + + result = ListComp.compare(gleich, fibonacci, ListComp.TypeOfSet.IN_LAST_ONLY); + expected = 7; + Assert.assertEquals("Erwartet: " + expected, expected, result.size()); + + resultExpected = new Integer[]{1,1,8,13,21,34,55}; + assertArrayElements(resultExpected, result); + } + + @Test + public void testTeilmengeLast2() { + + result = ListComp.compare(primzahlen, gleich, ListComp.TypeOfSet.IN_LAST_ONLY); + expected = 0; + Assert.assertEquals("Erwartet: " + expected, expected, result.size()); + + resultExpected = new Integer[]{}; + assertArrayElements(resultExpected, result); + } + + @Test + public void testTeilmenge() { + + result = ListComp.compare(primzahlen, gleich, ListComp.TypeOfSet.IN_BOTH); + expected = 3; + Assert.assertEquals("Erwartet: " + expected, expected, result.size()); + + resultExpected = new Integer[]{2,3,5}; + assertArrayElements(resultExpected, result); + } + + @Test + public void testTeilmengeGleich1() { + + result = ListComp.compare(gleich, gleich, ListComp.TypeOfSet.IN_BOTH); + expected = 3; + Assert.assertEquals("Erwartet: " + expected, expected, result.size()); + + resultExpected = new Integer[]{2,3,5}; + assertArrayElements(resultExpected, result); + } + + @Test + public void testTeilmengeGleich2() { + + result = ListComp.compare(gleich, gleich, ListComp.TypeOfSet.IN_LAST_ONLY); + expected = 0; + Assert.assertEquals("Erwartet: " + expected, expected, result.size()); + + resultExpected = new Integer[]{}; + assertArrayElements(resultExpected, result); + } + + @Test + public void testSchnittmengeFirst() { + + result = ListComp.compare(primzahlen, fibonacci, ListComp.TypeOfSet.IN_FIRST_ONLY); + expected = 5; + Assert.assertEquals("Erwartet: " + expected, expected, result.size()); + + resultExpected = new Integer[]{7,11,17,19,23}; + assertArrayElements(resultExpected, result); + } + + @Test + public void testSchnittmengeLast() { + + result = ListComp.compare(primzahlen, fibonacci, ListComp.TypeOfSet.IN_LAST_ONLY); + expected = 6; + Assert.assertEquals("Erwartet: " + expected, expected, result.size()); + + resultExpected = new Integer[]{1,1,8,21,34,55}; + assertArrayElements(resultExpected, result); + } + + @Test + public void testSchnittmenge() { + + result = ListComp.compare(primzahlen, fibonacci, ListComp.TypeOfSet.IN_BOTH); + expected = 4; + Assert.assertEquals("Erwartet: " + expected, expected, result.size()); + + resultExpected = new Integer[]{2,3,5,13}; + assertArrayElements(resultExpected, result); + } + + @Test + public void testKeineSchnittmengeFirst() { + + result = ListComp.compare(primzahlen, negativ, ListComp.TypeOfSet.IN_FIRST_ONLY); + expected = 9; + Assert.assertEquals("Erwartet: " + expected, expected, result.size()); + + resultExpected = new Integer[]{2,3,5,7,11,13,17,19,23}; + assertArrayElements(resultExpected, result); + } + + @Test + public void testKeineSchnittmengeLast() { + + result = ListComp.compare(primzahlen, negativ, ListComp.TypeOfSet.IN_LAST_ONLY); + expected = 3; + Assert.assertEquals("Erwartet: " + expected, expected, result.size()); + + resultExpected = new Integer[]{-1,-12,-18}; + assertArrayElements(resultExpected, result); + } + + @Test + public void testKeineSchnittmengeBoth() { + + result = ListComp.compare(primzahlen, negativ, ListComp.TypeOfSet.IN_BOTH); + expected = 0; + Assert.assertEquals("Erwartet: " + expected, expected, result.size()); + + resultExpected = new Integer[]{}; + assertArrayElements(resultExpected, result); + } + + private void assertArrayElements(Object[] resultExpected, List result) { + for (Object object : resultExpected) { + Assert.assertTrue(object + " sollte enthalten sein", result.contains(object)); + } + } +} 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 @@ + + + + + + + + + + + + + + + + + -- 2.39.5