⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basecontrollertestcase.java

📁 基于Maven的质量保证自动化环境配置和演示程序
💻 JAVA
字号:
package com.cib.webapp.controller;import java.lang.reflect.AccessibleObject;import java.lang.reflect.Field;import java.util.*;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import com.cib.model.BaseObject;import com.cib.util.DateUtil;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.mail.javamail.JavaMailSenderImpl;import org.springframework.mock.web.MockHttpServletRequest;import org.springframework.mock.web.MockServletContext;import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;import org.springframework.util.StringUtils;import org.springframework.web.context.support.XmlWebApplicationContext;public abstract class BaseControllerTestCase extends AbstractTransactionalDataSourceSpringContextTests {    protected transient final Log log = LogFactory.getLog(getClass());    private int smtpPort = 25250;    protected String[] getConfigLocations() {        setAutowireMode(AUTOWIRE_BY_NAME);        return new String[] {                "classpath:/applicationContext-resources.xml",                "classpath:/applicationContext-dao.xml",                "classpath:/applicationContext-service.xml",                "classpath*:/applicationContext.xml", // for modular archetypes                "/WEB-INF/applicationContext*.xml",                "/WEB-INF/dispatcher-servlet.xml"            };    }    @Override    protected void onSetUpBeforeTransaction() throws Exception {        smtpPort = smtpPort + (int) (Math.random() * 100);        // change the port on the mailSender so it doesn't conflict with an        // existing SMTP server on localhost        JavaMailSenderImpl mailSender = (JavaMailSenderImpl) applicationContext.getBean("mailSender");        mailSender.setPort(getSmtpPort());        mailSender.setHost("localhost");    }    protected int getSmtpPort() {        return smtpPort;    }    /**     * Subclasses can invoke this to get a context key for the given location.     * This doesn't affect the applicationContext instance variable in this class.     * Dependency Injection cannot be applied from such contexts.     */    protected ConfigurableApplicationContext loadContextLocations(String[] locations) {        if (logger.isInfoEnabled()) {            logger.info("Loading additional configuration from: " + StringUtils.arrayToCommaDelimitedString(locations));        }        XmlWebApplicationContext ctx = new XmlWebApplicationContext();        ctx.setConfigLocations(locations);        ctx.setServletContext(new MockServletContext());        ctx.refresh();        return ctx;    }        /**     * Convenience methods to make tests simpler     * @return a MockHttpServletRequest with a POST to the specified URL     * @param url the URL to post to     */    public MockHttpServletRequest newPost(String url) {        return new MockHttpServletRequest("POST", url);    }    public MockHttpServletRequest newGet(String url) {        return new MockHttpServletRequest("GET", url);    }    public void objectToRequestParameters(Object o, MockHttpServletRequest request) throws Exception {        objectToRequestParameters(o, request, null);    }    public void objectToRequestParameters(Object o, MockHttpServletRequest request, String prefix) throws Exception {        Class clazz = o.getClass();        Field[] fields = getDeclaredFields(clazz);        AccessibleObject.setAccessible(fields, true);        for (Field f : fields) {            Object field = (f.get(o));            if (field != null) {                if (field instanceof BaseObject) {                    // Fix for http://issues.appfuse.org/browse/APF-429                    if (prefix != null) {                        objectToRequestParameters(field, request, prefix + "." + f.getName());                    } else {                        objectToRequestParameters(field, request, f.getName());                    }                } else if (!(field instanceof List) && !(field instanceof Set)) {                    String paramName = f.getName();                    if (prefix != null) {                        paramName = prefix + "." + paramName;                    }                    String paramValue = String.valueOf(f.get(o));                    // handle Dates                    if (field instanceof Date) {                        paramValue = DateUtil.convertDateToString((Date) f.get(o));                        if ("null".equals(paramValue)) paramValue = "";                    }                    request.addParameter(paramName, paramValue);                }            }        }    }    private Field[] getDeclaredFields(Class clazz) {        Field[] f = new Field[0];        Class superClazz = clazz.getSuperclass();        Collection<Field> rval = new ArrayList<Field>();                if (superClazz != null) {            rval.addAll(Arrays.asList(getDeclaredFields(superClazz)));        }                rval.addAll(Arrays.asList(clazz.getDeclaredFields()));        return rval.toArray(f);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -