修复props.toBean 数组字段未赋值问题

This commit is contained in:
Looly
2023-03-26 05:53:45 +08:00
parent 8bd76de6fe
commit 5799e018c1
9 changed files with 106 additions and 62 deletions

View File

@@ -585,7 +585,7 @@ public final class Props extends Properties implements BasicTypeGetter<String>,
BeanUtil.setProperty(bean, StrUtil.subSuf(key, prefix.length()), entry.getValue());
} catch (Exception e) {
// 忽略注入失败的字段(这些字段可能用于其它配置)
StaticLog.debug("Ignore property: [{}]", key);
StaticLog.debug("Ignore property: [{}],because of: {}", key, e);
}
}

View File

@@ -0,0 +1,27 @@
package cn.hutool.setting;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.setting.dialect.Props;
import cn.hutool.setting.dialect.PropsUtil;
import lombok.Data;
import org.junit.Assert;
import org.junit.Test;
public class Issue3008Test {
/**
* 数组字段追加后生成新的数组,造成赋值丢失<br>
* 修复见BeanUtil.setFieldValue
*/
@Test
public void toBeanTest() {
final Props props = PropsUtil.get("issue3008");
final MyUser user = props.toBean(MyUser.class, "person");
Assert.assertEquals("[LOL, KFC, COFFE]", ArrayUtil.toString(user.getHobby()));
}
@Data
static class MyUser {
private String[] hobby;
}
}

View File

@@ -31,11 +31,11 @@ public class PropsTest {
@Test
public void propTest() {
//noinspection MismatchedQueryAndUpdateOfCollection
Props props = new Props("test.properties");
String user = props.getProperty("user");
final Props props = new Props("test.properties");
final String user = props.getProperty("user");
Assert.assertEquals(user, "root");
String driver = props.getStr("driver");
final String driver = props.getStr("driver");
Assert.assertEquals(driver, "com.mysql.jdbc.Driver");
}
@@ -43,19 +43,19 @@ public class PropsTest {
@Ignore
public void propTestForAbsPAth() {
//noinspection MismatchedQueryAndUpdateOfCollection
Props props = new Props("d:/test.properties");
String user = props.getProperty("user");
final Props props = new Props("d:/test.properties");
final String user = props.getProperty("user");
Assert.assertEquals(user, "root");
String driver = props.getStr("driver");
final String driver = props.getStr("driver");
Assert.assertEquals(driver, "com.mysql.jdbc.Driver");
}
@Test
public void toBeanTest() {
Props props = Props.getProp("to_bean_test.properties");
final Props props = Props.getProp("to_bean_test.properties");
ConfigProperties cfg = props.toBean(ConfigProperties.class, "mail");
final ConfigProperties cfg = props.toBean(ConfigProperties.class, "mail");
Assert.assertEquals("mailer@mail.com", cfg.getHost());
Assert.assertEquals(9000, cfg.getPort());
Assert.assertEquals("mailer@mail.com", cfg.getFrom());
@@ -73,14 +73,14 @@ public class PropsTest {
@Test
public void toBeanWithNullPrefixTest(){
Props configProp = new Props();
final Props configProp = new Props();
configProp.setProperty("createTime", Objects.requireNonNull(DateUtil.parse("2020-01-01")));
configProp.setProperty("isInit", true);
configProp.setProperty("stairPlan", 1);
configProp.setProperty("stageNum", 2);
configProp.setProperty("version", 3);
SystemConfig systemConfig = configProp.toBean(SystemConfig.class);
final SystemConfig systemConfig = configProp.toBean(SystemConfig.class);
Assert.assertEquals(DateUtil.parse("2020-01-01"), systemConfig.getCreateTime());
Assert.assertEquals(true, systemConfig.getIsInit());

View File

@@ -0,0 +1,3 @@
person.hobby[0]=LOL
person.hobby[1]=KFC
person.hobby[2]=COFFE