From f7fbeb8949419fbbb0415e93a8b9dd0480d020eb Mon Sep 17 00:00:00 2001 From: heck Date: Thu, 21 May 2020 21:07:45 +0200 Subject: [PATCH] A lot of pretty printing (StringUtils & Co) --- .../jniadapter/test/framework/TestLogger.java | 49 ++++--- .../jniadapter/test/framework/TestUtils.java | 138 +++++++++++++++++- .../test/utils/AdapterTestUtils.java | 21 +-- 3 files changed, 169 insertions(+), 39 deletions(-) diff --git a/test/java/foundation/pEp/jniadapter/test/framework/TestLogger.java b/test/java/foundation/pEp/jniadapter/test/framework/TestLogger.java index b33a0a9..7997f92 100644 --- a/test/java/foundation/pEp/jniadapter/test/framework/TestLogger.java +++ b/test/java/foundation/pEp/jniadapter/test/framework/TestLogger.java @@ -6,10 +6,12 @@ package foundation.pEp.jniadapter.test.framework; public class TestLogger { + // options private static boolean logEnabled = true; + private static int lineWidth = 80; - public TestLogger() { - } + // constants + private static int threadStrLen = 10; public static void setLoggingEnabled(boolean enabled) { logEnabled = enabled; @@ -19,40 +21,39 @@ public class TestLogger { return logEnabled; } + public static int getLineWidth() { + return lineWidth; + } + + public static void setLineWidth(int lineWidth) { + TestLogger.lineWidth = lineWidth; + } + public static void log(String msg) { if (logEnabled) { - String threadStr = String.format("%-10s", Thread.currentThread().getName()); - String logStr = threadStr + ": " + msg; + String indent = ""; + String separator = ": "; + int indentStrLen = threadStrLen + separator.length(); + String threadStr = String.format("%-" + threadStrLen + "s", Thread.currentThread().getName()); + indent = String.format("%" + indentStrLen + "s", " "); + msg = msg.replace("\n", "\n" + indent); + String logStr = threadStr + separator + msg; System.out.println(logStr); } } public static void logH1(String msg) { - log(getDecoratedString(msg, "=")); + log(TestUtils.fixedWidthPaddedString(msg, "=", lineWidth, TestUtils.Alignment.Center, null)); +// log(getDecoratedString(msg, "=")); } public static void logH2(String msg) { - log(getDecoratedString(msg, "-")); - } - - private static String getDecoratedString(String msg, String decoration) { - byte lineWidth = 80; - String ret = ""; - - for(int i = 0; (double)i < Math.ceil((double)((lineWidth - msg.length() + 2) / 2)); ++i) { - ret = ret + decoration; - } - - return ret + " " + msg + " " + ret; - } - - public static void spacer() { - System.out.print('\n'); + log(TestUtils.fixedWidthPaddedString(msg, "-", lineWidth, TestUtils.Alignment.Center, null)); +// log(getDecoratedString(msg, "-")); } - // Deprecated - public static void logSectEnd(String msg) { - log(msg + "\n"); + public static void logRaw(String msg) { + System.out.print(msg); } } diff --git a/test/java/foundation/pEp/jniadapter/test/framework/TestUtils.java b/test/java/foundation/pEp/jniadapter/test/framework/TestUtils.java index 2748782..c243d2b 100644 --- a/test/java/foundation/pEp/jniadapter/test/framework/TestUtils.java +++ b/test/java/foundation/pEp/jniadapter/test/framework/TestUtils.java @@ -1,7 +1,57 @@ package foundation.pEp.jniadapter.test.framework; +import java.io.OutputStream; +import java.io.PrintStream; +import java.util.HashMap; + public class TestUtils { + private static boolean stdoutHasBeenDisabled = false; + private static PrintStream origSTDOUT; + + private static boolean stderrHasBeenDisabled = false; + private static PrintStream origSTDERR; + + public static void standardOutErrDisable(boolean mute) { + standardOutDisabled(mute); + standardErrDisabled(mute); + } + + public static void standardOutDisabled(boolean disable) { + if (disable) { + origSTDOUT = System.out; + stdoutHasBeenDisabled = true; + System.setOut(new PrintStream(new OutputStream() { + public void write(int b) { + //DO NOTHING + } + })); + } else { + if (stdoutHasBeenDisabled) { + System.setOut(origSTDOUT); + } + } + } + + public static void standardErrDisabled(boolean disable) { + if (disable) { + origSTDOUT = System.err; + stderrHasBeenDisabled = true; + System.setErr(new PrintStream(new OutputStream() { + public void write(int b) { + //DO NOTHING + } + })); + } else { + if (stderrHasBeenDisabled) { + System.setErr(origSTDOUT); + } + } + } + + /* + Time Utils + */ public static void sleep(int mSec) { try { Thread.sleep(mSec); @@ -9,4 +59,90 @@ public class TestUtils { System.out.println("sleep got interrupted"); } } -} \ No newline at end of file + + /* + String Utils + */ + + public enum Alignment { + Left(0), + Center(1), + Right(2); + + public final int value; + + private static HashMap intMap; + + private Alignment(int value) { + this.value = value; + } + + public static Alignment getByInt(int value) { + if (intMap == null) { + intMap = new HashMap(); + for (Alignment s : Alignment.values()) { + intMap.put(s.value, s); + } + } + if (intMap.containsKey(value)) { + return intMap.get(value); + } + return null; + } + } + + public static String fixedWidthPaddedString(String str, String padChar, int len, Alignment alignment, String clipMsg) { + String ret = ""; + String padStr = repeatString(padChar, len); + padStr = padStr.substring(0, len); + if (str.length() <= len) { + if (alignment == Alignment.Left) { + ret = str + repeatString(padChar, len - str.length()); + } + if (alignment == Alignment.Right) { + ret = repeatString(padChar, len - str.length()) + str; + } + if (alignment == Alignment.Center) { + int padAmt = len - str.length(); + String pad = repeatString(padChar, (int) Math.ceil(padAmt / 2) + 1); + ret = pad + str + pad; + ret = clipString(ret, len, null); + } + } else { + ret = clipString(str, len, clipMsg); + } + return ret; + } + + public static String repeatString(String str, int times) { + String ret = ""; + for (int i = 0; i < times; i++) { + ret += str; + } + return ret; + } + + public static String clipString(String str, int len, String clipMsg) { + String ret = str; + if (str.length() > len) { + int effSpaceAvail = len; // max + if (clipMsg != null) { + effSpaceAvail = len - clipMsg.length(); + if (effSpaceAvail <= 0) { + clipMsg = null; + effSpaceAvail = len; + } + if (clipMsg.length() == 0) { + clipMsg = null; + } + } + + ret = str.substring(0, effSpaceAvail); + if (clipMsg != null) { + ret += clipMsg; + } + } + return ret; + } +} + diff --git a/test/java/foundation/pEp/jniadapter/test/utils/AdapterTestUtils.java b/test/java/foundation/pEp/jniadapter/test/utils/AdapterTestUtils.java index 9cc55e8..93d7cb7 100644 --- a/test/java/foundation/pEp/jniadapter/test/utils/AdapterTestUtils.java +++ b/test/java/foundation/pEp/jniadapter/test/utils/AdapterTestUtils.java @@ -4,6 +4,8 @@ import foundation.pEp.jniadapter.*; import java.util.ArrayList; import java.util.Vector; +import static foundation.pEp.jniadapter.test.framework.TestUtils.clipString; + public class AdapterTestUtils { public static String identityToString(Identity i, Boolean full) { String ret = ""; @@ -175,7 +177,7 @@ public class AdapterTestUtils { kvs.add(new Pair(key, value)); if (!full) { - kvs = clipStrings(kvs, 200); + kvs = clipStrings(kvs, 200, "clipped..."); } ret = stringPairListToString(kvs); @@ -246,7 +248,7 @@ public class AdapterTestUtils { ret += "-----BEGIN Attachment index: " + attachments.indexOf(a) + "-----\n"; String tmp = a.toString(); if (!full) { - tmp = clipString(tmp, 250); + tmp = clipString(tmp, 250, "clipped..."); } ret += tmp + "\n"; ret += "-----END Attachment index: " + attachments.indexOf(a) + "-----\n"; @@ -255,23 +257,14 @@ public class AdapterTestUtils { return ret; } - public static String clipString(String str, int len) { - String ret = str; - if (str.length() > len) { - ret = str.substring(0, len); - ret += " clipped... "; - } - return ret; - } - - public static ArrayList> clipStrings(ArrayList> spv, int len) { + public static ArrayList> clipStrings(ArrayList> spv, int len, String clipMsg) { for (Pair p : spv) { if(p != null) { if(p.first != null) { - p.first = clipString(p.first, len); + p.first = clipString(p.first, len, clipMsg); } if(p.second != null) { - p.second = clipString(p.second, len); + p.second = clipString(p.second, len,clipMsg); } } }