Search in sources :

Example 6 with SqlResultInfo

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());
}
Also used : SqlResultHandler(org.dbflute.hook.SqlResultHandler) ArrayList(java.util.ArrayList) SqlResultInfo(org.dbflute.hook.SqlResultInfo) MockBehaviorCommand(org.dbflute.mock.MockBehaviorCommand) MockBehaviorCommand(org.dbflute.mock.MockBehaviorCommand) CallbackContext(org.dbflute.hook.CallbackContext)

Aggregations

SqlResultInfo (org.dbflute.hook.SqlResultInfo)6 SqlLogInfo (org.dbflute.hook.SqlLogInfo)5 SqlResultHandler (org.dbflute.hook.SqlResultHandler)5 ArrayList (java.util.ArrayList)3 CallbackContext (org.dbflute.hook.CallbackContext)3 MockBehaviorCommand (org.dbflute.mock.MockBehaviorCommand)3 HashSet (java.util.HashSet)2 SqlLogDisplaySqlBuilder (org.dbflute.hook.SqlLogInfo.SqlLogDisplaySqlBuilder)2 SqlLogHandler (org.dbflute.hook.SqlLogHandler)1 ExecutionTimeInfo (org.dbflute.jdbc.ExecutionTimeInfo)1