mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add YamlUtil
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package cn.hutool.setting.test;
|
||||
package cn.hutool.setting;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.log.LogFactory;
|
||||
@@ -17,7 +17,7 @@ import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Setting单元测试
|
||||
*
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
@@ -50,23 +50,23 @@ public class PropsTest {
|
||||
String driver = props.getStr("driver");
|
||||
Assert.assertEquals(driver, "com.mysql.jdbc.Driver");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void toBeanTest() {
|
||||
Props props = Props.getProp("to_bean_test.properties");
|
||||
|
||||
|
||||
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());
|
||||
|
||||
|
||||
Assert.assertEquals("john", cfg.getCredentials().getUsername());
|
||||
Assert.assertEquals("password", cfg.getCredentials().getPassword());
|
||||
Assert.assertEquals("SHA1", cfg.getCredentials().getAuthMethod());
|
||||
|
||||
|
||||
Assert.assertEquals("true", cfg.getAdditionalHeaders().get("redelivery"));
|
||||
Assert.assertEquals("true", cfg.getAdditionalHeaders().get("secure"));
|
||||
|
||||
|
||||
Assert.assertEquals("admin@mail.com", cfg.getDefaultRecipients().get(0));
|
||||
Assert.assertEquals("owner@mail.com", cfg.getDefaultRecipients().get(1));
|
||||
}
|
||||
@@ -98,7 +98,7 @@ public class PropsTest {
|
||||
private List<String> defaultRecipients;
|
||||
private Map<String, String> additionalHeaders;
|
||||
}
|
||||
|
||||
|
||||
@Data
|
||||
public static class Credentials {
|
||||
private String authMethod;
|
@@ -1,4 +1,4 @@
|
||||
package cn.hutool.setting.test;
|
||||
package cn.hutool.setting;
|
||||
|
||||
import cn.hutool.setting.dialect.PropsUtil;
|
||||
import org.junit.Assert;
|
||||
@@ -7,7 +7,7 @@ import org.junit.Test;
|
||||
import java.util.Objects;
|
||||
|
||||
public class PropsUtilTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void getTest() {
|
||||
String driver = PropsUtil.get("test").getStr("driver");
|
@@ -1,12 +1,10 @@
|
||||
package cn.hutool.setting.test;
|
||||
package cn.hutool.setting;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.setting.Setting;
|
||||
|
||||
/**
|
||||
* Setting单元测试
|
||||
*
|
@@ -1,12 +1,10 @@
|
||||
package cn.hutool.setting.test;
|
||||
package cn.hutool.setting;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.setting.SettingUtil;
|
||||
|
||||
public class SettingUtilTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void getTest() {
|
||||
String driver = SettingUtil.get("test").get("demo", "driver");
|
@@ -0,0 +1,36 @@
|
||||
package cn.hutool.setting.yaml;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.lang.Dict;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class YamlUtilTest {
|
||||
|
||||
@Test
|
||||
public void loadByPathTest() {
|
||||
final Dict result = YamlUtil.loadByPath("test.yaml", Dict.class);
|
||||
|
||||
Assert.assertEquals("John", result.getStr("firstName"));
|
||||
|
||||
final List<Integer> numbers = result.getByPath("contactDetails.number");
|
||||
Assert.assertEquals(123456789, (int) numbers.get(0));
|
||||
Assert.assertEquals(456786868, (int) numbers.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void dumpTest() {
|
||||
final Dict dict = Dict.create()
|
||||
.set("name", "hutool")
|
||||
.set("count", 1000);
|
||||
|
||||
YamlUtil.dump(
|
||||
dict
|
||||
, FileUtil.getWriter("d:/test/dump.yaml", CharsetUtil.CHARSET_UTF_8, false));
|
||||
}
|
||||
}
|
13
hutool-setting/src/test/resources/test.yaml
Normal file
13
hutool-setting/src/test/resources/test.yaml
Normal file
@@ -0,0 +1,13 @@
|
||||
firstName: "John"
|
||||
lastName: "Doe"
|
||||
age: 31
|
||||
contactDetails:
|
||||
- type: "mobile"
|
||||
number: 123456789
|
||||
- type: "landline"
|
||||
number: 456786868
|
||||
homeAddress:
|
||||
line: "Xyz, DEF Street"
|
||||
city: "City Y"
|
||||
state: "State Y"
|
||||
zip: 345657
|
Reference in New Issue
Block a user