diff --git a/test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java b/test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java index 034ff21..def0b4d 100644 --- a/test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java +++ b/test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java @@ -5,50 +5,85 @@ import static foundation.pEp.jniadapter.test.framework.TestLogger.*; import java.util.function.Consumer; public class TestUnit implements Runnable { - String testUnitName = "default test unit"; - T ctx; - Consumer lambda; + private String testUnitName = "default test unit"; + private T ctx; + private Consumer lambda; + + private boolean verboseMode = true; + + // Defaults (line width 80) + private int logFmtTestNameLen = 35; + private int logFmtCtxNameLen = 24; + private int logFmtMsgLen = 8; + private String logFormat = ""; + + public boolean isVerboseMode() { + return verboseMode; + } + + public void setVerboseMode(boolean verboseMode) { + this.verboseMode = verboseMode; + } public TestUnit(String name, T context, Consumer lambda) { this.testUnitName = name; this.lambda = lambda; this.ctx = context; + + logFmtTestNameLen = (int) Math.floor(TestLogger.getLineWidth() * 0.45); + logFmtCtxNameLen = (int) Math.floor(TestLogger.getLineWidth() * 0.3); + logFmtMsgLen = (int) Math.floor(TestLogger.getLineWidth() * 0.2); } public void run() { if (ctx.isUninitializable()) { - // Context uninitializable - log("Context has been uninitializable"); + logH1("Skipping, context has been uninitializable"); } else { - // Init the Context - try { - if (!ctx.isInitialized()) { - logH1("TEST: '" + testUnitName + "' ==== CTX: '" + ctx.getTestContextName() + "' ===== CTX INIT"); + + // Init the Context if not already done + if (!ctx.isInitialized()) { + try { + if (!verboseMode) TestUtils.standardOutErrDisable(true); + logH1(logString("CTX INIT")); ctx.init(); ctx.setInitialized(true); + if (!verboseMode) TestUtils.standardOutErrDisable(false); + } catch (Throwable t) { + //Context init problems need to throw to fail + if (!verboseMode) TestUtils.standardOutErrDisable(false); + ctx.setUninitializable(true); + logH1(logString("CTX FAIL")); + log(t.toString()); + logRaw("\n"); + return; } - } catch (Throwable t) { - //Context Init problems need to throw for fail - logH1("TEST: '" + testUnitName + "' ==== CTX: '" + ctx.getTestContextName() + "' ===== CTX FAIL"); - log(t.toString()); - spacer(); - ctx.setUninitializable(true); - return; } // Run the test - logH1("TEST: '" + testUnitName + "' ==== CTX: '" + ctx.getTestContextName() + "' ===== STARTING"); try { + if (!verboseMode) TestUtils.standardOutErrDisable(true); + logH1(logString("STARTING")); lambda.accept(ctx); + if (!verboseMode) TestUtils.standardOutErrDisable(false); + logH1(logString("SUCCESS")); + if (verboseMode) logRaw("\n\n"); } catch (Throwable t) { //Test fails, upon cought exception, otherwise succeeds - logH1("TEST: '" + testUnitName + "' ==== CTX: '" + ctx.getTestContextName() + "' ===== FAILED"); + if (!verboseMode) TestUtils.standardOutErrDisable(false); + logH1(logString("FAILED")); log(t.toString()); - spacer(); + if (verboseMode) logRaw("\n\n"); return; } - logH1("TEST: '" + testUnitName + "' ==== CTX: '" + ctx.getTestContextName() + "' ===== SUCCESS"); - spacer(); } } + + private String logString(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, ".. "); + return testUnitNameFmtd + testCtxNameFmtd + strFmtd; + } + + }