~ubuntu-branches/ubuntu/saucy/commons-configuration/saucy

« back to all changes in this revision

Viewing changes to src/test/org/apache/commons/configuration/TestDynamicCombinedConfiguration.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2013-07-01 16:29:44 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20130701162944-98waq5gogha5gpn1
Tags: 1.9-1
* New upstream release
* debian/control:
  - Updated Standards-Version to 3.9.4 (no changes)
  - Use canonical URLs for the Vcs-* fields
  - Added new build dependencies (libjavacc-maven-plugin-java, junit4)
  - Upgraded the dependency on the Servlet API (2.5 -> 3.0)
  - Removed the dependency on the Activation Framework (glassfish-activation)
  - Replaced the dependency on glassfish-mail with libgnumail-java
  - Removed the unused dependencies:
    liblog4j1.2-java-doc, libmaven-assembly-plugin-java
  - Replaced the dependency on libcommons-jexl-java by libcommons-jexl2-java
* debian/watch: Changed to point the official Apache distribution server
* Removed the obsolete file debian/ant.properties
* Installed the upstream changelog in the binary packages
* Added the report plugins to maven.ignoreRules
* Added the classpath attribute to the jar manifest

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 
 * contributor license agreements.  See the NOTICE file distributed with
4
 
 * this work for additional information regarding copyright ownership.
5
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 
 * (the "License"); you may not use this file except in compliance with
7
 
 * the License.  You may obtain a copy of the License at
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
11
 
 * Unless required by applicable law or agreed to in writing, software
12
 
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 
 * See the License for the specific language governing permissions and
15
 
 * limitations under the License.
16
 
 */
17
 
 
18
 
package org.apache.commons.configuration;
19
 
 
20
 
import java.io.File;
21
 
import java.io.FileReader;
22
 
import java.io.FileWriter;
23
 
import java.io.IOException;
24
 
import java.io.Reader;
25
 
import java.io.Writer;
26
 
 
27
 
import junit.framework.TestCase;
28
 
 
29
 
import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
30
 
import org.apache.commons.lang.text.StrLookup;
31
 
 
32
 
public class TestDynamicCombinedConfiguration extends TestCase
33
 
{
34
 
    private static String PATTERN = "${sys:Id}";
35
 
    private static String PATTERN1 = "target/test-classes/testMultiConfiguration_${sys:Id}.xml";
36
 
    private static String DEFAULT_FILE = "target/test-classes/testMultiConfiguration_default.xml";
37
 
    private static final File MULTI_TENENT_FILE = new File(
38
 
            "conf/testMultiTenentConfigurationBuilder4.xml");
39
 
    private static final File MULTI_DYNAMIC_FILE = new File(
40
 
            "conf/testMultiTenentConfigurationBuilder5.xml");
41
 
 
42
 
    /** Constant for the number of test threads. */
43
 
    private static final int THREAD_COUNT = 3;
44
 
 
45
 
    /** Constant for the number of loops in the multi-thread tests. */
46
 
    private static final int LOOP_COUNT = 100;
47
 
 
48
 
    public void testConfiguration() throws Exception
49
 
    {
50
 
        DynamicCombinedConfiguration config = new DynamicCombinedConfiguration();
51
 
        XPathExpressionEngine engine = new XPathExpressionEngine();
52
 
        config.setExpressionEngine(engine);
53
 
        config.setKeyPattern(PATTERN);
54
 
        config.setDelimiterParsingDisabled(true);
55
 
        MultiFileHierarchicalConfiguration multi = new MultiFileHierarchicalConfiguration(PATTERN1);
56
 
        multi.setExpressionEngine(engine);
57
 
        config.addConfiguration(multi, "Multi");
58
 
        XMLConfiguration xml = new XMLConfiguration();
59
 
        xml.setExpressionEngine(engine);
60
 
        xml.setDelimiterParsingDisabled(true);
61
 
        xml.setFile(new File(DEFAULT_FILE));
62
 
        xml.load();
63
 
        config.addConfiguration(xml, "Default");
64
 
 
65
 
        verify("1001", config, 15);
66
 
        verify("1002", config, 25);
67
 
        verify("1003", config, 35);
68
 
        verify("1004", config, 50);
69
 
        assertEquals("a,b,c", config.getString("split/list3/@values"));
70
 
        assertEquals(0, config.getMaxIndex("split/list3/@values"));
71
 
        assertEquals("a\\,b\\,c", config.getString("split/list4/@values"));
72
 
        assertEquals("a,b,c", config.getString("split/list1"));
73
 
        assertEquals(0, config.getMaxIndex("split/list1"));
74
 
        assertEquals("a\\,b\\,c", config.getString("split/list2"));
75
 
    }
76
 
 
77
 
    public void testConcurrentGetAndReload() throws Exception
78
 
    {
79
 
        System.getProperties().remove("Id");
80
 
        DefaultConfigurationBuilder factory = new DefaultConfigurationBuilder();
81
 
        factory.setFile(MULTI_TENENT_FILE);
82
 
        CombinedConfiguration config = factory.getConfiguration(true);
83
 
 
84
 
        assertEquals(config.getString("rowsPerPage"), "50");
85
 
        Thread testThreads[] = new Thread[THREAD_COUNT];
86
 
        int failures[] = new int[THREAD_COUNT];
87
 
 
88
 
        for (int i = 0; i < testThreads.length; ++i)
89
 
        {
90
 
            testThreads[i] = new ReloadThread(config, failures, i, LOOP_COUNT, false, null, "50");
91
 
            testThreads[i].start();
92
 
        }
93
 
 
94
 
        int totalFailures = 0;
95
 
        for (int i = 0; i < testThreads.length; ++i)
96
 
        {
97
 
            testThreads[i].join();
98
 
            totalFailures += failures[i];
99
 
        }
100
 
        assertTrue(totalFailures + " failures Occurred", totalFailures == 0);
101
 
    }
102
 
 
103
 
    public void testConcurrentGetAndReload2() throws Exception
104
 
    {
105
 
        System.getProperties().remove("Id");
106
 
        DefaultConfigurationBuilder factory = new DefaultConfigurationBuilder();
107
 
        factory.setFile(MULTI_TENENT_FILE);
108
 
        CombinedConfiguration config = factory.getConfiguration(true);
109
 
 
110
 
        assertEquals(config.getString("rowsPerPage"), "50");
111
 
 
112
 
        Thread testThreads[] = new Thread[THREAD_COUNT];
113
 
        int failures[] = new int[THREAD_COUNT];
114
 
        System.setProperty("Id", "2002");
115
 
        assertEquals(config.getString("rowsPerPage"), "25");
116
 
        for (int i = 0; i < testThreads.length; ++i)
117
 
        {
118
 
            testThreads[i] = new ReloadThread(config, failures, i, LOOP_COUNT, false, null, "25");
119
 
            testThreads[i].start();
120
 
        }
121
 
 
122
 
        int totalFailures = 0;
123
 
        for (int i = 0; i < testThreads.length; ++i)
124
 
        {
125
 
            testThreads[i].join();
126
 
            totalFailures += failures[i];
127
 
        }
128
 
        System.getProperties().remove("Id");
129
 
        assertTrue(totalFailures + " failures Occurred", totalFailures == 0);
130
 
    }
131
 
 
132
 
    public void testConcurrentGetAndReloadMultipleClients() throws Exception
133
 
    {
134
 
        System.getProperties().remove("Id");
135
 
        DefaultConfigurationBuilder factory = new DefaultConfigurationBuilder();
136
 
        factory.setFile(MULTI_TENENT_FILE);
137
 
        CombinedConfiguration config = factory.getConfiguration(true);
138
 
 
139
 
        assertEquals(config.getString("rowsPerPage"), "50");
140
 
 
141
 
        Thread testThreads[] = new Thread[THREAD_COUNT];
142
 
        int failures[] = new int[THREAD_COUNT];
143
 
        String[] ids = new String[] {null, "2002", "3001", "3002", "3003"};
144
 
        String[] expected = new String[] {"50", "25", "15", "25", "50"};
145
 
        for (int i = 0; i < testThreads.length; ++i)
146
 
        {
147
 
            testThreads[i] = new ReloadThread(config, failures, i, LOOP_COUNT, true, ids[i], expected[i]);
148
 
            testThreads[i].start();
149
 
        }
150
 
 
151
 
        int totalFailures = 0;
152
 
        for (int i = 0; i < testThreads.length; ++i)
153
 
        {
154
 
            testThreads[i].join();
155
 
            totalFailures += failures[i];
156
 
        }
157
 
        System.getProperties().remove("Id");
158
 
        if (totalFailures != 0)
159
 
        {
160
 
            System.out.println("Failures:");
161
 
            for (int i = 0; i < testThreads.length; ++i)
162
 
            {
163
 
                System.out.println("Thread " + i + " " + failures[i]);
164
 
            }
165
 
        }
166
 
        assertTrue(totalFailures + " failures Occurred", totalFailures == 0);
167
 
    }
168
 
 
169
 
  public void testConcurrentGetAndReloadFile() throws Exception
170
 
    {
171
 
        final int threadCount = 25;
172
 
        System.getProperties().remove("Id");
173
 
        // create a new configuration
174
 
        File input = new File("target/test-classes/testMultiDynamic_default.xml");
175
 
        File output = new File("target/test-classes/testwrite/testMultiDynamic_default.xml");
176
 
        output.delete();
177
 
        output.getParentFile().mkdir();
178
 
        copyFile(input, output);
179
 
 
180
 
        DefaultConfigurationBuilder factory = new DefaultConfigurationBuilder();
181
 
        factory.setFile(MULTI_DYNAMIC_FILE);
182
 
        CombinedConfiguration config = factory.getConfiguration(true);
183
 
 
184
 
        assertEquals(config.getString("Product/FIIndex/FI[@id='123456781']"), "ID0001");
185
 
 
186
 
        ReaderThread testThreads[] = new ReaderThread[threadCount];
187
 
        for (int i = 0; i < testThreads.length; ++i)
188
 
        {
189
 
            testThreads[i] = new ReaderThread(config);
190
 
            testThreads[i].start();
191
 
        }
192
 
 
193
 
        Thread.sleep(2000);
194
 
 
195
 
        input = new File("target/test-classes/testMultiDynamic_default2.xml");
196
 
        copyFile(input, output);
197
 
 
198
 
        Thread.sleep(2000);
199
 
        String id = config.getString("Product/FIIndex/FI[@id='123456782']");
200
 
        assertNotNull("File did not reload, id is null", id);
201
 
        String rows = config.getString("rowsPerPage");
202
 
        assertTrue("Incorrect value for rowsPerPage", "25".equals(rows));
203
 
 
204
 
        for (int i = 0; i < testThreads.length; ++i)
205
 
        {
206
 
            testThreads[i].shutdown();
207
 
            testThreads[i].join();
208
 
        }
209
 
        for (int i = 0; i < testThreads.length; ++i)
210
 
        {
211
 
            assertFalse(testThreads[i].failed());
212
 
        }
213
 
        assertEquals("ID0002", config.getString("Product/FIIndex/FI[@id='123456782']"));
214
 
        output.delete();
215
 
    }
216
 
 
217
 
 
218
 
    private class ReloadThread extends Thread
219
 
    {
220
 
        CombinedConfiguration combined;
221
 
        int[] failures;
222
 
        int index;
223
 
        int count;
224
 
        String expected;
225
 
        String id;
226
 
        boolean useId;
227
 
 
228
 
        ReloadThread(CombinedConfiguration config, int[] failures, int index, int count,
229
 
                     boolean useId, String id, String expected)
230
 
        {
231
 
            combined = config;
232
 
            this.failures = failures;
233
 
            this.index = index;
234
 
            this.count = count;
235
 
            this.expected = expected;
236
 
            this.id = id;
237
 
            this.useId = useId;
238
 
        }
239
 
        public void run()
240
 
        {
241
 
            failures[index] = 0;
242
 
 
243
 
            if (useId)
244
 
            {
245
 
                ThreadLookup.setId(id);
246
 
            }
247
 
            for (int i = 0; i < count; i++)
248
 
            {
249
 
                try
250
 
                {
251
 
                    String value = combined.getString("rowsPerPage", null);
252
 
                    if (value == null || !value.equals(expected))
253
 
                    {
254
 
                        ++failures[index];
255
 
                    }
256
 
                }
257
 
                catch (Exception ex)
258
 
                {
259
 
                    ++failures[index];
260
 
                }
261
 
            }
262
 
        }
263
 
    }
264
 
 
265
 
    private class ReaderThread extends Thread
266
 
    {
267
 
        private boolean running = true;
268
 
        private boolean failed = false;
269
 
        CombinedConfiguration combined;
270
 
 
271
 
        public ReaderThread(CombinedConfiguration c)
272
 
        {
273
 
            combined = c;
274
 
        }
275
 
 
276
 
        public void run()
277
 
        {
278
 
            while (running)
279
 
            {
280
 
                String bcId = combined.getString("Product/FIIndex/FI[@id='123456781']");
281
 
                if ("ID0001".equalsIgnoreCase(bcId))
282
 
                {
283
 
                    if (failed)
284
 
                    {
285
 
                        System.out.println("Thread failed, but recovered");
286
 
                    }
287
 
                    failed = false;
288
 
                }
289
 
                else
290
 
                {
291
 
                    failed = true;
292
 
                }
293
 
            }
294
 
        }
295
 
 
296
 
        public boolean failed()
297
 
        {
298
 
            return failed;
299
 
        }
300
 
 
301
 
        public void shutdown()
302
 
        {
303
 
            running = false;
304
 
        }
305
 
 
306
 
    }
307
 
 
308
 
    private void verify(String key, DynamicCombinedConfiguration config, int rows)
309
 
    {
310
 
        System.setProperty("Id", key);
311
 
        assertTrue(config.getInt("rowsPerPage") == rows);
312
 
    }
313
 
 
314
 
    private void copyFile(File input, File output) throws IOException
315
 
    {
316
 
        Reader reader = new FileReader(input);
317
 
        Writer writer = new FileWriter(output);
318
 
        char[] buffer = new char[4096];
319
 
        int n = 0;
320
 
        while (-1 != (n = reader.read(buffer)))
321
 
        {
322
 
            writer.write(buffer, 0, n);
323
 
        }
324
 
        reader.close();
325
 
        writer.close();
326
 
    }
327
 
 
328
 
    public static class ThreadLookup extends StrLookup
329
 
    {
330
 
        private static ThreadLocal id = new ThreadLocal();
331
 
 
332
 
 
333
 
 
334
 
        public ThreadLookup()
335
 
        {
336
 
 
337
 
        }
338
 
 
339
 
        public static void setId(String value)
340
 
        {
341
 
            id.set(value);
342
 
        }
343
 
 
344
 
        public String lookup(String key)
345
 
        {
346
 
            if (key == null || !key.equals("Id"))
347
 
            {
348
 
                return null;
349
 
            }
350
 
            String value = System.getProperty("Id");
351
 
            if (value != null)
352
 
            {
353
 
                return value;
354
 
            }
355
 
            return (String)id.get();
356
 
 
357
 
        }
358
 
    }
359
 
}