Commit 31698097 authored by yihua.huang's avatar yihua.huang

invite h2 as test and standalone db, add spring profile for switch

parent 9a4eab44
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
<packaging>jar</packaging> <packaging>jar</packaging>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>us.codecraft</groupId> <groupId>us.codecraft</groupId>
<artifactId>webmagic-extension</artifactId> <artifactId>webmagic-extension</artifactId>
...@@ -75,19 +76,28 @@ ...@@ -75,19 +76,28 @@
<groupId>org.aspectj</groupId> <groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId> <artifactId>aspectjrt</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.aspectj</groupId> <groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId> <artifactId>aspectjweaver</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId> <artifactId>spring-core</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId> <artifactId>spring-webmvc</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.175</version>
</dependency>
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId> <artifactId>mockito-all</artifactId>
...@@ -97,18 +107,22 @@ ...@@ -97,18 +107,22 @@
<groupId>javax.servlet</groupId> <groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId> <artifactId>javax.servlet-api</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId> <artifactId>spring-context</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId> <artifactId>spring-context-support</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId> <artifactId>fastjson</artifactId>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
......
package us.codecraft.webmagic.dao; package us.codecraft.webmagic.dao;
import org.apache.ibatis.annotations.Insert;
import us.codecraft.webmagic.model.DynamicClass; import us.codecraft.webmagic.model.DynamicClass;
/** /**
...@@ -8,6 +7,5 @@ import us.codecraft.webmagic.model.DynamicClass; ...@@ -8,6 +7,5 @@ import us.codecraft.webmagic.model.DynamicClass;
*/ */
public interface DynamicClassDao { public interface DynamicClassDao {
@Insert("insert into DynamicClass (`ClassName`,`SourceCode`,`AddTime`,`UpdateTime`) values (#{className},#{sourceCode},now(),now())")
public int add(DynamicClass dynamicClass); public int add(DynamicClass dynamicClass);
} }
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2010-2013 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="us.codecraft.webmagic.dao.DynamicClassDao">
<insert id="add" parameterType="us.codecraft.webmagic.model.DynamicClass" databaseId="mysql">
insert into DynamicClass (`ClassName`,`SourceCode`,`AddTime`,`UpdateTime`)
values (#{className},#{sourceCode},now(),now())
</insert>
<insert id="add" parameterType="us.codecraft.webmagic.model.DynamicClass" databaseId="h2">
insert into DynamicClass (`ClassName`,`SourceCode`,`AddTime`,`UpdateTime`)
values (#{className},#{sourceCode},now(),now())
</insert>
</mapper>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/WebMagic?characterEncoding=UTF-8"/>
<property name="username" value="webmagic"/>
<property name="password" value="webmagic"/>
</bean>
<beans profile="test">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:mem:WebMagic;DB_CLOSE_DELAY=-1"/>
</bean>
<!--Refer to https://github.com/springside/springside4/wiki/H2-Database -->
<jdbc:initialize-database data-source="dataSource" ignore-failures="ALL">
<jdbc:script location="classpath:sql/h2/schema.sql" />
<!--<jdbc:script location="classpath:data/h2/import-data.sql" encoding="UTF-8"/>-->
</jdbc:initialize-database>
</beans>
<beans profile="standalone">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:file:~/.h2/WebMagic;AUTO_SERVER=TRUE"/>
</bean>
<!--Refer to https://github.com/springside/springside4/wiki/H2-Database -->
<jdbc:initialize-database data-source="dataSource" ignore-failures="ALL">
<jdbc:script location="classpath:sql/h2/schema.sql" />
<!--<jdbc:script location="classpath:data/h2/import-data.sql" encoding="UTF-8"/>-->
</jdbc:initialize-database>
</beans>
</beans>
\ No newline at end of file
...@@ -4,20 +4,30 @@ ...@@ -4,20 +4,30 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="us.codecraft.webmagic.dao" /> <property name="basePackage" value="us.codecraft.webmagic.dao" />
</bean> </bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" <bean id="vendorProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
destroy-method="close"> <property name="properties">
<property name="driverClassName" value="com.mysql.jdbc.Driver" /> <props>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/WebMagic?characterEncoding=UTF-8" /> <prop key="SQL Server">sqlserver</prop>
<property name="username" value="webmagic" /> <prop key="DB2">db2</prop>
<property name="password" value="webmagic" /> <prop key="Oracle">oracle</prop>
<prop key="MySQL">mysql</prop>
<prop key="H2">h2</prop>
</props>
</property>
</bean>
<bean id="databaseIdProvider" class="org.apache.ibatis.mapping.VendorDatabaseIdProvider">
<property name="properties" ref="vendorProperties"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="databaseIdProvider" ref="databaseIdProvider" />
<property name="mapperLocations" value="classpath*:/mapper/**/*.xml" />
</bean> </bean>
</beans> </beans>
\ No newline at end of file
...@@ -11,6 +11,10 @@ ...@@ -11,6 +11,10 @@
http://www.springframework.org/schema/context/spring-context-4.0.xsd"> http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:annotation-config/> <context:annotation-config/>
<context:property-placeholder ignore-resource-not-found="true"
location="classpath*:/applicationContext.properties,
classpath*:/applicationContext.test.properties" />
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames"> <property name="basenames">
<list> <list>
......
CREATE TABLE DynamicClass(
Id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`ClassName` varchar(200) NOT NULL,
`SourceCode` text NOT NULL,
`AddTime` datetime NOT NULL,
`UpdateTime` datetime NOT NULL,
UNIQUE INDEX `un_class_name` (`ClassName`)
);
\ No newline at end of file
package us.codecraft.webmagic;
import org.junit.runner.RunWith;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
/**
* @author code4crafter@gmail.com
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:/spring/applicationContext*.xml"})
@ActiveProfiles("test")
@Transactional
public abstract class AbstractTest {
}
package us.codecraft.webmagic.dao; package us.codecraft.webmagic.dao;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback; import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import us.codecraft.webmagic.AbstractTest;
import us.codecraft.webmagic.model.DynamicClass; import us.codecraft.webmagic.model.DynamicClass;
/** /**
* @author code4crafter@gmail.com * @author code4crafter@gmail.com
*/ */
@Ignore public class DynamicClassDaoTest extends AbstractTest {
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring/applicationContext-*.xml"})
@Transactional
public class DynamicClassDaoTest {
@Autowired @Autowired
private DynamicClassDao dynamicClassDao; private DynamicClassDao dynamicClassDao;
......
...@@ -2,15 +2,13 @@ package us.codecraft.webmagic.service; ...@@ -2,15 +2,13 @@ package us.codecraft.webmagic.service;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.mockito.Spy; import org.mockito.Spy;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import us.codecraft.forger.ForgerFactory; import us.codecraft.forger.ForgerFactory;
import us.codecraft.webmagic.AbstractTest;
import us.codecraft.webmagic.Foo; import us.codecraft.webmagic.Foo;
import us.codecraft.webmagic.dao.DynamicClassDao; import us.codecraft.webmagic.dao.DynamicClassDao;
import us.codecraft.webmagic.exception.DynamicClassCompileException; import us.codecraft.webmagic.exception.DynamicClassCompileException;
...@@ -22,9 +20,7 @@ import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown; ...@@ -22,9 +20,7 @@ import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
/** /**
* @author code4crafter@gmail.com * @author code4crafter@gmail.com
*/ */
@RunWith(SpringJUnit4ClassRunner.class) public class DynamicClassServiceImplTest extends AbstractTest {
@ContextConfiguration(locations = {"classpath*:/spring/applicationContext*.xml"})
public class DynamicClassServiceImplTest {
@Before @Before
public void setUp() { public void setUp() {
......
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="freemarkerConfigurer"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/pages/" />
<property name="defaultEncoding" value="utf-8" />
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">0</prop>
<prop key="locale">zh_CN</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="number_format">#.##</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
<property name="suffix" value=".ftl" />
<property name="contentType" value="text/html;charset=utf-8" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
<property name="exposeSpringMacroHelpers" value="true" />
</bean>
</beans>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="us.codecraft.webmagic.dao" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/WebMagic?characterEncoding=UTF-8" />
<property name="username" value="webmagic" />
<property name="password" value="webmagic" />
</bean>
</beans>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:annotation-config/>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>web_messages</value>
</list>
</property>
</bean>
<context:component-scan base-package="us.codecraft.webmagic"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean id="fastJsonHttpMessageConverter"
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
<mvc:annotation-driven>
</mvc:annotation-driven>
</beans>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment