Browse Source

TestFrameWork add TestResult / TestState

JNI-96
heck 5 years ago
parent
commit
cb769d6d42
  1. 6
      test/java/foundation/pEp/jniadapter/test/framework/AbstractTestContext.java
  2. 8
      test/java/foundation/pEp/jniadapter/test/framework/TestResult.java
  3. 12
      test/java/foundation/pEp/jniadapter/test/framework/TestState.java
  4. 129
      test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java
  5. 14
      test/java/foundation/pEp/jniadapter/test/framework/TestUtils.java
  6. 5
      test/java/foundation/pEp/jniadapter/test/framework/examples/Makefile.conf
  7. 2
      test/java/foundation/pEp/jniadapter/test/framework/examples/ctxinitfail/TestMain.java
  8. 12
      test/java/foundation/pEp/jniadapter/test/framework/examples/ctxmembers/TestMain.java
  9. 7
      test/java/foundation/pEp/jniadapter/test/framework/examples/helloworld/TestMain.java
  10. 2
      test/java/foundation/pEp/jniadapter/test/framework/examples/testsuite/TestMain.java

6
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;

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

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

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

@ -8,12 +8,17 @@ public class TestUnit<T extends TestContextInterface> implements Runnable {
private String testUnitName = "default test unit";
private T ctx;
private Consumer<T> 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<T> lambda) {
this.testUnitName = name;
@ -30,66 +35,101 @@ public class TestUnit<T extends TestContextInterface> implements Runnable {
this.verboseMode = verboseMode;
}
public TestResult getResult() {
return result;
}
public Throwable getLastException() {
return lastException;
}
public TestUnit<T> 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<T extends TestContextInterface> implements Runnable {
}
}
class TestFrameWorkContextInitFailedException extends Exception {
}
class TestFrameWorkContextUnitializableException extends Exception {
}

14
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() {

5
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

2
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<CtxInitFailContext>("ctxinitfail1",new CtxInitFailContext() , ctx -> {
// do stuff using the context

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

7
test/java/foundation/pEp/jniadapter/test/framework/examples/helloworld/TestMain.java

@ -17,19 +17,20 @@ class TestMain {
new TestUnit<HelloWorldTestContext>("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<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);
log("OK 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);
log("Failing Hello World 3 from: " + ctx.name);
throw new RuntimeException();
}).run();
}
}

2
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<TestSuiteTestContext>("Unit Test 1", new TestSuiteTestContext(), ctx -> {
log("Unit Test 1 " + ctx.name);

Loading…
Cancel
Save