📄 basedaotestcase.java
字号:
package org.appfuse.dao;
import java.io.FileInputStream;
import javax.naming.NamingException;
import javax.sql.DataSource;
import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.XmlDataSet;
import org.dbunit.operation.DatabaseOperation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.mock.jndi.SimpleNamingContextBuilder;
/**
* Base class for DAO TestCases.
* @author Matt Raible
*/
public class BaseDAOTestCase extends TestCase {
protected final Log log = LogFactory.getLog(getClass());
protected static ApplicationContext ctx = null;
private static IDatabaseConnection conn = null;
private static IDataSet dataSet = null;
/* Option 1: Reload context and database each time
protected void setUp() throws Exception {
String[] paths = {"/WEB-INF/applicationContext*.xml",
"/org/appfuse/dao/applicationContext-test.xml"};
ctx = new ClassPathXmlApplicationContext(paths);
DataSource ds = (DataSource) ctx.getBean("dataSource");
conn = new DatabaseConnection(ds.getConnection());
dataSet = new XmlDataSet(new FileInputStream("test/data/sample-data.xml"));
// clear table and insert only sample data
DatabaseOperation.CLEAN_INSERT.execute(conn, dataSet);
}
protected void tearDown() throws Exception {
// clear out database
DatabaseOperation.DELETE.execute(conn, dataSet);
conn.close();
conn = null;
}*/
/* Option 2: Use JUnit's TestSetup class
protected static void start() throws Exception {
String[] paths = {"/WEB-INF/applicationContext*.xml",
"/org/appfuse/dao/applicationContext-test.xml"};
ctx = new ClassPathXmlApplicationContext(paths);
DataSource ds = (DataSource) ctx.getBean("dataSource");
conn = new DatabaseConnection(ds.getConnection());
dataSet = new XmlDataSet(new FileInputStream("test/data/sample-data.xml"));
// clear table and insert only sample data
DatabaseOperation.CLEAN_INSERT.execute(conn, dataSet);
}
protected static void stop() throws Exception {
// clear out database
DatabaseOperation.DELETE.execute(conn, dataSet);
conn.close();
conn = null;
}*/
/* Option 3: Use a static initialization block, and reload database each time */
static {
try {
SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost/myusers");
ds.setUsername("root");
ds.setPassword("");
builder.bind("java:comp/env/jdbc/myusers", ds);
} catch (NamingException ne) {
// do nothing, test will fail on its own
}
String[] paths = {"/WEB-INF/applicationContext*.xml",
"/org/appfuse/dao/applicationContext-test.xml"};
ctx = new ClassPathXmlApplicationContext(paths);
}
protected void setUp() throws Exception {
DataSource ds = (DataSource) ctx.getBean("dataSource");
conn = new DatabaseConnection(ds.getConnection());
dataSet = new XmlDataSet(new FileInputStream("test/data/sample-data.xml"));
// clear table and insert only sample data
DatabaseOperation.CLEAN_INSERT.execute(conn, dataSet);
}
protected void tearDown() throws Exception {
// clear out database
DatabaseOperation.DELETE.execute(conn, dataSet);
conn.close();
conn = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -