1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.brabenetz.lib.securedproperties.snippets;
21
22 import net.brabenetz.lib.securedproperties.SecuredProperties;
23 import net.brabenetz.lib.securedproperties.SecuredPropertiesConfig;
24 import org.apache.commons.configuration2.CompositeConfiguration;
25 import org.apache.commons.configuration2.PropertiesConfiguration;
26 import org.apache.commons.configuration2.SystemConfiguration;
27 import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
28 import org.apache.commons.configuration2.builder.fluent.Parameters;
29 import org.junit.Test;
30
31 import java.io.File;
32
33 import static org.hamcrest.MatcherAssert.assertThat;
34 import static org.hamcrest.Matchers.is;
35
36
37
38
39 public class CommonsConfigurationExampleTest {
40
41 @Test
42 public void testSettings4jConfiguration() throws Exception {
43
44
45
46 File secretKey = new File("src/test/data/secretFileExample.key");
47 File propertiesFile = new File("src/test/data/TestProperties-Valid.properties");
48
49
50 SecuredPropertiesConfig config = new SecuredPropertiesConfig().withSecretFile(secretKey).initDefault();
51
52 SecuredProperties.encryptNonEncryptedValues(
53 config, propertiesFile, "mySecretPassword");
54
55 String myPassword = SecuredProperties.getSecretValue(
56 config, propertiesFile, "mySecretPassword");
57
58 System.setProperty("mySecretPassword", myPassword);
59
60
61 SystemConfiguration systemConfiguration = new SystemConfiguration();
62
63 PropertiesConfiguration propertiesConfiguration =
64 new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class)
65 .configure(new Parameters().properties().setFile(propertiesFile))
66 .getConfiguration();
67
68 CompositeConfiguration commonConfig = new CompositeConfiguration();
69 commonConfig.addConfiguration(systemConfiguration);
70 commonConfig.addConfiguration(propertiesConfiguration);
71
72
73 assertThat(commonConfig.getString("myTitle"), is("My Test"));
74 assertThat(commonConfig.getString("mySecretPassword"), is("test"));
75
76
77 }
78 }