From 333212a15ff67dc03ef5118afc33cd09c899395f Mon Sep 17 00:00:00 2001 From: zhengjie Date: Wed, 8 May 2019 17:00:20 +0800 Subject: [PATCH] Add unit tests for me.zhengjie.utils.StringUtils --- .../me/zhengjie/utils/StringUtilsTest.java | 49 +++++++++++++++++++ .../service/JwtUserDetailsService.java | 3 +- .../me/zhengjie/rest/AliPayController.java | 4 +- .../service/impl/AlipayServiceImpl.java | 2 +- .../service/impl/QiNiuServiceImpl.java | 3 +- .../{util => utils}/AliPayStatusEnum.java | 2 +- .../zhengjie/{util => utils}/AlipayUtils.java | 2 +- .../zhengjie/{util => utils}/QiNiuUtil.java | 4 +- 8 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 eladmin-common/src/test/java/me/zhengjie/utils/StringUtilsTest.java rename eladmin-tools/src/main/java/me/zhengjie/{util => utils}/AliPayStatusEnum.java (96%) rename eladmin-tools/src/main/java/me/zhengjie/{util => utils}/AlipayUtils.java (98%) rename eladmin-tools/src/main/java/me/zhengjie/{util => utils}/QiNiuUtil.java (96%) diff --git a/eladmin-common/src/test/java/me/zhengjie/utils/StringUtilsTest.java b/eladmin-common/src/test/java/me/zhengjie/utils/StringUtilsTest.java new file mode 100644 index 000000000..f18fb1d92 --- /dev/null +++ b/eladmin-common/src/test/java/me/zhengjie/utils/StringUtilsTest.java @@ -0,0 +1,49 @@ +package me.zhengjie.utils; + +import org.junit.Test; +import org.springframework.mock.web.MockHttpServletRequest; + +import java.text.SimpleDateFormat; +import java.util.*; + +import static me.zhengjie.utils.StringUtils.*; +import static org.junit.Assert.*; + +public class StringUtilsTest { + + @Test + public void testInString() { + assertTrue(inString("?", "?")); + assertFalse(inString("?", new String[]{})); + } + + @Test + public void testToCamelCase() { + assertNull(toCamelCase(null)); + } + + @Test + public void testToCapitalizeCamelCase() { + assertNull(StringUtils.toCapitalizeCamelCase(null)); + assertEquals("HelloWorld", toCapitalizeCamelCase("hello_world")); + } + + @Test + public void testToUnderScoreCase() { + assertNull(StringUtils.toUnderScoreCase(null)); + assertEquals("hello_world", toUnderScoreCase("helloWorld")); + assertEquals("\u0000\u0000", toUnderScoreCase("\u0000\u0000")); + assertEquals("\u0000_a", toUnderScoreCase("\u0000A")); + } + + @Test + public void testGetWeekDay() { + SimpleDateFormat simpleDateformat = new SimpleDateFormat("E"); + assertEquals(simpleDateformat.format(new Date()), getWeekDay()); + } + + @Test + public void testGetIP() { + assertEquals("127.0.0.1", getIP(new MockHttpServletRequest())); + } +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/security/service/JwtUserDetailsService.java b/eladmin-system/src/main/java/me/zhengjie/modules/security/service/JwtUserDetailsService.java index 7d07d6072..f5cc8720c 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/security/service/JwtUserDetailsService.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/security/service/JwtUserDetailsService.java @@ -1,5 +1,6 @@ package me.zhengjie.modules.security.service; +import me.zhengjie.exception.BadRequestException; import me.zhengjie.modules.system.domain.*; import me.zhengjie.exception.EntityNotFoundException; import me.zhengjie.modules.system.repository.PermissionRepository; @@ -40,7 +41,7 @@ public UserDetails loadUserByUsername(String username){ User user = userService.findByName(username); if (user == null) { - throw new EntityNotFoundException(User.class, "name", username); + throw new BadRequestException("账号不存在"); } else { return createJwtUser(user); } diff --git a/eladmin-tools/src/main/java/me/zhengjie/rest/AliPayController.java b/eladmin-tools/src/main/java/me/zhengjie/rest/AliPayController.java index 7c1632a00..da8fb4d77 100644 --- a/eladmin-tools/src/main/java/me/zhengjie/rest/AliPayController.java +++ b/eladmin-tools/src/main/java/me/zhengjie/rest/AliPayController.java @@ -6,8 +6,8 @@ import me.zhengjie.domain.AlipayConfig; import me.zhengjie.domain.vo.TradeVo; import me.zhengjie.service.AlipayService; -import me.zhengjie.util.AliPayStatusEnum; -import me.zhengjie.util.AlipayUtils; +import me.zhengjie.utils.AliPayStatusEnum; +import me.zhengjie.utils.AlipayUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; diff --git a/eladmin-tools/src/main/java/me/zhengjie/service/impl/AlipayServiceImpl.java b/eladmin-tools/src/main/java/me/zhengjie/service/impl/AlipayServiceImpl.java index 4257b4bca..682218154 100644 --- a/eladmin-tools/src/main/java/me/zhengjie/service/impl/AlipayServiceImpl.java +++ b/eladmin-tools/src/main/java/me/zhengjie/service/impl/AlipayServiceImpl.java @@ -9,7 +9,7 @@ import me.zhengjie.exception.BadRequestException; import me.zhengjie.repository.AlipayRepository; import me.zhengjie.service.AlipayService; -import me.zhengjie.util.AlipayUtils; +import me.zhengjie.utils.AlipayUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; diff --git a/eladmin-tools/src/main/java/me/zhengjie/service/impl/QiNiuServiceImpl.java b/eladmin-tools/src/main/java/me/zhengjie/service/impl/QiNiuServiceImpl.java index a787305db..027782f82 100644 --- a/eladmin-tools/src/main/java/me/zhengjie/service/impl/QiNiuServiceImpl.java +++ b/eladmin-tools/src/main/java/me/zhengjie/service/impl/QiNiuServiceImpl.java @@ -15,7 +15,7 @@ import me.zhengjie.repository.QiNiuConfigRepository; import me.zhengjie.repository.QiniuContentRepository; import me.zhengjie.service.QiNiuService; -import me.zhengjie.util.QiNiuUtil; +import me.zhengjie.utils.QiNiuUtil; import me.zhengjie.utils.FileUtil; import me.zhengjie.utils.ValidationUtil; import org.springframework.beans.factory.annotation.Autowired; @@ -25,7 +25,6 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; -import java.time.LocalDate; import java.util.Optional; /** diff --git a/eladmin-tools/src/main/java/me/zhengjie/util/AliPayStatusEnum.java b/eladmin-tools/src/main/java/me/zhengjie/utils/AliPayStatusEnum.java similarity index 96% rename from eladmin-tools/src/main/java/me/zhengjie/util/AliPayStatusEnum.java rename to eladmin-tools/src/main/java/me/zhengjie/utils/AliPayStatusEnum.java index 5a54da5a5..46e6b7cfc 100644 --- a/eladmin-tools/src/main/java/me/zhengjie/util/AliPayStatusEnum.java +++ b/eladmin-tools/src/main/java/me/zhengjie/utils/AliPayStatusEnum.java @@ -1,4 +1,4 @@ -package me.zhengjie.util; +package me.zhengjie.utils; /** * 支付状态 diff --git a/eladmin-tools/src/main/java/me/zhengjie/util/AlipayUtils.java b/eladmin-tools/src/main/java/me/zhengjie/utils/AlipayUtils.java similarity index 98% rename from eladmin-tools/src/main/java/me/zhengjie/util/AlipayUtils.java rename to eladmin-tools/src/main/java/me/zhengjie/utils/AlipayUtils.java index e57c43e2e..6c6b2dccb 100644 --- a/eladmin-tools/src/main/java/me/zhengjie/util/AlipayUtils.java +++ b/eladmin-tools/src/main/java/me/zhengjie/utils/AlipayUtils.java @@ -1,4 +1,4 @@ -package me.zhengjie.util; +package me.zhengjie.utils; import cn.hutool.core.util.StrUtil; import com.alipay.api.AlipayApiException; diff --git a/eladmin-tools/src/main/java/me/zhengjie/util/QiNiuUtil.java b/eladmin-tools/src/main/java/me/zhengjie/utils/QiNiuUtil.java similarity index 96% rename from eladmin-tools/src/main/java/me/zhengjie/util/QiNiuUtil.java rename to eladmin-tools/src/main/java/me/zhengjie/utils/QiNiuUtil.java index eea842209..bc0d13f60 100644 --- a/eladmin-tools/src/main/java/me/zhengjie/util/QiNiuUtil.java +++ b/eladmin-tools/src/main/java/me/zhengjie/utils/QiNiuUtil.java @@ -1,8 +1,8 @@ -package me.zhengjie.util; +package me.zhengjie.utils; import com.qiniu.common.Zone; import com.qiniu.storage.Configuration; -import me.zhengjie.utils.FileUtil; + import java.text.SimpleDateFormat; import java.util.Date;