mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add test
This commit is contained in:
parent
0781be4e1c
commit
a6869ae721
@ -8,6 +8,7 @@
|
|||||||
### 新特性
|
### 新特性
|
||||||
* 【core 】 修改FastDateParser策略,与JDK保持一致(issue#I1AXIN@Gitee)
|
* 【core 】 修改FastDateParser策略,与JDK保持一致(issue#I1AXIN@Gitee)
|
||||||
* 【core 】 增加tree(树状结构)(pr#100@Gitee)
|
* 【core 】 增加tree(树状结构)(pr#100@Gitee)
|
||||||
|
* 【core 】 增加randomEleList(pr#764@Github)
|
||||||
### Bug修复
|
### Bug修复
|
||||||
* 【setting】 修复Props.toBean方法null的问题
|
* 【setting】 修复Props.toBean方法null的问题
|
||||||
* 【core 】 修复DataUtil.parseLocalDateTime无时间部分报错问题(issue#I1B18H@Gitee)
|
* 【core 】 修复DataUtil.parseLocalDateTime无时间部分报错问题(issue#I1B18H@Gitee)
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
/**
|
/**
|
||||||
* 提供通用树生成,特点:
|
* 提供通用树生成,特点:<p>
|
||||||
* <pre>
|
* 1、每个字段可自定义<br>
|
||||||
* 1、每个字段可自定义
|
* 2、支持排序 树深度配置,自定义转换器等<br>
|
||||||
* 2、支持排序 树深度配置,自定义转换器等
|
* 3、支持额外属性扩展<br>
|
||||||
* 3、支持额外属性扩展
|
* 4、贴心 许多属性,特性都有默认值处理<br>
|
||||||
* 4、贴心 许多属性,特性都有默认值处理
|
* 5、使用简单 可一行代码生成树<br>
|
||||||
* 5、使用简单 可一行代码生成树
|
|
||||||
* 6、代码简洁轻量无额外依赖
|
* 6、代码简洁轻量无额外依赖
|
||||||
* <pre/>
|
* </p>
|
||||||
*
|
*
|
||||||
* @author liangbaikai(https://gitee.com/liangbaikai00/)
|
* @author liangbaikai(https://gitee.com/liangbaikai00/)
|
||||||
* @since 5.2.1
|
* @since 5.2.1
|
||||||
|
@ -351,6 +351,28 @@ public class RandomUtil {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 随机获得列表中的一定量的元素,返回List<br>
|
||||||
|
* 此方法与{@link #randomEles(List, int)} 不同点在于,不会获取重复位置的元素
|
||||||
|
*
|
||||||
|
* @param source 列表
|
||||||
|
* @param count 随机取出的个数
|
||||||
|
* @param <T> 元素类型
|
||||||
|
* @return 随机列表
|
||||||
|
* @since 5.2.1
|
||||||
|
*/
|
||||||
|
public static <T> List<T> randomEleList(List<T> source, int count) {
|
||||||
|
if (count >= source.size()) {
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
final int[] randomList = ArrayUtil.sub(randomInts(source.size()), 0, count);
|
||||||
|
List<T> result = new ArrayList<>();
|
||||||
|
for (int e : randomList) {
|
||||||
|
result.add(source.get(e));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 随机获得列表中的一定量的不重复元素,返回Set
|
* 随机获得列表中的一定量的不重复元素,返回Set
|
||||||
*
|
*
|
||||||
@ -374,6 +396,7 @@ public class RandomUtil {
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建指定长度的随机索引
|
* 创建指定长度的随机索引
|
||||||
*
|
*
|
||||||
|
@ -5,6 +5,8 @@ import java.util.Date;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import cn.hutool.core.annotation.Alias;
|
||||||
|
import lombok.Data;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -33,7 +35,6 @@ import cn.hutool.json.test.bean.report.SuiteReport;
|
|||||||
* JSONObject单元测试
|
* JSONObject单元测试
|
||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class JSONObjectTest {
|
public class JSONObjectTest {
|
||||||
|
|
||||||
@ -224,7 +225,12 @@ public class JSONObjectTest {
|
|||||||
@SuppressWarnings("ConstantConditions")
|
@SuppressWarnings("ConstantConditions")
|
||||||
@Test
|
@Test
|
||||||
public void toBeanTest6() {
|
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);
|
TokenAuthWarp2 bean = json.toBean(TokenAuthWarp2.class);
|
||||||
Assert.assertEquals("http://test.com", bean.getTargetUrl());
|
Assert.assertEquals("http://test.com", bean.getTargetUrl());
|
||||||
@ -361,6 +367,24 @@ public class JSONObjectTest {
|
|||||||
Assert.assertEquals("defaultBBB", bbb);
|
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 {
|
public enum TestEnum {
|
||||||
TYPE_A, TYPE_B
|
TYPE_A, TYPE_B
|
||||||
}
|
}
|
||||||
@ -369,99 +393,33 @@ public class JSONObjectTest {
|
|||||||
* 测试Bean
|
* 测试Bean
|
||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
@Data
|
||||||
public static class TestBean {
|
public static class TestBean {
|
||||||
private String strValue;
|
private String strValue;
|
||||||
private int intValue;
|
private int intValue;
|
||||||
private Double doubleValue;
|
private Double doubleValue;
|
||||||
private subBean beanValue;
|
private SubBean beanValue;
|
||||||
private List<String> list;
|
private List<String> list;
|
||||||
private TestEnum testEnum;
|
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<String> getList() {
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setList(List<String> 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
|
* 测试子Bean
|
||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public static class subBean {
|
@Data
|
||||||
|
public static class SubBean {
|
||||||
private String value1;
|
private String value1;
|
||||||
private BigDecimal value2;
|
private BigDecimal value2;
|
||||||
|
|
||||||
public String getValue1() {
|
|
||||||
return value1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setValue1(String value1) {
|
@Data
|
||||||
this.value1 = value1;
|
public static class BeanWithAlias {
|
||||||
}
|
@Alias("name")
|
||||||
|
private String value1;
|
||||||
public BigDecimal getValue2() {
|
@Alias("age")
|
||||||
return value2;
|
private Integer value2;
|
||||||
}
|
|
||||||
|
|
||||||
public void setValue2(BigDecimal value2) {
|
|
||||||
this.value2 = value2;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "subBean [value1=" + value1 + ", value2=" + value2 + "]";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user