View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package test.de.smartics.properties.impl.config.ds;
17  
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.equalTo;
20  import static org.hamcrest.Matchers.is;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  import de.smartics.properties.api.config.domain.Property;
29  import de.smartics.properties.api.config.domain.PropertyLocation;
30  import de.smartics.properties.impl.config.ds.hsql.HSqlDataSourceProxy;
31  import de.smartics.properties.spi.config.ds.PropertiesStore;
32  import de.smartics.testdoc.annotations.Uut;
33  
34  /**
35   * Tests {@link PropertiesStore#setProperty(String, String, String)}.
36   */
37  public class PropertiesStoreSetPropertyTest
38  {
39    // ********************************* Fields *********************************
40  
41    // --- constants ------------------------------------------------------------
42  
43    private static final String CONFIG_NAME = "test";
44  
45    private static final String PROPERTY_KEY = "test.key";
46  
47    // --- members --------------------------------------------------------------
48  
49    @Uut(method = "setProperty(String, String, String)")
50    private PropertiesStore uut;
51  
52    // ****************************** Inner Classes *****************************
53  
54    // ********************************* Methods ********************************
55  
56    // --- prepare --------------------------------------------------------------
57  
58    @Before
59    public void setUp()
60    {
61      final HSqlDataSourceProxy dataSourceProxy = HSqlDataSourceProxy.create();
62  
63      final Property expectedProperty =
64          new Property(new PropertyLocation(dataSourceProxy.getDataSourceId()
65                                            + "@config>test"), PROPERTY_KEY,
66              "test-value");
67      final Map<String, String> properties = new HashMap<String, String>();
68      properties.put(expectedProperty.getName(), expectedProperty.getValue());
69      final Map<String, Map<String, String>> initialProperties =
70          new HashMap<String, Map<String, String>>();
71      initialProperties.put(CONFIG_NAME, properties);
72  
73      final PropertiesStore.Builder builder = new PropertiesStore.Builder();
74      builder.setCreateTableSqlStatementTemplate(dataSourceProxy
75          .getCreateTableSqlTemplate());
76      builder.setInsertOrUpdateSqlStatementTemplate(dataSourceProxy
77          .getInsertOrUpdateSqlTemplate());
78      builder.setDataSourceProxy(dataSourceProxy);
79      builder.setInitialProperties(initialProperties);
80      uut = builder.build();
81      uut.createConfigTable();
82    }
83  
84    // --- helper ---------------------------------------------------------------
85  
86    // --- tests ----------------------------------------------------------------
87  
88    @Test
89    public void allowsToUpdateAnExistingProperty()
90    {
91      final String newValue = "new-test-value";
92      uut.setProperty(CONFIG_NAME, PROPERTY_KEY, newValue);
93  
94      final Property propertyRead = uut.getProperty(CONFIG_NAME, PROPERTY_KEY);
95      final String valueRead = propertyRead.getValue();
96  
97      assertThat(valueRead, is(equalTo(newValue)));
98    }
99  
100   @Test
101   public void allowsToInsertANewProperty()
102   {
103     final String newKey = "test.key.new";
104     final String newValue = "new-test-value";
105     uut.setProperty(CONFIG_NAME, newKey, newValue);
106 
107     final Property propertyRead = uut.getProperty(CONFIG_NAME, newKey);
108     final String valueRead = propertyRead.getValue();
109 
110     assertThat(valueRead, is(equalTo(newValue)));
111   }
112 }