add base service test

This commit is contained in:
东皇 2017-04-19 11:21:50 +08:00
parent 36ddf9e0a5
commit 169ab1b99a
19 changed files with 288 additions and 57 deletions

View File

@ -3,6 +3,7 @@ package me.chyxion.tigon.model;
import java.util.*;
import java.lang.reflect.Field;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import me.chyxion.tigon.util.WordUtils;
import me.chyxion.tigon.mybatis.PrimaryKey;
import org.springframework.util.ReflectionUtils;
@ -12,7 +13,8 @@ import org.springframework.util.ReflectionUtils;
* chyxion@163.com <br>
* Feb 21, 2017 18:39:39
*/
public class BaseModel<PK> extends MappableSupport implements Mappable {
@Slf4j
public abstract class BaseModel<PK> extends MappableSupport implements Mappable {
private static final long serialVersionUID = 1L;
// id
@ -53,8 +55,18 @@ public class BaseModel<PK> extends MappableSupport implements Mappable {
@SuppressWarnings("unchecked")
public PK primaryKeyValue() {
return (PK) ReflectionUtils.getField(
ReflectionUtils.findField(getClass(), primaryKeyName()), this);
}
/**
* set primary key
* @param value primary key
* @return previous primary key value
*/
public void primaryKeyValue(PK value) {
ReflectionUtils.setField(
ReflectionUtils.findField(getClass(),
primaryKeyName()), getClass());
primaryKeyName()), this, value);
}
/**
@ -101,14 +113,14 @@ public class BaseModel<PK> extends MappableSupport implements Mappable {
* before insert model, for override
*/
public void beforeInsert() {
log.debug("Model [{}] Before Insert.", this);
}
/**
* before update model, for override
*/
public void beforeUpdate() {
log.debug("Model [{}] Before Update.", this);
}
/**

View File

@ -32,4 +32,12 @@ public class M0<Id> extends BaseModel<Id> {
public Id primaryKeyValue() {
return id;
}
/**
* {@inheritDoc}
*/
@Override
public void primaryKeyValue(Id id) {
this.id = id;
}
}

View File

@ -80,7 +80,8 @@
<choose>
<!-- Model -->
<when test="_parameter.containsKey(&quot;model&quot;)">
<foreach collection="model.cols()"
<bind name="__before_insert_dummy__" value="model.beforeInsert()" />
<foreach collection="model.cols()"
item="__col__" open="(" separator=", " close=")">
${__col__}
</foreach>
@ -98,6 +99,7 @@
</foreach>
values
<foreach item="model" collection="models" separator=", ">
<bind name="__before_insert_dummy__" value="model.beforeInsert()" />
<foreach collection="model.values()"
item="__val__" open="(" separator=", " close=")">
#{__val__}
@ -134,6 +136,7 @@
</foreach>
</when>
<otherwise>
<bind name="__before_update_dummy__" value="model.beforeUpdate()" />
<foreach collection="model.updateMap()" index="__m_col__" item="__val__" separator=", ">
${__m_col__} = #{__val__}
</foreach>

View File

@ -9,8 +9,8 @@
<PatternLayout pattern="${pattern}" />
</Console>
<RollingFile name="File"
fileName="${log.dir}/test.log"
filePattern="${log.dir}/test-%d{yyyy-MM-dd}-%i.log">
fileName="${log.dir}/tigon-mybatis.log"
filePattern="${log.dir}/tigon-mybatis-%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="${pattern}" />
<Policies>
<TimeBasedTriggeringPolicy />

View File

