Search in sources :

Example 6 with CallbackContext

use of org.dbflute.hook.CallbackContext in project dbflute-core by dbflute.

the class TnAbstractBasicSqlHandlerTest method test_logSql_whitebox_bigThree.

public void test_logSql_whitebox_bigThree() {
    // ## Arrange ##
    final List<String> markList = new ArrayList<String>();
    final Object[] args = new Object[] {};
    final Class<?>[] argsTypes = new Class<?>[] {};
    TnAbstractBasicSqlHandler handler = new TnAbstractBasicSqlHandler(null, null, null) {

        @Override
        protected String buildDisplaySql(String sql, Object[] args) {
            markList.add("getDisplaySql");
            return "select ..." + ln() + "  from ...";
        }

        @Override
        protected void logDisplaySql(String displaySql) {
            markList.add("logDisplaySql");
            assertEquals("select ..." + ln() + "  from ...", displaySql);
            super.logDisplaySql(displaySql);
        }

        @Override
        protected void log(String msg) {
            markList.add("log");
            assertEquals(ln() + "select ..." + ln() + "  from ...", msg);
        }

        @Override
        protected void saveResultSqlLogInfo(SqlLogInfo sqlLogInfo) {
            markList.add("saveResultSqlLogInfo");
            super.saveResultSqlLogInfo(sqlLogInfo);
        }

        @Override
        protected boolean isLogEnabled() {
            return true;
        }

        @Override
        protected void assertObjectNotNull(String variableName, Object value) {
        // for no check of constructor
        }
    };
    // ## Act ##
    try {
        CallbackContext callbackContext = new CallbackContext();
        callbackContext.setSqlLogHandler(new SqlLogHandler() {

            public void handle(SqlLogInfo info) {
                markList.add("handle");
                assertEquals("select ..." + ln() + "  from ...", info.getDisplaySql());
                assertEquals(newArrayList(args), newArrayList(info.getBindArgs()));
                assertEquals(newArrayList(argsTypes), newArrayList(info.getBindArgTypes()));
            }
        });
        callbackContext.setSqlResultHandler(new SqlResultHandler() {

            public void handle(SqlResultInfo sqlResultInfo) {
                throw new IllegalStateException("handle should not be called!");
            }
        });
        CallbackContext.setCallbackContextOnThread(callbackContext);
        handler.logSql(args, argsTypes);
        assertEquals("select ..." + ln() + "  from ...", InternalMapContext.getResultSqlLogInfo().getDisplaySql());
    } finally {
        CallbackContext.clearCallbackContextOnThread();
        InternalMapContext.clearInternalMapContextOnThread();
    }
    // ## Assert ##
    assertEquals(5, markList.size());
    assertEquals("getDisplaySql", markList.get(0));
    assertEquals("logDisplaySql", markList.get(1));
    assertEquals("log", markList.get(2));
    assertEquals("handle", markList.get(3));
    assertEquals("saveResultSqlLogInfo", markList.get(4));
}
Also used : SqlLogHandler(org.dbflute.hook.SqlLogHandler) SqlResultHandler(org.dbflute.hook.SqlResultHandler) ArrayList(java.util.ArrayList) SqlResultInfo(org.dbflute.hook.SqlResultInfo) SqlLogInfo(org.dbflute.hook.SqlLogInfo) CallbackContext(org.dbflute.hook.CallbackContext)

Example 7 with CallbackContext

use of org.dbflute.hook.CallbackContext 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

CallbackContext (org.dbflute.hook.CallbackContext)7 ArrayList (java.util.ArrayList)4 SqlResultHandler (org.dbflute.hook.SqlResultHandler)4 SqlLogHandler (org.dbflute.hook.SqlLogHandler)3 SqlLogInfo (org.dbflute.hook.SqlLogInfo)3 SqlResultInfo (org.dbflute.hook.SqlResultInfo)3 SqlStringFilter (org.dbflute.hook.SqlStringFilter)2 RomanticTraceableSqlStringFilter (org.lastaflute.db.dbflute.callbackcontext.traceablesql.RomanticTraceableSqlStringFilter)2 BehaviorCommandHook (org.dbflute.bhv.core.BehaviorCommandHook)1 ExecutedSqlCounter (org.dbflute.bhv.proposal.callback.ExecutedSqlCounter)1 AccessContext (org.dbflute.hook.AccessContext)1 SqlFireHook (org.dbflute.hook.SqlFireHook)1 MockBehaviorCommand (org.dbflute.mock.MockBehaviorCommand)1 PreparedAccessContext (org.lastaflute.db.dbflute.accesscontext.PreparedAccessContext)1 RomanticTraceableSqlFireHook (org.lastaflute.db.dbflute.callbackcontext.traceablesql.RomanticTraceableSqlFireHook)1 RomanticTraceableSqlResultHandler (org.lastaflute.db.dbflute.callbackcontext.traceablesql.RomanticTraceableSqlResultHandler)1