Browse Source

Testframework add TestSuite

JNI-96
heck 5 years ago
parent
commit
d5e75fd373
  1. 8
      test/java/foundation/pEp/jniadapter/test/framework/TestLogger.java
  2. 29
      test/java/foundation/pEp/jniadapter/test/framework/TestSuite.java
  3. 114
      test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java
  4. 15
      test/java/foundation/pEp/jniadapter/test/framework/examples/ctxmembers/TestMain.java
  5. 16
      test/java/foundation/pEp/jniadapter/test/framework/examples/helloworld/TestMain.java

8
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) {

29
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<TestUnit> tests = new ArrayList<TestUnit>();
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();
}
}
}

114
test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java

@ -8,14 +8,19 @@ public class TestUnit<T extends TestContextInterface> implements Runnable {
private String testUnitName = "default test unit";
private T ctx;
private Consumer<T> 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<T> lambda) {
this.testUnitName = name;
this.lambda = lambda;
this.ctx = context;
logLayout();
}
public boolean isVerboseMode() {
return verboseMode;
@ -25,65 +30,80 @@ public class TestUnit<T extends TestContextInterface> implements Runnable {
this.verboseMode = verboseMode;
}
public TestUnit(String name, T context, Consumer<T> 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<T> 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 {
}

15
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){

16
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<HelloWorldTestContext>("Hello World",new HelloWorldTestContext() , ctx -> {
new TestUnit<HelloWorldTestContext>("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<HelloWorldTestContext>("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<HelloWorldTestContext>("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();
}
}

Loading…
Cancel
Save