diff --git a/test/java/foundation/pEp/jniadapter/test/framework/TestLogger.java b/test/java/foundation/pEp/jniadapter/test/framework/TestLogger.java index 7997f92..9d1fcca 100644 --- a/test/java/foundation/pEp/jniadapter/test/framework/TestLogger.java +++ b/test/java/foundation/pEp/jniadapter/test/framework/TestLogger.java @@ -1,8 +1,3 @@ -// -// Source code recreated from a .class file by IntelliJ IDEA -// (powered by Fernflower decompiler) -// - package foundation.pEp.jniadapter.test.framework; public class TestLogger { @@ -40,17 +35,14 @@ public class TestLogger { String logStr = threadStr + separator + msg; System.out.println(logStr); } - } public static void logH1(String msg) { log(TestUtils.fixedWidthPaddedString(msg, "=", lineWidth, TestUtils.Alignment.Center, null)); -// log(getDecoratedString(msg, "=")); } public static void logH2(String msg) { log(TestUtils.fixedWidthPaddedString(msg, "-", lineWidth, TestUtils.Alignment.Center, null)); -// log(getDecoratedString(msg, "-")); } public static void logRaw(String msg) { diff --git a/test/java/foundation/pEp/jniadapter/test/framework/TestSuite.java b/test/java/foundation/pEp/jniadapter/test/framework/TestSuite.java new file mode 100644 index 0000000..55a1189 --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/framework/TestSuite.java @@ -0,0 +1,29 @@ +package foundation.pEp.jniadapter.test.framework; + +import java.util.ArrayList; + +public class TestSuite { + private static ArrayList tests = new ArrayList(); + private static boolean verbose = false; + + private TestSuite() { } + + public static boolean isVerbose() { + return verbose; + } + + public static void setVerbose(boolean v) { + verbose = v; + } + + public static void add(TestUnit t) { + tests.add(t); + } + + public static void run() { + for (TestUnit t : tests) { + t.setVerboseMode(verbose); + t.run(); + } + } +} diff --git a/test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java b/test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java index def0b4d..559d920 100644 --- a/test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java +++ b/test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java @@ -8,14 +8,19 @@ public class TestUnit implements Runnable { 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 TestUnit(String name, T context, Consumer lambda) { + this.testUnitName = name; + this.lambda = lambda; + this.ctx = context; + logLayout(); + } public boolean isVerboseMode() { return verboseMode; @@ -25,65 +30,80 @@ public class TestUnit implements Runnable { 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 TestUnit add() { + TestSuite.add(this); + return this; } public void run() { - if (ctx.isUninitializable()) { - logH1("Skipping, context has been uninitializable"); - } else { - - // 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) { + if (!verboseMode) TestUtils.standardOutErrDisable(true); + try { + if (!ctx.isUninitializable()) { + // Init the Context if not already done + if (!ctx.isInitialized()) { //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; + try { + ctxInit(); + } catch (Throwable t) { + ctx.setUninitializable(true); + throw new TestFrameWorkContextInitFailedException(); + } + ctx.setInitialized(true); } - } - - // Run the test - 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 + //tests need to throw to fail + runTest(); if (!verboseMode) TestUtils.standardOutErrDisable(false); - logH1(logString("FAILED")); - log(t.toString()); - if (verboseMode) logRaw("\n\n"); - return; + testSuceeded(); + } else { + throw new TestFrameWorkContextUnitializableException(); } + } catch (Throwable t) { + if (!verboseMode) TestUtils.standardOutErrDisable(false); + testFailed(t); + return; } } - private String logString(String str) { + private void ctxInit() throws Throwable{ + logH1(makeLogString("CTX INIT")); + ctx.init(); + } + + private void runTest() throws Throwable{ + logH1(makeLogString("STARTING")); + //tests need to throw to fail + lambda.accept(ctx); + } + + private void testSuceeded() { + logH1(makeLogString("SUCCESS")); + if (verboseMode) logRaw("\n\n"); + } + + private void testFailed(Throwable t) { + logH1(makeLogString("FAILED")); + log("ERROR: " + t.toString()); + if (verboseMode) logRaw("\n\n"); + } + + private void logLayout() { + logFmtTestNameLen = (int) Math.floor(TestLogger.getLineWidth() * 0.45); + logFmtCtxNameLen = (int) Math.floor(TestLogger.getLineWidth() * 0.3); + logFmtMsgLen = (int) Math.floor(TestLogger.getLineWidth() * 0.2); + } + + 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, ".. "); - return testUnitNameFmtd + testCtxNameFmtd + strFmtd; + return testUnitNameFmtd + testCtxNameFmtd + strFmtd; } +} +class TestFrameWorkContextInitFailedException extends Exception { } + +class TestFrameWorkContextUnitializableException extends Exception { + +} \ No newline at end of file diff --git a/test/java/foundation/pEp/jniadapter/test/framework/examples/ctxmembers/TestMain.java b/test/java/foundation/pEp/jniadapter/test/framework/examples/ctxmembers/TestMain.java index 24786c1..54930db 100644 --- a/test/java/foundation/pEp/jniadapter/test/framework/examples/ctxmembers/TestMain.java +++ b/test/java/foundation/pEp/jniadapter/test/framework/examples/ctxmembers/TestMain.java @@ -38,21 +38,6 @@ class TestMain { - - - - - - - - - - - - - - - // Just an example member obj class ExampleCtxMember { ExampleCtxMember(boolean correct){ diff --git a/test/java/foundation/pEp/jniadapter/test/framework/examples/helloworld/TestMain.java b/test/java/foundation/pEp/jniadapter/test/framework/examples/helloworld/TestMain.java index 284c6d0..35a6c43 100644 --- a/test/java/foundation/pEp/jniadapter/test/framework/examples/helloworld/TestMain.java +++ b/test/java/foundation/pEp/jniadapter/test/framework/examples/helloworld/TestMain.java @@ -14,10 +14,22 @@ class HelloWorldTestContext extends AbstractTestContext { class TestMain { public static void main(String[] args) throws Exception { - new TestUnit("Hello World",new HelloWorldTestContext() , ctx -> { + new TestUnit("Hello World1",new HelloWorldTestContext() , ctx -> { // do stuff using the context // Test FAILS on unhandled exception, otherwise SUCCESS - log("Hello World from: " + ctx.name); + log("Hello World 1 from: " + ctx.name); + }).run(); + + new TestUnit("Hello World2",new HelloWorldTestContext() , ctx -> { + // do stuff using the context + // Test FAILS on unhandled exception, otherwise SUCCESS + log("Hello World 2 from: " + ctx.name); + }).run(); + + new TestUnit("Hello World3",new HelloWorldTestContext() , ctx -> { + // do stuff using the context + // Test FAILS on unhandled exception, otherwise SUCCESS + log("Hello World 3 from: " + ctx.name); }).run(); } }