Dear all,

I'm using StrutsSpringTestCase for some under-the-skin tests.

However, I felt the need to set up my database for a test. My tests that
need the database extend from
AbstractTransactionalJUnit4SpringContextTests, which makes them
transactional.


I therefore created a StrutsSpringTransactionalTestCase that merges the
behaviour of both StrutsSpringTestCase and
AbstractTransactionalJUnit4SpringContextTests. I currently just
copy-pasted the relevant information from
AbstractTransactionalJUnit4SpringContextTests.

I'd like you to take a look at it and comment on whether it makes sense
to include this in Struts. If so, feel free to use my contribution.

Cheers,

Miguel Almeida
package org.apache.struts2;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;

/**
 * Class to extend from when you want both the {@link StrutsSpringTestCase} and
 * the {@link AbstractTransactionalJUnit4SpringContextTests} behaviour.
 * 
 * @author malmeida
 * 
 */
@Transactional
@TestExecutionListeners({ TransactionalTestExecutionListener.class,
        DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class })
public class StrutsSpringTransactionalTestCase extends StrutsSpringTestCase {

    @Autowired
    protected ApplicationContext applicationContext;

    /**
     * Overrides the previous in order to skip applicationContext assignment:
     * context is @autowired
     * 
     * @see org.apache.struts2.StrutsSpringTestCase#setupBeforeInitDispatcher()
     **/
    @Override
    protected void setupBeforeInitDispatcher() throws Exception {
        // init context

        servletContext.setAttribute(
                WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
                applicationContext);
    }

    /**
     * The SimpleJdbcTemplate that this base class manages, available to
     * subclasses.
     */
    protected SimpleJdbcTemplate simpleJdbcTemplate;

    private String sqlScriptEncoding;

    /**
     * Copied from
     * {@link AbstractTransactionalJUnit4SpringContextTests#setDataSource(DataSource)}
     */
    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
    }

    /**
     * Copied from
     * {@link AbstractTransactionalJUnit4SpringContextTests#setSqlScriptEncoding(String)}
     */
    public void setSqlScriptEncoding(String sqlScriptEncoding) {
        this.sqlScriptEncoding = sqlScriptEncoding;
    }

    /**
     * Copied from
     * {@link AbstractTransactionalJUnit4SpringContextTests#countRowsInTable}
     */
    protected int countRowsInTable(String tableName) {
        return SimpleJdbcTestUtils.countRowsInTable(this.simpleJdbcTemplate,
                tableName);
    }

    /**
     * Copied from
     * {@link AbstractTransactionalJUnit4SpringContextTests#deleteFromTables}
     */
    protected int deleteFromTables(String... names) {
        return SimpleJdbcTestUtils.deleteFromTables(this.simpleJdbcTemplate,
                names);
    }

    /**
     * Copied from
     * {@link AbstractTransactionalJUnit4SpringContextTests#executeSqlScript}
     */
    protected void executeSqlScript(String sqlResourcePath,
            boolean continueOnError) throws DataAccessException {
        Resource resource = applicationContext.getResource(sqlResourcePath);
        SimpleJdbcTestUtils.executeSqlScript(this.simpleJdbcTemplate,
                new EncodedResource(resource, this.sqlScriptEncoding),
                continueOnError);
    }

}

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@struts.apache.org
For additional commands, e-mail: dev-h...@struts.apache.org

Reply via email to