Search in sources :

Example 6 with SqlResultHandler

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

the class BehaviorCommandInvokerTest method test_callbackSqlResultHanler_notExistsDisplaySql.

public void test_callbackSqlResultHanler_notExistsDisplaySql() {
    // ## Arrange ##
    BehaviorCommandInvoker invoker = new BehaviorCommandInvoker();
    final long before = 123;
    final long after = 456;
    final Object ret = new Object();
    MockBehaviorCommand mockCmd = new MockBehaviorCommand() {

        @Override
        public String getTableDbName() {
            return "FOO";
        }

        @Override
        public String getCommandName() {
            return "BAR";
        }
    };
    final HashSet<String> markSet = new HashSet<String>();
    SqlLogInfo sqlLogInfo = new SqlLogInfo(mockCmd, "select ...", new Object[] {}, new Class<?>[] {}, new SqlLogDisplaySqlBuilder() {

        public String build(String executedSql, Object[] bindArgs, Class<?>[] bindArgTypes) {
            return "select ...";
        }
    });
    InternalMapContext.setResultSqlLogInfo(sqlLogInfo);
    try {
        // ## Act & Assert ##
        invoker.callbackSqlResultHanler(mockCmd, new SqlResultHandler() {

            public void handle(SqlResultInfo info) {
                long actualBefore = info.getExecutionTimeInfo().getCommandBeforeTimeMillis();
                long actualAfter = info.getExecutionTimeInfo().getCommandAfterTimeMillis();
                assertEquals(ret, info.getResult());
                assertEquals("FOO", info.getMeta().getTableDbName());
                assertEquals("BAR", info.getMeta().getCommandName());
                String displaySql = info.getSqlLogInfo().getDisplaySql();
                assertEquals("select ...", displaySql);
                assertEquals(before, actualBefore);
                assertEquals(after, actualAfter);
                assertNotNull(info.getCause());
                markSet.add("handle()");
                log(info.getResult() + ":" + displaySql + ":" + actualBefore + ":" + actualAfter);
            }
        }, ret, before, after, new IllegalStateException());
        assertTrue(markSet.size() == 1);
        assertTrue(markSet.contains("handle()"));
    } finally {
        InternalMapContext.clearInternalMapContextOnThread();
    }
}
Also used : SqlResultHandler(org.dbflute.hook.SqlResultHandler) SqlLogDisplaySqlBuilder(org.dbflute.hook.SqlLogInfo.SqlLogDisplaySqlBuilder) SqlResultInfo(org.dbflute.hook.SqlResultInfo) MockBehaviorCommand(org.dbflute.mock.MockBehaviorCommand) SqlLogInfo(org.dbflute.hook.SqlLogInfo) HashSet(java.util.HashSet)

Example 7 with SqlResultHandler

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

SqlResultHandler (org.dbflute.hook.SqlResultHandler)7 SqlResultInfo (org.dbflute.hook.SqlResultInfo)5 CallbackContext (org.dbflute.hook.CallbackContext)4 SqlLogInfo (org.dbflute.hook.SqlLogInfo)4 ArrayList (java.util.ArrayList)3 MockBehaviorCommand (org.dbflute.mock.MockBehaviorCommand)3 HashSet (java.util.HashSet)2 SqlLogHandler (org.dbflute.hook.SqlLogHandler)2 SqlLogDisplaySqlBuilder (org.dbflute.hook.SqlLogInfo.SqlLogDisplaySqlBuilder)2 BehaviorCommandHook (org.dbflute.bhv.core.BehaviorCommandHook)1 SqlFireHook (org.dbflute.hook.SqlFireHook)1 SqlStringFilter (org.dbflute.hook.SqlStringFilter)1 RomanticTraceableSqlFireHook (org.lastaflute.db.dbflute.callbackcontext.traceablesql.RomanticTraceableSqlFireHook)1 RomanticTraceableSqlResultHandler (org.lastaflute.db.dbflute.callbackcontext.traceablesql.RomanticTraceableSqlResultHandler)1 RomanticTraceableSqlStringFilter (org.lastaflute.db.dbflute.callbackcontext.traceablesql.RomanticTraceableSqlStringFilter)1