View Javadoc
1   /* ***************************************************************************
2    * Copyright (c) 2008 Brabenetz Harald, Austria.
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   *****************************************************************************/
17  
18  package org.settings4j.config;
19  
20  import java.io.File;
21  import java.sql.Connection;
22  import java.sql.PreparedStatement;
23  import java.sql.ResultSet;
24  import java.util.Map;
25  
26  import javax.sql.DataSource;
27  
28  import org.apache.commons.io.FileUtils;
29  import org.settings4j.Connector;
30  import org.settings4j.Settings4jInstance;
31  import org.settings4j.Settings4jRepository;
32  import org.settings4j.test.TestUtils;
33  import org.settings4j.settings.SettingsManager;
34  
35  /**
36   * TestCases for {@link Settings4jInstance}.
37   * <p>
38   * Checkstyle:OFF MagicNumber
39   */
40  public class TestSettings4jConfig extends AbstractTestSettings4jConfig {
41  
42      /**
43       * Test parsing of defaultsettings4j.xml (FALLBACK-Configuration).
44       */
45      public void testDefaultSettings4jConfig() {
46          final Settings4jRepository settingsRepository = TestUtils
47              .getConfiguredSettingsRepository(SettingsManager.DEFAULT_FALLBACK_CONFIGURATION_FILE);
48  
49          final Settings4jInstance settings = settingsRepository.getSettings();
50  
51          // the rootSettings have four Connectors
52          final int expectedConnectorSize = 4;
53          assertEquals(expectedConnectorSize, settings.getConnectors().size());
54  
55          // check if there is no Exception thrown:
56          assertNull(settings.getString("xyz"));
57          assertNull(settings.getContent("xyz"));
58          assertNull(settings.getObject("xyz"));
59  
60          assertEquals(expectedConnectorSize, settings.getConnectors().size());
61      }
62  
63      /**
64       * test corrupt config  xml file.
65       * 
66       */
67      public void testCorruptConfig() {
68          final Settings4jRepository settingsRepository = TestUtils
69              .getConfiguredSettingsRepository("org/settings4j/config/testConfigCorrupt.xml");
70  
71          assertEquals(0, settingsRepository.getSettings().getConnectors().size());
72      }
73  
74  
75      /**
76       * test for {@link org.settings4j.connector.FSConnector}.
77       * 
78       * @throws Exception in case of an error. 
79       */
80      public void testFSConfigTempFolder() throws Exception {
81          final Settings4jRepository settingsRepository = TestUtils
82              .getConfiguredSettingsRepository("org/settings4j/config/testConfigFSTempfolder.xml");
83  
84          // store values into the default java temporary directory with subfolder "Settings4j"
85          // String tmpdir = System.getProperty("java.io.tmpdir");
86          final Settings4jInstance settings = settingsRepository.getSettings();
87          final File tmpFolder = TestUtils.getTmpFolder();
88          final File fileXyz = new File(tmpFolder, "xyz");
89          assertFalse(fileXyz.exists());
90          FileUtils.writeStringToFile(fileXyz, "abc", "UTF-8");
91          assertTrue(fileXyz.exists());
92  
93          FileUtils.writeStringToFile(new File(tmpFolder, "xyz2"), "abc2", "UTF-8");
94  
95          // every settings have read access to the same FSConnector.
96          assertEquals("abc", settings.getString("xyz"));
97          assertEquals("abc2", settings.getString("xyz2"));
98  
99          assertEquals(2, settings.getConnectors().size());
100 
101         Connector connector;
102         connector = settings.getConnector("FSConnector");
103         assertNotNull(connector);
104         connector = settings.getConnector("SystemPropertyConnector");
105         assertNotNull(connector);
106         connector = settings.getConnector("nichtVorhanden");
107         assertNull(connector);
108 
109     }
110 
111     /**
112      * test for {@link org.settings4j.connector.FSConnector}.
113      * 
114      * @throws Exception in case of an error. 
115      */
116     public void testFSConfigTestFolder() throws Exception {
117         final Settings4jRepository settingsRepository = TestUtils
118             .getConfiguredSettingsRepository("org/settings4j/config/testConfigFSTestfolder.xml");
119 
120         // store values into the default java temporary directory with subfolder "Settings4j"
121         // String tmpdir = System.getProperty("java.io.tmpdir");
122         final Settings4jInstance settings = settingsRepository.getSettings();
123         final File testFolder = TestUtils.getTestFolder();
124         final File fileXyz = new File(testFolder, "xyz");
125         assertFalse(fileXyz.exists());
126         FileUtils.writeStringToFile(fileXyz, "abc", "UTF-8");
127         assertTrue(fileXyz.exists());
128 
129         FileUtils.writeStringToFile(new File(testFolder, "xyz2"), "abc2", "UTF-8");
130 
131         // every settings have read access to the same FSConnector.
132         assertEquals("abc", settings.getString("xyz"));
133         assertEquals("abc2", settings.getString("xyz2"));
134 
135         assertEquals(1, settings.getConnectors().size());
136     }
137 
138     /**
139      * Test ObjectReolver with cached Connector.
140      */
141     public void testObjectResolverConfig() {
142 
143 
144         testCaching("org/settings4j/config/testConfigObjectResolver-nocaching.xml", //
145             "org/settings4j/objectresolver/test1", //
146             false); // no caching!
147 
148     }
149 
150     /**
151      * Test ObjectReolver with cached Connector.
152      */
153     public void testObjectResolverConfig1() {
154 
155 
156         testCaching("org/settings4j/config/testConfigObjectResolver1.xml", //
157             "org/settings4j/objectresolver/test1", //
158             true); // The FSConnector is cached!
159 
160     }
161 
162     /**
163      * Test ObjectReolver with cached ObjectReolver.
164      */
165     public void testObjectResolverConfig2() {
166 
167         testCaching("org/settings4j/config/testConfigObjectResolver2.xml", //
168             "org/settings4j/objectresolver/test2", //
169             true); // The JavaXMLBeansObjectResolver is cached!
170     }
171 
172     /**
173      * Test ObjectReolver with cached Object-Property-config.
174      */
175     public void testObjectResolverConfig3() {
176 
177         testCaching("org/settings4j/config/testConfigObjectResolver-nocaching.xml", //
178             "org/settings4j/objectresolver/test2", //
179             true); // The test2.properties is cached set to true!
180     }
181 
182 
183     private void testCaching(final String settingsRepositoryUrl, final String objectKey, final boolean mustBeTheSame) {
184         final Settings4jRepository settingsRepository = TestUtils
185             .getConfiguredSettingsRepository(settingsRepositoryUrl);
186 
187         final Settings4jInstance settings = settingsRepository.getSettings();
188 
189         final Map result1 = (Map) settings.getObject(objectKey);
190         assertNotNull(result1);
191         final Map result2 = (Map) settings.getObject(objectKey);
192         if (mustBeTheSame) {
193             assertTrue(result1 == result2);
194         } else {
195             assertTrue(result1 != result2);
196         }
197     }
198 
199     /**
200      * Test ObjectReolver with cached Object-Property-config.
201      * 
202      * @throws Exception if an error occurs.
203      */
204     public void testObjectResolverConfig4Spring() throws Exception {
205 
206         final Settings4jRepository settingsRepository = TestUtils
207             .getConfiguredSettingsRepository("org/settings4j/config/testConfigObjectResolver4Spring.xml");
208 
209         final String key1 = "org/settings4j/objectresolver/testSpring1"; // spring-configuration
210 
211         // store values into the default java temporary directory with subfolder "Settings4j"
212         // String tmpdir = System.getProperty("java.io.tmpdir");
213         final Settings4jInstance settings1 = settingsRepository.getSettings();
214 
215         // the propety-file "org/settings4j/objectresolver/test1.properties must exists"
216         final DataSource dataSource = (DataSource) settings1.getObject(key1);
217         assertNotNull(dataSource);
218 
219         // test DataSource
220         Connection conn = null;
221         try {
222             conn = dataSource.getConnection();
223             PreparedStatement pstmt = conn.prepareStatement("create Table test ( name VARCHAR )");
224             pstmt.execute();
225             pstmt.close();
226 
227             pstmt = conn.prepareStatement("insert into test(name) values (?)");
228             pstmt.setString(1, "Hello World");
229             pstmt.execute();
230             pstmt.close();
231 
232             pstmt = conn.prepareStatement("select * from test");
233             final ResultSet rs = pstmt.executeQuery(); // NOPMD must have Result - An exception would break the test.
234             rs.next();
235             final String result = rs.getString(1);
236             rs.close();
237             pstmt.close();
238 
239             pstmt = conn.prepareStatement("drop Table test");
240             pstmt.execute();
241             pstmt.close();
242 
243             assertEquals("Hello World", result);
244 
245         } finally {
246             if (conn != null) {
247                 conn.close();
248             }
249         }
250     }
251 
252     /**
253      * test {@link org.settings4j.connector.PropertyFileConnector#setPropertyFromContent(byte[])}.
254      */
255     public void testPropertyFileConfig() {
256         final Settings4jRepository settingsRepository = TestUtils
257             .getConfiguredSettingsRepository("org/settings4j/config/testConfigPropertyFile.xml");
258 
259         final Settings4jInstance settings = settingsRepository.getSettings();
260 
261         // every settings have read access to the same FSConnector.
262         assertEquals("Value from Property-File", settings.getString("xyz"));
263 
264         assertEquals(2, settings.getConnectors().size());
265     }
266 
267     /**
268      * test {@link org.settings4j.connector.PropertyFileConnector#setPropertyFromPath(String)} with classpath url.
269      */
270     public void testPropertyFileConfigFromPath1() {
271         final Settings4jRepository settingsRepository = TestUtils
272             .getConfiguredSettingsRepository("org/settings4j/config/testConfigPropertyFileFromPath1.xml");
273 
274         final Settings4jInstance settings = settingsRepository.getSettings();
275 
276         // every settings have read access to the same FSConnector.
277         assertEquals("Value from Property-File", settings.getString("xyz"));
278 
279         assertEquals(1, settings.getConnectors().size());
280     }
281 
282     /**
283      * test {@link org.settings4j.connector.PropertyFileConnector#setPropertyFromPath(String)} with filepath url.
284      */
285     public void testPropertyFileConfigFromPath2() {
286         final Settings4jRepository settingsRepository = TestUtils
287             .getConfiguredSettingsRepository("org/settings4j/config/testConfigPropertyFileFromPath2.xml");
288 
289         final Settings4jInstance settings = settingsRepository.getSettings();
290 
291         // every settings have read access to the same FSConnector.
292         assertEquals("Value from Property-File", settings.getString("xyz"));
293 
294         assertEquals(1, settings.getConnectors().size());
295     }
296 
297     /**
298      * test {@link org.settings4j.connector.PropertyFileConnector#setPropertyFromPath(String)} with empty String.
299      */
300     public void testPropertyFileConfigFromPathException1() {
301         final Settings4jRepository settingsRepository = TestUtils
302             .getConfiguredSettingsRepository("org/settings4j/config/testConfigPropertyFileFromPathException1.xml");
303 
304         final Settings4jInstance settings = settingsRepository.getSettings();
305 
306         assertNull(settings.getString("xyz"));
307         assertEquals(1, settings.getConnectors().size());
308     }
309 
310     /**
311      * test {@link org.settings4j.connector.PropertyFileConnector#setPropertyFromPath(String)} without prefix.
312      */
313     public void testPropertyFileConfigFromPathException2() {
314         final Settings4jRepository settingsRepository = TestUtils
315             .getConfiguredSettingsRepository("org/settings4j/config/testConfigPropertyFileFromPathException2.xml");
316 
317         final Settings4jInstance settings = settingsRepository.getSettings();
318 
319         assertNull(settings.getString("xyz"));
320         assertEquals(1, settings.getConnectors().size());
321     }
322 }