Search in sources :

Example 1 with MockBehaviorCommand

use of org.dbflute.mock.MockBehaviorCommand in project dbflute-core by dbflute.

the class BehaviorCommandInvokerTest method test_dispatchInvoking_whitebox_logDisabled.

public void test_dispatchInvoking_whitebox_logDisabled() {
    // ## 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!");
        }

        @Override
        protected long systemTime() {
            throw new IllegalStateException("systemTime() should not be called!");
        }
    };
    // ## Act ##
    final Object actualResult = invoker.dispatchInvoking(new MockBehaviorCommand() {

        @Override
        public Object[] getSqlExecutionArgument() {
            return args;
        }
    });
    // ## 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(5, markList.size());
}
Also used : ArrayList(java.util.ArrayList) MockBehaviorCommand(org.dbflute.mock.MockBehaviorCommand) MockBehaviorCommand(org.dbflute.mock.MockBehaviorCommand)

Example 2 with MockBehaviorCommand

use of org.dbflute.mock.MockBehaviorCommand 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 3 with MockBehaviorCommand

use of org.dbflute.mock.MockBehaviorCommand in project dbflute-core by dbflute.

the class BehaviorCommandInvokerTest method test_dispatchInvoking_whitebox_logEnabled.

public void test_dispatchInvoking_whitebox_logEnabled() {
    // ## 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 true;
        }

        @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) {
            markList.add("logReturn");
            log("before=" + before + ", after=" + after);
            assertTrue(before > 0);
            assertTrue(after > 0);
            assertTrue((after - before) > 999);
        }
    };
    // ## Act ##
    final Object actualResult = invoker.dispatchInvoking(new MockBehaviorCommand() {

        @Override
        public Object[] getSqlExecutionArgument() {
            return args;
        }
    });
    // ## 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("logReturn", markList.get(5));
    assertEquals(6, markList.size());
}
Also used : ArrayList(java.util.ArrayList) MockBehaviorCommand(org.dbflute.mock.MockBehaviorCommand) MockBehaviorCommand(org.dbflute.mock.MockBehaviorCommand)

Example 4 with MockBehaviorCommand

use of org.dbflute.mock.MockBehaviorCommand in project dbflute-core by dbflute.

the class TnAbstractBasicSqlHandlerTest method prepareMockBehaviorCommand.

protected void prepareMockBehaviorCommand() {
    MockBehaviorCommand behaviorCommand = new MockBehaviorCommand();
    ResourceContext resourceContext = new ResourceContext();
    resourceContext.setBehaviorCommand(behaviorCommand);
    ResourceContext.setResourceContextOnThread(resourceContext);
}
Also used : MockBehaviorCommand(org.dbflute.mock.MockBehaviorCommand) ResourceContext(org.dbflute.bhv.core.context.ResourceContext)

Example 5 with MockBehaviorCommand

use of org.dbflute.mock.MockBehaviorCommand in project dbflute-core by dbflute.

the class BehaviorCommandInvokerTest method test_dispatchInvoking_initializeOnly.

public void test_dispatchInvoking_initializeOnly() {
    // ## Arrange ##
    final List<String> markList = new ArrayList<String>();
    BehaviorCommandInvoker invoker = new BehaviorCommandInvoker() {

        @Override
        protected boolean isLogEnabled() {
            markList.add("isLogEnabled");
            return true;
        }

        @Override
        protected <RESULT> void initializeSqlExecution(BehaviorCommand<RESULT> behaviorCommand) {
            markList.add("initializeSqlExecution");
        }

        @Override
        protected <RESULT> SqlExecution findSqlExecution(BehaviorCommand<RESULT> behaviorCommand) {
            throw new IllegalStateException("findSqlExecution should not be called!");
        }

        @Override
        protected long deriveCommandBeforeAfterTimeIfNeeds(boolean logEnabled, boolean existsSqlResultHandler) {
            throw new IllegalStateException("deriveCommandBeforeAfterTimeIfNeeds should not be called!");
        }

        @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 = invoker.dispatchInvoking(new MockBehaviorCommand() {

        @Override
        public boolean isInitializeOnly() {
            return true;
        }
    });
    // ## Assert ##
    assertNull(actualResult);
    assertEquals("isLogEnabled", markList.get(0));
    assertEquals("initializeSqlExecution", markList.get(1));
    assertEquals(2, markList.size());
}
Also used : MockBehaviorCommand(org.dbflute.mock.MockBehaviorCommand) ArrayList(java.util.ArrayList) MockBehaviorCommand(org.dbflute.mock.MockBehaviorCommand)

Aggregations

MockBehaviorCommand (org.dbflute.mock.MockBehaviorCommand)7 ArrayList (java.util.ArrayList)4 SqlResultHandler (org.dbflute.hook.SqlResultHandler)3 SqlResultInfo (org.dbflute.hook.SqlResultInfo)3 HashSet (java.util.HashSet)2 SqlLogInfo (org.dbflute.hook.SqlLogInfo)2 SqlLogDisplaySqlBuilder (org.dbflute.hook.SqlLogInfo.SqlLogDisplaySqlBuilder)2 ResourceContext (org.dbflute.bhv.core.context.ResourceContext)1 CallbackContext (org.dbflute.hook.CallbackContext)1