Browse Source

Testframework TermColors

JNI-96
heck 5 years ago
parent
commit
b0aeb6531b
  1. 41
      test/java/foundation/pEp/jniadapter/test/framework/TestLogger.java
  2. 23
      test/java/foundation/pEp/jniadapter/test/framework/TestSuite.java
  3. 28
      test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java
  4. 62
      test/java/foundation/pEp/jniadapter/test/framework/TestUtils.java
  5. 6
      test/java/foundation/pEp/jniadapter/test/framework/examples/ctxmembers/TestMain.java
  6. 15
      test/java/foundation/pEp/jniadapter/test/framework/examples/testsuite/TestMain.java

41
test/java/foundation/pEp/jniadapter/test/framework/TestLogger.java

@ -1,5 +1,7 @@
package foundation.pEp.jniadapter.test.framework; package foundation.pEp.jniadapter.test.framework;
import static foundation.pEp.jniadapter.test.framework.TestUtils.TermColor;
public class TestLogger { public class TestLogger {
// options // options
private static boolean logEnabled = true; private static boolean logEnabled = true;
@ -24,6 +26,7 @@ public class TestLogger {
TestLogger.lineWidth = lineWidth; TestLogger.lineWidth = lineWidth;
} }
// Log
public static void log(String msg) { public static void log(String msg) {
if (logEnabled) { if (logEnabled) {
String indent = ""; String indent = "";
@ -37,15 +40,49 @@ public class TestLogger {
} }
} }
public static void log(String msg, TermColor color) {
setTermColor(color);
log(msg);
setTermColor(TermColor.RESET);
}
// LogH1
public static void logH1(String msg) { public static void logH1(String msg) {
log(TestUtils.fixedWidthPaddedString(msg, "=", lineWidth, TestUtils.Alignment.Center, null)); log(TestUtils.padOrClipString(msg, "=", lineWidth, TestUtils.Alignment.Center, null));
} }
public static void logH1(String msg, TermColor color) {
setTermColor(color);
logH1(msg);
setTermColor(TermColor.RESET);
}
// LogH2
public static void logH2(String msg) { public static void logH2(String msg) {
log(TestUtils.fixedWidthPaddedString(msg, "-", lineWidth, TestUtils.Alignment.Center, null)); log(TestUtils.padOrClipString(msg, "-", lineWidth, TestUtils.Alignment.Center, null));
}
public static void logH2(String msg, TermColor color) {
setTermColor(color);
logH2(msg);
setTermColor(TermColor.RESET);
} }
// LogRaw
public static void logRaw(String msg) { public static void logRaw(String msg) {
System.out.print(msg); System.out.print(msg);
} }
public static void logRaw(String msg, TermColor color) {
setTermColor(color);
logRaw(msg);
setTermColor(TermColor.RESET);
}
public static void setTermColor(TermColor c) {
logRaw(c.toString());
}
} }

23
test/java/foundation/pEp/jniadapter/test/framework/TestSuite.java

@ -1,4 +1,6 @@
package foundation.pEp.jniadapter.test.framework; package foundation.pEp.jniadapter.test.framework;
import static foundation.pEp.jniadapter.test.framework.TestLogger.*;
import static foundation.pEp.jniadapter.test.framework.TestUtils.TermColor;
import java.util.ArrayList; import java.util.ArrayList;
@ -25,5 +27,26 @@ public class TestSuite {
t.setVerboseMode(verbose); t.setVerboseMode(verbose);
t.run(); t.run();
} }
printStats();
}
private static void printStats() {
int totalCount = tests.size();
int skippedCount = 0;
int failedCount = 0;
int successCount = 0;
for (TestUnit t : tests) {
if (t.getResult() == TestResult.SKIPPED) skippedCount++;
if (t.getResult() == TestResult.FAILED) failedCount++;
if (t.getResult() == TestResult.SUCCESS) successCount++;
}
failedCount = failedCount + skippedCount;
log("SUCCESS: " + successCount, TermColor.GREEN);
String failedStr = "FAILED : " + failedCount;
if(skippedCount > 0 ) failedStr += " ("+skippedCount + " Skipped)";
log(failedStr, TermColor.RED);
log("TOTAL : " + totalCount);
} }
} }

28
test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java

