use of org.dbflute.hook.SqlResultInfo in project dbflute-core by dbflute.
the class BehaviorCommandInvokerTest method test_dispatchInvoking_whitebox_logDisabled_sqlResultHandler.
public void test_dispatchInvoking_whitebox_logDisabled_sqlResultHandler() {
// ## Arrange ##
final List<String> markList = new ArrayList<String>();
final Object result = new Object();
final Object[] args = new Object[] { "foo", "bar" };
BehaviorCommandInvoker invoker = new BehaviorCommandInvoker() {
@Override
protected boolean isLogEnabled() {
markList.add("isLogEnabled");
return false;
}
@Override
protected <RESULT> void initializeSqlExecution(BehaviorCommand<RESULT> behaviorCommand) {
throw new IllegalStateException("initializeSqlExecution should not be called!");
}
@Override
protected <RESULT> SqlExecution findSqlExecution(BehaviorCommand<RESULT> behaviorCommand) {
markList.add("findSqlExecution");
return new SqlExecution() {
public Object execute(Object[] actualArgs) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
markList.add("SqlExecution.execute");
assertEquals(args[0], actualArgs[0]);
assertEquals(args[1], actualArgs[1]);
return result;
}
};
}
@Override
protected long deriveCommandBeforeAfterTimeIfNeeds(boolean logEnabled, boolean existsSqlResultHandler) {
if (markList.contains("deriveCommandBeforeAfterTimeIfNeeds")) {
markList.add("deriveCommandBeforeAfterTimeIfNeeds2");
} else {
markList.add("deriveCommandBeforeAfterTimeIfNeeds");
}
return super.deriveCommandBeforeAfterTimeIfNeeds(logEnabled, existsSqlResultHandler);
}
@Override
protected <RESULT> void logResult(BehaviorCommand<RESULT> behaviorCommand, Class<?> retType, Object ret, long before, long after) {
throw new IllegalStateException("logReturn() should not be called!");
}
};
// ## Act ##
final Object actualResult;
try {
CallbackContext callbackContext = new CallbackContext();
callbackContext.setSqlResultHandler(new SqlResultHandler() {
public void handle(SqlResultInfo info) {
markList.add("handle");
long before = info.getExecutionTimeInfo().getCommandBeforeTimeMillis();
long after = info.getExecutionTimeInfo().getCommandAfterTimeMillis();
log("before=" + before + ", after=" + after);
assertTrue(before > 0);
assertTrue(after > 0);
assertTrue((after - before) > 999);
}
});
CallbackContext.setCallbackContextOnThread(callbackContext);
actualResult = invoker.dispatchInvoking(new MockBehaviorCommand() {
@Override
public Object[] getSqlExecutionArgument() {
return args;
}
});
} finally {
CallbackContext.clearCallbackContextOnThread();
InternalMapContext.clearInternalMapContextOnThread();
}
// ## Assert ##
assertEquals(result, actualResult);
assertEquals("isLogEnabled", markList.get(0));
assertEquals("findSqlExecution", markList.get(1));
assertEquals("deriveCommandBeforeAfterTimeIfNeeds", markList.get(2));
assertEquals("SqlExecution.execute", markList.get(3));
assertEquals("deriveCommandBeforeAfterTimeIfNeeds2", markList.get(4));
assertEquals("handle", markList.get(5));
assertEquals(6, markList.size());
}
Aggregations