fixing formatting

This commit is contained in:
m.raheem 2020-02-20 07:24:35 +02:00
parent a44ab7dc56
commit 4414f4f390
13 changed files with 194 additions and 191 deletions

View File

@ -2,27 +2,27 @@ package com.baeldung.boot.configurationproperties;
public class Credentials { public class Credentials {
private String username; private String username;
private String password; private String password;
public Credentials(String username, String password) { public Credentials(String username, String password) {
this.username = username; this.username = username;
this.password = password; this.password = password;
} }
public String getUsername() { public String getUsername() {
return username; return username;
} }
public void setUsername(String username) { public void setUsername(String username) {
this.username = username; this.username = username;
} }
public String getPassword() { public String getPassword() {
return password; return password;
} }
public void setPassword(String password) { public void setPassword(String password) {
this.password = password; this.password = password;
} }
} }

View File

@ -8,9 +8,9 @@ import org.springframework.stereotype.Component;
@ConfigurationPropertiesBinding @ConfigurationPropertiesBinding
public class CustomCredentialsConverter implements Converter<String, Credentials> { public class CustomCredentialsConverter implements Converter<String, Credentials> {
@Override @Override
public Credentials convert(String source) { public Credentials convert(String source) {
String data[] = source.split(","); String[] data = source.split(",");
return new Credentials(data[0], data[1]); return new Credentials(data[0], data[1]);
} }
} }

View File

@ -19,41 +19,41 @@ import org.springframework.validation.annotation.Validated;
@Validated @Validated
public class MailServer { public class MailServer {
@NotNull @NotNull
@NotEmpty @NotEmpty
private Map<String, @NotBlank String> propertiesMap; private Map<String, @NotBlank String> propertiesMap;
@Valid @Valid
private MailConfig mailConfig = new MailConfig(); private MailConfig mailConfig = new MailConfig();
public static class MailConfig { public static class MailConfig {
@NotBlank @NotBlank
@Email @Email
private String address; private String address;
public String getAddress() { public String getAddress() {
return address; return address;
} }
public void setAddress(String address) { public void setAddress(String address) {
this.address = address; this.address = address;
} }
} }
public Map<String, String> getPropertiesMap() { public Map<String, String> getPropertiesMap() {
return propertiesMap; return propertiesMap;
} }
public void setPropertiesMap(Map<String, String> propertiesMap) { public void setPropertiesMap(Map<String, String> propertiesMap) {
this.propertiesMap = propertiesMap; this.propertiesMap = propertiesMap;
} }
public MailConfig getMailConfig() { public MailConfig getMailConfig() {
return mailConfig; return mailConfig;
} }
public void setMailConfig(MailConfig mailConfig) { public void setMailConfig(MailConfig mailConfig) {
this.mailConfig = mailConfig; this.mailConfig = mailConfig;
} }
} }

View File

@ -13,55 +13,55 @@ import org.springframework.util.unit.DataUnit;
@ConfigurationProperties(prefix = "server") @ConfigurationProperties(prefix = "server")
public class PropertyConversion { public class PropertyConversion {
private DataSize uploadSpeed; private DataSize uploadSpeed;
@DataSizeUnit(DataUnit.GIGABYTES) @DataSizeUnit(DataUnit.GIGABYTES)
private DataSize downloadSpeed; private DataSize downloadSpeed;
private Duration backupDay; private Duration backupDay;
@DurationUnit(ChronoUnit.HOURS) @DurationUnit(ChronoUnit.HOURS)
private Duration backupHour; private Duration backupHour;
private Credentials credentials; private Credentials credentials;
public Duration getBackupDay() { public Duration getBackupDay() {
return backupDay; return backupDay;
} }
public void setBackupDay(Duration backupDay) { public void setBackupDay(Duration backupDay) {
this.backupDay = backupDay; this.backupDay = backupDay;
} }
public Duration getBackupHour() { public Duration getBackupHour() {
return backupHour; return backupHour;
} }
public void setBackupHour(Duration backupHour) { public void setBackupHour(Duration backupHour) {
this.backupHour = backupHour; this.backupHour = backupHour;
} }
public DataSize getUploadSpeed() { public DataSize getUploadSpeed() {
return uploadSpeed; return uploadSpeed;
} }
public void setUploadSpeed(DataSize uploadSpeed) { public void setUploadSpeed(DataSize uploadSpeed) {
this.uploadSpeed = uploadSpeed; this.uploadSpeed = uploadSpeed;
} }
public DataSize getDownloadSpeed() { public DataSize getDownloadSpeed() {
return downloadSpeed; return downloadSpeed;
} }
public void setDownloadSpeed(DataSize downloadSpeed) { public void setDownloadSpeed(DataSize downloadSpeed) {
this.downloadSpeed = downloadSpeed; this.downloadSpeed = downloadSpeed;
} }
public Credentials getCredentials() { public Credentials getCredentials() {
return credentials; return credentials;
} }
public void setCredentials(Credentials credentials) { public void setCredentials(Credentials credentials) {
this.credentials = credentials; this.credentials = credentials;
} }
} }

