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.Ignore;
27  import org.junit.Test;
28  
29  import de.smartics.properties.api.config.domain.Property;
30  import de.smartics.properties.api.config.domain.PropertyLocation;
31  import de.smartics.properties.impl.config.ds.mysql.MySqlDataSourceProxy;
32  import de.smartics.properties.spi.config.ds.PropertiesStore;
33  import de.smartics.testdoc.annotations.Uut;
34  
35  /**
36   * Tests {@link PropertiesStore#setProperty(String, String, String)}.
37   */
38  @Ignore("Requires a running MySQL instance at jdbc:mysql://localhost:3306/properties.")
39  public class PropertiesStoreSetPropertyTest
40  {
41    // ********************************* Fields *********************************
42  
43    // --- constants ------------------------------------------------------------
44  
45    private static final String CONFIG_NAME = "test";
46  
47    private static final String PROPERTY_KEY = "test.key";
48  
49    // --- members --------------------------------------------------------------
50  
51    @Uut(method = "setProperty(String, String, String)")
52    private PropertiesStore uut;
53  
54    // ****************************** Inner Classes *****************************
55  
56    // ********************************* Methods ********************************
57  
58    // --- prepare --------------------------------------------------------------
59  
60    @Before
61    public void setUp()
62    {
63      final MySqlDataSourceProxy dataSourceProxy = MySqlDataSourceProxy.create();
64  
65      final Property expectedProperty =
66          new Property(new PropertyLocation(dataSourceProxy.getDataSourceId()
67                                            + "@config>test"), PROPERTY_KEY,
68              "test-value");
69      final Map<String, String> properties = new HashMap<String, String>();
70      properties.put(expectedProperty.getName(), expectedProperty.getValue());
71      final Map<String, Map<String, String>> initialProperties =
72          new HashMap<String, Map<String, String>>();
73      initialProperties.put(CONFIG_NAME, properties);
74  
75      final PropertiesStore.Builder builder = new PropertiesStore.Builder();
76      builder.setCreateTableSqlStatementTemplate(dataSourceProxy
77          .getCreateTableSqlTemplate());
78      builder.setInsertOrUpdateSqlStatementTemplate(dataSourceProxy
79          .getInsertOrUpdateSqlTemplate());
80      builder.setDataSourceProxy(dataSourceProxy);
81      builder.setInitialProperties(initialProperties);
82      uut = builder.build();
83      uut.createConfigTable();
84    }
85  
86    // --- helper ---------------------------------------------------------------
87  
88    // --- tests ----------------------------------------------------------------
89  
90    @Test
91    public void allowsToUpdateAnExistingProperty()
92    {
93      final String newValue = "new-test-value";
94      uut.setProperty(CONFIG_NAME, PROPERTY_KEY, newValue);
95  
96      final Property propertyRead = uut.getProperty(CONFIG_NAME, PROPERTY_KEY);
97      final String valueRead = propertyRead.getValue();
98  
99      assertThat(valueRead, is(equalTo(newValue)));
100   }
101 
102   @Test
103   public void allowsToInsertANewProperty()
104   {
105     final String newKey = "test.key.new";
106     final String newValue = "new-test-value";
107     uut.setProperty(CONFIG_NAME, newKey, newValue);
108 
109     final Property propertyRead = uut.getProperty(CONFIG_NAME, newKey);
110     final String valueRead = propertyRead.getValue();
111 
112     assertThat(valueRead, is(equalTo(newValue)));
113   }
114 }