refactor: 使用Junit 5 / 新增DatePickUtil / AbstractQuery 单元测试
This commit is contained in:
@@ -161,6 +161,19 @@
|
||||
<version>1.6.2</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -7,12 +7,14 @@ import com.baomidou.mybatisplus.extension.activerecord.Model;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import java.util.Date;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* Entity基类
|
||||
*
|
||||
* @author valarchie
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class BaseEntity<T extends Model<?>> extends Model<T> {
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.util.List;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 分页模型类
|
||||
* @author valarchie
|
||||
*/
|
||||
@Data
|
||||
@@ -24,6 +25,7 @@ public class PageDTO {
|
||||
this.total = (long) list.size();
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public PageDTO(Page page) {
|
||||
this.rows = page.getRecords();
|
||||
this.total = page.getTotal();
|
||||
|
||||
@@ -2,12 +2,27 @@ package com.agileboot.common.exception.error;
|
||||
|
||||
public interface ErrorCodeInterface {
|
||||
|
||||
/**
|
||||
* @return 返回枚举名称
|
||||
*/
|
||||
String name();
|
||||
|
||||
/**
|
||||
* 返回错误码
|
||||
* @return 错误码
|
||||
*/
|
||||
int code();
|
||||
|
||||
/**
|
||||
*
|
||||
* @return 错误描述
|
||||
*/
|
||||
String message();
|
||||
|
||||
/**
|
||||
* i18n资源文件的key, 详见messages.properties文件
|
||||
* @return key
|
||||
*/
|
||||
default String i18nKey() {
|
||||
return code() + "_" + name();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.agileboot.common.utils.ip;
|
||||
|
||||
/**
|
||||
* IP地理位置工具类
|
||||
* @author valarchie
|
||||
*/
|
||||
public class IpRegionUtil {
|
||||
|
||||
@@ -4,6 +4,7 @@ package com.agileboot.common.utils.jackson;
|
||||
* @author valarchie
|
||||
*/
|
||||
public class JacksonException extends RuntimeException {
|
||||
|
||||
public JacksonException() {
|
||||
super();
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import javax.servlet.http.HttpServletResponse;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/**
|
||||
* 自定义Excel 导入导出工具
|
||||
* @author valarchie
|
||||
*/
|
||||
public class CustomExcelUtil {
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.agileboot.common.utils.time;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import java.util.Date;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @author valarchie
|
||||
*/
|
||||
@Slf4j
|
||||
public class DatePickUtil {
|
||||
|
||||
/**
|
||||
* 安全地获取日期的一天开始时间, date为null 则返回null
|
||||
* DateUtil.beginOfDay(date) 如果传null 会NPE
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public static Date getBeginOfTheDay(Date date) {
|
||||
if (date == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return DateUtil.beginOfDay(date);
|
||||
} catch (Exception e) {
|
||||
log.error("pick begin of the day failed, due to: ", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全地获取日期的一天结束时间, date为null 则返回null。 避免NPE
|
||||
* DateUtil.endOfDay(date) 如果传null 会NPE
|
||||
* @param date 23:59:59
|
||||
* @return
|
||||
*/
|
||||
public static Date getEndOfTheDay(Date date) {
|
||||
if (date == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return DateUtil.endOfDay(date);
|
||||
} catch (Exception e) {
|
||||
log.error("pick end of the day failed, due to: ", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package com.agileboot.common.utils.time;
|
||||
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import java.util.Date;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @author valarchie
|
||||
*/
|
||||
@Slf4j
|
||||
public class DatePicker {
|
||||
|
||||
public static Date getBeginOfTheDay(Object date) {
|
||||
if (date == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
DateTime parse = DateUtil.parse(date.toString());
|
||||
return DateUtil.beginOfDay(parse);
|
||||
} catch (Exception e) {
|
||||
log.error("pick begin of day failed, due to: ", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Date getEndOfTheDay(Object date) {
|
||||
if (date == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
DateTime parse = DateUtil.parse(date.toString());
|
||||
return DateUtil.endOfDay(parse);
|
||||
} catch (Exception e) {
|
||||
log.error("pick end of day failed, due to: ", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -9,16 +9,21 @@ public class ApiExceptionTest {
|
||||
@Test
|
||||
public void testVarargsWithArrayArgs() {
|
||||
String errorMsg = "these parameters are null: %s, %s, %s.";
|
||||
|
||||
Object[] array = new Object[] { "param1" , "param2" , "param3"};
|
||||
|
||||
String format1 = String.format(errorMsg, array);
|
||||
String format2 = String.format(errorMsg, "param1", "param2", "param3");
|
||||
String formatWithArray = String.format(errorMsg, array);
|
||||
String formatWithVarargs = String.format(errorMsg, "param1", "param2", "param3");
|
||||
|
||||
System.out.println(format1);
|
||||
System.out.println(format2);
|
||||
Assert.assertEquals(formatWithVarargs, formatWithArray);
|
||||
}
|
||||
|
||||
Assert.assertEquals(format1, format2);
|
||||
@Test
|
||||
public void testVarargsWithNullArgs() {
|
||||
String errorMsg = "these parameters are null: %s, %s, %s.";
|
||||
|
||||
String format = String.format(errorMsg, "param1", null, null);
|
||||
|
||||
Assert.assertEquals("these parameters are null: param1, null, null.", format);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.agileboot.common.utils;
|
||||
package com.agileboot.common.utils.jackson;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.agileboot.common.utils.jackson.JacksonUtil;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import org.junit.Assert;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.agileboot.common.utils;
|
||||
package com.agileboot.common.utils.jackson;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.agileboot.common.utils.time;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class DatePickUtilTest {
|
||||
|
||||
@Test
|
||||
void testGetBeginOfTheDay() {
|
||||
Date beginOfTheDay = DatePickUtil.getBeginOfTheDay(new Date());
|
||||
|
||||
Calendar instance = Calendar.getInstance();
|
||||
instance.setTime(beginOfTheDay);
|
||||
|
||||
Assertions.assertEquals(0, instance.get(Calendar.HOUR));
|
||||
Assertions.assertEquals(0, instance.get(Calendar.MINUTE));
|
||||
Assertions.assertEquals(0, instance.get(Calendar.SECOND));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetBeginOfTheDayWhenNull() {
|
||||
Assertions.assertDoesNotThrow(() -> DatePickUtil.getBeginOfTheDay(null)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetEndOfTheDay() {
|
||||
Date endOfTheDay = DatePickUtil.getEndOfTheDay(new Date());
|
||||
|
||||
Calendar instance = Calendar.getInstance();
|
||||
instance.setTime(endOfTheDay);
|
||||
|
||||
Assertions.assertEquals(23, instance.get(Calendar.HOUR_OF_DAY));
|
||||
Assertions.assertEquals(59, instance.get(Calendar.MINUTE));
|
||||
Assertions.assertEquals(59, instance.get(Calendar.SECOND));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetEndOfTheDayWhenNull() {
|
||||
Assertions.assertDoesNotThrow(() -> DatePickUtil.getEndOfTheDay(null)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.agileboot.orm.query;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import java.util.Date;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class AbstractQueryTest {
|
||||
|
||||
private AbstractQuery query;
|
||||
|
||||
@BeforeEach
|
||||
public void getNewQuery() {
|
||||
query = new AbstractQuery() {
|
||||
@Override
|
||||
public QueryWrapper toQueryWrapper() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void addTimeConditionWithNull() {
|
||||
QueryWrapper<Object> queryWrapper = new QueryWrapper<>();
|
||||
query.addTimeCondition(queryWrapper, "login_time");
|
||||
|
||||
String targetSql = queryWrapper.getTargetSql();
|
||||
Assertions.assertEquals("", targetSql);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void addTimeConditionWithBothValue() {
|
||||
query.setBeginTime(new Date());
|
||||
query.setEndTime(new Date());
|
||||
|
||||
QueryWrapper<Object> queryWrapper = new QueryWrapper<>();
|
||||
query.addTimeCondition(queryWrapper, "login_time");
|
||||
|
||||
String targetSql = queryWrapper.getTargetSql();
|
||||
Assertions.assertEquals("(login_time >= ? AND login_time <= ?)", targetSql);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void addTimeConditionWithBeginValueOnly() {
|
||||
query.setBeginTime(new Date());
|
||||
|
||||
QueryWrapper<Object> queryWrapper = new QueryWrapper<>();
|
||||
query.addTimeCondition(queryWrapper, "login_time");
|
||||
|
||||
String targetSql = queryWrapper.getTargetSql();
|
||||
Assertions.assertEquals("(login_time >= ?)", targetSql);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package com.agileboot.orm.query;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.agileboot.common.utils.time.DatePicker;
|
||||
import com.agileboot.common.utils.time.DatePickUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
@@ -60,10 +60,10 @@ public abstract class AbstractQuery {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void addTimeCondition(QueryWrapper queryWrapper, String fieldName) {
|
||||
if(queryWrapper!=null) {
|
||||
if (queryWrapper != null) {
|
||||
queryWrapper
|
||||
.ge(beginTime != null, fieldName, DatePicker.getBeginOfTheDay(beginTime))
|
||||
.le(endTime != null, fieldName, DatePicker.getEndOfTheDay(endTime));
|
||||
.ge(beginTime != null, fieldName, DatePickUtil.getBeginOfTheDay(beginTime))
|
||||
.le(endTime != null, fieldName, DatePickUtil.getEndOfTheDay(endTime));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
15
pom.xml
15
pom.xml
@@ -32,7 +32,7 @@
|
||||
<velocity.version>2.3</velocity.version>
|
||||
<jwt.version>0.9.1</jwt.version>
|
||||
<hutool.version>5.7.22</hutool.version>
|
||||
<junit.version>4.13.1</junit.version>
|
||||
<!-- <junit.version>4.13.1</junit.version>-->
|
||||
<lombok.version>1.18.24</lombok.version>
|
||||
<mybatis-plus.version>3.5.2</mybatis-plus.version>
|
||||
<mybatis-plus-generator.version>3.5.1</mybatis-plus-generator.version>
|
||||
@@ -212,12 +212,13 @@
|
||||
<version>${hutool.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>junit</groupId>-->
|
||||
<!-- <artifactId>junit</artifactId>-->
|
||||
<!-- <version>${junit.version}</version>-->
|
||||
<!-- <scope>test</scope>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
|
||||
Reference in New Issue
Block a user