fix copyProperties bug

This commit is contained in:
Looly
2020-05-09 21:50:15 +08:00
parent 8289e6a8da
commit c1edd9b401
6 changed files with 82 additions and 11 deletions

View File

@@ -5,12 +5,14 @@ import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.bean.copier.ValueProvider;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.junit.Assert;
import org.junit.Test;
import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.time.LocalDate;
import java.time.LocalDateTime;
@@ -213,7 +215,7 @@ public class BeanUtilTest {
}
@Test
public void copyProperties() {
public void copyPropertiesTest() {
SubPerson person = new SubPerson();
person.setAge(14);
person.setOpenid("11213232");
@@ -331,9 +333,9 @@ public class BeanUtilTest {
}
@Test
public void beanToBeanTest(){
public void beanToBeanTest() {
// 修复对象无getter方法导致报错的问题
Page page1=new Page();
Page page1 = new Page();
BeanUtil.toBean(page1, Page.class);
}
@@ -349,4 +351,30 @@ public class BeanUtilTest {
return this;
}
}
@Test
public void copyBeanToBeanTest() {
// 测试在copyProperties方法中alias是否有效
Food info = new Food();
info.setBookID("0");
info.setCode("123");
HllFoodEntity entity = new HllFoodEntity();
BeanUtil.copyProperties(info, entity);
Assert.assertEquals(info.getBookID(), entity.getBookId());
Assert.assertEquals(info.getCode(), entity.getCode2());
}
@Data
public static class Food {
@Alias("bookId")
private String bookID;
private String code;
}
@Data
public static class HllFoodEntity implements Serializable {
private String bookId;
@Alias("code")
private String code2;
}
}

View File

@@ -6,7 +6,9 @@ import cn.hutool.core.lang.Filter;
import org.junit.Assert;
import org.junit.Test;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Map;
import java.util.Objects;
@@ -161,6 +163,17 @@ public class ArrayUtilTest {
double maxDouble = ArrayUtil.max(1D, 2.4D, 13.0D, 4.55D, 5D);
Assert.assertEquals(13.0, maxDouble, 2);
BigDecimal one = new BigDecimal("1.00");
BigDecimal two = new BigDecimal("2.0");
BigDecimal three = new BigDecimal("3");
BigDecimal[] bigDecimals = {two,one,three};
BigDecimal minAccuracy = ArrayUtil.min(bigDecimals, Comparator.comparingInt(BigDecimal::scale));
Assert.assertEquals(minAccuracy,three);
BigDecimal maxAccuracy = ArrayUtil.max(bigDecimals,Comparator.comparingInt(BigDecimal::scale));
Assert.assertEquals(maxAccuracy,one);
}
@Test