View File

@ -9,35 +9,35 @@ import org.springframework.context.annotation.Configuration;
@ConfigurationProperties(prefix = "server") @ConfigurationProperties(prefix = "server")
public class ServerConfig { public class ServerConfig {
private Address address; private Address address;
private Map<String, String> resourcesPath; private Map<String, String> resourcesPath;
public static class Address { public static class Address {
private String ip; private String ip;
public String getIp() { public String getIp() {
return ip; return ip;
} }
public void setIp(String ip) { public void setIp(String ip) {
this.ip = ip; this.ip = ip;
} }
} }
public Address getAddress() { public Address getAddress() {
return address; return address;
} }
public void setAddress(Address address) { public void setAddress(Address address) {
this.address = address; this.address = address;
} }
public Map<String, String> getResourcesPath() { public Map<String, String> getResourcesPath() {
return resourcesPath; return resourcesPath;
} }
public void setResourcesPath(Map<String, String> resourcesPath) { public void setResourcesPath(Map<String, String> resourcesPath) {
this.resourcesPath = resourcesPath; this.resourcesPath = resourcesPath;
} }
} }

View File

@ -7,9 +7,9 @@ import org.springframework.context.annotation.Configuration;
@Configuration @Configuration
public class ServerConfigFactory { public class ServerConfigFactory {
@Bean @Bean
@ConfigurationProperties(prefix = "server.default") @ConfigurationProperties(prefix = "server.default")
public ServerConfig getDefaultConfigs() { public ServerConfig getDefaultConfigs() {
return new ServerConfig(); return new ServerConfig();
} }
} }

View File

@ -19,16 +19,18 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
@TestPropertySource("classpath:server-config-test.properties") @TestPropertySource("classpath:server-config-test.properties")
public class BindingPropertiesToBeanMethodsUnitTest { public class BindingPropertiesToBeanMethodsUnitTest {
@Autowired @Autowired
private ServerConfigFactory configFactory; private ServerConfigFactory configFactory;
@Test @Test
void givenBeanAnnotatedMethod_whenBindingProperties_thenAllFieldsAreSet() { void givenBeanAnnotatedMethod_whenBindingProperties_thenAllFieldsAreSet() {
assertEquals("192.168.0.2", configFactory.getDefaultConfigs()
.getAddress()
.getIp());
assertEquals("192.168.0.2", configFactory.getDefaultConfigs().getAddress().getIp()); Map<String, String> expectedResourcesPath = new HashMap<>();
expectedResourcesPath.put("imgs", "/root/def/imgs");
Map<String, String> expectedResourcesPath = new HashMap<>(); assertEquals(expectedResourcesPath, configFactory.getDefaultConfigs()
expectedResourcesPath.put("imgs", "/root/def/imgs"); .getResourcesPath());
assertEquals(expectedResourcesPath, configFactory.getDefaultConfigs().getResourcesPath()); }
}
} }

View File

@ -17,16 +17,16 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
@TestPropertySource("classpath:server-config-test.properties") @TestPropertySource("classpath:server-config-test.properties")
public class BindingPropertiesToUserDefinedPOJOUnitTest { public class BindingPropertiesToUserDefinedPOJOUnitTest {
@Autowired @Autowired
private ServerConfig serverConfig; private ServerConfig serverConfig;
@Test @Test
void givenUserDefinedPOJO_whenBindingPropertiesFile_thenAllFieldsAreSet() { void givenUserDefinedPOJO_whenBindingPropertiesFile_thenAllFieldsAreSet() {
assertEquals("192.168.0.1", serverConfig.getAddress()
.getIp());
assertEquals("192.168.0.1", serverConfig.getAddress().getIp()); Map<String, String> expectedResourcesPath = new HashMap<>();
expectedResourcesPath.put("imgs", "/root/imgs");
Map<String, String> expectedResourcesPath = new HashMap<>(); assertEquals(expectedResourcesPath, serverConfig.getResourcesPath());
expectedResourcesPath.put("imgs", "/root/imgs"); }
assertEquals(expectedResourcesPath, serverConfig.getResourcesPath());
}
} }

View File

@ -18,16 +18,16 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
@ActiveProfiles("test") @ActiveProfiles("test")
public class BindingYMLPropertiesUnitTest { public class BindingYMLPropertiesUnitTest {
@Autowired @Autowired
private ServerConfig serverConfig; private ServerConfig serverConfig;
@Test @Test
void whenBindingYMLConfigFile_thenAllFieldsAreSet() { void whenBindingYMLConfigFile_thenAllFieldsAreSet() {
assertEquals("192.168.0.4", serverConfig.getAddress()
.getIp());
assertEquals("192.168.0.4", serverConfig.getAddress().getIp()); Map<String, String> expectedResourcesPath = new HashMap<>();
expectedResourcesPath.put("imgs", "/etc/test/imgs");
Map<String, String> expectedResourcesPath = new HashMap<>(); assertEquals(expectedResourcesPath, serverConfig.getResourcesPath());
expectedResourcesPath.put("imgs", "/etc/test/imgs"); }
assertEquals(expectedResourcesPath, serverConfig.getResourcesPath());
}
} }

