关闭

JUnit4测试基于Spring的Action组件

816人阅读 评论(0) 收藏 举报
分类:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.opensymphony.xwork2.ActionContext;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:com/sf/module/test/resource/spring/applicationContext.xml",
								 "classpath:com/sf/module/test/resource/spring/authorization.xml"})
public class BasicTestCase extends JdbcDaoSupport implements ApplicationContextAware
{
	protected static ApplicationContext applicationContext;
	
	private static final String REQUEST_NAME = "com.opensymphony.xwork2.dispatcher.HttpServletRequest";
	private static final String SERVLET_CONTEXT = "com.opensymphony.xwork2.dispatcher.ServletContext";
	
	
	@BeforeClass
	public static void beforeClass()
	{
		MockServletContext msc = new MockServletContext("com/mypackage/test");
		//msc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);
		
		MockHttpSession session = new MockHttpSession();
		session.setAttribute("123456", "zhang");
		
		MockHttpServletRequest request = new MockHttpServletRequest();
		request.setSession(session);
		
		ActionContext context = new ActionContext(new HashMap<String, Object>());
		context.put(REQUEST_NAME, request);
		context.put(SERVLET_CONTEXT, msc);
		
		ActionContext.setContext(context);
	}
	
	
	public static void addMockParameters(Map<String, Object> params)
	{
		ActionContext.getContext().setParameters(params);
		
		((MockHttpServletRequest)(ActionContext.getContext().get(REQUEST_NAME))).setParameters(params);
	}
	
	
	public static File getImportFile(String classPath) throws Exception
	{
		int bufferSize = 16384;
		byte buffer[] = new byte[bufferSize];
		
		File file = new File("./temp.xls");
		if(!file.exists())
		{
			file.createNewFile();
		}
		
		OutputStream fos = new FileOutputStream(file);
		BufferedOutputStream bos = new BufferedOutputStream(fos, bufferSize);
		
		InputStream is = BasicTestCase.class.getClassLoader().getResourceAsStream(classPath);
		BufferedInputStream bis = new BufferedInputStream(is, bufferSize);
		
		int readSize = -1;
		while (null != bis && (readSize = bis.read(buffer)) != -1)
		{
			bos.write(buffer, 0, readSize);
		}
		
		bos.close();
		bis.close();
		
		return file;
	}
	


	@Override
	public void setApplicationContext(ApplicationContext appContext) throws BeansException
	{
		applicationContext = appContext;
	}
	
	
	public static ApplicationContext getApplicationContext() throws BeansException
	{
		return applicationContext;
	}

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    文章分类