#14 开始整理Modules的单元测试,提高测试覆盖率,并顺手改进代码。
This commit is contained in:
@@ -93,12 +93,12 @@ public class DataFixtures {
|
||||
protected static IDatabaseConnection getConnection(DataSource dataSource) throws DatabaseUnitException,
|
||||
SQLException {
|
||||
Connection connection = dataSource.getConnection();
|
||||
String dbName = connection.getMetaData().getURL();
|
||||
if (StringUtils.contains(dbName, ":h2:")) {
|
||||
String jdbcUrl = connection.getMetaData().getURL();
|
||||
if (StringUtils.contains(jdbcUrl, ":h2:")) {
|
||||
return new H2Connection(connection, null);
|
||||
} else if (StringUtils.contains(dbName, ":mysql:")) {
|
||||
} else if (StringUtils.contains(jdbcUrl, ":mysql:")) {
|
||||
return new MySqlConnection(connection, null);
|
||||
} else if (StringUtils.contains(dbName, ":oracle:")) {
|
||||
} else if (StringUtils.contains(jdbcUrl, ":oracle:")) {
|
||||
return new OracleConnection(connection, null);
|
||||
} else {
|
||||
return new DatabaseConnection(connection);
|
||||
|
||||
@@ -4,6 +4,11 @@ import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
/**
|
||||
* 在Spring Context启动时,使用DBUnit一次性初始化数据。
|
||||
*
|
||||
* @author calvin
|
||||
*/
|
||||
public class DataInitializer implements InitializingBean {
|
||||
|
||||
private DataSource dataSource;
|
||||
|
||||
@@ -18,9 +18,6 @@ public class RandomData {
|
||||
|
||||
private static Random random = new Random();
|
||||
|
||||
private RandomData() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回随机ID.
|
||||
*/
|
||||
@@ -39,7 +36,8 @@ public class RandomData {
|
||||
* 从输入list中随机返回一个对象.
|
||||
*/
|
||||
public static <T> T randomOne(List<T> list) {
|
||||
return randomSome(list, 1).get(0);
|
||||
Collections.shuffle(list);
|
||||
return list.get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,9 +19,6 @@ public class JettyFactory {
|
||||
|
||||
private static final String DEFAULT_WEBAPP_PATH = "src/main/webapp";
|
||||
|
||||
private JettyFactory() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建用于开发运行调试的Jetty Server, 以src/main/webapp为Web应用目录.
|
||||
*/
|
||||
|
||||
-3
@@ -24,9 +24,6 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
public class WebDriverFactory {
|
||||
|
||||
private WebDriverFactory() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据driverName创建各种WebDriver的简便方法.
|
||||
*
|
||||
|
||||
@@ -45,16 +45,6 @@ public class Log4jMockAppender extends AppenderSkeleton {
|
||||
return getFirstLog().getMessage().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回之前append的第一个log的按layout pattern格式化的信息.
|
||||
*/
|
||||
public String getFirstRenderedMessage() {
|
||||
if (logs.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return getLayout().format(getFirstLog());
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回之前append的最后一个log.
|
||||
*/
|
||||
@@ -69,20 +59,10 @@ public class Log4jMockAppender extends AppenderSkeleton {
|
||||
* 返回之前append的最后一个log的信息.
|
||||
*/
|
||||
public String getLastMessage() {
|
||||
if (getLastLog() == null) {
|
||||
return null;
|
||||
}
|
||||
return getLastLog().getMessage().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回之前append的最后一个log的按layout pattern格式化的信息.
|
||||
*/
|
||||
public String getLastRenderedMessage() {
|
||||
if (logs.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return getLayout().format(getLastLog());
|
||||
return getLastLog().getMessage().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+15
-13
@@ -10,22 +10,15 @@ import org.apache.shiro.subject.support.SubjectThreadState;
|
||||
import org.apache.shiro.util.ThreadState;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
public class ShiroTestHelper {
|
||||
/**
|
||||
* 在单元测试中的Shiro工具类,
|
||||
*
|
||||
* @author calvin
|
||||
*/
|
||||
public class ShiroTestUtils {
|
||||
|
||||
private static ThreadState threadState;
|
||||
|
||||
private ShiroTestHelper() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 綁定Subject到當前線程.
|
||||
*/
|
||||
public static void bindSubject(Subject subject) {
|
||||
clearSubject();
|
||||
threadState = new SubjectThreadState(subject);
|
||||
threadState.bind();
|
||||
}
|
||||
|
||||
/**
|
||||
* 用Mockito快速創建一個已認證的用户.
|
||||
*/
|
||||
@@ -37,6 +30,15 @@ public class ShiroTestHelper {
|
||||
bindSubject(subject);
|
||||
}
|
||||
|
||||
/**
|
||||
* 綁定Subject到當前線程.
|
||||
*/
|
||||
protected static void bindSubject(Subject subject) {
|
||||
clearSubject();
|
||||
threadState = new SubjectThreadState(subject);
|
||||
threadState.bind();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除當前線程中的Subject.
|
||||
*/
|
||||
-88
@@ -1,88 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2005-2012 springside.org.cn
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
*/
|
||||
package org.springside.modules.test.spring;
|
||||
|
||||
import org.apache.commons.lang3.Validate;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
|
||||
/**
|
||||
* 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候取出ApplicaitonContext.
|
||||
*
|
||||
* @author calvin
|
||||
*/
|
||||
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {
|
||||
|
||||
private static ApplicationContext applicationContext = null;
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class);
|
||||
|
||||
/**
|
||||
* 取得存储在静态变量中的ApplicationContext.
|
||||
*/
|
||||
public static ApplicationContext getApplicationContext() {
|
||||
assertContextInjected();
|
||||
return applicationContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
||||
*/
|
||||
public static <T> T getBean(String name) {
|
||||
assertContextInjected();
|
||||
return (T) applicationContext.getBean(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
||||
*/
|
||||
public static <T> T getBean(Class<T> requiredType) {
|
||||
assertContextInjected();
|
||||
return applicationContext.getBean(requiredType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除SpringContextHolder中的ApplicationContext为Null.
|
||||
*/
|
||||
public static void clearHolder() {
|
||||
logger.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
|
||||
applicationContext = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 实现ApplicationContextAware接口, 注入Context到静态变量中.
|
||||
*/
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) {
|
||||
logger.debug("注入ApplicationContext到SpringContextHolder:{}", applicationContext);
|
||||
|
||||
if (SpringContextHolder.applicationContext != null) {
|
||||
logger.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:"
|
||||
+ SpringContextHolder.applicationContext);
|
||||
}
|
||||
|
||||
SpringContextHolder.applicationContext = applicationContext; //NOSONAR
|
||||
}
|
||||
|
||||
/**
|
||||
* 实现DisposableBean接口, 在Context关闭时清理静态变量.
|
||||
*/
|
||||
@Override
|
||||
public void destroy() throws Exception {
|
||||
SpringContextHolder.clearHolder();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查ApplicationContext不为空.
|
||||
*/
|
||||
private static void assertContextInjected() {
|
||||
Validate.validState(applicationContext != null,
|
||||
"applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder.");
|
||||
}
|
||||
}
|
||||
-58
@@ -1,58 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2005-2012 springside.org.cn
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
*/
|
||||
package org.springside.modules.test.spring;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.context.ConfigurableWebApplicationContext;
|
||||
import org.springframework.web.context.ContextLoader;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.support.XmlWebApplicationContext;
|
||||
|
||||
/**
|
||||
* 将Spring WebApplicationContext初始化到ServletContext的集成测试工具类.
|
||||
*
|
||||
* @author calvin
|
||||
*/
|
||||
public class SpringWebTestHelper {
|
||||
|
||||
private SpringWebTestHelper() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 在ServletContext里初始化Spring WebApplicationContext.
|
||||
*
|
||||
* @param configLocations application context文件路径列表.
|
||||
*/
|
||||
public static void initWebApplicationContext(MockServletContext servletContext, String... configLocations) {
|
||||
String configLocationsString = StringUtils.join(configLocations, ",");
|
||||
servletContext.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, configLocationsString);
|
||||
new ContextLoader().initWebApplicationContext(servletContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* 在ServletContext里初始化Spring WebApplicationContext.
|
||||
*
|
||||
* @param applicationContext 已创建的ApplicationContext.
|
||||
*/
|
||||
public static void initWebApplicationContext(MockServletContext servletContext,
|
||||
ApplicationContext applicationContext) {
|
||||
ConfigurableWebApplicationContext wac = new XmlWebApplicationContext();
|
||||
wac.setParent(applicationContext);
|
||||
wac.setServletContext(servletContext);
|
||||
wac.setConfigLocation("");
|
||||
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
wac.refresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭ServletContext中的Spring WebApplicationContext.
|
||||
*/
|
||||
public static void closeWebApplicationContext(MockServletContext servletContext) {
|
||||
new ContextLoader().closeWebApplicationContext(servletContext);
|
||||
}
|
||||
}
|
||||
+10
-1
@@ -15,15 +15,25 @@ public class Log4jMockAppenderTest {
|
||||
Log4jMockAppender appender = new Log4jMockAppender();
|
||||
appender.addToLogger(Log4jMockAppenderTest.class);
|
||||
|
||||
//null
|
||||
assertNull(appender.getFirstLog());
|
||||
assertNull(appender.getLastLog());
|
||||
assertNull(appender.getFirstMessage());
|
||||
assertNull(appender.getFirstMessage());
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(Log4jMockAppenderTest.class);
|
||||
logger.warn(testString1);
|
||||
logger.warn(testString2);
|
||||
|
||||
//getFirstLog/getLastLog
|
||||
assertEquals(testString1, appender.getFirstLog().getMessage());
|
||||
assertEquals(testString2, appender.getLastLog().getMessage());
|
||||
|
||||
assertEquals(testString1, appender.getFirstMessage());
|
||||
assertEquals(testString2, appender.getLastMessage());
|
||||
|
||||
//getAllLogs
|
||||
assertEquals(2, appender.getLogsCount());
|
||||
assertEquals(2, appender.getAllLogs().size());
|
||||
assertEquals(testString2, appender.getAllLogs().get(1).getMessage());
|
||||
|
||||
@@ -31,7 +41,6 @@ public class Log4jMockAppenderTest {
|
||||
appender.clearLogs();
|
||||
assertNull(appender.getFirstLog());
|
||||
assertNull(appender.getLastLog());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package org.springside.modules.test.mail;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springside.modules.test.spring.SpringContextTestCase;
|
||||
|
||||
import com.icegreen.greenmail.util.GreenMail;
|
||||
|
||||
@ContextConfiguration(locations = { "/applicationContext-test.xml" })
|
||||
public class MailServerSimulatorTest extends SpringContextTestCase {
|
||||
|
||||
@Autowired
|
||||
private GreenMail greenMail;
|
||||
|
||||
@Test
|
||||
public void greenMail() {
|
||||
assertEquals(3025, greenMail.getSmtp().getPort());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.springside.modules.test.shiro;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.junit.Test;
|
||||
import org.springside.modules.test.security.shiro.ShiroTestUtils;
|
||||
|
||||
public class ShiroTestUtilsTest {
|
||||
|
||||
@Test
|
||||
public void mockSubject() {
|
||||
ShiroTestUtils.mockSubject("foo");
|
||||
assertTrue(SecurityUtils.getSubject().isAuthenticated());
|
||||
assertEquals("foo", SecurityUtils.getSubject().getPrincipal());
|
||||
|
||||
ShiroTestUtils.clearSubject();
|
||||
|
||||
ShiroTestUtils.mockSubject("bar");
|
||||
assertTrue(SecurityUtils.getSubject().isAuthenticated());
|
||||
assertEquals("bar", SecurityUtils.getSubject().getPrincipal());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-41
@@ -1,41 +0,0 @@
|
||||
package org.springside.modules.test.spring;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class SpringContextHolderTest {
|
||||
|
||||
@Test
|
||||
public void testGetBean() {
|
||||
|
||||
SpringContextHolder.clearHolder();
|
||||
try {
|
||||
SpringContextHolder.getBean("foo");
|
||||
fail("No exception throw for applicationContxt hadn't been init.");
|
||||
} catch (IllegalStateException e) {
|
||||
|
||||
}
|
||||
|
||||
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"classpath:/applicationContext-test.xml");
|
||||
assertNotNull(SpringContextHolder.getApplicationContext());
|
||||
|
||||
SpringContextHolder holder = SpringContextHolder.getBean("springContextHolder");
|
||||
assertEquals(SpringContextHolder.class, holder.getClass());
|
||||
|
||||
SpringContextHolder holderByClass = SpringContextHolder.getBean(SpringContextHolder.class);
|
||||
assertEquals(SpringContextHolder.class, holderByClass.getClass());
|
||||
|
||||
context.close();
|
||||
|
||||
try {
|
||||
holder.getApplicationContext();
|
||||
fail("No exception throw for applicationContxt had been close.");
|
||||
} catch (IllegalStateException e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
-41
@@ -1,41 +0,0 @@
|
||||
package org.springside.modules.test.spring;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.context.support.WebApplicationContextUtils;
|
||||
|
||||
public class SpringWebTestUtilsTest {
|
||||
|
||||
@Test
|
||||
public void initByPaths() {
|
||||
MockServletContext servletContext = new MockServletContext();
|
||||
SpringWebTestHelper.initWebApplicationContext(servletContext, "classpath:/applicationContext-test.xml");
|
||||
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
|
||||
assertNotNull(context);
|
||||
assertNotNull(context.getBean("springContextHolder"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initByApplicationConetxt() {
|
||||
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/applicationContext-test.xml");
|
||||
MockServletContext servletContext = new MockServletContext();
|
||||
SpringWebTestHelper.initWebApplicationContext(servletContext, ac);
|
||||
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
|
||||
assertNotNull(context);
|
||||
assertNotNull(context.getBean("springContextHolder"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void closeApplicationContext() {
|
||||
MockServletContext servletContext = new MockServletContext();
|
||||
SpringWebTestHelper.initWebApplicationContext(servletContext, "classpath:/applicationContext-test.xml");
|
||||
|
||||
SpringWebTestHelper.closeWebApplicationContext(servletContext);
|
||||
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
|
||||
assertNull(context);
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,11 @@
|
||||
|
||||
<!-- 使用annotation定义事务 -->
|
||||
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
|
||||
|
||||
<bean id="springContextHolder" class="org.springside.modules.test.spring.SpringContextHolder" lazy-init="false" />
|
||||
|
||||
<bean id="greenmail" class="org.springside.modules.test.mail.MailServerSimulator"/>
|
||||
|
||||
<bean id="memcachedSimulator" class="org.springside.modules.test.cache.memcached.MemcachedSimulator" lazy-init="false">
|
||||
<property name="serverUrl" value="localhost:11511" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user