浏览 1744 次
|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2006-02-11
[ 本人原创] 同时支持STRUTS+SPRING和SessionInViewer的测试CASE!
代码如下: [code:1] package common.test; import java.io.*; import org.hibernate.*; import org.springframework.context.*; import org.springframework.context.support.*; import org.springframework.orm.hibernate3.*; import org.springframework.transaction.support.*; import org.springframework.web.context.*; import org.springframework.web.context.support.*; import servletunit.struts.*; /** * <p>Title: </p> * * <p>Description: </p> * * <p>Copyright: Copyright (c) 2005</p> * * <p>Company: </p> * * @author 段洪杰 * @version 1.0 */ public class SpringTestCase extends MockStrutsTestCase { /** * 项目基准路径 */ public static String BASE_DIRECTORY = "F:/working/infoweb3.0.3-power/infoweb"; public static String[] Configuration_Location = new String[] { BASE_DIRECTORY + "/WEB-INF/applicationContext-hibernate.xml", BASE_DIRECTORY + "/WEB-INF/infoContext-hibernate.xml", BASE_DIRECTORY + "/WEB-INF/applyInfoContext-hibernate.xml", BASE_DIRECTORY + "/WEB-INF/orderContext-hibernate.xml" }; public SpringTestCase(String testName) { super(testName); } public void setUp() throws Exception { super.setUp(); setContextDirectory(new File(BASE_DIRECTORY)); XmlWebApplicationContext wac = null; wac = new XmlWebApplicationContext(); wac.setServletContext(context); wac.setConfigLocations(Configuration_Location); wac.refresh(); // SessionFactory sessionFactory = (SessionFactory) wac.getBean("mySessionFactory"); Session session = SessionFactoryUtils.getSession(sessionFactory, true); session.setFlushMode(FlushMode.NEVER); TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session)); // context.setAttribute(WebApplicationContext. ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); } public void tearDown() throws Exception { super.tearDown(); } /** * 静态方法,作为helper方法调用 * service层测试ApplicationContext * @return ApplicationContext */ public static ApplicationContext getApplicationContext() { ApplicationContext ctx = new FileSystemXmlApplicationContext( Configuration_Location); //ApplicationContext ctx = new ClassPathXmlApplicationContext(paths); return ctx; } } [/code:1] 使用方法如下: [code:1] package infoweb.web; import common.test.*; import infoweb.pojo.*; /** * <p>Title: </p> * * <p>Description: </p> * * <p>Copyright: Copyright (c) 2005</p> * * <p>Company: </p> * * @author 段洪杰 * @version 1.0 */ public class TestClientInfoAction extends SpringTestCase { public TestClientInfoAction(String testName) { super(testName); } public void setUp() throws Exception { super.setUp(); setConfigFile("/WEB-INF/struts-config.xml"); setConfigFile("info_client", "/WEB-INF/struts-config-infoweb_client.xml"); setRequestPathInfo("/info_client", "/ClientInfoAction.do"); } public void tearDown() throws Exception { super.tearDown(); } public void testView() { addRequestParameter("method", "view"); addRequestParameter("id","402881ec077b150901077b1c45a20004"); actionPerform(); verifyForward("view"); Info info=(Info)request.getAttribute("info"); assertEquals("j-我的小店",info.getTitle()); verifyNoActionErrors(); } } [/code:1] 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
时间:2006-02-12
以前也写了这个,抽取出在一个独立的MockOpenSessionInViewFilter
setUp(),以支持Service与DAO,一样的测试.测试时在setUp里beginFilter(),tearDown()时调用endFilter()就行了 [code:1] public class MockOpenSessionInViewFilter { /** * Logger for this class */ private static final Logger log = Logger .getLogger(MockOpenSessionInViewFilter.class); private static ThreadLocal<Boolean> participate = new ThreadLocal<Boolean>(); private static Session session; /**default flushMode = FlushMode.NERVER*/ public static FlushMode flushMode = FlushMode.NEVER; /**defaul singleSession=true*/ private static boolean singleSession = true; /**must set sessionFactory*/ private static SessionFactory sessionFactory; public static void setSessionFactory(SessionFactory factory) { sessionFactory = factory; } public static void setSingleSession(boolean isSingle) { singleSession = isSingle; } protected static void closeSession() { // TransactionSynchronizationManager.unbindResource(sessionFactory); SessionFactoryUtils.releaseSession(session, sessionFactory); } protected static Session openSession() throws DataAccessResourceFailureException { Assert.notNull(sessionFactory); Session session = SessionFactoryUtils.getSession(sessionFactory, true); session.setFlushMode(flushMode); // TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session)); return session; } protected Session getSession() { return openSession(); } public static Session newSession() { closeSession(); return openSession(); } protected static boolean isSingleSession() { return singleSession; } public static void beginFilter() { participate.set(false); if (isSingleSession()) { // single session mode if (TransactionSynchronizationManager.hasResource(sessionFactory)) { // Do not modify the Session: just set the participate flag. participate.set(true); } else { log.info("Opening single Hibernate Session in MockOpenSessionInViewFilter"); session = openSession(); TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session)); } } else { // deferred close mode if (SessionFactoryUtils.isDeferredCloseActive(sessionFactory)) { // Do not modify deferred close: just set the participate flag. participate.set(true); } else { SessionFactoryUtils.initDeferredClose(sessionFactory); } } } public static void endFilter() { if (!participate.get()) { if (isSingleSession()) { // single session mode TransactionSynchronizationManager.unbindResource(sessionFactory); log.info("Closing single Hibernate Session in OpenSessionInViewFilter"); try { closeSession(); } catch (RuntimeException ex) { log.error("Unexpected exception on closing Hibernate Session", ex); } } else { // deferred close mode SessionFactoryUtils.processDeferredClose(sessionFactory); } } } } [/code:1] setUp()与tearDown()里的调用可以为这样 [code:1] final public void setUp() throws Exception { super.setUp(); applicationContext = getContext(); onSetUpBeforeTransaction(); MockOpenSessionInViewFilter.setSessionFactory(getSessionFactory()); MockOpenSessionInViewFilter.setSingleSession(isSingleSession()); MockOpenSessionInViewFilter.beginFilter(); onSetUpInTransaction(); } final public void tearDown() throws Exception { super.tearDown(); onTearDownInTransaction(); MockOpenSessionInViewFilter.endFilter(); onTearDownAfterTransaction(); } [/code:1] |
|
| 返回顶楼 | |





