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.junit.Test;
25  import org.settings4j.Settings4j;
26  import org.settings4j.connector.PropertyFileConnector;
27  import org.settings4j.connector.SystemPropertyConnector;
28  
29  import java.io.File;
30  
31  import static org.hamcrest.MatcherAssert.assertThat;
32  import static org.hamcrest.Matchers.is;
33  import static org.settings4j.ConnectorPositions.afterLast;
34  
35  /**
36   * Snippet for src/site/markdown/exampleSettings4j.md with compile validation.
37   */
38  public class Settings4jExampleTest {
39  
40      @Test
41      public void testSettings4jConfiguration() throws Exception {
42  
43          // START SNIPPET: configExample
44          // initialization
45          File secretKey = new File("src/test/data/secretFileExample.key");
46          File propertiesFile = new File("src/test/data/TestProperties-Valid.properties");
47  
48          // initialization - get decrypted value
49          SecuredPropertiesConfig config = new SecuredPropertiesConfig().withSecretFile(secretKey).initDefault();
50          // initialization - auto encrypt values in the property files:
51          SecuredProperties.encryptNonEncryptedValues(
52                  config, propertiesFile, "mySecretPassword");
53          // initialization - get decrypted value
54          String myPassword = SecuredProperties.getSecretValue(
55                  config, propertiesFile, "mySecretPassword");
56          // initialization - store plane-text-PW into System-Properties
57          System.setProperty("mySecretPassword", myPassword);
58  
59          // initialization - add custom Settings4j PropertyFileConnector after existing SystemPropertyConnector
60          PropertyFileConnector myConnector = new PropertyFileConnector();
61          myConnector.setName("myCustomConfig");
62          myConnector.setPropertyFromPath(propertiesFile.toURI().toString());
63          Settings4j.getSettingsRepository().getSettings().addConnector(myConnector, afterLast(SystemPropertyConnector.class));
64  
65          // somewhere in your application:
66          assertThat(Settings4j.getString("myTitle"), is("My Test")); // some value from PropertyFile
67          assertThat(Settings4j.getString("mySecretPassword"), is("test")); // decrypted Password from SystemProperties.
68          // END SNIPPET: configExample
69  
70      }
71  }