package edu.northwestern.bioinformatics.studycalendar.testing;

import edu.nwu.bioinformatics.commons.testing.DbTestCase;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor;

import javax.sql.DataSource;

/**
 * @author Rhett Sutphin
 */
public abstract class DaoTestCase extends DbTestCase {
    private static ApplicationContext applicationContext = null;
    protected final Log log = LogFactory.getLog(getClass());

    protected MockHttpServletRequest request = new MockHttpServletRequest();
    protected MockHttpServletResponse response = new MockHttpServletResponse();
    private boolean shouldFlush = true;

    protected void setUp() throws Exception {
        super.setUp();
        beginSession();
    }

    protected void tearDown() throws Exception {
        endSession();
        super.tearDown();
    }

    public void runBare() throws Throwable {
        setUp();
        try {
            runTest();
        } catch (Throwable throwable) {
            shouldFlush = false;
            throw throwable;
        } finally {
            tearDown();
        }
    }

    private void beginSession() {
        log.info("-- beginning DaoTestCase interceptor session --");
        findOpenSessionInViewInterceptor().preHandle(request, response, null);
    }

    private void endSession() {
        log.info("--    ending DaoTestCase interceptor session --");
        OpenSessionInViewInterceptor interceptor = findOpenSessionInViewInterceptor();
        if (shouldFlush) {
            interceptor.postHandle(request, response, null, null);
        }
        interceptor.afterCompletion(request, response, null, null);
    }

    protected void interruptSession() {
        endSession();
        log.info("-- interrupted DaoTestCase session --");
        beginSession();
    }

    private OpenSessionInViewInterceptor findOpenSessionInViewInterceptor() {
        return (OpenSessionInViewInterceptor) getApplicationContext().getBean("openSessionInViewInterceptor");
    }


    protected DataSource getDataSource() {
        return (DataSource) getApplicationContext().getBean("dataSource");
    }

    public ApplicationContext getApplicationContext() {
        synchronized (DaoTestCase.class) {
            if (applicationContext == null) {
                applicationContext = ContextTools.createDeployedApplicationContext();
            }
            return applicationContext;
        }
    }
}

@@ -114,16 +114,67 @@
 estCase;
+%0Aimport edu.nwu.bioinformatics.commons.StringUtils;
 %0A%0Aimport
@@ -512,36 +512,272 @@
 or;%0A
-%0Aimport javax.sql.DataSource
+import org.springframework.jdbc.core.JdbcTemplate;%0Aimport org.springframework.jdbc.core.ColumnMapRowMapper;%0A%0Aimport javax.sql.DataSource;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.List;%0Aimport java.sql.SQLException;%0Aimport java.sql.ResultSet
 ;%0A%0A/
@@ -2983,11 +2983,1660 @@
  %7D%0A    %7D
+%0A%0A    protected final void dumpResults(String sql) %7B%0A        List%3CMap%3CString, String%3E%3E rows = new JdbcTemplate(getDataSource()).query(%0A            sql,%0A            new ColumnMapRowMapper() %7B%0A                protected Object getColumnValue(ResultSet rs, int index) throws SQLException %7B%0A                    Object value = super.getColumnValue(rs, index);%0A                    return value == null ? %22null%22 : value.toString();%0A                %7D%0A            %7D%0A        );%0A        StringBuffer dump = new StringBuffer(sql).append('%5Cn');%0A        if (rows.size() %3E 0) %7B%0A            Map%3CString, Integer%3E colWidths = new HashMap%3CString, Integer%3E();%0A            for (String colName : rows.get(0).keySet()) %7B%0A                colWidths.put(colName, colName.length());%0A                for (Map%3CString, String%3E row : rows) %7B%0A                    colWidths.put(colName, Math.max(colWidths.get(colName), row.get(colName).length()));%0A                %7D%0A            %7D%0A%0A            for (String colName : rows.get(0).keySet()) %7B%0A                StringUtils.appendWithPadding(colName, colWidths.get(colName), false, dump);%0A                dump.append(%22   %22);%0A            %7D%0A            dump.append('%5Cn');%0A%0A            for (Map%3CString, String%3E row : rows) %7B%0A                for (String colName : row.keySet()) %7B%0A                    StringUtils.appendWithPadding(row.get(colName), colWidths.get(colName), false, dump);%0A                    dump.append(%22 %7C %22);%0A                %7D%0A                dump.append('%5Cn');%0A            %7D%0A        %7D%0A        dump.append(%22  %22).append(rows.size()).append(%22 row%22).append(rows.size() != 1 ? %22s%5Cn%22 : %22%5Cn%22);%0A%0A        System.out.print(dump);%0A    %7D
 %0A%7D%0A

Add dumpResults debugging method