Merge pull request #9412 from chris9408/feature/BAEL-4070
BAEL-4070: Injecting List or Arrays from a Spring properties file
This commit is contained in:
commit
25661128ae
@ -0,0 +1,88 @@
|
||||
package com.baeldung.properties.lists;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = SpringListPropertiesApplication.class)
|
||||
public class ListsPropertiesUnitTest {
|
||||
|
||||
@Value("${arrayOfStrings}")
|
||||
private String[] arrayOfStrings;
|
||||
|
||||
@Value("${arrayOfStrings}")
|
||||
private List<String> unexpectedListOfStrings;
|
||||
|
||||
@Value("#{'${arrayOfStrings}'.split(',')}")
|
||||
private List<String> listOfStrings;
|
||||
|
||||
@Value("#{${listOfStrings}}")
|
||||
private List<String> listOfStringsV2;
|
||||
|
||||
@Value("#{'${listOfStringsWithCustomDelimiter}'.split(';')}")
|
||||
private List<String> listOfStringsWithCustomDelimiter;
|
||||
|
||||
@Value("#{'${listOfBooleans}'.split(',')}")
|
||||
private List<Boolean> listOfBooleans;
|
||||
|
||||
@Value("#{'${listOfIntegers}'.split(',')}")
|
||||
private List<Integer> listOfIntegers;
|
||||
|
||||
@Value("#{'${listOfCharacters}'.split(',')}")
|
||||
private List<Character> listOfCharacters;
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Test
|
||||
public void whenContextIsInitialized_thenInjectedArrayContainsExpectedValues() {
|
||||
assertEquals(new String[] {"Baeldung", "dot", "com"}, arrayOfStrings);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenContextIsInitialized_thenInjectedListContainsUnexpectedValues() {
|
||||
assertEquals(Collections.singletonList("Baeldung,dot,com"), unexpectedListOfStrings);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenContextIsInitialized_thenInjectedListContainsExpectedValues() {
|
||||
assertEquals(Arrays.asList("Baeldung", "dot", "com"), listOfStrings);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenContextIsInitialized_thenInjectedListV2ContainsExpectedValues() {
|
||||
assertEquals(Arrays.asList("Baeldung", "dot", "com"), listOfStringsV2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenContextIsInitialized_thenInjectedListWithCustomDelimiterContainsExpectedValues() {
|
||||
assertEquals(Arrays.asList("Baeldung", "dot", "com"), listOfStringsWithCustomDelimiter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenContextIsInitialized_thenInjectedListOfBasicTypesContainsExpectedValues() {
|
||||
assertEquals(Arrays.asList(false, false, true), listOfBooleans);
|
||||
assertEquals(Arrays.asList(1, 2, 3, 4), listOfIntegers);
|
||||
assertEquals(Arrays.asList('a', 'b', 'c'), listOfCharacters);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadingFromSpringEnvironment_thenPropertiesHaveExpectedValues() {
|
||||
String[] arrayOfStrings = environment.getProperty("arrayOfStrings", String[].class);
|
||||
List<String> listOfStrings = (List<String>)environment.getProperty("arrayOfStrings", List.class);
|
||||
|
||||
assertEquals(new String[] {"Baeldung", "dot", "com"}, arrayOfStrings);
|
||||
assertEquals(Arrays.asList("Baeldung", "dot", "com"), listOfStrings);
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.baeldung.properties.lists;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
@Configuration
|
||||
@PropertySource(value = "lists.properties")
|
||||
public class SpringListPropertiesApplication {
|
||||
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
arrayOfStrings=Baeldung,dot,com
|
||||
listOfStrings={'Baeldung','dot','com'}
|
||||
listOfStringsWithCustomDelimiter=Baeldung;dot;com
|
||||
listOfBooleans=false,false,true
|
||||
listOfIntegers=1,2,3,4
|
||||
listOfCharacters=a,b,c
|
Loading…
x
Reference in New Issue
Block a user