diff --git a/test/java/foundation/pEp/jniadapter/test/framework/AbstractTestContext.java b/test/java/foundation/pEp/jniadapter/test/framework/AbstractTestContext.java index f9eb4b8..0de51ea 100644 --- a/test/java/foundation/pEp/jniadapter/test/framework/AbstractTestContext.java +++ b/test/java/foundation/pEp/jniadapter/test/framework/AbstractTestContext.java @@ -3,7 +3,11 @@ package foundation.pEp.jniadapter.test.framework; public abstract class AbstractTestContext implements TestContextInterface{ private boolean isInitialized = false; private boolean isUninitializable = false; - private String testContextName = "AbstractTestContext"; + private String testContextName = "Undefined"; + + public AbstractTestContext() { + setTestContextName(this.getClass().getSimpleName()); + } public boolean isInitialized() { return isInitialized; diff --git a/test/java/foundation/pEp/jniadapter/test/framework/TestResult.java b/test/java/foundation/pEp/jniadapter/test/framework/TestResult.java new file mode 100644 index 0000000..57d6073 --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/framework/TestResult.java @@ -0,0 +1,8 @@ +package foundation.pEp.jniadapter.test.framework; + +public enum TestResult { + UNEVALUATED, + SKIPPED, + SUCCESS, + FAILED; +} \ No newline at end of file diff --git a/test/java/foundation/pEp/jniadapter/test/framework/TestState.java b/test/java/foundation/pEp/jniadapter/test/framework/TestState.java new file mode 100644 index 0000000..5fde124 --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/framework/TestState.java @@ -0,0 +1,12 @@ +package foundation.pEp.jniadapter.test.framework; + +public enum TestState { + UNEVALUATED, + SKIPPED, + SUCCESS, + FAILED, + STARTING, + CTX_INIT, + CTX_INIT_FAILED, + RUNNING; +} \ No newline at end of file diff --git a/test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java b/test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java index 559d920..9e8ba5b 100644 --- a/test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java +++ b/test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java @@ -8,12 +8,17 @@ public class TestUnit implements Runnable { private String testUnitName = "default test unit"; private T ctx; private Consumer lambda; + + private TestResult result = TestResult.UNEVALUATED; + private TestState state = TestState.UNEVALUATED; + private Throwable lastException; + private boolean verboseMode = true; // Defaults (line width 80) private int logFmtTestNameLen = 35; - private int logFmtCtxNameLen = 24; - private int logFmtMsgLen = 8; + private int logFmtCtxNameLen = 25; + private int logFmtMsgLen = 12; public TestUnit(String name, T context, Consumer lambda) { this.testUnitName = name; @@ -30,66 +35,101 @@ public class TestUnit implements Runnable { this.verboseMode = verboseMode; } + public TestResult getResult() { + return result; + } + + public Throwable getLastException() { + return lastException; + } + public TestUnit add() { TestSuite.add(this); return this; } public void run() { - if (!verboseMode) TestUtils.standardOutErrDisable(true); + TestUtils.standardOutErrEnabled(verboseMode); + if (ctx.isUninitializable()) { + setTestState(TestState.CTX_INIT_FAILED); + return; + } try { - if (!ctx.isUninitializable()) { - // Init the Context if not already done - if (!ctx.isInitialized()) { - //Context init problems need to throw to fail - try { - ctxInit(); - } catch (Throwable t) { - ctx.setUninitializable(true); - throw new TestFrameWorkContextInitFailedException(); - } - ctx.setInitialized(true); + setTestState(TestState.STARTING); + // Init the Context if not already done + if (!ctx.isInitialized()) { + //Context init problems need to throw to fail + try { + setTestState(TestState.CTX_INIT); + ctx.init(); + } catch (Throwable t) { + lastException = t; + setTestState(TestState.CTX_INIT_FAILED); + return; } - //tests need to throw to fail - runTest(); - if (!verboseMode) TestUtils.standardOutErrDisable(false); - testSuceeded(); - } else { - throw new TestFrameWorkContextUnitializableException(); + ctx.setInitialized(true); } + //tests need to throw to fail + setTestState(TestState.RUNNING); + lambda.accept(ctx); + setTestState(TestState.SUCCESS); } catch (Throwable t) { - if (!verboseMode) TestUtils.standardOutErrDisable(false); - testFailed(t); + lastException = t; + setTestState(TestState.FAILED); return; } + TestUtils.standardOutErrEnabled(true); } - private void ctxInit() throws Throwable{ - logH1(makeLogString("CTX INIT")); - ctx.init(); - } + private void setTestState(TestState s) { + state = s; - private void runTest() throws Throwable{ - logH1(makeLogString("STARTING")); - //tests need to throw to fail - lambda.accept(ctx); + switch (state) { + case UNEVALUATED: { + setTestResult(TestResult.UNEVALUATED); + break; + } + case SKIPPED: { + setTestResult(TestResult.SKIPPED); + break; + } + case SUCCESS: { + setTestResult(TestResult.SUCCESS); + break; + } + case FAILED: { + setTestResult(TestResult.FAILED); + break; + } + case STARTING: + case CTX_INIT: + case RUNNING: { + logH1(makeLogString(state.toString())); + break; + } + case CTX_INIT_FAILED: { + logH1(makeLogString(state.toString())); + setTestResult(TestResult.SKIPPED); + break; + } + } } - private void testSuceeded() { - logH1(makeLogString("SUCCESS")); + private void setTestResult(TestResult r) { + result = r; + TestUtils.standardOutErrEnabled(true); + logH1(makeLogString(result.toString())); + if( r != TestResult.SUCCESS) { + log("ERROR: " + getLastException().toString()); + } if (verboseMode) logRaw("\n\n"); - } - - private void testFailed(Throwable t) { - logH1(makeLogString("FAILED")); - log("ERROR: " + t.toString()); - if (verboseMode) logRaw("\n\n"); + TestUtils.standardOutErrEnabled(verboseMode); } 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); + logFmtTestNameLen = (int) Math.floor(TestLogger.getLineWidth() * 0.39); + logFmtCtxNameLen = (int) Math.floor(TestLogger.getLineWidth() * 0.28); + logFmtMsgLen = (int) Math.floor(TestLogger.getLineWidth() * 0.25); } private String makeLogString(String str) { @@ -100,10 +140,3 @@ public class TestUnit implements Runnable { } } -class TestFrameWorkContextInitFailedException extends Exception { - -} - -class TestFrameWorkContextUnitializableException extends Exception { - -} \ No newline at end of file diff --git a/test/java/foundation/pEp/jniadapter/test/framework/TestUtils.java b/test/java/foundation/pEp/jniadapter/test/framework/TestUtils.java index c243d2b..a9fcce2 100644 --- a/test/java/foundation/pEp/jniadapter/test/framework/TestUtils.java +++ b/test/java/foundation/pEp/jniadapter/test/framework/TestUtils.java @@ -11,13 +11,13 @@ public class TestUtils { private static boolean stderrHasBeenDisabled = false; private static PrintStream origSTDERR; - public static void standardOutErrDisable(boolean mute) { - standardOutDisabled(mute); - standardErrDisabled(mute); + public static void standardOutErrEnabled(boolean mute) { + standardOutEnabled(mute); + standardErrEnabled(mute); } - public static void standardOutDisabled(boolean disable) { - if (disable) { + public static void standardOutEnabled(boolean enable) { + if (!enable) { origSTDOUT = System.out; stdoutHasBeenDisabled = true; System.setOut(new PrintStream(new OutputStream() { @@ -32,8 +32,8 @@ public class TestUtils { } } - public static void standardErrDisabled(boolean disable) { - if (disable) { + public static void standardErrEnabled(boolean enable) { + if (!enable) { origSTDOUT = System.err; stderrHasBeenDisabled = true; System.setErr(new PrintStream(new OutputStream() { diff --git a/test/java/foundation/pEp/jniadapter/test/framework/examples/Makefile.conf b/test/java/foundation/pEp/jniadapter/test/framework/examples/Makefile.conf index 4d0c988..e90272d 100644 --- a/test/java/foundation/pEp/jniadapter/test/framework/examples/Makefile.conf +++ b/test/java/foundation/pEp/jniadapter/test/framework/examples/Makefile.conf @@ -7,5 +7,8 @@ JAVA_CLASSES_FRAMEWORK= \ ../../TestSuite.class \ ../../TestUnit.class \ ../../TestContextInterface.class \ + ../../AbstractTestContext.class \ ../../TestLogger.class \ - ../../TestUtils.class + ../../TestUtils.class \ + ../../TestState.class \ + ../../TestResult.class diff --git a/test/java/foundation/pEp/jniadapter/test/framework/examples/ctxinitfail/TestMain.java b/test/java/foundation/pEp/jniadapter/test/framework/examples/ctxinitfail/TestMain.java index 6c2b49c..91fdedb 100644 --- a/test/java/foundation/pEp/jniadapter/test/framework/examples/ctxinitfail/TestMain.java +++ b/test/java/foundation/pEp/jniadapter/test/framework/examples/ctxinitfail/TestMain.java @@ -15,7 +15,7 @@ class CtxInitFailContext extends AbstractTestContext { class TestMain { public static void main(String[] args) throws Exception { -// TestSuite.setVerbose(true); + TestSuite.setVerbose(true); new TestUnit("ctxinitfail1",new CtxInitFailContext() , ctx -> { // do stuff using the context 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 54930db..202c2c4 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 @@ -11,7 +11,7 @@ import foundation.pEp.jniadapter.test.framework.*; // Otherwise, all test contexts in a program partially execute before the test using it is actually being run // Context init() is part of the test -class HelloWorldTestContext extends AbstractTestContext { +class CtxMembersTestContext extends AbstractTestContext { String name; ExampleCtxMember correct; ExampleCtxMember incorrect = new ExampleCtxMember(false); // WRONG @@ -26,15 +26,17 @@ class HelloWorldTestContext extends AbstractTestContext { class TestMain { public static void main(String[] args) throws Exception { - new TestUnit("Hello World",new HelloWorldTestContext() , ctx -> { + TestUnit test = new TestUnit("Hello World",new CtxMembersTestContext() , ctx -> { // do stuff using the context // Test FAILS on unhandled exception, otherwise SUCCESS log("Hello World from: " + ctx.name); - }).run(); - } -} + }); + TestUtils.sleep(2000); + test.run(); + } +} 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 35a6c43..23cbe5b 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 @@ -17,19 +17,20 @@ class TestMain { new TestUnit("Hello World1",new HelloWorldTestContext() , ctx -> { // do stuff using the context // Test FAILS on unhandled exception, otherwise SUCCESS - log("Hello World 1 from: " + ctx.name); + log("OK 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); + log("OK 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); + log("Failing Hello World 3 from: " + ctx.name); + throw new RuntimeException(); }).run(); } } diff --git a/test/java/foundation/pEp/jniadapter/test/framework/examples/testsuite/TestMain.java b/test/java/foundation/pEp/jniadapter/test/framework/examples/testsuite/TestMain.java index 80cc074..dbd9951 100644 --- a/test/java/foundation/pEp/jniadapter/test/framework/examples/testsuite/TestMain.java +++ b/test/java/foundation/pEp/jniadapter/test/framework/examples/testsuite/TestMain.java @@ -13,7 +13,7 @@ class TestSuiteTestContext extends AbstractTestContext { class TestMain { public static void main(String[] args) throws Exception { - TestSuite.setVerbose(true); +// TestSuite.setVerbose(true); new TestUnit("Unit Test 1", new TestSuiteTestContext(), ctx -> { log("Unit Test 1 " + ctx.name);