Search in sources :

Example 1 with SqlLogDisplaySqlBuilder

use of org.dbflute.hook.SqlLogInfo.SqlLogDisplaySqlBuilder in project dbflute-core by dbflute.

the class BehaviorCommandInvokerTest method test_callbackSqlResultHanler_basic.

public void test_callbackSqlResultHanler_basic() {
    // ## 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>();
    try {
        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);
        // ## 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);
                assertNull(info.getCause());
                markSet.add("handle()");
                log(info.getResult() + ":" + displaySql + ":" + actualBefore + ":" + actualAfter);
            }
        }, ret, before, after, null);
        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 2 with SqlLogDisplaySqlBuilder

use of org.dbflute.hook.SqlLogInfo.SqlLogDisplaySqlBuilder 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)

Aggregations

HashSet (java.util.HashSet)2 SqlLogInfo (org.dbflute.hook.SqlLogInfo)2 SqlLogDisplaySqlBuilder (org.dbflute.hook.SqlLogInfo.SqlLogDisplaySqlBuilder)2 SqlResultHandler (org.dbflute.hook.SqlResultHandler)2 SqlResultInfo (org.dbflute.hook.SqlResultInfo)2 MockBehaviorCommand (org.dbflute.mock.MockBehaviorCommand)2