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

« back to all changes in this revision

Viewing changes to src/test/org/apache/commons/configuration/TestHierarchicalINIConfiguration.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 this
4
 
 * work for additional information regarding copyright ownership. The ASF
5
 
 * licenses this file to You under the Apache License, Version 2.0 (the
6
 
 * "License"); you may not use this file except in compliance with the License.
7
 
 * 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, WITHOUT
13
 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
 
 * License for the specific language governing permissions and limitations under
15
 
 * the License.
16
 
 */
17
 
 
18
 
package org.apache.commons.configuration;
19
 
 
20
 
import java.io.File;
21
 
import java.io.FileWriter;
22
 
import java.io.IOException;
23
 
import java.io.PrintWriter;
24
 
import java.io.StringReader;
25
 
import java.io.StringWriter;
26
 
import java.io.Writer;
27
 
import java.util.HashSet;
28
 
import java.util.Iterator;
29
 
import java.util.Set;
30
 
 
31
 
import junit.framework.TestCase;
32
 
 
33
 
/**
34
 
 * Test class for HierarchicalINIConfiguration.
35
 
 *
36
 
 * @author <a
37
 
 *         href="http://commons.apache.org/configuration/team-list.html">Commons
38
 
 *         Configuration team</a>
39
 
 * @version $Id: TestHierarchicalINIConfiguration.java 1153637 2011-08-03 20:11:23Z oheger $
40
 
 */
41
 
public class TestHierarchicalINIConfiguration extends TestCase
42
 
{
43
 
    private static String LINE_SEPARATOR = System.getProperty("line.separator");
44
 
 
45
 
    /** Constant for the content of an ini file. */
46
 
    private static final String INI_DATA = "[section1]" + LINE_SEPARATOR
47
 
            + "var1 = foo" + LINE_SEPARATOR + "var2 = 451" + LINE_SEPARATOR
48
 
            + LINE_SEPARATOR + "[section2]" + LINE_SEPARATOR + "var1 = 123.45"
49
 
            + LINE_SEPARATOR + "var2 = bar" + LINE_SEPARATOR + LINE_SEPARATOR
50
 
            + "[section3]" + LINE_SEPARATOR + "var1 = true" + LINE_SEPARATOR
51
 
            + "interpolated = ${section3.var1}" + LINE_SEPARATOR
52
 
            + "multi = foo" + LINE_SEPARATOR + "multi = bar" + LINE_SEPARATOR
53
 
            + LINE_SEPARATOR;
54
 
 
55
 
    private static final String INI_DATA2 = "[section4]" + LINE_SEPARATOR
56
 
            + "var1 = \"quoted value\"" + LINE_SEPARATOR
57
 
            + "var2 = \"quoted value\\nwith \\\"quotes\\\"\"" + LINE_SEPARATOR
58
 
            + "var3 = 123 ; comment" + LINE_SEPARATOR
59
 
            + "var4 = \"1;2;3\" ; comment" + LINE_SEPARATOR
60
 
            + "var5 = '\\'quoted\\' \"value\"' ; comment" + LINE_SEPARATOR
61
 
            + "var6 = \"\"" + LINE_SEPARATOR;
62
 
 
63
 
    private static final String INI_DATA3 = "[section5]" + LINE_SEPARATOR
64
 
            + "multiLine = one \\" + LINE_SEPARATOR
65
 
            + "    two      \\" + LINE_SEPARATOR
66
 
            + " three" + LINE_SEPARATOR
67
 
            + "singleLine = C:\\Temp\\" + LINE_SEPARATOR
68
 
            + "multiQuoted = one \\" + LINE_SEPARATOR
69
 
            + "\"  two  \" \\" + LINE_SEPARATOR
70
 
            + "  three" + LINE_SEPARATOR
71
 
            + "multiComment = one \\ ; a comment" + LINE_SEPARATOR
72
 
            + "two" + LINE_SEPARATOR
73
 
            + "multiQuotedComment = \" one \" \\ ; comment" + LINE_SEPARATOR
74
 
            + "two" + LINE_SEPARATOR
75
 
            + "noFirstLine = \\" + LINE_SEPARATOR
76
 
            + "  line 2" + LINE_SEPARATOR
77
 
            + "continueNoLine = one \\" + LINE_SEPARATOR;
78
 
 
79
 
    private static final String INI_DATA_SEPARATORS = "[section]"
80
 
            + LINE_SEPARATOR + "var1 = value1" + LINE_SEPARATOR
81
 
            + "var2 : value2" + LINE_SEPARATOR
82
 
            + "var3=value3" + LINE_SEPARATOR
83
 
            + "var4:value4" + LINE_SEPARATOR
84
 
            + "var5 : value=5" + LINE_SEPARATOR
85
 
            + "var:6=value" + LINE_SEPARATOR
86
 
            + "var:7=\"value7\"" + LINE_SEPARATOR
87
 
            + "var:8 =  \"value8\"" + LINE_SEPARATOR;
88
 
 
89
 
    /** An ini file that contains only a property in the global section. */
90
 
    private static final String INI_DATA_GLOBAL_ONLY = "globalVar = testGlobal"
91
 
            + LINE_SEPARATOR + LINE_SEPARATOR;
92
 
 
93
 
    /** An ini file with a global section. */
94
 
    private static final String INI_DATA_GLOBAL = INI_DATA_GLOBAL_ONLY
95
 
            + INI_DATA;
96
 
 
97
 
    /** A test ini file. */
98
 
    private static final File TEST_FILE = new File("target/test.ini");
99
 
 
100
 
    protected void tearDown() throws Exception
101
 
    {
102
 
        if (TEST_FILE.exists())
103
 
        {
104
 
            assertTrue("Cannot remove test file: " + TEST_FILE, TEST_FILE
105
 
                    .delete());
106
 
        }
107
 
 
108
 
        super.tearDown();
109
 
    }
110
 
 
111
 
    /**
112
 
     * Creates a HierarchicalINIConfiguration object that is initialized from
113
 
     * the given data.
114
 
     *
115
 
     * @param data the data of the configuration (an ini file as string)
116
 
     * @return the initialized configuration
117
 
     * @throws ConfigurationException if an error occurs
118
 
     */
119
 
    private static HierarchicalINIConfiguration setUpConfig(String data)
120
 
            throws ConfigurationException
121
 
    {
122
 
        StringReader reader = new StringReader(data);
123
 
        HierarchicalINIConfiguration instance = new HierarchicalINIConfiguration();
124
 
        instance.load(reader);
125
 
        reader.close();
126
 
        return instance;
127
 
    }
128
 
 
129
 
    /**
130
 
     * Writes a test ini file.
131
 
     *
132
 
     * @param content the content of the file
133
 
     * @throws IOException if an error occurs
134
 
     */
135
 
    private static void writeTestFile(String content) throws IOException
136
 
    {
137
 
        PrintWriter out = new PrintWriter(new FileWriter(TEST_FILE));
138
 
        try
139
 
        {
140
 
            out.println(content);
141
 
        }
142
 
        finally
143
 
        {
144
 
            out.close();
145
 
        }
146
 
    }
147
 
 
148
 
    /**
149
 
     * Test of save method, of class {@link HierarchicalINIConfiguration}.
150
 
     */
151
 
    public void testSave() throws Exception
152
 
    {
153
 
        Writer writer = new StringWriter();
154
 
        HierarchicalINIConfiguration instance = new HierarchicalINIConfiguration();
155
 
        instance.addProperty("section1.var1", "foo");
156
 
        instance.addProperty("section1.var2", "451");
157
 
        instance.addProperty("section2.var1", "123.45");
158
 
        instance.addProperty("section2.var2", "bar");
159
 
        instance.addProperty("section3.var1", "true");
160
 
        instance.addProperty("section3.interpolated", "${section3.var1}");
161
 
        instance.addProperty("section3.multi", "foo");
162
 
        instance.addProperty("section3.multi", "bar");
163
 
        instance.save(writer);
164
 
 
165
 
        assertEquals("Wrong content of ini file", INI_DATA, writer.toString());
166
 
    }
167
 
 
168
 
    /**
169
 
     * Helper method for testing a save operation. This method constructs a
170
 
     * configuration from the specified content string. Then it saves this
171
 
     * configuration and checks whether the result matches the original content.
172
 
     *
173
 
     * @param content the content of the configuration
174
 
     * @throws ConfigurationException if an error occurs
175
 
     */
176
 
    private void checkSave(String content) throws ConfigurationException
177
 
    {
178
 
        HierarchicalINIConfiguration config = setUpConfig(content);
179
 
        StringWriter writer = new StringWriter();
180
 
        config.save(writer);
181
 
        assertEquals("Wrong content of ini file", content, writer.toString());
182
 
    }
183
 
 
184
 
    /**
185
 
     * Tests saving a configuration that contains a global section.
186
 
     */
187
 
    public void testSaveWithGlobalSection() throws ConfigurationException
188
 
    {
189
 
        checkSave(INI_DATA_GLOBAL);
190
 
    }
191
 
 
192
 
    /**
193
 
     * Tests whether a configuration that contains only a global section can be
194
 
     * saved correctly.
195
 
     */
196
 
    public void testSaveWithOnlyGlobalSection() throws ConfigurationException
197
 
    {
198
 
        checkSave(INI_DATA_GLOBAL_ONLY);
199
 
    }
200
 
 
201
 
    /**
202
 
     * Test of load method, of class {@link HierarchicalINIConfiguration}.
203
 
     */
204
 
    public void testLoad() throws Exception
205
 
    {
206
 
        checkLoad(INI_DATA);
207
 
    }
208
 
 
209
 
    /**
210
 
     * Tests the load() method when the alternative value separator is used (a
211
 
     * ':' for '=').
212
 
     */
213
 
    public void testLoadAlternativeSeparator() throws Exception
214
 
    {
215
 
        checkLoad(INI_DATA.replace('=', ':'));
216
 
    }
217
 
 
218
 
    /**
219
 
     * Tests loading a configuration from a File.
220
 
     */
221
 
    public void testLoadFile() throws ConfigurationException, IOException
222
 
    {
223
 
        writeTestFile(INI_DATA);
224
 
        HierarchicalINIConfiguration config = new HierarchicalINIConfiguration(
225
 
                TEST_FILE);
226
 
        checkContent(config);
227
 
    }
228
 
 
229
 
    /**
230
 
     * Tests loading a configuration from a file name.
231
 
     */
232
 
    public void testLoadFileName() throws ConfigurationException, IOException
233
 
    {
234
 
        writeTestFile(INI_DATA);
235
 
        HierarchicalINIConfiguration config = new HierarchicalINIConfiguration(
236
 
                TEST_FILE.getAbsolutePath());
237
 
        checkContent(config);
238
 
    }
239
 
 
240
 
    /**
241
 
     * Tests loading a configuration from a URL.
242
 
     */
243
 
    public void testLoadURL() throws ConfigurationException, IOException
244
 
    {
245
 
        writeTestFile(INI_DATA);
246
 
        HierarchicalINIConfiguration config = new HierarchicalINIConfiguration(
247
 
                TEST_FILE.toURI().toURL());
248
 
        checkContent(config);
249
 
    }
250
 
 
251
 
    /**
252
 
     * Tests the values of some properties to ensure that the configuration was
253
 
     * correctly loaded.
254
 
     *
255
 
     * @param instance the configuration to check
256
 
     */
257
 
    private void checkContent(HierarchicalINIConfiguration instance)
258
 
    {
259
 
        assertTrue(instance.getString("section1.var1").equals("foo"));
260
 
        assertTrue(instance.getInt("section1.var2") == 451);
261
 
        assertTrue(instance.getDouble("section2.var1") == 123.45);
262
 
        assertTrue(instance.getString("section2.var2").equals("bar"));
263
 
        assertTrue(instance.getBoolean("section3.var1"));
264
 
        assertTrue(instance.getSections().size() == 3);
265
 
    }
266
 
 
267
 
    /**
268
 
     * Helper method for testing the load operation. Loads the specified content
269
 
     * into a configuration and then checks some properties.
270
 
     *
271
 
     * @param data the data to load
272
 
     */
273
 
    private void checkLoad(String data) throws ConfigurationException
274
 
    {
275
 
        HierarchicalINIConfiguration instance = setUpConfig(data);
276
 
        checkContent(instance);
277
 
    }
278
 
 
279
 
    /**
280
 
     * Test of isCommentLine method, of class
281
 
     * {@link HierarchicalINIConfiguration}.
282
 
     */
283
 
    public void testIsCommentLine()
284
 
    {
285
 
        HierarchicalINIConfiguration instance = new HierarchicalINIConfiguration();
286
 
        assertTrue(instance.isCommentLine("#comment1"));
287
 
        assertTrue(instance.isCommentLine(";comment1"));
288
 
        assertFalse(instance.isCommentLine("nocomment=true"));
289
 
        assertFalse(instance.isCommentLine(null));
290
 
    }
291
 
 
292
 
    /**
293
 
     * Test of isSectionLine method, of class
294
 
     * {@link HierarchicalINIConfiguration}.
295
 
     */
296
 
    public void testIsSectionLine()
297
 
    {
298
 
        HierarchicalINIConfiguration instance = new HierarchicalINIConfiguration();
299
 
        assertTrue(instance.isSectionLine("[section]"));
300
 
        assertFalse(instance.isSectionLine("nosection=true"));
301
 
        assertFalse(instance.isSectionLine(null));
302
 
    }
303
 
 
304
 
    /**
305
 
     * Test of getSections method, of class {@link HierarchicalINIConfiguration}
306
 
     * .
307
 
     */
308
 
    public void testGetSections()
309
 
    {
310
 
        HierarchicalINIConfiguration instance = new HierarchicalINIConfiguration();
311
 
        instance.addProperty("test1.foo", "bar");
312
 
        instance.addProperty("test2.foo", "abc");
313
 
        Set expResult = new HashSet();
314
 
        expResult.add("test1");
315
 
        expResult.add("test2");
316
 
        Set result = instance.getSections();
317
 
        assertEquals(expResult, result);
318
 
    }
319
 
 
320
 
    public void testQuotedValue() throws Exception
321
 
    {
322
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA2);
323
 
        assertEquals("value", "quoted value", config.getString("section4.var1"));
324
 
    }
325
 
 
326
 
    public void testQuotedValueWithQuotes() throws Exception
327
 
    {
328
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA2);
329
 
        assertEquals("value", "quoted value\\nwith \"quotes\"", config
330
 
                .getString("section4.var2"));
331
 
    }
332
 
 
333
 
    public void testValueWithComment() throws Exception
334
 
    {
335
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA2);
336
 
        assertEquals("value", "123", config.getString("section4.var3"));
337
 
    }
338
 
 
339
 
    public void testQuotedValueWithComment() throws Exception
340
 
    {
341
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA2);
342
 
        assertEquals("value", "1;2;3", config.getString("section4.var4"));
343
 
    }
344
 
 
345
 
    public void testQuotedValueWithSingleQuotes() throws Exception
346
 
    {
347
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA2);
348
 
        assertEquals("value", "'quoted' \"value\"", config
349
 
                .getString("section4.var5"));
350
 
    }
351
 
 
352
 
    public void testWriteValueWithCommentChar() throws Exception
353
 
    {
354
 
        HierarchicalINIConfiguration config = new HierarchicalINIConfiguration();
355
 
        config.setProperty("section.key1", "1;2;3");
356
 
 
357
 
        StringWriter writer = new StringWriter();
358
 
        config.save(writer);
359
 
 
360
 
        HierarchicalINIConfiguration config2 = new HierarchicalINIConfiguration();
361
 
        config2.load(new StringReader(writer.toString()));
362
 
 
363
 
        assertEquals("value", "1;2;3", config2.getString("section.key1"));
364
 
    }
365
 
 
366
 
    /**
367
 
     * Tests whether whitespace is left unchanged for quoted values.
368
 
     */
369
 
    public void testQuotedValueWithWhitespace() throws Exception
370
 
    {
371
 
        final String content = "CmdPrompt = \" [test@cmd ~]$ \"";
372
 
        HierarchicalINIConfiguration config = setUpConfig(content);
373
 
        assertEquals("Wrong propert value", " [test@cmd ~]$ ", config
374
 
                .getString("CmdPrompt"));
375
 
    }
376
 
 
377
 
    /**
378
 
     * Tests a quoted value with space and a comment.
379
 
     */
380
 
    public void testQuotedValueWithWhitespaceAndComment() throws Exception
381
 
    {
382
 
        final String content = "CmdPrompt = \" [test@cmd ~]$ \" ; a comment";
383
 
        HierarchicalINIConfiguration config = setUpConfig(content);
384
 
        assertEquals("Wrong propert value", " [test@cmd ~]$ ", config
385
 
                .getString("CmdPrompt"));
386
 
    }
387
 
 
388
 
    /**
389
 
     * Tests an empty quoted value.
390
 
     */
391
 
    public void testQuotedValueEmpty() throws ConfigurationException
392
 
    {
393
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA2);
394
 
        assertEquals("Wrong value for empty property", "", config
395
 
                .getString("section4.var6"));
396
 
    }
397
 
 
398
 
    /**
399
 
     * Tests a property that has no value.
400
 
     */
401
 
    public void testGetPropertyNoValue() throws ConfigurationException
402
 
    {
403
 
        final String data = INI_DATA2 + LINE_SEPARATOR + "noValue ="
404
 
                + LINE_SEPARATOR;
405
 
        HierarchicalINIConfiguration config = setUpConfig(data);
406
 
        assertEquals("Wrong value of key", "", config
407
 
                .getString("section4.noValue"));
408
 
    }
409
 
 
410
 
    /**
411
 
     * Tests a property that has no key.
412
 
     */
413
 
    public void testGetPropertyNoKey() throws ConfigurationException
414
 
    {
415
 
        final String data = INI_DATA2 + LINE_SEPARATOR + "= noKey"
416
 
                + LINE_SEPARATOR;
417
 
        HierarchicalINIConfiguration config = setUpConfig(data);
418
 
        assertEquals("Cannot find property with no key", "noKey", config
419
 
                .getString("section4. "));
420
 
    }
421
 
 
422
 
    /**
423
 
     * Tests reading a property from the global section.
424
 
     */
425
 
    public void testGlobalProperty() throws ConfigurationException
426
 
    {
427
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA_GLOBAL);
428
 
        assertEquals("Wrong value of global property", "testGlobal", config
429
 
                .getString("globalVar"));
430
 
    }
431
 
 
432
 
    /**
433
 
     * Tests whether the specified configuration contains exactly the expected
434
 
     * sections.
435
 
     *
436
 
     * @param config the configuration to check
437
 
     * @param expected an array with the expected sections
438
 
     */
439
 
    private void checkSectionNames(HierarchicalINIConfiguration config,
440
 
            String[] expected)
441
 
    {
442
 
        Set sectionNames = config.getSections();
443
 
        Iterator it = sectionNames.iterator();
444
 
        for (int idx = 0; idx < expected.length; idx++)
445
 
        {
446
 
            assertEquals("Wrong section at " + idx, expected[idx], it.next());
447
 
        }
448
 
        assertFalse("Too many sections", it.hasNext());
449
 
    }
450
 
 
451
 
    /**
452
 
     * Tests the names of the sections returned by the configuration.
453
 
     *
454
 
     * @param data the data of the ini configuration
455
 
     * @param expected the expected section names
456
 
     * @return the configuration instance
457
 
     */
458
 
    private HierarchicalINIConfiguration checkSectionNames(String data,
459
 
            String[] expected) throws ConfigurationException
460
 
    {
461
 
        HierarchicalINIConfiguration config = setUpConfig(data);
462
 
        checkSectionNames(config, expected);
463
 
        return config;
464
 
    }
465
 
 
466
 
    /**
467
 
     * Tests querying the sections if a global section if available.
468
 
     */
469
 
    public void testGetSectionsWithGlobal() throws ConfigurationException
470
 
    {
471
 
        checkSectionNames(INI_DATA_GLOBAL, new String[] {
472
 
                null, "section1", "section2", "section3"
473
 
        });
474
 
    }
475
 
 
476
 
    /**
477
 
     * Tests querying the sections if there is no global section.
478
 
     */
479
 
    public void testGetSectionsNoGlobal() throws ConfigurationException
480
 
    {
481
 
        checkSectionNames(INI_DATA, new String[] {
482
 
                "section1", "section2", "section3"
483
 
        });
484
 
    }
485
 
 
486
 
    /**
487
 
     * Tests whether the sections of a configuration can be queried that
488
 
     * contains only a global section.
489
 
     */
490
 
    public void testGetSectionsGlobalOnly() throws ConfigurationException
491
 
    {
492
 
        checkSectionNames(INI_DATA_GLOBAL_ONLY, new String[] {
493
 
            null
494
 
        });
495
 
    }
496
 
 
497
 
    /**
498
 
     * Tests whether variables containing a dot are not misinterpreted as
499
 
     * sections. This test is related to CONFIGURATION-327.
500
 
     */
501
 
    public void testGetSectionsDottedVar() throws ConfigurationException
502
 
    {
503
 
        final String data = "dotted.var = 1" + LINE_SEPARATOR + INI_DATA_GLOBAL;
504
 
        HierarchicalINIConfiguration config = checkSectionNames(data,
505
 
                new String[] {
506
 
                        null, "section1", "section2", "section3"
507
 
                });
508
 
        assertEquals("Wrong value of dotted variable", 1, config
509
 
                .getInt("dotted..var"));
510
 
    }
511
 
 
512
 
    /**
513
 
     * Tests whether a section added later is also found by getSections().
514
 
     */
515
 
    public void testGetSectionsAdded() throws ConfigurationException
516
 
    {
517
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA2);
518
 
        config.addProperty("section5.test", Boolean.TRUE);
519
 
        checkSectionNames(config, new String[] {
520
 
                "section4", "section5"
521
 
        });
522
 
    }
523
 
 
524
 
    /**
525
 
     * Tests querying the properties of an existing section.
526
 
     */
527
 
    public void testGetSectionExisting() throws ConfigurationException
528
 
    {
529
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA);
530
 
        SubnodeConfiguration section = config.getSection("section1");
531
 
        assertEquals("Wrong value of var1", "foo", section.getString("var1"));
532
 
        assertEquals("Wrong value of var2", "451", section.getString("var2"));
533
 
    }
534
 
 
535
 
    /**
536
 
     * Tests querying the properties of a section that was merged from two
537
 
     * sections with the same name.
538
 
     */
539
 
    public void testGetSectionMerged() throws ConfigurationException
540
 
    {
541
 
        final String data = INI_DATA + "[section1]" + LINE_SEPARATOR
542
 
                + "var3 = merged" + LINE_SEPARATOR;
543
 
        HierarchicalINIConfiguration config = setUpConfig(data);
544
 
        SubnodeConfiguration section = config.getSection("section1");
545
 
        assertEquals("Wrong value of var1", "foo", section.getString("var1"));
546
 
        assertEquals("Wrong value of var2", "451", section.getString("var2"));
547
 
        assertEquals("Wrong value of var3", "merged", section.getString("var3"));
548
 
    }
549
 
 
550
 
    /**
551
 
     * Tests querying the content of the global section.
552
 
     */
553
 
    public void testGetSectionGlobal() throws ConfigurationException
554
 
    {
555
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA_GLOBAL);
556
 
        SubnodeConfiguration section = config.getSection(null);
557
 
        assertEquals("Wrong value of global variable", "testGlobal", section
558
 
                .getString("globalVar"));
559
 
    }
560
 
 
561
 
    /**
562
 
     * Tests concurrent access to the global section.
563
 
     */
564
 
    public void testGetSectionGloabalMultiThreaded()
565
 
            throws ConfigurationException, InterruptedException
566
 
    {
567
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA_GLOBAL);
568
 
        final int threadCount = 10;
569
 
        GlobalSectionTestThread[] threads = new GlobalSectionTestThread[threadCount];
570
 
        for (int i = 0; i < threadCount; i++)
571
 
        {
572
 
            threads[i] = new GlobalSectionTestThread(config);
573
 
            threads[i].start();
574
 
        }
575
 
        for (int i = 0; i < threadCount; i++)
576
 
        {
577
 
            threads[i].join();
578
 
            assertFalse("Exception occurred", threads[i].error);
579
 
        }
580
 
    }
581
 
 
582
 
    /**
583
 
     * Tests querying the content of the global section if there is none.
584
 
     */
585
 
    public void testGetSectionGlobalNonExisting() throws ConfigurationException
586
 
    {
587
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA);
588
 
        SubnodeConfiguration section = config.getSection(null);
589
 
        assertTrue("Sub config not empty", section.isEmpty());
590
 
    }
591
 
 
592
 
    /**
593
 
     * Tests querying a non existing section.
594
 
     */
595
 
    public void testGetSectionNonExisting() throws ConfigurationException
596
 
    {
597
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA);
598
 
        SubnodeConfiguration section = config
599
 
                .getSection("Non existing section");
600
 
        assertTrue("Sub config not empty", section.isEmpty());
601
 
    }
602
 
 
603
 
    /**
604
 
     * Tests a property whose value spans multiple lines.
605
 
     */
606
 
    public void testLineContinuation() throws ConfigurationException
607
 
    {
608
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA3);
609
 
        assertEquals("Wrong value", "one" + LINE_SEPARATOR + "two"
610
 
                + LINE_SEPARATOR + "three", config
611
 
                .getString("section5.multiLine"));
612
 
    }
613
 
 
614
 
    /**
615
 
     * Tests a property value that ends on a backslash, which is no line
616
 
     * continuation character.
617
 
     */
618
 
    public void testLineContinuationNone() throws ConfigurationException
619
 
    {
620
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA3);
621
 
        assertEquals("Wrong value", "C:\\Temp\\", config
622
 
                .getString("section5.singleLine"));
623
 
    }
624
 
 
625
 
    /**
626
 
     * Tests a property whose value spans multiple lines when quoting is
627
 
     * involved. In this case whitespace must not be trimmed.
628
 
     */
629
 
    public void testLineContinuationQuoted() throws ConfigurationException
630
 
    {
631
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA3);
632
 
        assertEquals("Wrong value", "one" + LINE_SEPARATOR + "  two  "
633
 
                + LINE_SEPARATOR + "three", config
634
 
                .getString("section5.multiQuoted"));
635
 
    }
636
 
 
637
 
    /**
638
 
     * Tests a property whose value spans multiple lines with a comment.
639
 
     */
640
 
    public void testLineContinuationComment() throws ConfigurationException
641
 
    {
642
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA3);
643
 
        assertEquals("Wrong value", "one" + LINE_SEPARATOR + "two", config
644
 
                .getString("section5.multiComment"));
645
 
    }
646
 
 
647
 
    /**
648
 
     * Tests a property with a quoted value spanning multiple lines and a
649
 
     * comment.
650
 
     */
651
 
    public void testLineContinuationQuotedComment()
652
 
            throws ConfigurationException
653
 
    {
654
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA3);
655
 
        assertEquals("Wrong value", " one " + LINE_SEPARATOR + "two", config
656
 
                .getString("section5.multiQuotedComment"));
657
 
    }
658
 
 
659
 
    /**
660
 
     * Tests a multi-line property value with an empty line.
661
 
     */
662
 
    public void testLineContinuationEmptyLine() throws ConfigurationException
663
 
    {
664
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA3);
665
 
        assertEquals("Wrong value", LINE_SEPARATOR + "line 2", config
666
 
                .getString("section5.noFirstLine"));
667
 
    }
668
 
 
669
 
    /**
670
 
     * Tests a line continuation at the end of the file.
671
 
     */
672
 
    public void testLineContinuationAtEnd() throws ConfigurationException
673
 
    {
674
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA3);
675
 
        assertEquals("Wrong value", "one" + LINE_SEPARATOR, config
676
 
                .getString("section5.continueNoLine"));
677
 
    }
678
 
 
679
 
    /**
680
 
     * Tests whether a configuration can be saved that contains section keys
681
 
     * with delimiter characters. This test is related to CONFIGURATION-409.
682
 
     */
683
 
    public void testSaveKeysWithDelimiters() throws ConfigurationException
684
 
    {
685
 
        HierarchicalINIConfiguration conf = new HierarchicalINIConfiguration();
686
 
        final String section = "Section..with..dots";
687
 
        conf.addProperty(section + ".test1", "test1");
688
 
        conf.addProperty(section + ".test2", "test2");
689
 
        conf.save(TEST_FILE);
690
 
        conf = new HierarchicalINIConfiguration();
691
 
        conf.load(TEST_FILE);
692
 
        assertEquals("Wrong value (1)", "test1", conf.getString(section + ".test1"));
693
 
        assertEquals("Wrong value (2)", "test2", conf.getString(section + ".test2"));
694
 
    }
695
 
 
696
 
    /**
697
 
     * Tests whether a value which contains a semicolon can be loaded
698
 
     * successfully. This test is related to CONFIGURATION-434.
699
 
     */
700
 
    public void testValueWithSemicolon() throws ConfigurationException
701
 
    {
702
 
        final String path =
703
 
                "C:\\Program Files\\jar\\manage.jar;"
704
 
                        + "C:\\Program Files\\jar\\guiLauncher.jar";
705
 
        final String content =
706
 
                "[Environment]" + LINE_SEPARATOR + "Application Type=any"
707
 
                        + LINE_SEPARATOR + "Class Path=" + path + "  ;comment"
708
 
                        + LINE_SEPARATOR + "Path=" + path
709
 
                        + "\t; another comment";
710
 
        HierarchicalINIConfiguration config = setUpConfig(content);
711
 
        assertEquals("Wrong class path", path,
712
 
                config.getString("Environment.Class Path"));
713
 
        assertEquals("Wrong path", path, config.getString("Environment.Path"));
714
 
    }
715
 
 
716
 
    /**
717
 
     * Tests whether the different separators with or without whitespace are
718
 
     * recognized.
719
 
     */
720
 
    public void testSeparators() throws ConfigurationException
721
 
    {
722
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA_SEPARATORS);
723
 
        for (int i = 1; i <= 4; i++)
724
 
        {
725
 
            assertEquals("Wrong value", "value" + i,
726
 
                    config.getString("section.var" + i));
727
 
        }
728
 
    }
729
 
 
730
 
    /**
731
 
     * Tests property definitions containing multiple separators.
732
 
     */
733
 
    public void testMultipleSeparators() throws ConfigurationException
734
 
    {
735
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA_SEPARATORS);
736
 
        assertEquals("Wrong value for var5", "value=5",
737
 
                config.getString("section.var5"));
738
 
        assertEquals("Wrong value for var6", "6=value",
739
 
                config.getString("section.var"));
740
 
    }
741
 
 
742
 
    /**
743
 
     * Tests property definitions containing multiple separators that are
744
 
     * quoted.
745
 
     */
746
 
    public void testMultipleSeparatorsQuoted() throws ConfigurationException
747
 
    {
748
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA_SEPARATORS);
749
 
        assertEquals("Wrong value for var7", "value7",
750
 
                config.getString("section.var:7"));
751
 
        assertEquals("Wrong value for var8", "value8",
752
 
                config.getString("section.var:8"));
753
 
    }
754
 
 
755
 
    /**
756
 
     * Tests whether a section that has been cleared can be manipulated and
757
 
     * saved later.
758
 
     */
759
 
    public void testSaveClearedSection() throws ConfigurationException
760
 
    {
761
 
        final String data = "[section]\ntest = failed\n";
762
 
        HierarchicalINIConfiguration config = setUpConfig(data);
763
 
        SubnodeConfiguration sub = config.getSection("section");
764
 
        assertFalse("No content", sub.isEmpty());
765
 
        sub.clear();
766
 
        sub.setProperty("test", "success");
767
 
        StringWriter writer = new StringWriter();
768
 
        config.save(writer);
769
 
        HierarchicalConfiguration config2 = setUpConfig(writer.toString());
770
 
        assertEquals("Wrong value", "success",
771
 
                config2.getString("section.test"));
772
 
    }
773
 
 
774
 
    /**
775
 
     * Tests whether a duplicate session is merged.
776
 
     */
777
 
    public void testMergeDuplicateSection() throws ConfigurationException
778
 
    {
779
 
        final String data =
780
 
                "[section]\nvar1 = sec1\n\n" + "[section]\nvar2 = sec2\n";
781
 
        HierarchicalINIConfiguration config = setUpConfig(data);
782
 
        assertEquals("Wrong value 1", "sec1", config.getString("section.var1"));
783
 
        assertEquals("Wrong value 2", "sec2", config.getString("section.var2"));
784
 
        SubnodeConfiguration sub = config.getSection("section");
785
 
        assertEquals("Wrong sub value 1", "sec1", sub.getString("var1"));
786
 
        assertEquals("Wrong sub value 2", "sec2", sub.getString("var2"));
787
 
        StringWriter writer = new StringWriter();
788
 
        config.save(writer);
789
 
        String content = writer.toString();
790
 
        int pos = content.indexOf("[section]");
791
 
        assertTrue("Section not found: " + content, pos >= 0);
792
 
        assertTrue("Section found multiple times: " + content,
793
 
                content.indexOf("[section]", pos + 1) < 0);
794
 
    }
795
 
 
796
 
    /**
797
 
     * Tests whether a section that was created by getSection() can be
798
 
     * manipulated.
799
 
     */
800
 
    public void testGetSectionNonExistingManipulate()
801
 
            throws ConfigurationException
802
 
    {
803
 
        HierarchicalINIConfiguration config = setUpConfig(INI_DATA);
804
 
        SubnodeConfiguration section = config.getSection("newSection");
805
 
        section.addProperty("test", "success");
806
 
        assertEquals("Main config not updated", "success",
807
 
                config.getString("newSection.test"));
808
 
        StringWriter writer = new StringWriter();
809
 
        config.save(writer);
810
 
        HierarchicalINIConfiguration config2 = setUpConfig(writer.toString());
811
 
        section = config2.getSection("newSection");
812
 
        assertEquals("Wrong value", "success", section.getString("test"));
813
 
    }
814
 
 
815
 
    /**
816
 
     * Tests whether getSection() can deal with duplicate sections.
817
 
     */
818
 
    public void testGetSectionDuplicate()
819
 
    {
820
 
        HierarchicalINIConfiguration config =
821
 
                new HierarchicalINIConfiguration();
822
 
        config.addProperty("section.var1", "value1");
823
 
        config.addProperty("section(-1).var2", "value2");
824
 
        SubnodeConfiguration section = config.getSection("section");
825
 
        Iterator keys = section.getKeys();
826
 
        assertEquals("Wrong key", "var1", keys.next());
827
 
        assertFalse("Too many keys", keys.hasNext());
828
 
    }
829
 
 
830
 
    /**
831
 
     * A thread class for testing concurrent access to the global section.
832
 
     */
833
 
    private static class GlobalSectionTestThread extends Thread
834
 
    {
835
 
        /** The configuration. */
836
 
        private final HierarchicalINIConfiguration config;
837
 
 
838
 
        /** A flag whether an error was found. */
839
 
        volatile boolean error;
840
 
 
841
 
        /**
842
 
         * Creates a new instance of <code>GlobalSectionTestThread</code> and
843
 
         * initializes it.
844
 
         *
845
 
         * @param conf the configuration object
846
 
         */
847
 
        public GlobalSectionTestThread(HierarchicalINIConfiguration conf)
848
 
        {
849
 
            config = conf;
850
 
        }
851
 
 
852
 
        /**
853
 
         * Accesses the global section in a loop. If there is no correct
854
 
         * synchronization, this can cause an exception.
855
 
         */
856
 
        public void run()
857
 
        {
858
 
            final int loopCount = 250;
859
 
 
860
 
            for (int i = 0; i < loopCount && !error; i++)
861
 
            {
862
 
                try
863
 
                {
864
 
                    config.getSection(null);
865
 
                }
866
 
                catch (IllegalStateException istex)
867
 
                {
868
 
                    error = true;
869
 
                }
870
 
            }
871
 
        }
872
 
    }
873
 
}