From 88a899a283a15b022ba93915ff95e6c8d660e682 Mon Sep 17 00:00:00 2001 From: Cristian Rosu Date: Mon, 1 Jun 2020 23:32:38 +0300 Subject: [PATCH] BAEL-4070: Injecting List or Arrays from a Spring properties file --- .../lists/ListsPropertiesIntegrationTest.java | 88 +++++++++++++++++++ .../SpringListPropertiesApplication.java | 10 +++ .../src/test/resources/lists.properties | 6 ++ 3 files changed, 104 insertions(+) create mode 100644 spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/lists/ListsPropertiesIntegrationTest.java create mode 100644 spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/lists/SpringListPropertiesApplication.java create mode 100644 spring-boot-modules/spring-boot-properties/src/test/resources/lists.properties diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/lists/ListsPropertiesIntegrationTest.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/lists/ListsPropertiesIntegrationTest.java new file mode 100644 index 0000000000..4047e1ea95 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/lists/ListsPropertiesIntegrationTest.java @@ -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 ListsPropertiesIntegrationTest { + + @Value("${arrayOfStrings}") + private String[] arrayOfStrings; + + @Value("${arrayOfStrings}") + private List unexpectedListOfStrings; + + @Value("#{'${arrayOfStrings}'.split(',')}") + private List listOfStrings; + + @Value("#{${listOfStrings}}") + private List listOfStringsV2; + + @Value("#{'${listOfStringsWithCustomDelimiter}'.split(';')}") + private List listOfStringsWithCustomDelimiter; + + @Value("#{'${listOfBooleans}'.split(',')}") + private List listOfBooleans; + + @Value("#{'${listOfIntegers}'.split(',')}") + private List listOfIntegers; + + @Value("#{'${listOfCharacters}'.split(',')}") + private List listOfCharacters; + + @Autowired + private Environment environment; + + @Test + public void givenContextIsInitialized_ThenInjectedArrayContainsExpectedValues() { + assertEquals(arrayOfStrings, new String[] {"Baeldung", "dot", "com"}); + } + + @Test + public void givenContextIsInitialized_ThenInjectedListContainsUnexpectedValues() { + assertEquals(unexpectedListOfStrings, Collections.singletonList("Baeldung,dot,com")); + } + + @Test + public void givenContextIsInitialized_ThenInjectedListContainsExpectedValues() { + assertEquals(listOfStrings, Arrays.asList("Baeldung", "dot", "com")); + } + + @Test + public void givenContextIsInitialized_ThenInjectedListV2ContainsExpectedValues() { + assertEquals(listOfStringsV2, Arrays.asList("Baeldung", "dot", "com")); + } + + @Test + public void givenContextIsInitialized_ThenInjectedListWithCustomDelimiterContainsExpectedValues() { + assertEquals(listOfStringsWithCustomDelimiter, Arrays.asList("Baeldung", "dot", "com")); + } + + @Test + public void givenContextIsInitialized_ThenInjectedListOfBasicTypesContainsExpectedValues() { + assertEquals(listOfBooleans, Arrays.asList(false, false, true)); + assertEquals(listOfIntegers, Arrays.asList(1, 2, 3, 4)); + assertEquals(listOfCharacters, Arrays.asList('a', 'b', 'c')); + } + + @Test + public void givenContextIsInitialized_WhenReadingFromEnvironment_ThenPropertiesHaveExpectedValues() { + String[] arrayOfStrings = environment.getProperty("arrayOfStrings", String[].class); + List listOfStrings = (List)environment.getProperty("arrayOfStrings", List.class); + + assertEquals(arrayOfStrings, new String[] {"Baeldung", "dot", "com"}); + assertEquals(listOfStrings, Arrays.asList("Baeldung", "dot", "com")); + } +} diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/lists/SpringListPropertiesApplication.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/lists/SpringListPropertiesApplication.java new file mode 100644 index 0000000000..8a66079201 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/lists/SpringListPropertiesApplication.java @@ -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 { + +} diff --git a/spring-boot-modules/spring-boot-properties/src/test/resources/lists.properties b/spring-boot-modules/spring-boot-properties/src/test/resources/lists.properties new file mode 100644 index 0000000000..cc54d699a7 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/test/resources/lists.properties @@ -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 \ No newline at end of file