@ -21,6 +21,10 @@
<groupId>me.chyxion.tigon</groupId>
<artifactId>tigon-service-api</artifactId>
</dependency>
<dependency>
<groupId>me.chyxion.tigon</groupId>
<artifactId>tigon-sequence</artifactId>
</dependency>
<dependency>
<groupId>me.chyxion.tigon</groupId>
<artifactId>tigon-mybatis</artifactId>
@ -35,10 +39,45 @@
<scope>provided</scope>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -10,6 +10,7 @@ import me.chyxion.tigon.mybatis.Search;
import me.chyxion.tigon.model.ViewModel;
import me.chyxion.tigon.mybatis.BaseMapper;
import javax.validation.constraints.NotNull;
import org.apache.commons.lang3.StringUtils;
import me.chyxion.tigon.service.BaseCrudService;
import org.hibernate.validator.constraints.NotEmpty;
import me.chyxion.tigon.validation.annotation.NotNullOrBlank;
@ -51,7 +52,6 @@ public class BaseCrudServiceSupport
*/
@Override
public ViewModel<Model> create(Model model) {
model.beforeInsert();
beforeInsert(model);
mapper.insert(model);
afterInsert(model);
@ -76,7 +76,6 @@ public class BaseCrudServiceSupport
*/
@Override
public ViewModel<Model> update(Model model) {
model.beforeUpdate();
beforeUpdate(model);
mapper.update(model);
afterUpdate(model);
@ -113,6 +112,12 @@ public class BaseCrudServiceSupport
// private methods
protected void beforeInsert(Model model) {
if (idType.equals(String.class) &&
StringUtils.isBlank((String) model.primaryKeyValue())) {
final String id = idSeq.get();
log.debug("Insert Model [{}] Id Is Blank, Generate [{}].", model, id);
model.primaryKeyValue((PrimaryKey) id);
}
// Hook
}

View File

@ -1,5 +1,7 @@
package me.chyxion.tigon.service.support;
import lombok.Getter;
import lombok.Setter;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
@ -9,6 +11,7 @@ import me.chyxion.tigon.model.M1;
import me.chyxion.tigon.model.BaseModel;
import me.chyxion.tigon.model.ViewModel;
import me.chyxion.tigon.mybatis.BaseMapper;
import me.chyxion.tigon.sequence.IdSequence;
import me.chyxion.tigon.service.BaseService;
import org.springframework.core.GenericTypeResolver;
import org.springframework.beans.factory.annotation.Autowired;
@ -21,6 +24,8 @@ import org.springframework.beans.factory.annotation.Autowired;
* Nov 8, 2016 10:09:29 AM
*/
@Slf4j
@Getter
@Setter
public class BaseServiceSupport
<PrimaryKey, Model extends BaseModel<PrimaryKey>,
Mapper extends BaseMapper<PrimaryKey, Model>>
@ -28,6 +33,8 @@ public class BaseServiceSupport
@Autowired
protected Mapper mapper;
@Autowired
protected IdSequence idSeq;
protected Class<PrimaryKey> idType;
protected Class<Model> modelType;
@ -44,20 +51,6 @@ public class BaseServiceSupport
modelType = (Class<Model>) clazzArr[1];
}
/**
* @return the mapper
*/
public Mapper getMapper() {
return mapper;
}
/**
* @param mapper the mapper to set
*/
public void setMapper(Mapper mapper) {
this.mapper = mapper;
}
/**
* {@inheritDoc}
*/
@ -89,9 +82,9 @@ public class BaseServiceSupport
}
/**
* @param model
* @param url
* @return
* @param model model
* @param url url
* @return url
*/
protected String urlCacheClear(M1<PrimaryKey> model, String url) {
Date dateUpdated = model.getDateUpdated();
@ -100,8 +93,8 @@ public class BaseServiceSupport
}
/**
* @param viewModel
* @param model
* @param viewModel view model
* @param model model
*/
protected void processViewModel(ViewModel<Model> viewModel, Model model) {
// For Subclass Override

View File

@ -0,0 +1,12 @@
package me.chyxion.tigon.mapper;
import me.chyxion.tigon.model.TestModel;
import me.chyxion.tigon.mybatis.BaseMapper;
/**
* @author Shaun Chyxion <br>
* chyxion@163.com <br>
* Apr 19, 2017 10:58:45
*/
public interface TestModelMapper extends BaseMapper<String, TestModel> {
}

View File

@ -0,0 +1,17 @@
package me.chyxion.tigon.model;
import lombok.Getter;
import lombok.Setter;
import me.chyxion.tigon.mybatis.Table;
/**
* @author Shaun Chyxion <br>
* chyxion@163.com <br>
* Apr 19, 2017 10:58:52
*/
@Getter
@Setter
@Table("tigon_test_model")
public class TestModel extends M3<String, String> {
private String name;
}

View File

@ -1,8 +1,6 @@
package me.chyxion.tigon.service.support;
package me.chyxion.tigon.service;
/**
* @version 0.0.1
* @since 0.0.1
* @author Shaun Chyxion <br>
* chyxion@163.com <br>
* Nov 23, 2016 3:22:44 PM

View File

@ -0,0 +1,11 @@
package me.chyxion.tigon.service;
import me.chyxion.tigon.model.TestModel;
/**
* @author Shaun Chyxion <br>
* chyxion@163.com <br>
* Apr 19, 2017 10:59:31
*/
public interface TestModelService extends BaseCrudService<String, TestModel> {
}

View File

@ -0,0 +1,15 @@
package me.chyxion.tigon.service.support;
import me.chyxion.tigon.model.TestModel;
import me.chyxion.tigon.mapper.TestModelMapper;
import me.chyxion.tigon.service.TestModelService;
/**
* @author Shaun Chyxion <br>
* chyxion@163.com <br>
* Apr 19, 2017 10:59:00
*/
public class TestModelServiceSupport
extends BaseCrudServiceSupport<String, TestModel, TestModelMapper>
implements TestModelService {
}

View File

@ -0,0 +1,36 @@
package me.chyxion.tigon.service.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import me.chyxion.tigon.model.TestModel;
import me.chyxion.tigon.service.TestModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Shaun Chyxion <br>
* chyxion@163.com <br>
* Apr 19, 2017 10:59:07
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:spring/spring-*.xml")
public class TestModelServiceTest {
@Autowired
private TestModelService testModelService;
@Test
public void testInsert() {
TestModel testModel = new TestModel();
testModel.setName("test");
testModel.setNote("test note");
testModel.setCreatedBy("donghuang");
testModel.setEnabled(true);
testModelService.create(testModel);
}
@Test
public void testUpdate() {
System.err.println(testModelService.list(null));
}
}

View File

@ -0,0 +1,36 @@
/*
Navicat Premium Data Transfer
Source Server : Localhost
Source Server Type : MySQL
Source Server Version : 50717
Source Host : localhost
Source Database : tigon_test
Target Server Type : MySQL
Target Server Version : 50717
File Encoding : utf-8
Date: 04/19/2017 11:19:03 AM
*/
SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for `tigon_test_model`
-- ----------------------------
DROP TABLE IF EXISTS `tigon_test_model`;
CREATE TABLE `tigon_test_model` (
`id` varchar(36) NOT NULL,
`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`date_updated` timestamp NULL DEFAULT NULL,
`created_by` varchar(36) NOT NULL,
`updated_by` varchar(36) DEFAULT NULL,
`note` varchar(64) DEFAULT NULL,
`name` varchar(36) DEFAULT NULL,
`enabled` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
SET FOREIGN_KEY_CHECKS = 1;

View File

@ -1,15 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Configuration status="WARN">
<Properties>
<Property name="pattern">%-d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%t][%c{1}] %m%n</Property>
<Property name="log.dir">.logs</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${pattern}" />
</Console>
<RollingFile name="File"
fileName="${log.dir}/log-${sys:http.port}.log"
filePattern="${log.dir}/$${date:yyyy-MM}/log-${sys:http.port}-%d{yyyy-MM-dd}-%i.log.gz">
<RollingFile name="File"
fileName="${log.dir}/tigon-service-support.log"
filePattern="${log.dir}/tigon-service-support-%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="${pattern}" />
<Policies>
<TimeBasedTriggeringPolicy />
@ -17,29 +18,15 @@
</Policies>
<DefaultRolloverStrategy max="32" />
</RollingFile>
<SMTP name="Mail"
subject="${log.mail.subject}"
to="${log.mail.to}"
cc="${log.mail.cc}"
from="${log.mail.from}"
smtpHost="${log.mail.host}"
smtpPort="${log.mail.port}"
smtpUsername="${log.mail.user}"
smtpPassword="${log.mail.password}"
smtpDebug="false"
bufferSize="64">
</SMTP>
<!-- appender to send mails asynchronously -->
<Async name="AsyncMail">
<AppenderRef ref="Mail" />
</Async>
<Async name="AsyncFile">
<Async name="AsyncFile">
<AppenderRef ref="File" />
</Async>
</Appenders>
<Loggers>
<Root level="DEBUG" additivity="false">
${log.appender}
<AppenderRef ref="Console" level="DEBUG" />
</Root>
</Loggers>
</Configuration>

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* @version 0.0.1
* @since 0.0.1
* @author Auto Generated <br>
* Tech Support <a href="mailto:chyxion@163.com">Shaun Chyxion</a><br>
* Dec 6, 2016 12:10:56 PM
*/
-->
<!DOCTYPE mapper PUBLIC
"-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="me.chyxion.tigon.mapper.TestModelMapper">
</mapper>

View File

@ -0,0 +1,9 @@
# Config Dev
# Database
db.url=jdbc:mysql://127.0.0.1:3306/tigon_test?useUnicode=true&characterEncoding=utf-8&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
db.user=root
db.password=12345678
redis.host=127.0.0.1
redis.password=0211

View File

@ -0,0 +1,24 @@
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init"
destroy-method="close"
p:url="${db.url}"
p:username="${db.user}"
p:password="${db.password}"
p:maxActive="16" />
<!-- MyBatis SqlSessionFactory -->
<bean id="sqlSessionFactory"
class="me.chyxion.tigon.mybatis.TigonSqlSessionFactoryBean"
p:dataSource-ref="dataSource"
p:typeAliasesPackage="me.chyxion.tigon.model"
p:mapperLocations="classpath*:mybatis/mapper/*-mapper.xml" />
<!-- MyBatis Mappers Auto Scan -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:basePackage="me.chyxion.tigon.mapper"
p:sqlSessionFactoryBeanName="sqlSessionFactory" />
</beans>

View File

@ -0,0 +1,11 @@
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:spring/config.properties" />
<bean class="me.chyxion.tigon.service.support.TestModelServiceSupport" />
</beans>