Created
October 24, 2010 17:45
-
-
Save thesurlydev/643711 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
...package imports omitted for brevity... | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@TestExecutionListeners({ | |
TransactionalTestExecutionListener.class, | |
DependencyInjectionTestExecutionListener.class, | |
DirtiesContextTestExecutionListener.class | |
}) | |
@Transactional | |
public abstract class StrutsSpringTransactionalTests extends StrutsTestCase implements ApplicationContextAware { | |
protected final Log logger = LogFactory.getLog(getClass()); | |
protected DataSource dataSource; | |
/** | |
* The {@link org.springframework.context.ApplicationContext} that was injected into this test instance | |
* via {@link #setApplicationContext(org.springframework.context.ApplicationContext)}. | |
*/ | |
protected ApplicationContext applicationContext; | |
public abstract void setDataSource(DataSource dataSource); | |
/** | |
* Set the {@link ApplicationContext} to be used by this test instance, | |
* provided via {@link ApplicationContextAware} semantics. | |
*/ | |
public final void setApplicationContext(final ApplicationContext applicationContext) { | |
this.applicationContext = applicationContext; | |
} | |
/** | |
* The SimpleJdbcTemplate that this base class manages, available to subclasses. | |
*/ | |
protected SimpleJdbcTemplate simpleJdbcTemplate; | |
private String sqlScriptEncoding; | |
/** | |
* Specify the encoding for SQL scripts, if different from the platform encoding. | |
* | |
* @see #executeSqlScript | |
*/ | |
public void setSqlScriptEncoding(String sqlScriptEncoding) { | |
this.sqlScriptEncoding = sqlScriptEncoding; | |
} | |
/** | |
* Count the rows in the given table. | |
* | |
* @param tableName table name to count rows in | |
* @return the number of rows in the table | |
*/ | |
protected int countRowsInTable(String tableName) { | |
return SimpleJdbcTestUtils.countRowsInTable(this.simpleJdbcTemplate, tableName); | |
} | |
/** | |
* Convenience method for deleting all rows from the specified tables. | |
* Use with caution outside of a transaction! | |
* | |
* @param names the names of the tables from which to delete | |
* @return the total number of rows deleted from all specified tables | |
*/ | |
protected int deleteFromTables(String... names) { | |
return SimpleJdbcTestUtils.deleteFromTables(this.simpleJdbcTemplate, names); | |
} | |
/** | |
* Execute the given SQL script. Use with caution outside of a transaction! | |
* <p>The script will normally be loaded by classpath. There should be one statement | |
* per line. Any semicolons will be removed. <b>Do not use this method to execute | |
* DDL if you expect rollback.</b> | |
* | |
* @param sqlResourcePath the Spring resource path for the SQL script | |
* @param continueOnError whether or not to continue without throwing an | |
* exception in the event of an error | |
* @throws org.springframework.dao.DataAccessException | |
* if there is an error executing a statement | |
* and continueOnError was <code>false</code> | |
*/ | |
protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) | |
throws DataAccessException { | |
Resource resource = this.applicationContext.getResource(sqlResourcePath); | |
SimpleJdbcTestUtils.executeSqlScript( | |
this.simpleJdbcTemplate, new EncodedResource(resource, this.sqlScriptEncoding), continueOnError); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment