1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
37
38
39
40 public class TestSettings4jConfig extends AbstractTestSettings4jConfig {
41
42
43
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
52 final int expectedConnectorSize = 4;
53 assertEquals(expectedConnectorSize, settings.getConnectors().size());
54
55
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
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
77
78
79
80 public void testFSConfigTempFolder() throws Exception {
81 final Settings4jRepository settingsRepository = TestUtils
82 .getConfiguredSettingsRepository("org/settings4j/config/testConfigFSTempfolder.xml");
83
84
85
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
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
113
114
115
116 public void testFSConfigTestFolder() throws Exception {
117 final Settings4jRepository settingsRepository = TestUtils
118 .getConfiguredSettingsRepository("org/settings4j/config/testConfigFSTestfolder.xml");
119
120
121
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
132 assertEquals("abc", settings.getString("xyz"));
133 assertEquals("abc2", settings.getString("xyz2"));
134
135 assertEquals(1, settings.getConnectors().size());
136 }
137
138
139
140
141 public void testObjectResolverConfig() {
142
143
144 testCaching("org/settings4j/config/testConfigObjectResolver-nocaching.xml",
145 "org/settings4j/objectresolver/test1",
146 false);
147
148 }
149
150
151
152
153 public void testObjectResolverConfig1() {
154
155
156 testCaching("org/settings4j/config/testConfigObjectResolver1.xml",
157 "org/settings4j/objectresolver/test1",
158 true);
159
160 }
161
162
163
164
165 public void testObjectResolverConfig2() {
166
167 testCaching("org/settings4j/config/testConfigObjectResolver2.xml",
168 "org/settings4j/objectresolver/test2",
169 true);
170 }
171
172
173
174
175 public void testObjectResolverConfig3() {
176
177 testCaching("org/settings4j/config/testConfigObjectResolver-nocaching.xml",
178 "org/settings4j/objectresolver/test2",
179 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
201
202
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";
210
211
212
213 final Settings4jInstance settings1 = settingsRepository.getSettings();
214
215
216 final DataSource dataSource = (DataSource) settings1.getObject(key1);
217 assertNotNull(dataSource);
218
219
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();
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
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
262 assertEquals("Value from Property-File", settings.getString("xyz"));
263
264 assertEquals(2, settings.getConnectors().size());
265 }
266
267
268
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
277 assertEquals("Value from Property-File", settings.getString("xyz"));
278
279 assertEquals(1, settings.getConnectors().size());
280 }
281
282
283
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
292 assertEquals("Value from Property-File", settings.getString("xyz"));
293
294 assertEquals(1, settings.getConnectors().size());
295 }
296
297
298
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
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 }