diff --git a/test/java/foundation/pEp/jniadapter/test/framework/AbstractTestContext.java b/test/java/foundation/pEp/jniadapter/test/framework/AbstractTestContext.java index 92cb36d..f9eb4b8 100644 --- a/test/java/foundation/pEp/jniadapter/test/framework/AbstractTestContext.java +++ b/test/java/foundation/pEp/jniadapter/test/framework/AbstractTestContext.java @@ -1,5 +1,32 @@ package foundation.pEp.jniadapter.test.framework; -public interface AbstractTestContext { - void init() throws Throwable; +public abstract class AbstractTestContext implements TestContextInterface{ + private boolean isInitialized = false; + private boolean isUninitializable = false; + private String testContextName = "AbstractTestContext"; + + public boolean isInitialized() { + return isInitialized; + } + + public void setInitialized(boolean initialized) { + isInitialized = initialized; + } + + public boolean isUninitializable() { + return isUninitializable; + } + + public void setUninitializable(boolean uninitializable) { + isUninitializable = uninitializable; + } + + public String getTestContextName() { + return testContextName; + } + + public void setTestContextName(String name) { + this.testContextName = name; + } + } \ No newline at end of file diff --git a/test/java/foundation/pEp/jniadapter/test/framework/TestContextInterface.java b/test/java/foundation/pEp/jniadapter/test/framework/TestContextInterface.java new file mode 100644 index 0000000..5913874 --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/framework/TestContextInterface.java @@ -0,0 +1,11 @@ +package foundation.pEp.jniadapter.test.framework; + +public interface TestContextInterface { + void init() throws Throwable; + boolean isInitialized(); + void setInitialized(boolean initialized); + boolean isUninitializable(); + void setUninitializable(boolean uninitializable); + String getTestContextName(); + void setTestContextName(String name); +} diff --git a/test/java/foundation/pEp/jniadapter/test/framework/TestLogger.java b/test/java/foundation/pEp/jniadapter/test/framework/TestLogger.java index d3c594f..b33a0a9 100644 --- a/test/java/foundation/pEp/jniadapter/test/framework/TestLogger.java +++ b/test/java/foundation/pEp/jniadapter/test/framework/TestLogger.java @@ -37,16 +37,21 @@ public class TestLogger { } private static String getDecoratedString(String msg, String decoration) { - byte var2 = 80; + byte lineWidth = 80; String ret = ""; - for(int i = 0; (double)i < Math.ceil((double)((var2 - msg.length() + 2) / 2)); ++i) { + for(int i = 0; (double)i < Math.ceil((double)((lineWidth - msg.length() + 2) / 2)); ++i) { ret = ret + decoration; } return ret + " " + msg + " " + ret; } + public static void spacer() { + System.out.print('\n'); + } + + // Deprecated public static void logSectEnd(String msg) { log(msg + "\n"); } diff --git a/test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java b/test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java index 4df9ba2..034ff21 100644 --- a/test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java +++ b/test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java @@ -1,8 +1,10 @@ package foundation.pEp.jniadapter.test.framework; +import static foundation.pEp.jniadapter.test.framework.TestLogger.*; + import java.util.function.Consumer; -public class TestUnit implements Runnable { +public class TestUnit implements Runnable { String testUnitName = "default test unit"; T ctx; Consumer lambda; @@ -14,19 +16,39 @@ public class TestUnit implements Runnable { } public void run() { - TestLogger.logH1(testUnitName); + if (ctx.isUninitializable()) { + // Context uninitializable + log("Context has been uninitializable"); + } else { + // Init the Context + try { + if (!ctx.isInitialized()) { + logH1("TEST: '" + testUnitName + "' ==== CTX: '" + ctx.getTestContextName() + "' ===== CTX INIT"); + ctx.init(); + ctx.setInitialized(true); + } + } 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; + } - try { - //Init the Context - ctx.init(); - //Run the test against the context - lambda.accept(ctx); - } catch (Throwable t) { - //Test fails, upon cought exception, otherwise succeeds - TestLogger.logH1("TestUnit FAILED: " + t.toString()); - return; + // Run the test + logH1("TEST: '" + testUnitName + "' ==== CTX: '" + ctx.getTestContextName() + "' ===== STARTING"); + try { + lambda.accept(ctx); + } catch (Throwable t) { + //Test fails, upon cought exception, otherwise succeeds + logH1("TEST: '" + testUnitName + "' ==== CTX: '" + ctx.getTestContextName() + "' ===== FAILED"); + log(t.toString()); + spacer(); + return; + } + logH1("TEST: '" + testUnitName + "' ==== CTX: '" + ctx.getTestContextName() + "' ===== SUCCESS"); + spacer(); } - - TestLogger.logH2("SUCCESS!"); } } 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 8c12ccd..d283ba8 100644 --- a/test/java/foundation/pEp/jniadapter/test/framework/examples/Makefile.conf +++ b/test/java/foundation/pEp/jniadapter/test/framework/examples/Makefile.conf @@ -5,6 +5,6 @@ JAVA=java JAVA_CLASSES_FRAMEWORK= \ ../../TestUnit.class \ - ../../AbstractTestContext.class \ + ../../TestContextInterface.class \ ../../TestLogger.class \ ../../TestUtils.class 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 60f96b4..dd406e2 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 @@ -1,7 +1,9 @@ package foundation.pEp.jniadapter.test.framework.examples.helloworld; +import static foundation.pEp.jniadapter.test.framework.TestLogger.*; import foundation.pEp.jniadapter.test.framework.*; -class HelloWorldTestContext implements AbstractTestContext { + +class HelloWorldTestContext implements TestContextInterface { String name; @Override @@ -15,7 +17,7 @@ class TestMain { new TestUnit("Hello World",new HelloWorldTestContext() , ctx -> { // do stuff using the context // Test FAILS on unhandled exception, otherwise SUCCESS - TestLogger.log("Hello World from: " + ctx.name); + log("Hello World from: " + ctx.name); }).run(); } }