@ -1,6 +1,8 @@
package foundation.pEp.jniadapter.test.framework; package foundation.pEp.jniadapter.test.framework;
import static foundation.pEp.jniadapter.test.framework.TestLogger.*; import static foundation.pEp.jniadapter.test.framework.TestLogger.*;
import static foundation.pEp.jniadapter.test.framework.TestUtils.TermColor;
import static foundation.pEp.jniadapter.test.framework.TestUtils.colorString;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -52,6 +54,7 @@ public class TestUnit<T extends TestContextInterface> implements Runnable {
TestUtils.standardOutErrEnabled(verboseMode); TestUtils.standardOutErrEnabled(verboseMode);
if (ctx.isUninitializable()) { if (ctx.isUninitializable()) {
setTestState(TestState.CTX_INIT_FAILED); setTestState(TestState.CTX_INIT_FAILED);
TestUtils.standardOutErrEnabled(true);
return; return;
} }
try { try {
@ -61,23 +64,32 @@ public class TestUnit<T extends TestContextInterface> implements Runnable {
//Context init problems need to throw to fail //Context init problems need to throw to fail
try { try {
setTestState(TestState.CTX_INIT); setTestState(TestState.CTX_INIT);
setTermColor(TermColor.BLUE);
ctx.init(); ctx.init();
setTermColor(TermColor.RESET);
} catch (Throwable t) { } catch (Throwable t) {
lastException = t; lastException = t;
setTermColor(TermColor.RESET);
setTestState(TestState.CTX_INIT_FAILED); setTestState(TestState.CTX_INIT_FAILED);
TestUtils.standardOutErrEnabled(true);
return; return;
} }
ctx.setInitialized(true); ctx.setInitialized(true);
} }
//tests need to throw to fail //tests need to throw to fail
setTestState(TestState.RUNNING); setTestState(TestState.RUNNING);
setTermColor(TermColor.CYAN);
lambda.accept(ctx); lambda.accept(ctx);
setTermColor(TermColor.RESET);
setTestState(TestState.SUCCESS); setTestState(TestState.SUCCESS);
} catch (Throwable t) { } catch (Throwable t) {
lastException = t; lastException = t;
setTermColor(TermColor.RESET);
setTestState(TestState.FAILED); setTestState(TestState.FAILED);
TestUtils.standardOutErrEnabled(true);
return; return;
} }
setTermColor(TermColor.RESET);
TestUtils.standardOutErrEnabled(true); TestUtils.standardOutErrEnabled(true);
} }
@ -117,8 +129,16 @@ public class TestUnit<T extends TestContextInterface> implements Runnable {
private void setTestResult(TestResult r) { private void setTestResult(TestResult r) {
result = r; result = r;
String resultStr = r.toString();
if(r != TestResult.SUCCESS) {
resultStr = colorString(resultStr, TermColor.RED);
} else {
resultStr = colorString(resultStr, TermColor.GREEN);
}
TestUtils.standardOutErrEnabled(true); TestUtils.standardOutErrEnabled(true);
logH1(makeLogString(result.toString())); logH1(makeLogString(resultStr));
if( r != TestResult.SUCCESS) { if( r != TestResult.SUCCESS) {
log("ERROR: " + getLastException().toString()); log("ERROR: " + getLastException().toString());
} }
@ -133,9 +153,9 @@ public class TestUnit<T extends TestContextInterface> implements Runnable {
} }
private String makeLogString(String str) { private String makeLogString(String str) {
String testUnitNameFmtd = TestUtils.fixedWidthPaddedString(" TEST: '" + testUnitName + "' ", "=", logFmtTestNameLen, TestUtils.Alignment.Left, ".. "); String testUnitNameFmtd = TestUtils.padOrClipString(" TEST: '" + testUnitName + "' ", "=", logFmtTestNameLen, TestUtils.Alignment.Left, ".. ");
String testCtxNameFmtd = TestUtils.fixedWidthPaddedString(" CTX: '" + ctx.getTestContextName() + "' ", "=", logFmtCtxNameLen, TestUtils.Alignment.Center, ".. "); String testCtxNameFmtd = TestUtils.padOrClipString(" CTX: '" + ctx.getTestContextName() + "' ", "=", logFmtCtxNameLen, TestUtils.Alignment.Center, ".. ");
String strFmtd = TestUtils.fixedWidthPaddedString(" " + str + " ", "=", logFmtMsgLen, TestUtils.Alignment.Right, ".. "); String strFmtd = TestUtils.padOrClipString(" " + str + " ", "=", logFmtMsgLen, TestUtils.Alignment.Right, ".. ");
return testUnitNameFmtd + testCtxNameFmtd + strFmtd; return testUnitNameFmtd + testCtxNameFmtd + strFmtd;
} }
} }

