View Javadoc
1   /*-
2    * #%L
3    * Secured Properties
4    * ===============================================================
5    * Copyright (C) 2016 Brabenetz Harald, Austria
6    * ===============================================================
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
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   * Snippet for src/site/markdown/exampleCommonsConfiguration.md with compile validation.
38   */
39  public class CommonsConfigurationExampleTest {
40  
41      @Test
42      public void testSettings4jConfiguration() throws Exception {
43  
44          // START SNIPPET: configExample
45          // initialization
46          File secretKey = new File("src/test/data/secretFileExample.key");
47          File propertiesFile = new File("src/test/data/TestProperties-Valid.properties");
48  
49          // initialization - get decrypted value
50          SecuredPropertiesConfig config = new SecuredPropertiesConfig().withSecretFile(secretKey).initDefault();
51          // initialization - auto encrypt values in the property files:
52          SecuredProperties.encryptNonEncryptedValues(
53                  config, propertiesFile, "mySecretPassword");
54          // initialization - get decrypted value
55          String myPassword = SecuredProperties.getSecretValue(
56                  config, propertiesFile, "mySecretPassword");
57          // initialization - store plane-text-PW into System-Properties
58          System.setProperty("mySecretPassword", myPassword);
59  
60          // initialization - create CompositeConfiguration which first reads SystemProperties and then the PropertiesFile.
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          // somewhere in your application (e.g. with dependency injection of commonConfig):
73          assertThat(commonConfig.getString("myTitle"), is("My Test")); // some value from PropertyFile
74          assertThat(commonConfig.getString("mySecretPassword"), is("test")); // decrypted Password from SystemProperties.
75          // END SNIPPET: configExample
76  
77      }
78  }