View File

@ -17,17 +17,17 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
@TestPropertySource(properties = { "validate.mail_config.address=new_user@test" }) @TestPropertySource(properties = { "validate.mail_config.address=new_user@test" })
public class OverridingConfigurationPropertiesUnitTest { public class OverridingConfigurationPropertiesUnitTest {
@Autowired @Autowired
private MailServer mailServer; private MailServer mailServer;
@Test @Test
void givenUsingPropertiesAttribute_whenAssiginingNewValueToProprty_thenSpringUsesNewValue() { void givenUsingPropertiesAttribute_whenAssiginingNewValueToProprty_thenSpringUsesNewValue() {
assertEquals("new_user@test", mailServer.getMailConfig()
.getAddress());
assertEquals("new_user@test", mailServer.getMailConfig().getAddress()); Map<String, String> expectedMap = new HashMap<>();
expectedMap.put("first", "prop1");
Map<String, String> expectedMap = new HashMap<>(); expectedMap.put("second", "prop2");
expectedMap.put("first", "prop1"); assertEquals(expectedMap, mailServer.getPropertiesMap());
expectedMap.put("second", "prop2"); }
assertEquals(expectedMap, mailServer.getPropertiesMap());
}
} }

View File

@ -18,20 +18,22 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
@TestPropertySource("classpath:property-validation-test.properties") @TestPropertySource("classpath:property-validation-test.properties")
public class PropertyValidationUnitTest { public class PropertyValidationUnitTest {
@Autowired @Autowired
private MailServer mailServer; private MailServer mailServer;
private static Validator propertyValidator; private static Validator propertyValidator;
@BeforeAll @BeforeAll
public static void setup() { public static void setup() {
propertyValidator = Validation.buildDefaultValidatorFactory().getValidator(); propertyValidator = Validation.buildDefaultValidatorFactory()
} .getValidator();
}
@Test @Test
void whenBindingPropertiesToValidatedBeans_thenConstrainsAreChecked() { void whenBindingPropertiesToValidatedBeans_thenConstrainsAreChecked() {
assertEquals(0, propertyValidator.validate(mailServer.getPropertiesMap())
assertEquals(0, propertyValidator.validate(mailServer.getPropertiesMap()).size()); .size());
assertEquals(0, propertyValidator.validate(mailServer.getMailConfig()).size()); assertEquals(0, propertyValidator.validate(mailServer.getMailConfig())
} .size());
}
} }

View File

@ -19,27 +19,26 @@ import org.springframework.util.unit.DataSize;
@TestPropertySource("classpath:spring-conversion-test.properties") @TestPropertySource("classpath:spring-conversion-test.properties")
public class SpringPropertiesConversionUnitTest { public class SpringPropertiesConversionUnitTest {
@Autowired @Autowired
private PropertyConversion propertyConversion; private PropertyConversion propertyConversion;
@Test @Test
void whenUsingSpringDefaultSizeConversion_thenDataSizeObjectIsSet() { void whenUsingSpringDefaultSizeConversion_thenDataSizeObjectIsSet() {
assertEquals(DataSize.ofMegabytes(500), propertyConversion.getUploadSpeed());
assertEquals(DataSize.ofGigabytes(10), propertyConversion.getDownloadSpeed());
}
assertEquals(DataSize.ofMegabytes(500), propertyConversion.getUploadSpeed()); @Test
assertEquals(DataSize.ofGigabytes(10), propertyConversion.getDownloadSpeed()); void whenUsingSpringDefaultDurationConversion_thenDurationObjectIsSet() {
} assertEquals(Duration.ofDays(1), propertyConversion.getBackupDay());
assertEquals(Duration.ofHours(8), propertyConversion.getBackupHour());
}
@Test @Test
void whenUsingSpringDefaultDurationConversion_thenDurationObjectIsSet() { void whenRegisteringCustomCredentialsConverter_thenCredentialsAreParsed() {
assertEquals("user", propertyConversion.getCredentials()
assertEquals(Duration.ofDays(1), propertyConversion.getBackupDay()); .getUsername());
assertEquals(Duration.ofHours(8), propertyConversion.getBackupHour()); assertEquals("123", propertyConversion.getCredentials()
} .getPassword());
}
@Test
void whenRegisteringCustomCredentialsConverter_thenCredentialsAreParsed() {
assertEquals("user", propertyConversion.getCredentials().getUsername());
assertEquals("123", propertyConversion.getCredentials().getPassword());
}
} }