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;
import static foundation.pEp.jniadapter.test.framework.TestUtils.TermColor;
public class TestLogger {
// options
private static boolean logEnabled = true;
@ -24,6 +26,7 @@ public class TestLogger {
TestLogger.lineWidth = lineWidth;
}
// Log
public static void log(String msg) {
if (logEnabled) {
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) {
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) {
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) {
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;
import static foundation.pEp.jniadapter.test.framework.TestLogger.*;
import static foundation.pEp.jniadapter.test.framework.TestUtils.TermColor;
import java.util.ArrayList;
@ -25,5 +27,26 @@ public class TestSuite {
t.setVerboseMode(verbose);
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;
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;
@ -52,6 +54,7 @@ public class TestUnit<T extends TestContextInterface> implements Runnable {
TestUtils.standardOutErrEnabled(verboseMode);
if (ctx.isUninitializable()) {
setTestState(TestState.CTX_INIT_FAILED);
TestUtils.standardOutErrEnabled(true);
return;
}
try {
@ -61,23 +64,32 @@ public class TestUnit<T extends TestContextInterface> implements Runnable {
//Context init problems need to throw to fail
try {
setTestState(TestState.CTX_INIT);
setTermColor(TermColor.BLUE);
ctx.init();
setTermColor(TermColor.RESET);
} catch (Throwable t) {
lastException = t;
setTermColor(TermColor.RESET);
setTestState(TestState.CTX_INIT_FAILED);
TestUtils.standardOutErrEnabled(true);
return;
}
ctx.setInitialized(true);
}
//tests need to throw to fail
setTestState(TestState.RUNNING);
setTermColor(TermColor.CYAN);
lambda.accept(ctx);
setTermColor(TermColor.RESET);
setTestState(TestState.SUCCESS);
} catch (Throwable t) {
lastException = t;
setTermColor(TermColor.RESET);
setTestState(TestState.FAILED);
TestUtils.standardOutErrEnabled(true);
return;
}
setTermColor(TermColor.RESET);
TestUtils.standardOutErrEnabled(true);
}
@ -117,8 +129,16 @@ public class TestUnit<T extends TestContextInterface> implements Runnable {
private void setTestResult(TestResult 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);
logH1(makeLogString(result.toString()));
logH1(makeLogString(resultStr));
if( r != TestResult.SUCCESS) {
log("ERROR: " + getLastException().toString());
}
@ -133,9 +153,9 @@ public class TestUnit<T extends TestContextInterface> implements Runnable {
}
private String makeLogString(String str) {
String testUnitNameFmtd = TestUtils.fixedWidthPaddedString(" TEST: '" + testUnitName + "' ", "=", logFmtTestNameLen, TestUtils.Alignment.Left, ".. ");
String testCtxNameFmtd = TestUtils.fixedWidthPaddedString(" CTX: '" + ctx.getTestContextName() + "' ", "=", logFmtCtxNameLen, TestUtils.Alignment.Center, ".. ");
String strFmtd = TestUtils.fixedWidthPaddedString(" " + str + " ", "=", logFmtMsgLen, TestUtils.Alignment.Right, ".. ");
String testUnitNameFmtd = TestUtils.padOrClipString(" TEST: '" + testUnitName + "' ", "=", logFmtTestNameLen, TestUtils.Alignment.Left, ".. ");
String testCtxNameFmtd = TestUtils.padOrClipString(" CTX: '" + ctx.getTestContextName() + "' ", "=", logFmtCtxNameLen, TestUtils.Alignment.Center, ".. ");
String strFmtd = TestUtils.padOrClipString(" " + str + " ", "=", logFmtMsgLen, TestUtils.Alignment.Right, ".. ");
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.util.HashMap;
import static foundation.pEp.jniadapter.test.framework.TestLogger.log;
public class TestUtils {
private static boolean stdoutHasBeenDisabled = false;
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 padStr = repeatString(padChar, len);
padStr = padStr.substring(0, len);
if (str.length() <= len) {
int strLen = str.length();
len += (substringOccurencesCount(str, "\u001B") * 4);
if (strLen <= len) {
if (alignment == Alignment.Left) {
ret = str + repeatString(padChar, len - str.length());
ret = str + repeatString(padChar, len - strLen);
}
if (alignment == Alignment.Right) {
ret = repeatString(padChar, len - str.length()) + str;
ret = repeatString(padChar, len - strLen) + str;
}
if (alignment == Alignment.Center) {
int padAmt = len - str.length();
int padAmt = len - strLen;
String pad = repeatString(padChar, (int) Math.ceil(padAmt / 2) + 1);
ret = pad + str + pad;
ret = clipString(ret, len, null);
@ -114,6 +116,25 @@ public class TestUtils {
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) {
String ret = "";
for (int i = 0; i < times; i++) {
@ -144,5 +165,32 @@ public class TestUtils {
}
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
public void init() throws Throwable {
log("=== OUTPUT FROM TEST CONTEXT INIT BEGIN ===");
log("HelloWorldTestContext: init() called");
log(getTestContextName()+ " - init() called");
correct = new ExampleCtxMember(true);
name = "UnitTestFrameWorkWithoutAName";
log("=== OUTPUT FROM TEST CONTEXT INIT END ===");
@ -28,7 +28,7 @@ class CtxMembersTestContext extends AbstractTestContext {
class TestMain {
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
// Test FAILS on unhandled exception, otherwise SUCCESS
log("=== OUTPUT FROM THE TEST ITSELF BEGIN ===");
@ -50,7 +50,7 @@ class ExampleCtxMember {
if(correct) {
log("Hya from member obj used correctly");
} 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 foundation.pEp.jniadapter.test.framework.*;
class TestSuiteTestContext extends AbstractTestContext {
class TestSuiteContext extends AbstractTestContext {
String name;
@Override
@ -15,26 +15,27 @@ class TestMain {
public static void main(String[] args) throws Exception {
// 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("Unit Test 1 " + ctx.name);
ctx.name = "new name";
log("=== OUTPUT FROM THE TEST ITSELF END ===");
}).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("Unit Test 2 " + ctx.name);
log("Unit Test 3 Failing " + ctx.name);
int x = 4 / 0;
log("=== OUTPUT FROM THE TEST ITSELF END ===");
}).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("Unit Test 3 Failing " + ctx.name);
int x = 4 / 0;
log("Unit Test 2 " + ctx.name);
log("=== OUTPUT FROM THE TEST ITSELF END ===");
}).add();
TestSuite.run();
}
}
Loading…
Cancel
Save