|
|
@ -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 { |
|
|
|
|
|
|
|
} |