From a6869ae721bb40c3f7d1300b8e242f50c25e3aaa Mon Sep 17 00:00:00 2001 From: Looly Date: Tue, 10 Mar 2020 01:12:19 +0800 Subject: [PATCH] add test --- CHANGELOG.md | 1 + .../hutool/core/lang/tree/package-info.java | 17 ++- .../java/cn/hutool/core/util/RandomUtil.java | 29 +++- .../java/cn/hutool/json/JSONObjectTest.java | 130 ++++++------------ 4 files changed, 79 insertions(+), 98 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef1443ebd..bd62b4301 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### 新特性 * 【core 】 修改FastDateParser策略,与JDK保持一致(issue#I1AXIN@Gitee) * 【core 】 增加tree(树状结构)(pr#100@Gitee) +* 【core 】 增加randomEleList(pr#764@Github) ### Bug修复 * 【setting】 修复Props.toBean方法null的问题 * 【core 】 修复DataUtil.parseLocalDateTime无时间部分报错问题(issue#I1B18H@Gitee) diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/tree/package-info.java b/hutool-core/src/main/java/cn/hutool/core/lang/tree/package-info.java index db431330d..d07c5d728 100644 --- a/hutool-core/src/main/java/cn/hutool/core/lang/tree/package-info.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/tree/package-info.java @@ -1,13 +1,12 @@ /** - * 提供通用树生成,特点: - *
- *     1、每个字段可自定义
- *     2、支持排序 树深度配置,自定义转换器等
- *     3、支持额外属性扩展
- *     4、贴心 许多属性,特性都有默认值处理
- *     5、使用简单 可一行代码生成树
- *     6、代码简洁轻量无额外依赖
- * 
+ * 提供通用树生成,特点:

+ * 1、每个字段可自定义
+ * 2、支持排序 树深度配置,自定义转换器等
+ * 3、支持额外属性扩展
+ * 4、贴心 许多属性,特性都有默认值处理
+ * 5、使用简单 可一行代码生成树
+ * 6、代码简洁轻量无额外依赖 + *

