提交 ae004780 编写于 作者: C Calvin

#29 改进Functional Test不止对嵌入式Jetty执行,也可以对在运行的其他服务器执行, Fixtures改进可适用于多种数据库。

上级 5153c906
......@@ -24,19 +24,19 @@ import org.springside.modules.utils.PropertiesLoader;
@Ignore
public class BaseFunctionalTestCase {
protected static String baseUrl;
protected static Server jettyServer;
protected static SimpleDriverDataSource dataSource;
protected static String baseUrl;
protected static PropertiesLoader propertiesLoader = new PropertiesLoader(
"classpath:/application.functional.properties", "classpath:/application.functional-local.properties");
private static Logger logger = LoggerFactory.getLogger(BaseFunctionalTestCase.class);
private static PropertiesLoader propertiesLoader = new PropertiesLoader(
"classpath:/application.functional.properties", "classpath:/application.functional-local.properties");
@BeforeClass
public static void startAll() throws Exception {
public static void beforeClass() throws Exception {
baseUrl = propertiesLoader.getProperty("baseUrl", Start.BASE_URL);
Boolean isEmbedded = Boolean.valueOf(propertiesLoader.getProperty("embedded", "true"));
......@@ -54,8 +54,10 @@ public class BaseFunctionalTestCase {
*/
protected static void startJettyOnce() throws Exception {
if (jettyServer == null) {
jettyServer = JettyFactory.buildTestServer(new URL(baseUrl).getPort(), Start.CONTEXT);
jettyServer = JettyFactory.createServer(new URL(baseUrl).getPort(), Start.CONTEXT,
"src/test/resources/web.xml");
jettyServer.start();
logger.info("Jetty Server started");
}
}
......@@ -68,7 +70,6 @@ public class BaseFunctionalTestCase {
dataSource.setUrl(propertiesLoader.getProperty("jdbc.url"));
dataSource.setUsername(propertiesLoader.getProperty("jdbc.username"));
dataSource.setPassword(propertiesLoader.getProperty("jdbc.password"));
}
}
......
......@@ -15,7 +15,7 @@ public class Start {
public static final String BASE_URL = "http://localhost:8080/mini-service";
public static void main(String[] args) throws Exception {
Server server = JettyFactory.buildNormalServer(PORT, CONTEXT);
Server server = JettyFactory.createServer(PORT, CONTEXT);
server.start();
System.out.println("Server running at " + BASE_URL);
......
......@@ -6,6 +6,16 @@
default-lazy-init="true">
<description>Apache CXF Web Service Client端配置</description>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath*:/application.functional.properties</value>
<value>classpath*:/application.functional-local.properties</value>
</list>
</property>
</bean>
<jaxws:client id="accountWebServiceClient" serviceClass="org.springside.examples.miniservice.webservice.ws.AccountWebService"
address="http://localhost:8084/mini-service/ws/accountservice" />
address="${baseUrl}/ws/accountservice" />
</beans>
......@@ -2,7 +2,8 @@ package org.springside.examples.miniweb.functional;
import static org.junit.Assert.*;
import javax.sql.DataSource;
import java.net.URL;
import java.sql.Driver;
import org.eclipse.jetty.server.Server;
import org.junit.AfterClass;
......@@ -10,51 +11,72 @@ import org.junit.BeforeClass;
import org.junit.Ignore;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springside.modules.test.data.Fixtures;
import org.springside.modules.test.functional.JettyFactory;
import org.springside.modules.test.functional.Selenium2;
import org.springside.modules.test.functional.WebDriverFactory;
import org.springside.modules.test.spring.SpringContextHolder;
import org.springside.modules.utils.PropertiesLoader;
/**
* 功能测试基类.
*
* 在整个测试期间启动一次Jetty Server, 并在每个TestCase执行前重新载入默认数据, 创建WebDriver.
* 在整个测试期间启动一次Jetty Server, 并在每个TestCase Class执行前重新载入默认数据.
* 在每个TestCase Class执行前创建WebDriver,并在每个TestCase执行后关闭WebDriver.
*
* @author calvin
*/
@Ignore
public class BaseFunctionalTestCase {
protected static String baseUrl;
protected static Server jettyServer;
protected static DataSource dataSource;
protected static SimpleDriverDataSource dataSource;
protected static Selenium2 s;
protected static PropertiesLoader propertiesLoader = new PropertiesLoader(
"classpath:/application.functional.properties", "classpath:/application.functional-local.properties");
private static Logger logger = LoggerFactory.getLogger(BaseFunctionalTestCase.class);
@BeforeClass
public static void startAll() throws Exception {
startJetty();
public static void beforeClass() throws Exception {
baseUrl = propertiesLoader.getProperty("baseUrl", Start.BASE_URL);
Boolean isEmbedded = Boolean.valueOf(propertiesLoader.getProperty("embedded", "true"));
if (isEmbedded) {
startJettyOnce();
}
buildDataSourceOnce();
reloadSampleData();
createSelenium();
loginAsAdminIfNecessary();
}
@AfterClass
public static void stopAll() throws Exception {
public static void afeterClass() throws Exception {
quitSelenium();
}
/**
* 启动Jetty服务器, 仅启动一次.
*/
protected static void startJetty() throws Exception {
protected static void startJettyOnce() throws Exception {
if (jettyServer == null) {
jettyServer = JettyFactory.buildTestServer(Start.TEST_PORT, Start.CONTEXT);
jettyServer = JettyFactory.createServer(new URL(baseUrl).getPort(), Start.CONTEXT,
"src/test/resources/web.xml");
jettyServer.start();
dataSource = SpringContextHolder.getBean("dataSource");
logger.info("Jetty Server started");
}
}
......@@ -69,13 +91,11 @@ public class BaseFunctionalTestCase {
* 创建Selenium.
*/
protected static void createSelenium() throws Exception {
PropertiesLoader propertiesLoader = new PropertiesLoader("classpath:/application.test.properties",
"classpath:/application.test-local.properties");
String driverName = propertiesLoader.getProperty("selenium.driver");
WebDriver driver = WebDriverFactory.createDriver(driverName);
s = new Selenium2(driver, Start.TEST_BASE_URL);
s = new Selenium2(driver, baseUrl);
}
/**
......@@ -109,4 +129,16 @@ public class BaseFunctionalTestCase {
s.type(By.name("password"), password);
s.click(By.id("submit"));
}
private static void buildDataSourceOnce() throws ClassNotFoundException {
if (dataSource == null) {
dataSource = new SimpleDriverDataSource();
dataSource.setDriverClass((Class<? extends Driver>) Class.forName(propertiesLoader
.getProperty("jdbc.driver")));
dataSource.setUrl(propertiesLoader.getProperty("jdbc.url"));
dataSource.setUsername(propertiesLoader.getProperty("jdbc.username"));
dataSource.setPassword(propertiesLoader.getProperty("jdbc.password"));
}
}
}
\ No newline at end of file
......@@ -11,13 +11,11 @@ import org.springside.modules.test.functional.JettyFactory;
public class Start {
public static final int PORT = 8080;
public static final int TEST_PORT = 8082;
public static final String CONTEXT = "/mini-web";
public static final String BASE_URL = "http://localhost:8080/mini-web";
public static final String TEST_BASE_URL = "http://localhost:8082/mini-web";
public static void main(String[] args) throws Exception {
Server server = JettyFactory.buildNormalServer(PORT, CONTEXT);
Server server = JettyFactory.createServer(PORT, CONTEXT);
server.start();
System.out.println("Server running at " + BASE_URL);
......
baseUrl=http://localhost:8082/mini-web
embedded=true
jdbc.driver=org.h2.Driver
jdbc.url=jdbc:h2:mem:mini-web4;DB_CLOSE_DELAY=-1
jdbc.username=sa
jdbc.password=
#selenium settings, options include firefox,ie,chrome,remote:localhost:3000:firefox
selenium.driver=firefox
......@@ -7,7 +7,4 @@ hibernate.dialect=org.hibernate.dialect.H2Dialect
#log4jdbc driver
#jdbc.driver=net.sf.log4jdbc.DriverSpy
#jdbc.url=jdbc:log4jdbc:h2:mem:mini-web4;DB_CLOSE_DELAY=-1
#selenium settings, options include firefox,ie,chrome,remote:localhost:3000:firefox
selenium.driver=firefox
\ No newline at end of file
#jdbc.url=jdbc:log4jdbc:h2:mem:mini-web4;DB_CLOSE_DELAY=-1
\ No newline at end of file
......@@ -116,7 +116,7 @@
<!-- 数据源配置,在测试环境使用JDBC直接连接 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="driverClass" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
......
package org.springside.examples.showcase.functional;
import javax.sql.DataSource;
import java.net.URL;
import java.sql.Driver;
import org.eclipse.jetty.server.Server;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springside.modules.test.data.Fixtures;
import org.springside.modules.test.functional.JettyFactory;
import org.springside.modules.test.spring.SpringContextHolder;
import org.springside.modules.utils.PropertiesLoader;
/**
* 功能测试基类.
......@@ -18,25 +22,41 @@ import org.springside.modules.test.spring.SpringContextHolder;
*/
@Ignore
public class BaseFunctionalTestCase {
protected static String baseUrl;
protected static Server jettyServer;
protected static DataSource dataSource;
protected static SimpleDriverDataSource dataSource;
protected static PropertiesLoader propertiesLoader = new PropertiesLoader(
"classpath:/application.functional.properties", "classpath:/application.functional-local.properties");
private static Logger logger = LoggerFactory.getLogger(BaseFunctionalTestCase.class);
@BeforeClass
public static void startAll() throws Exception {
startJetty();
public static void beforeClass() throws Exception {
baseUrl = propertiesLoader.getProperty("baseUrl", Start.BASE_URL);
Boolean isEmbedded = Boolean.valueOf(propertiesLoader.getProperty("embedded", "true"));
if (isEmbedded) {
startJettyOnce();
}
buildDataSourceOnce();
reloadSampleData();
}
/**
* 启动Jetty服务器, 仅启动一次.
*/
protected static void startJetty() throws Exception {
protected static void startJettyOnce() throws Exception {
if (jettyServer == null) {
jettyServer = JettyFactory.buildTestServer(Start.TEST_PORT, Start.CONTEXT);
jettyServer = JettyFactory.createServer(new URL(baseUrl).getPort(), Start.CONTEXT,
"src/test/resources/web.xml");
jettyServer.start();
dataSource = SpringContextHolder.getBean("dataSource");
logger.info("Jetty Server started");
}
}
......@@ -46,4 +66,16 @@ public class BaseFunctionalTestCase {
protected static void reloadSampleData() throws Exception {
Fixtures.reloadData(dataSource, "/data/sample-data.xml");
}
private static void buildDataSourceOnce() throws ClassNotFoundException {
if (dataSource == null) {
dataSource = new SimpleDriverDataSource();
dataSource.setDriverClass((Class<? extends Driver>) Class.forName(propertiesLoader
.getProperty("jdbc.driver")));
dataSource.setUrl(propertiesLoader.getProperty("jdbc.url"));
dataSource.setUsername(propertiesLoader.getProperty("jdbc.username"));
dataSource.setPassword(propertiesLoader.getProperty("jdbc.password"));
}
}
}
......@@ -4,16 +4,13 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springside.modules.test.functional.Selenium2;
import org.springside.modules.test.functional.WebDriverFactory;
import org.springside.modules.utils.PropertiesLoader;
/**
* 功能测试基类.
*
* 在整个测试期间启动一次Jetty Server, 并在每个TestCase执行前中重新载入默认数据.
* 在BaseFunctionalTestCase的基础上,加入对Selenium WebDriver的启动和关闭.
*
* @author calvin
*/
......@@ -22,35 +19,17 @@ public class BaseSeleniumTestCase extends BaseFunctionalTestCase {
protected static Selenium2 s;
private static Logger logger = LoggerFactory.getLogger(BaseFunctionalTestCase.class);
@BeforeClass
public static void startSelenium() throws Exception {
createSelenium();
}
@AfterClass
public static void stopSelenium() throws Exception {
quitSelenium();
}
/**
* 创建Selenium.
*/
protected static void createSelenium() throws Exception {
PropertiesLoader propertiesLoader = new PropertiesLoader("classpath:/application.test.properties",
"classpath:/application.test-local.properties");
String driverName = propertiesLoader.getProperty("selenium.driver");
WebDriver driver = WebDriverFactory.createDriver(driverName);
s = new Selenium2(driver, Start.TEST_BASE_URL);
s = new Selenium2(driver, baseUrl);
}
/**
* 关闭Selenium.
*/
protected static void quitSelenium() {
@AfterClass
public static void stopSelenium() throws Exception {
s.quit();
}
}
......@@ -11,13 +11,11 @@ import org.springside.modules.test.functional.JettyFactory;
public class Start {
public static final int PORT = 8080;
public static final int TEST_PORT = 8083;
public static final String CONTEXT = "/showcase";
public static final String BASE_URL = "http://localhost:8080/showcase";
public static final String TEST_BASE_URL = "http://localhost:8083/showcase";
public static void main(String[] args) throws Exception {
Server server = JettyFactory.buildNormalServer(PORT, CONTEXT);
Server server = JettyFactory.createServer(PORT, CONTEXT);
server.start();
System.out.println("Server running at " + BASE_URL);
......
......@@ -8,7 +8,6 @@ import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.springside.examples.showcase.functional.BaseFunctionalTestCase;
import org.springside.examples.showcase.functional.Start;
import org.springside.examples.showcase.functional.category.Smoke;
import org.springside.examples.showcase.webservice.rs.client.UserResourceClient;
import org.springside.examples.showcase.webservice.rs.dto.UserDTO;
......@@ -22,7 +21,7 @@ public class UserResourceServiceIT extends BaseFunctionalTestCase {
@BeforeClass
public static void setUpClient() {
client = new UserResourceClient();
client.setBaseUrl(Start.TEST_BASE_URL + "/rs");
client.setBaseUrl(baseUrl + "/rs");
}
/**
......
baseUrl=http://localhost:8083/showcase
embedded=true
jdbc.driver=org.h2.Driver
jdbc.url=jdbc:h2:mem:showcase4;DB_CLOSE_DELAY=-1
jdbc.username=sa
jdbc.password=
#selenium settings, options include firefox,ie,chrome,remote:localhost:3000:firefox
selenium.driver=firefox
......@@ -12,9 +12,6 @@ quartz.jdbc.url=jdbc:h2:mem:showcase-quartz4;DB_CLOSE_DELAY=-1
quartz.jdbc.username=sa
quartz.jdbc.password=
#selenium settings, options include firefox,ie,chrome,remote:localhost:3000:firefox
selenium.driver=firefox
#memcached settings
memcached.url=localhost:11311
......
......@@ -90,12 +90,12 @@ public class Fixtures {
protected static IDatabaseConnection getConnection(DataSource dataSource) throws DatabaseUnitException,
SQLException {
Connection connection = dataSource.getConnection();
String driverName = connection.getMetaData().getDriverName();
if (StringUtils.contains(driverName, "h2")) {
String dbName = connection.getMetaData().getDatabaseProductName().toLowerCase();
if (StringUtils.contains(dbName, "h2")) {
return new H2Connection(connection, null);
} else if (StringUtils.contains(driverName, "mysql")) {
} else if (StringUtils.contains(dbName, "mysql")) {
return new MySqlConnection(connection, null);
} else if (StringUtils.contains(driverName, "oracle")) {
} else if (StringUtils.contains(dbName, "oracle")) {
return new OracleConnection(connection, null);
} else {
return new DatabaseConnection(connection);
......
......@@ -21,7 +21,7 @@ public class JettyFactory {
/**
* 创建用于开发运行调试的Jetty Server, 以src/main/webapp为Web应用目录.
*/
public static Server buildNormalServer(int port, String contextPath) {
public static Server createServer(int port, String contextPath) {
Server server = new Server(port);
server.setStopAtShutdown(true);
......@@ -33,13 +33,11 @@ public class JettyFactory {
}
/**
* 创建用于Functional Test的Jetty Server:
* 1.以src/main/webapp为Web应用目录.
* 2.以test/resources/web.xml指向applicationContext-test.xml创建测试环境.
* 创建Jetty Server,以 以src/main/webapp为Web应用目录并重新制定web.xml路径。
*/
public static Server buildTestServer(int port, String contextPath) {
Server server = buildNormalServer(port, contextPath);
((WebAppContext) server.getHandler()).setDescriptor("src/test/resources/web.xml");
public static Server createServer(int port, String contextPath, String webxmlPath) {
Server server = createServer(port, contextPath);
((WebAppContext) server.getHandler()).setDescriptor(webxmlPath);
return server;
}
}
......@@ -5,13 +5,12 @@ import static org.junit.Assert.*;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import org.junit.Test;
import org.springside.modules.test.functional.JettyFactory;
public class JettyFactoryTest {
@Test
public void buildNormalServer() {
Server server = JettyFactory.buildNormalServer(1978, "core");
public void createNormalServer() {
Server server = JettyFactory.createServer(1978, "core");
assertEquals(1978, server.getConnectors()[0].getPort());
assertEquals("core", ((WebAppContext) server.getHandler()).getContextPath());
......@@ -19,8 +18,8 @@ public class JettyFactoryTest {
}
@Test
public void buildTestServer() {
Server server = JettyFactory.buildTestServer(1978, "core");
public void createTestServer() {
Server server = JettyFactory.createServer(1978, "core", "src/test/resources/web.xml");
assertEquals(1978, server.getConnectors()[0].getPort());
assertEquals("core", ((WebAppContext) server.getHandler()).getContextPath());
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册