mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
Props add toBean method
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package cn.hutool.setting.test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
@@ -8,37 +11,76 @@ import org.junit.Test;
|
||||
import cn.hutool.log.LogFactory;
|
||||
import cn.hutool.log.dialect.console.ConsoleLogFactory;
|
||||
import cn.hutool.setting.dialect.Props;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* Setting单元测试
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class PropsTest {
|
||||
|
||||
|
||||
@Before
|
||||
public void init(){
|
||||
public void init() {
|
||||
LogFactory.setCurrentLogFactory(ConsoleLogFactory.class);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void propTest(){
|
||||
public void propTest() {
|
||||
Props props = new Props("test.properties");
|
||||
String user = props.getProperty("user");
|
||||
Assert.assertEquals(user, "root");
|
||||
|
||||
|
||||
String driver = props.getStr("driver");
|
||||
Assert.assertEquals(driver, "com.mysql.jdbc.Driver");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void propTestForAbsPAth() {
|
||||
Props props = new Props("d:/test.properties");
|
||||
String user = props.getProperty("user");
|
||||
Assert.assertEquals(user, "root");
|
||||
|
||||
String driver = props.getStr("driver");
|
||||
Assert.assertEquals(driver, "com.mysql.jdbc.Driver");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void propTestForAbsPAth(){
|
||||
Props props = new Props("d:/test.properties");
|
||||
String user = props.getProperty("user");
|
||||
Assert.assertEquals(user, "root");
|
||||
public void toBeanTest() {
|
||||
Props props = Props.getProp("to_bean_test.properties");
|
||||
|
||||
String driver = props.getStr("driver");
|
||||
Assert.assertEquals(driver, "com.mysql.jdbc.Driver");
|
||||
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));
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class ConfigProperties {
|
||||
private String host;
|
||||
private int port;
|
||||
private String from;
|
||||
private Credentials credentials;
|
||||
private List<String> defaultRecipients;
|
||||
private Map<String, String> additionalHeaders;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Credentials {
|
||||
private String authMethod;
|
||||
private String username;
|
||||
private String password;
|
||||
}
|
||||
}
|
||||
|
20
hutool-setting/src/test/resources/to_bean_test.properties
Normal file
20
hutool-setting/src/test/resources/to_bean_test.properties
Normal file
@@ -0,0 +1,20 @@
|
||||
#Simple properties
|
||||
mail.host=mailer@mail.com
|
||||
mail.port=9000
|
||||
mail.from=mailer@mail.com
|
||||
|
||||
#List properties
|
||||
mail.defaultRecipients[0]=admin@mail.com
|
||||
mail.defaultRecipients[1]=owner@mail.com
|
||||
|
||||
#Map Properties
|
||||
mail.additionalHeaders.redelivery=true
|
||||
mail.additionalHeaders.secure=true
|
||||
|
||||
#Object properties
|
||||
mail.credentials.username=john
|
||||
mail.credentials.password=password
|
||||
mail.credentials.authMethod=SHA1
|
||||
|
||||
# ignore properties
|
||||
mail.ignore.filed = balabala
|
Reference in New Issue
Block a user