* * @author liangbaikai(https://gitee.com/liangbaikai00/) * @since 5.2.1 diff --git a/hutool-core/src/main/java/cn/hutool/core/util/RandomUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/RandomUtil.java index 58cbed0df..f15665bf7 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/RandomUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/RandomUtil.java @@ -351,6 +351,28 @@ public class RandomUtil { return result; } + /** + * 随机获得列表中的一定量的元素,返回List
+ * 此方法与{@link #randomEles(List, int)} 不同点在于,不会获取重复位置的元素 + * + * @param source 列表 + * @param count 随机取出的个数 + * @param 元素类型 + * @return 随机列表 + * @since 5.2.1 + */ + public static List randomEleList(List source, int count) { + if (count >= source.size()) { + return source; + } + final int[] randomList = ArrayUtil.sub(randomInts(source.size()), 0, count); + List result = new ArrayList<>(); + for (int e : randomList) { + result.add(source.get(e)); + } + return result; + } + /** * 随机获得列表中的一定量的不重复元素,返回Set * @@ -374,6 +396,7 @@ public class RandomUtil { return result; } + /** * 创建指定长度的随机索引 * @@ -381,11 +404,11 @@ public class RandomUtil { * @return 随机索引 * @since 5.2.1 */ - public static int[] randomInts(int length){ + public static int[] randomInts(int length) { final int[] range = ArrayUtil.range(length); for (int i = 0; i < length; i++) { - int random = randomInt(i,length); - ArrayUtil.swap(range,i,random); + int random = randomInt(i, length); + ArrayUtil.swap(range, i, random); } return range; } diff --git a/hutool-json/src/test/java/cn/hutool/json/JSONObjectTest.java b/hutool-json/src/test/java/cn/hutool/json/JSONObjectTest.java index 5ca532c84..293efb2fe 100644 --- a/hutool-json/src/test/java/cn/hutool/json/JSONObjectTest.java +++ b/hutool-json/src/test/java/cn/hutool/json/JSONObjectTest.java @@ -5,6 +5,8 @@ import java.util.Date; import java.util.List; import java.util.Objects; +import cn.hutool.core.annotation.Alias; +import lombok.Data; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; @@ -31,9 +33,8 @@ import cn.hutool.json.test.bean.report.SuiteReport; /** * JSONObject单元测试 - * - * @author Looly * + * @author Looly */ public class JSONObjectTest { @@ -64,13 +65,13 @@ public class JSONObjectTest { .setDateFormat(DatePattern.NORM_DATE_PATTERN); Assert.assertEquals("{\"dateTime\":\"2019-05-02\"}", json.toString()); } - + @Test public void toStringWithDateTest() { JSONObject json = JSONUtil.createObj().put("date", DateUtil.parse("2019-05-08 19:18:21")); assert json != null; Assert.assertEquals("{\"date\":1557314301000}", json.toString()); - + json = Objects.requireNonNull(JSONUtil.createObj().put("date", DateUtil.parse("2019-05-08 19:18:21"))).setDateFormat(DatePattern.NORM_DATE_PATTERN); Assert.assertEquals("{\"date\":\"2019-05-08\"}", json.toString()); } @@ -122,7 +123,7 @@ public class JSONObjectTest { JSONObject json = new JSONObject(jsonStr); Assert.assertEquals("体”、“文", json.getStr("test")); } - + @Test @Ignore public void parseStringWithBomTest() { @@ -224,7 +225,12 @@ public class JSONObjectTest { @SuppressWarnings("ConstantConditions") @Test public void toBeanTest6() { - JSONObject json = JSONUtil.createObj().put("targetUrl", "http://test.com").put("success", "true").put("result", JSONUtil.createObj().put("token", "tokenTest").put("userId", "测试用户1")); + JSONObject json = JSONUtil.createObj() + .put("targetUrl", "http://test.com") + .put("success", "true") + .put("result", JSONUtil.createObj() + .put("token", "tokenTest") + .put("userId", "测试用户1")); TokenAuthWarp2 bean = json.toBean(TokenAuthWarp2.class); Assert.assertEquals("http://test.com", bean.getTargetUrl()); @@ -356,112 +362,64 @@ public class JSONObjectTest { Assert.assertEquals("yyb\nbbb", jsonObject.getStr("name")); // 转义按照字符串显示 Assert.assertEquals("yyb\\nbbb", jsonObject.getStrEscaped("name")); - + String bbb = jsonObject.getStr("bbb", "defaultBBB"); Assert.assertEquals("defaultBBB", bbb); } + @Test + public void aliasTest(){ + final BeanWithAlias beanWithAlias = new BeanWithAlias(); + beanWithAlias.setValue1("张三"); + beanWithAlias.setValue2(35); + + final JSONObject jsonObject = JSONUtil.parseObj(beanWithAlias); + Assert.assertEquals("张三", jsonObject.getStr("name")); + Assert.assertEquals(new Integer(35), jsonObject.getInt("age")); + + JSONObject json = JSONUtil.createObj() + .put("name", "张三") + .put("age", 35); + final BeanWithAlias bean = JSONUtil.toBean(Objects.requireNonNull(json).toString(), BeanWithAlias.class); + Assert.assertEquals("张三", bean.getValue1()); + Assert.assertEquals(new Integer(35), bean.getValue2()); + } + public enum TestEnum { TYPE_A, TYPE_B } /** * 测试Bean - * - * @author Looly * + * @author Looly */ + @Data public static class TestBean { private String strValue; private int intValue; private Double doubleValue; - private subBean beanValue; + private SubBean beanValue; private List list; private TestEnum testEnum; - - public String getStrValue() { - return strValue; - } - - public void setStrValue(String strValue) { - this.strValue = strValue; - } - - public int getIntValue() { - return intValue; - } - - public void setIntValue(int intValue) { - this.intValue = intValue; - } - - public Double getDoubleValue() { - return doubleValue; - } - - public void setDoubleValue(Double doubleValue) { - this.doubleValue = doubleValue; - } - - public subBean getBeanValue() { - return beanValue; - } - - public void setBeanValue(subBean beanValue) { - this.beanValue = beanValue; - } - - public List getList() { - return list; - } - - public void setList(List list) { - this.list = list; - } - - public TestEnum getTestEnum() { - return testEnum; - } - - public void setTestEnum(TestEnum testEnum) { - this.testEnum = testEnum; - } - - @Override - public String toString() { - return "TestBean [strValue=" + strValue + ", intValue=" + intValue + ", doubleValue=" + doubleValue + ", beanValue=" + beanValue + ", list=" + list + ", testEnum=" + testEnum + "]"; - } } /** * 测试子Bean - * - * @author Looly * + * @author Looly */ - public static class subBean { + @Data + public static class SubBean { private String value1; private BigDecimal value2; + } - public String getValue1() { - return value1; - } - - public void setValue1(String value1) { - this.value1 = value1; - } - - public BigDecimal getValue2() { - return value2; - } - - public void setValue2(BigDecimal value2) { - this.value2 = value2; - } - - @Override - public String toString() { - return "subBean [value1=" + value1 + ", value2=" + value2 + "]"; - } + @Data + public static class BeanWithAlias { + @Alias("name") + private String value1; + @Alias("age") + private Integer value2; } }