BAEL-2510 Add nested properties section (#6212)
Add nested properties section to ConfigurationProperties article.
This commit is contained in:
parent
4bd87241ce
commit
8ccec9a413
|
@ -8,7 +8,6 @@ import javax.validation.constraints.Min;
|
|||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Pattern;
|
||||
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
@ -20,41 +19,8 @@ import org.springframework.validation.annotation.Validated;
|
|||
@Validated
|
||||
public class ConfigProperties {
|
||||
|
||||
@Validated
|
||||
public static class Credentials {
|
||||
|
||||
@Length(max = 4, min = 1)
|
||||
private String authMethod;
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public String getAuthMethod() {
|
||||
return authMethod;
|
||||
}
|
||||
|
||||
public void setAuthMethod(String authMethod) {
|
||||
this.authMethod = authMethod;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
|
||||
@NotBlank
|
||||
private String host;
|
||||
private String hostName;
|
||||
|
||||
@Min(1025)
|
||||
@Max(65536)
|
||||
|
@ -63,16 +29,16 @@ public class ConfigProperties {
|
|||
@Pattern(regexp = "^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,6}$")
|
||||
private String from;
|
||||
|
||||
private Credentials credentials;
|
||||
private List<String> defaultRecipients;
|
||||
private Map<String, String> additionalHeaders;
|
||||
private Credentials credentials;
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
public String getHostName() {
|
||||
return hostName;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
public void setHostName(String hostName) {
|
||||
this.hostName = hostName;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
|
@ -91,14 +57,6 @@ public class ConfigProperties {
|
|||
this.from = from;
|
||||
}
|
||||
|
||||
public Credentials getCredentials() {
|
||||
return credentials;
|
||||
}
|
||||
|
||||
public void setCredentials(Credentials credentials) {
|
||||
this.credentials = credentials;
|
||||
}
|
||||
|
||||
public List<String> getDefaultRecipients() {
|
||||
return defaultRecipients;
|
||||
}
|
||||
|
@ -114,4 +72,12 @@ public class ConfigProperties {
|
|||
public void setAdditionalHeaders(Map<String, String> additionalHeaders) {
|
||||
this.additionalHeaders = additionalHeaders;
|
||||
}
|
||||
|
||||
public Credentials getCredentials() {
|
||||
return credentials;
|
||||
}
|
||||
|
||||
public void setCredentials(Credentials credentials) {
|
||||
this.credentials = credentials;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
package org.baeldung.properties;
|
||||
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
@Validated
|
||||
public class Credentials {
|
||||
|
||||
@Length(max = 4, min = 1)
|
||||
private String authMethod;
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public String getAuthMethod() {
|
||||
return authMethod;
|
||||
}
|
||||
|
||||
public void setAuthMethod(String authMethod) {
|
||||
this.authMethod = authMethod;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
#Simple properties
|
||||
mail.host=mailer@mail.com
|
||||
mail.hostname=host@mail.com
|
||||
mail.port=9000
|
||||
mail.from=mailer@mail.com
|
||||
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
package org.baeldung.properties;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
@ -18,26 +21,36 @@ public class ConfigPropertiesIntegrationTest {
|
|||
|
||||
@Test
|
||||
public void whenSimplePropertyQueriedthenReturnsProperty() throws Exception {
|
||||
Assert.assertTrue("From address is read as null!", properties.getFrom() != null);
|
||||
Assert.assertEquals("Incorrectly bound hostName property", "host@mail.com", properties.getHostName());
|
||||
Assert.assertEquals("Incorrectly bound port property", 9000, properties.getPort());
|
||||
Assert.assertEquals("Incorrectly bound from property", "mailer@mail.com", properties.getFrom());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenListPropertyQueriedthenReturnsProperty() throws Exception {
|
||||
Assert.assertTrue("Couldn't bind list property!", properties.getDefaultRecipients().size() == 2);
|
||||
Assert.assertTrue("Incorrectly bound list property. Expected 2 entries!", properties.getDefaultRecipients().size() == 2);
|
||||
List<String> defaultRecipients = properties.getDefaultRecipients();
|
||||
Assert.assertTrue("Couldn't bind list property!", defaultRecipients.size() == 2);
|
||||
Assert.assertTrue("Incorrectly bound list property. Expected 2 entries!", defaultRecipients.size() == 2);
|
||||
Assert.assertEquals("Incorrectly bound list[0] property", "admin@mail.com", defaultRecipients.get(0));
|
||||
Assert.assertEquals("Incorrectly bound list[1] property", "owner@mail.com", defaultRecipients.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMapPropertyQueriedthenReturnsProperty() throws Exception {
|
||||
Assert.assertTrue("Couldn't bind map property!", properties.getAdditionalHeaders() != null);
|
||||
Assert.assertTrue("Incorrectly bound map property. Expected 3 Entries!", properties.getAdditionalHeaders().size() == 3);
|
||||
Map<String, String> additionalHeaders = properties.getAdditionalHeaders();
|
||||
Assert.assertTrue("Couldn't bind map property!", additionalHeaders != null);
|
||||
Assert.assertTrue("Incorrectly bound map property. Expected 3 Entries!", additionalHeaders.size() == 3);
|
||||
Assert.assertEquals("Incorrectly bound map[redelivery] property", "true", additionalHeaders.get("redelivery"));
|
||||
Assert.assertEquals("Incorrectly bound map[secure] property", "true", additionalHeaders.get("secure"));
|
||||
Assert.assertEquals("Incorrectly bound map[p3] property", "value", additionalHeaders.get("p3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenObjectPropertyQueriedthenReturnsProperty() throws Exception {
|
||||
Assert.assertTrue("Couldn't bind map property!", properties.getCredentials() != null);
|
||||
Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getAuthMethod().equals("SHA1"));
|
||||
Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getUsername().equals("john"));
|
||||
Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getPassword().equals("password"));
|
||||
Credentials credentials = properties.getCredentials();
|
||||
Assert.assertTrue("Couldn't bind map property!", credentials != null);
|
||||
Assert.assertEquals("Incorrectly bound object property, authMethod", "SHA1", credentials.getAuthMethod());
|
||||
Assert.assertEquals("Incorrectly bound object property, username", "john", credentials.getUsername());
|
||||
Assert.assertEquals("Incorrectly bound object property, password", "password", credentials.getPassword());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#Simple properties
|
||||
mail.host=mailer@mail.com
|
||||
mail.hostname=host@mail.com
|
||||
mail.port=9000
|
||||
mail.from=mailer@mail.com
|
||||
|
||||
|
|
Loading…
Reference in New Issue