package edu.northwestern.bioinformatics.studycalendar.testing;

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

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 org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ColumnMapRowMapper;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.ServletWebRequest;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.sql.SQLException;
import java.sql.ResultSet;

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

    protected MockHttpServletRequest request = new MockHttpServletRequest();
    protected MockHttpServletResponse response = new MockHttpServletResponse();
    protected WebRequest webRequest = new ServletWebRequest(request);
    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(webRequest);
    }

    private void endSession() {
        log.info("--    ending DaoTestCase interceptor session --");
        OpenSessionInViewInterceptor interceptor = findOpenSessionInViewInterceptor();
        if (shouldFlush) {
            interceptor.postHandle(webRequest, null);
        }
        interceptor.afterCompletion(webRequest, 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 static ApplicationContext getApplicationContext() {
        return StudyCalendarTestCase.getDeployedApplicationContext();
    }

    protected final void dumpResults(String sql) {
        List<Map<String, String>> rows = new JdbcTemplate(getDataSource()).query(
            sql,
            new ColumnMapRowMapper() {
                protected Object getColumnValue(ResultSet rs, int index) throws SQLException {
                    Object value = super.getColumnValue(rs, index);
                    return value == null ? "null" : value.toString();
                }
            }
        );
        StringBuffer dump = new StringBuffer(sql).append('\n');
        if (rows.size() > 0) {
            Map<String, Integer> colWidths = new HashMap<String, Integer>();
            for (String colName : rows.get(0).keySet()) {
                colWidths.put(colName, colName.length());
                for (Map<String, String> row : rows) {
                    colWidths.put(colName, Math.max(colWidths.get(colName), row.get(colName).length()));
                }
            }

            for (String colName : rows.get(0).keySet()) {
                StringUtils.appendWithPadding(colName, colWidths.get(colName), false, dump);
                dump.append("   ");
            }
            dump.append('\n');

            for (Map<String, String> row : rows) {
                for (String colName : row.keySet()) {
                    StringUtils.appendWithPadding(row.get(colName), colWidths.get(colName), false, dump);
                    dump.append(" | ");
                }
                dump.append('\n');
            }
        }
        dump.append("  ").append(rows.size()).append(" row").append(rows.size() != 1 ? "s\n" : "\n");

        System.out.print(dump);
    }
}

@@ -99,26 +99,19 @@
 ons.
-testing.DbTestCase
+StringUtils
 ;%0Aim
@@ -146,27 +146,34 @@
 commons.
-StringUtils
+testing.DbTestCase
 ;%0A%0Aimpor
@@ -307,16 +307,124 @@
 ontext;%0A
+import org.springframework.jdbc.core.ColumnMapRowMapper;%0Aimport org.springframework.jdbc.core.JdbcTemplate;%0A
 import o
@@ -647,87 +647,45 @@
 ork.
-jdbc.core.JdbcTemplate;%0Aimport org.springframework.jdbc.core.ColumnMapRowMapper
+web.context.request.ServletWebRequest
 ;%0Aim
@@ -745,75 +745,65 @@
 st;%0A
+%0A
 import 
-org.springframework.web.context.request.ServletWebReques
+javax.sql.DataSource;%0Aimport java.sql.ResultSe
 t;%0A
-%0A
 impo
@@ -809,32 +809,33 @@
 ort java
-x
 .sql.
-DataSource
+SQLException
 ;%0Aimport
@@ -858,30 +858,8 @@
 ap;%0A
-import java.util.Map;%0A
 impo
@@ -893,51 +893,16 @@
 ava.
-sql.SQLException;%0Aimport java.sql.ResultSet
+util.Map
 ;%0A%0A/

Optimize imports