Browse Source

Enhance Testframework for shared context (across tests) with single initialization

JNI-96
heck 5 years ago
parent
commit
5a7fa52b9e
  1. 31
      test/java/foundation/pEp/jniadapter/test/framework/AbstractTestContext.java
  2. 11
      test/java/foundation/pEp/jniadapter/test/framework/TestContextInterface.java
  3. 9
      test/java/foundation/pEp/jniadapter/test/framework/TestLogger.java
  4. 48
      test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java
  5. 2
      test/java/foundation/pEp/jniadapter/test/framework/examples/Makefile.conf
  6. 6
      test/java/foundation/pEp/jniadapter/test/framework/examples/helloworld/TestMain.java

31
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;
}
}

11
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);
}

9
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");
}

48
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<T extends AbstractTestContext> implements Runnable {
public class TestUnit<T extends TestContextInterface> implements Runnable {
String testUnitName = "default test unit";
T ctx;
Consumer<T> lambda;
@ -14,19 +16,39 @@ public class TestUnit<T extends AbstractTestContext> 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!");
}
}

2
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

6
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<HelloWorldTestContext>("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();
}
}

Loading…
Cancel
Save