Skip to content

Commit afc62ca

Browse files
JonCookecheyne
authored andcommitted
BAEL-2193 - Merge 2 java.util.Properties objects (eugenp#5571)
* BAEL-2268 - Guide to JerseyTest - second attempt without formatting changes * BAEL-2268 - Guide to JerseyTest - Add line break to end of file * BAEL-2193 - Merge 2 java.util.Properties objects * BAEL-2193 - Merge 2 java.util.Properties object
1 parent cb1b3d5 commit afc62ca

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.baeldung.properties;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertThat;
6+
7+
import java.util.Map;
8+
import java.util.Properties;
9+
import java.util.Set;
10+
import java.util.stream.Stream;
11+
12+
import org.junit.Test;
13+
14+
public class MergePropertiesUnitTest {
15+
16+
@Test
17+
public void givenTwoProperties_whenMergedUsingIteration_thenAllPropertiesInResult() {
18+
Properties globalProperties = mergePropertiesByIteratingKeySet(propertiesA(), propertiesB());
19+
20+
testMergedProperties(globalProperties);
21+
}
22+
23+
@Test
24+
public void givenTwoProperties_whenMergedUsingPutAll_thenAllPropertiesInResult() {
25+
Properties globalProperties = mergePropertiesByUsingPutAll(propertiesA(), propertiesB());
26+
27+
testMergedProperties(globalProperties);
28+
}
29+
30+
@Test
31+
public void givenTwoProperties_whenMergedUsingStreamAPI_thenAllPropertiesInResult() {
32+
Properties globalProperties = mergePropertiesByUsingStreamApi(propertiesB(), propertiesA());
33+
34+
testMergedProperties(globalProperties);
35+
}
36+
37+
private Properties mergePropertiesByIteratingKeySet(Properties... properties) {
38+
Properties mergedProperties = new Properties();
39+
for (Properties property : properties) {
40+
Set<String> propertyNames = property.stringPropertyNames();
41+
for (String name : propertyNames) {
42+
String propertyValue = property.getProperty(name);
43+
mergedProperties.setProperty(name, propertyValue);
44+
}
45+
}
46+
return mergedProperties;
47+
}
48+
49+
private Properties mergePropertiesByUsingPutAll(Properties... properties) {
50+
Properties mergedProperties = new Properties();
51+
for (Properties property : properties) {
52+
mergedProperties.putAll(property);
53+
}
54+
return mergedProperties;
55+
}
56+
57+
private Properties mergePropertiesByUsingStreamApi(Properties... properties) {
58+
return Stream.of(properties)
59+
.collect(Properties::new, Map::putAll, Map::putAll);
60+
}
61+
62+
private Properties propertiesA() {
63+
Properties properties = new Properties();
64+
properties.setProperty("application.name", "my-app");
65+
properties.setProperty("application.version", "1.0");
66+
return properties;
67+
}
68+
69+
private Properties propertiesB() {
70+
Properties properties = new Properties();
71+
properties.setProperty("property-1", "sample property");
72+
properties.setProperty("property-2", "another sample property");
73+
return properties;
74+
}
75+
76+
private void testMergedProperties(Properties globalProperties) {
77+
assertThat("There should be 4 properties", globalProperties.size(), equalTo(4));
78+
assertEquals("Property should be", globalProperties.getProperty("application.name"), "my-app");
79+
assertEquals("Property should be", globalProperties.getProperty("application.version"), "1.0");
80+
assertEquals("Property should be", globalProperties.getProperty("property-1"), "sample property");
81+
assertEquals("Property should be", globalProperties.getProperty("property-2"), "another sample property");
82+
}
83+
84+
}

0 commit comments

Comments
 (0)