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
9
* http://www.apache.org/licenses/LICENSE-2.0
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
18
package org.apache.commons.configuration;
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;
31
import junit.framework.TestCase;
34
* Test class for HierarchicalINIConfiguration.
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 $
41
public class TestHierarchicalINIConfiguration extends TestCase
43
private static String LINE_SEPARATOR = System.getProperty("line.separator");
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
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;
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;
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;
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;
93
/** An ini file with a global section. */
94
private static final String INI_DATA_GLOBAL = INI_DATA_GLOBAL_ONLY
97
/** A test ini file. */
98
private static final File TEST_FILE = new File("target/test.ini");
100
protected void tearDown() throws Exception
102
if (TEST_FILE.exists())
104
assertTrue("Cannot remove test file: " + TEST_FILE, TEST_FILE
112
* Creates a HierarchicalINIConfiguration object that is initialized from
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
119
private static HierarchicalINIConfiguration setUpConfig(String data)
120
throws ConfigurationException
122
StringReader reader = new StringReader(data);
123
HierarchicalINIConfiguration instance = new HierarchicalINIConfiguration();
124
instance.load(reader);
130
* Writes a test ini file.
132
* @param content the content of the file
133
* @throws IOException if an error occurs
135
private static void writeTestFile(String content) throws IOException
137
PrintWriter out = new PrintWriter(new FileWriter(TEST_FILE));
140
out.println(content);
149
* Test of save method, of class {@link HierarchicalINIConfiguration}.
151
public void testSave() throws Exception
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);
165
assertEquals("Wrong content of ini file", INI_DATA, writer.toString());
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.
173
* @param content the content of the configuration
174
* @throws ConfigurationException if an error occurs
176
private void checkSave(String content) throws ConfigurationException
178
HierarchicalINIConfiguration config = setUpConfig(content);
179
StringWriter writer = new StringWriter();
181
assertEquals("Wrong content of ini file", content, writer.toString());
185
* Tests saving a configuration that contains a global section.
187
public void testSaveWithGlobalSection() throws ConfigurationException
189
checkSave(INI_DATA_GLOBAL);
193
* Tests whether a configuration that contains only a global section can be
196
public void testSaveWithOnlyGlobalSection() throws ConfigurationException
198
checkSave(INI_DATA_GLOBAL_ONLY);
202
* Test of load method, of class {@link HierarchicalINIConfiguration}.
204
public void testLoad() throws Exception
210
* Tests the load() method when the alternative value separator is used (a
213
public void testLoadAlternativeSeparator() throws Exception
215
checkLoad(INI_DATA.replace('=', ':'));
219
* Tests loading a configuration from a File.
221
public void testLoadFile() throws ConfigurationException, IOException
223
writeTestFile(INI_DATA);
224
HierarchicalINIConfiguration config = new HierarchicalINIConfiguration(
226
checkContent(config);
230
* Tests loading a configuration from a file name.
232
public void testLoadFileName() throws ConfigurationException, IOException
234
writeTestFile(INI_DATA);
235
HierarchicalINIConfiguration config = new HierarchicalINIConfiguration(
236
TEST_FILE.getAbsolutePath());
237
checkContent(config);
241
* Tests loading a configuration from a URL.
243
public void testLoadURL() throws ConfigurationException, IOException
245
writeTestFile(INI_DATA);
246
HierarchicalINIConfiguration config = new HierarchicalINIConfiguration(
247
TEST_FILE.toURI().toURL());
248
checkContent(config);
252
* Tests the values of some properties to ensure that the configuration was
255
* @param instance the configuration to check
257
private void checkContent(HierarchicalINIConfiguration instance)
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);
268
* Helper method for testing the load operation. Loads the specified content
269
* into a configuration and then checks some properties.
271
* @param data the data to load
273
private void checkLoad(String data) throws ConfigurationException
275
HierarchicalINIConfiguration instance = setUpConfig(data);
276
checkContent(instance);
280
* Test of isCommentLine method, of class
281
* {@link HierarchicalINIConfiguration}.
283
public void testIsCommentLine()
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));
293
* Test of isSectionLine method, of class
294
* {@link HierarchicalINIConfiguration}.
296
public void testIsSectionLine()
298
HierarchicalINIConfiguration instance = new HierarchicalINIConfiguration();
299
assertTrue(instance.isSectionLine("[section]"));
300
assertFalse(instance.isSectionLine("nosection=true"));
301
assertFalse(instance.isSectionLine(null));
305
* Test of getSections method, of class {@link HierarchicalINIConfiguration}
308
public void testGetSections()
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);
320
public void testQuotedValue() throws Exception
322
HierarchicalINIConfiguration config = setUpConfig(INI_DATA2);
323
assertEquals("value", "quoted value", config.getString("section4.var1"));
326
public void testQuotedValueWithQuotes() throws Exception
328
HierarchicalINIConfiguration config = setUpConfig(INI_DATA2);
329
assertEquals("value", "quoted value\\nwith \"quotes\"", config
330
.getString("section4.var2"));
333
public void testValueWithComment() throws Exception
335
HierarchicalINIConfiguration config = setUpConfig(INI_DATA2);
336
assertEquals("value", "123", config.getString("section4.var3"));
339
public void testQuotedValueWithComment() throws Exception
341
HierarchicalINIConfiguration config = setUpConfig(INI_DATA2);
342
assertEquals("value", "1;2;3", config.getString("section4.var4"));
345
public void testQuotedValueWithSingleQuotes() throws Exception
347
HierarchicalINIConfiguration config = setUpConfig(INI_DATA2);
348
assertEquals("value", "'quoted' \"value\"", config
349
.getString("section4.var5"));
352
public void testWriteValueWithCommentChar() throws Exception
354
HierarchicalINIConfiguration config = new HierarchicalINIConfiguration();
355
config.setProperty("section.key1", "1;2;3");
357
StringWriter writer = new StringWriter();
360
HierarchicalINIConfiguration config2 = new HierarchicalINIConfiguration();
361
config2.load(new StringReader(writer.toString()));
363
assertEquals("value", "1;2;3", config2.getString("section.key1"));
367
* Tests whether whitespace is left unchanged for quoted values.
369
public void testQuotedValueWithWhitespace() throws Exception
371
final String content = "CmdPrompt = \" [test@cmd ~]$ \"";
372
HierarchicalINIConfiguration config = setUpConfig(content);
373
assertEquals("Wrong propert value", " [test@cmd ~]$ ", config
374
.getString("CmdPrompt"));
378
* Tests a quoted value with space and a comment.
380
public void testQuotedValueWithWhitespaceAndComment() throws Exception
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"));
389
* Tests an empty quoted value.
391
public void testQuotedValueEmpty() throws ConfigurationException
393
HierarchicalINIConfiguration config = setUpConfig(INI_DATA2);
394
assertEquals("Wrong value for empty property", "", config
395
.getString("section4.var6"));
399
* Tests a property that has no value.
401
public void testGetPropertyNoValue() throws ConfigurationException
403
final String data = INI_DATA2 + LINE_SEPARATOR + "noValue ="
405
HierarchicalINIConfiguration config = setUpConfig(data);
406
assertEquals("Wrong value of key", "", config
407
.getString("section4.noValue"));
411
* Tests a property that has no key.
413
public void testGetPropertyNoKey() throws ConfigurationException
415
final String data = INI_DATA2 + LINE_SEPARATOR + "= noKey"
417
HierarchicalINIConfiguration config = setUpConfig(data);
418
assertEquals("Cannot find property with no key", "noKey", config
419
.getString("section4. "));
423
* Tests reading a property from the global section.
425
public void testGlobalProperty() throws ConfigurationException
427
HierarchicalINIConfiguration config = setUpConfig(INI_DATA_GLOBAL);
428
assertEquals("Wrong value of global property", "testGlobal", config
429
.getString("globalVar"));
433
* Tests whether the specified configuration contains exactly the expected
436
* @param config the configuration to check
437
* @param expected an array with the expected sections
439
private void checkSectionNames(HierarchicalINIConfiguration config,
442
Set sectionNames = config.getSections();
443
Iterator it = sectionNames.iterator();
444
for (int idx = 0; idx < expected.length; idx++)
446
assertEquals("Wrong section at " + idx, expected[idx], it.next());
448
assertFalse("Too many sections", it.hasNext());
452
* Tests the names of the sections returned by the configuration.
454
* @param data the data of the ini configuration
455
* @param expected the expected section names
456
* @return the configuration instance
458
private HierarchicalINIConfiguration checkSectionNames(String data,
459
String[] expected) throws ConfigurationException
461
HierarchicalINIConfiguration config = setUpConfig(data);
462
checkSectionNames(config, expected);
467
* Tests querying the sections if a global section if available.
469
public void testGetSectionsWithGlobal() throws ConfigurationException
471
checkSectionNames(INI_DATA_GLOBAL, new String[] {
472
null, "section1", "section2", "section3"
477
* Tests querying the sections if there is no global section.
479
public void testGetSectionsNoGlobal() throws ConfigurationException
481
checkSectionNames(INI_DATA, new String[] {
482
"section1", "section2", "section3"
487
* Tests whether the sections of a configuration can be queried that
488
* contains only a global section.
490
public void testGetSectionsGlobalOnly() throws ConfigurationException
492
checkSectionNames(INI_DATA_GLOBAL_ONLY, new String[] {
498
* Tests whether variables containing a dot are not misinterpreted as
499
* sections. This test is related to CONFIGURATION-327.
501
public void testGetSectionsDottedVar() throws ConfigurationException
503
final String data = "dotted.var = 1" + LINE_SEPARATOR + INI_DATA_GLOBAL;
504
HierarchicalINIConfiguration config = checkSectionNames(data,
506
null, "section1", "section2", "section3"
508
assertEquals("Wrong value of dotted variable", 1, config
509
.getInt("dotted..var"));
513
* Tests whether a section added later is also found by getSections().
515
public void testGetSectionsAdded() throws ConfigurationException
517
HierarchicalINIConfiguration config = setUpConfig(INI_DATA2);
518
config.addProperty("section5.test", Boolean.TRUE);
519
checkSectionNames(config, new String[] {
520
"section4", "section5"
525
* Tests querying the properties of an existing section.
527
public void testGetSectionExisting() throws ConfigurationException
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"));
536
* Tests querying the properties of a section that was merged from two
537
* sections with the same name.
539
public void testGetSectionMerged() throws ConfigurationException
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"));
551
* Tests querying the content of the global section.
553
public void testGetSectionGlobal() throws ConfigurationException
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"));
562
* Tests concurrent access to the global section.
564
public void testGetSectionGloabalMultiThreaded()
565
throws ConfigurationException, InterruptedException
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++)
572
threads[i] = new GlobalSectionTestThread(config);
575
for (int i = 0; i < threadCount; i++)
578
assertFalse("Exception occurred", threads[i].error);
583
* Tests querying the content of the global section if there is none.
585
public void testGetSectionGlobalNonExisting() throws ConfigurationException
587
HierarchicalINIConfiguration config = setUpConfig(INI_DATA);
588
SubnodeConfiguration section = config.getSection(null);
589
assertTrue("Sub config not empty", section.isEmpty());
593
* Tests querying a non existing section.
595
public void testGetSectionNonExisting() throws ConfigurationException
597
HierarchicalINIConfiguration config = setUpConfig(INI_DATA);
598
SubnodeConfiguration section = config
599
.getSection("Non existing section");
600
assertTrue("Sub config not empty", section.isEmpty());
604
* Tests a property whose value spans multiple lines.
606
public void testLineContinuation() throws ConfigurationException
608
HierarchicalINIConfiguration config = setUpConfig(INI_DATA3);
609
assertEquals("Wrong value", "one" + LINE_SEPARATOR + "two"
610
+ LINE_SEPARATOR + "three", config
611
.getString("section5.multiLine"));
615
* Tests a property value that ends on a backslash, which is no line
616
* continuation character.
618
public void testLineContinuationNone() throws ConfigurationException
620
HierarchicalINIConfiguration config = setUpConfig(INI_DATA3);
621
assertEquals("Wrong value", "C:\\Temp\\", config
622
.getString("section5.singleLine"));
626
* Tests a property whose value spans multiple lines when quoting is
627
* involved. In this case whitespace must not be trimmed.
629
public void testLineContinuationQuoted() throws ConfigurationException
631
HierarchicalINIConfiguration config = setUpConfig(INI_DATA3);
632
assertEquals("Wrong value", "one" + LINE_SEPARATOR + " two "
633
+ LINE_SEPARATOR + "three", config
634
.getString("section5.multiQuoted"));
638
* Tests a property whose value spans multiple lines with a comment.
640
public void testLineContinuationComment() throws ConfigurationException
642
HierarchicalINIConfiguration config = setUpConfig(INI_DATA3);
643
assertEquals("Wrong value", "one" + LINE_SEPARATOR + "two", config
644
.getString("section5.multiComment"));
648
* Tests a property with a quoted value spanning multiple lines and a
651
public void testLineContinuationQuotedComment()
652
throws ConfigurationException
654
HierarchicalINIConfiguration config = setUpConfig(INI_DATA3);
655
assertEquals("Wrong value", " one " + LINE_SEPARATOR + "two", config
656
.getString("section5.multiQuotedComment"));
660
* Tests a multi-line property value with an empty line.
662
public void testLineContinuationEmptyLine() throws ConfigurationException
664
HierarchicalINIConfiguration config = setUpConfig(INI_DATA3);
665
assertEquals("Wrong value", LINE_SEPARATOR + "line 2", config
666
.getString("section5.noFirstLine"));
670
* Tests a line continuation at the end of the file.
672
public void testLineContinuationAtEnd() throws ConfigurationException
674
HierarchicalINIConfiguration config = setUpConfig(INI_DATA3);
675
assertEquals("Wrong value", "one" + LINE_SEPARATOR, config
676
.getString("section5.continueNoLine"));
680
* Tests whether a configuration can be saved that contains section keys
681
* with delimiter characters. This test is related to CONFIGURATION-409.
683
public void testSaveKeysWithDelimiters() throws ConfigurationException
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"));
697
* Tests whether a value which contains a semicolon can be loaded
698
* successfully. This test is related to CONFIGURATION-434.
700
public void testValueWithSemicolon() throws ConfigurationException
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"));
717
* Tests whether the different separators with or without whitespace are
720
public void testSeparators() throws ConfigurationException
722
HierarchicalINIConfiguration config = setUpConfig(INI_DATA_SEPARATORS);
723
for (int i = 1; i <= 4; i++)
725
assertEquals("Wrong value", "value" + i,
726
config.getString("section.var" + i));
731
* Tests property definitions containing multiple separators.
733
public void testMultipleSeparators() throws ConfigurationException
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"));
743
* Tests property definitions containing multiple separators that are
746
public void testMultipleSeparatorsQuoted() throws ConfigurationException
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"));
756
* Tests whether a section that has been cleared can be manipulated and
759
public void testSaveClearedSection() throws ConfigurationException
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());
766
sub.setProperty("test", "success");
767
StringWriter writer = new StringWriter();
769
HierarchicalConfiguration config2 = setUpConfig(writer.toString());
770
assertEquals("Wrong value", "success",
771
config2.getString("section.test"));
775
* Tests whether a duplicate session is merged.
777
public void testMergeDuplicateSection() throws ConfigurationException
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();
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);
797
* Tests whether a section that was created by getSection() can be
800
public void testGetSectionNonExistingManipulate()
801
throws ConfigurationException
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();
810
HierarchicalINIConfiguration config2 = setUpConfig(writer.toString());
811
section = config2.getSection("newSection");
812
assertEquals("Wrong value", "success", section.getString("test"));
816
* Tests whether getSection() can deal with duplicate sections.
818
public void testGetSectionDuplicate()
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());
831
* A thread class for testing concurrent access to the global section.
833
private static class GlobalSectionTestThread extends Thread
835
/** The configuration. */
836
private final HierarchicalINIConfiguration config;
838
/** A flag whether an error was found. */
839
volatile boolean error;
842
* Creates a new instance of <code>GlobalSectionTestThread</code> and
845
* @param conf the configuration object
847
public GlobalSectionTestThread(HierarchicalINIConfiguration conf)
853
* Accesses the global section in a loop. If there is no correct
854
* synchronization, this can cause an exception.
858
final int loopCount = 250;
860
for (int i = 0; i < loopCount && !error; i++)
864
config.getSection(null);
866
catch (IllegalStateException istex)