62
test/java/foundation/pEp/jniadapter/test/framework/TestUtils.java

@ -4,6 +4,8 @@ import java.io.OutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.HashMap; import java.util.HashMap;
import static foundation.pEp.jniadapter.test.framework.TestLogger.log;
public class TestUtils { public class TestUtils {
private static boolean stdoutHasBeenDisabled = false; private static boolean stdoutHasBeenDisabled = false;
private static PrintStream origSTDOUT; private static PrintStream origSTDOUT;
@ -91,19 +93,19 @@ public class TestUtils {
} }
} }
public static String fixedWidthPaddedString(String str, String padChar, int len, Alignment alignment, String clipMsg) { public static String padOrClipString(String str, String padChar, int len, Alignment alignment, String clipMsg) {
String ret = ""; String ret = "";
String padStr = repeatString(padChar, len); int strLen = str.length();
padStr = padStr.substring(0, len); len += (substringOccurencesCount(str, "\u001B") * 4);
if (str.length() <= len) { if (strLen <= len) {
if (alignment == Alignment.Left) { if (alignment == Alignment.Left) {
ret = str + repeatString(padChar, len - str.length()); ret = str + repeatString(padChar, len - strLen);
} }
if (alignment == Alignment.Right) { if (alignment == Alignment.Right) {
ret = repeatString(padChar, len - str.length()) + str; ret = repeatString(padChar, len - strLen) + str;
} }
if (alignment == Alignment.Center) { if (alignment == Alignment.Center) {
int padAmt = len - str.length(); int padAmt = len - strLen;
String pad = repeatString(padChar, (int) Math.ceil(padAmt / 2) + 1); String pad = repeatString(padChar, (int) Math.ceil(padAmt / 2) + 1);
ret = pad + str + pad; ret = pad + str + pad;
ret = clipString(ret, len, null); ret = clipString(ret, len, null);
@ -114,6 +116,25 @@ public class TestUtils {
return ret; return ret;
} }
public static int stringLenMinusEscSeq(String str) {
int ret;
int escSeqCount = substringOccurencesCount(str, "\u001B[");
ret = str.length() - (escSeqCount * 3);
if(ret < 0) ret = 0;
return ret;
}
public static int substringOccurencesCount(String str, String substring) {
int ret = 0;
int fromIndex = 0;
while ((fromIndex = str.indexOf(substring, fromIndex)) != -1) {
ret++;
fromIndex++;
}
return ret;
}
public static String repeatString(String str, int times) { public static String repeatString(String str, int times) {
String ret = ""; String ret = "";
for (int i = 0; i < times; i++) { for (int i = 0; i < times; i++) {
@ -144,5 +165,32 @@ public class TestUtils {
} }
return ret; return ret;
} }
public static String colorString(String str, TermColor color) {
return color.toString() + str + TermColor.RESET;
}
public enum TermColor {
RESET("\u001B[0m"),
BLACK("\u001B[30m"),
RED("\u001B[31m"),
GREEN("\u001B[32m"),
YELLOW("\u001B[33m"),
BLUE("\u001B[34m"),
PURPLE("\u001B[35m"),
CYAN("\u001B[36m"),
WHITE("\u001B[37m");
private final String text;
TermColor(final String text) {
this.text = text;
}
@Override
public String toString() {
return text;
}
}
} }

6
test/java/foundation/pEp/jniadapter/test/framework/examples/ctxmembers/TestMain.java

@ -19,7 +19,7 @@ class CtxMembersTestContext extends AbstractTestContext {
@Override @Override
public void init() throws Throwable { public void init() throws Throwable {
log("=== OUTPUT FROM TEST CONTEXT INIT BEGIN ==="); log("=== OUTPUT FROM TEST CONTEXT INIT BEGIN ===");
log("HelloWorldTestContext: init() called"); log(getTestContextName()+ " - init() called");
correct = new ExampleCtxMember(true); correct = new ExampleCtxMember(true);
name = "UnitTestFrameWorkWithoutAName"; name = "UnitTestFrameWorkWithoutAName";
log("=== OUTPUT FROM TEST CONTEXT INIT END ==="); log("=== OUTPUT FROM TEST CONTEXT INIT END ===");
@ -28,7 +28,7 @@ class CtxMembersTestContext extends AbstractTestContext {
class TestMain { class TestMain {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
TestUnit test = new TestUnit<CtxMembersTestContext>("Hello World",new CtxMembersTestContext() , ctx -> { TestUnit test = new TestUnit<CtxMembersTestContext>("ctxmembers",new CtxMembersTestContext() , ctx -> {
// do stuff using the context // do stuff using the context
// Test FAILS on unhandled exception, otherwise SUCCESS // Test FAILS on unhandled exception, otherwise SUCCESS
log("=== OUTPUT FROM THE TEST ITSELF BEGIN ==="); log("=== OUTPUT FROM THE TEST ITSELF BEGIN ===");
@ -50,7 +50,7 @@ class ExampleCtxMember {
if(correct) { if(correct) {
log("Hya from member obj used correctly"); log("Hya from member obj used correctly");
} else { } else {
log("Hya from member obj used wrong"); log("Hya from member obj used wrong", TestUtils.TermColor.RED);
} }
} }
} }

15
test/java/foundation/pEp/jniadapter/test/framework/examples/testsuite/TestMain.java

@ -2,7 +2,7 @@ package foundation.pEp.jniadapter.test.framework.examples.testsuite;
import static foundation.pEp.jniadapter.test.framework.TestLogger.*; import static foundation.pEp.jniadapter.test.framework.TestLogger.*;
import foundation.pEp.jniadapter.test.framework.*; import foundation.pEp.jniadapter.test.framework.*;
class TestSuiteTestContext extends AbstractTestContext { class TestSuiteContext extends AbstractTestContext {
String name; String name;
@Override @Override
@ -15,26 +15,27 @@ class TestMain {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
// TestSuite.setVerbose(true); // TestSuite.setVerbose(true);
new TestUnit<TestSuiteTestContext>("Unit Test 1", new TestSuiteTestContext(), ctx -> { new TestUnit<TestSuiteContext>("Unit Test 1", new TestSuiteContext(), ctx -> {
log("=== OUTPUT FROM THE TEST ITSELF BEGIN ==="); log("=== OUTPUT FROM THE TEST ITSELF BEGIN ===");
log("Unit Test 1 " + ctx.name); log("Unit Test 1 " + ctx.name);
ctx.name = "new name"; ctx.name = "new name";
log("=== OUTPUT FROM THE TEST ITSELF END ==="); log("=== OUTPUT FROM THE TEST ITSELF END ===");
}).add(); }).add();
new TestUnit<TestSuiteTestContext>("Unit Test 2", new TestSuiteTestContext(), ctx -> { new TestUnit<TestSuiteContext>("Unit Test 3", new TestSuiteContext(), ctx -> {
log("=== OUTPUT FROM THE TEST ITSELF BEGIN ==="); log("=== OUTPUT FROM THE TEST ITSELF BEGIN ===");
log("Unit Test 2 " + ctx.name); log("Unit Test 3 Failing " + ctx.name);
int x = 4 / 0;
log("=== OUTPUT FROM THE TEST ITSELF END ==="); log("=== OUTPUT FROM THE TEST ITSELF END ===");
}).add(); }).add();
new TestUnit<TestSuiteTestContext>("Unit Test 3", new TestSuiteTestContext(), ctx -> { new TestUnit<TestSuiteContext>("Unit Test 2", new TestSuiteContext(), ctx -> {
log("=== OUTPUT FROM THE TEST ITSELF BEGIN ==="); log("=== OUTPUT FROM THE TEST ITSELF BEGIN ===");
log("Unit Test 3 Failing " + ctx.name); log("Unit Test 2 " + ctx.name);
int x = 4 / 0;
log("=== OUTPUT FROM THE TEST ITSELF END ==="); log("=== OUTPUT FROM THE TEST ITSELF END ===");
}).add(); }).add();
TestSuite.run(); TestSuite.run();
} }
} }
Loading…
Cancel
Save