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;
20
import static org.junit.Assert.assertEquals;
21
import static org.junit.Assert.assertFalse;
22
import static org.junit.Assert.assertTrue;
24
import java.io.IOException;
25
import java.io.Reader;
26
import java.io.StringReader;
27
import java.io.StringWriter;
28
import java.io.Writer;
29
import java.util.HashSet;
32
import org.junit.Test;
35
* Test class for INIConfiguration.
37
* @author Trevor Miller
38
* @version $Id: TestINIConfiguration.java 1224770 2011-12-26 17:18:36Z oheger $
40
@SuppressWarnings("deprecation")
41
public class TestINIConfiguration
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 =
47
"[section1]" + LINE_SEPARATOR
48
+ "var1 = foo" + LINE_SEPARATOR
49
+ "var2 = 451" + LINE_SEPARATOR
51
+ "[section2]" + LINE_SEPARATOR
52
+ "var1 = 123.45" + LINE_SEPARATOR
53
+ "var2 = bar" + LINE_SEPARATOR
55
+ "[section3]" + LINE_SEPARATOR
56
+ "var1 = true" + LINE_SEPARATOR
57
+ "interpolated = ${section3.var1}" + LINE_SEPARATOR
58
+ "multi = foo" + LINE_SEPARATOR
59
+ "multi = bar" + LINE_SEPARATOR
62
private static final String INI_DATA2 =
63
"[section4]" + LINE_SEPARATOR
64
+ "var1 = \"quoted value\"" + LINE_SEPARATOR
65
+ "var2 = \"quoted value\\nwith \\\"quotes\\\"\"" + LINE_SEPARATOR
66
+ "var3 = 123 ; comment" + LINE_SEPARATOR
67
+ "var4 = \"1;2;3\" ; comment" + LINE_SEPARATOR
68
+ "var5 = '\\'quoted\\' \"value\"' ; comment";
71
* Test of save method, of class {@link INIConfiguration}.
74
public void testSave() throws Exception
76
Writer writer = new StringWriter();
77
INIConfiguration instance = new INIConfiguration();
78
instance.addProperty("section1.var1", "foo");
79
instance.addProperty("section1.var2", "451");
80
instance.addProperty("section2.var1", "123.45");
81
instance.addProperty("section2.var2", "bar");
82
instance.addProperty("section3.var1", "true");
83
instance.addProperty("section3.interpolated", "${section3.var1}");
84
instance.addProperty("section3.multi", "foo");
85
instance.addProperty("section3.multi", "bar");
86
instance.save(writer);
88
assertEquals("Wrong content of ini file", INI_DATA, writer.toString());
92
* Test of load method, of class {@link INIConfiguration}.
95
public void testLoad() throws Exception
101
* Tests the load() method when the alternative value separator is used (a
105
public void testLoadAlternativeSeparator() throws Exception
107
checkLoad(INI_DATA.replace('=', ':'));
111
* Helper method for testing the load operation. Loads the specified content
112
* into a configuration and then checks some properties.
114
* @param data the data to load
116
private void checkLoad(String data) throws ConfigurationException, IOException
118
Reader reader = new StringReader(data);
119
INIConfiguration instance = new INIConfiguration();
120
instance.load(reader);
122
assertTrue(instance.getString("section1.var1").equals("foo"));
123
assertTrue(instance.getInt("section1.var2") == 451);
124
assertTrue(instance.getDouble("section2.var1") == 123.45);
125
assertTrue(instance.getString("section2.var2").equals("bar"));
126
assertTrue(instance.getBoolean("section3.var1"));
127
assertTrue(instance.getSections().size() == 3);
131
* Test of isCommentLine method, of class {@link INIConfiguration}.
134
public void testIsCommentLine()
136
INIConfiguration instance = new INIConfiguration();
137
assertTrue(instance.isCommentLine("#comment1"));
138
assertTrue(instance.isCommentLine(";comment1"));
139
assertFalse(instance.isCommentLine("nocomment=true"));
140
assertFalse(instance.isCommentLine(null));
144
* Test of isSectionLine method, of class {@link INIConfiguration}.
147
public void testIsSectionLine()
149
INIConfiguration instance = new INIConfiguration();
150
assertTrue(instance.isSectionLine("[section]"));
151
assertFalse(instance.isSectionLine("nosection=true"));
152
assertFalse(instance.isSectionLine(null));
156
* Test of getSections method, of class {@link INIConfiguration}.
159
public void testGetSections()
161
INIConfiguration instance = new INIConfiguration();
162
instance.addProperty("test1.foo", "bar");
163
instance.addProperty("test2.foo", "abc");
164
Set<String> expResult = new HashSet<String>();
165
expResult.add("test1");
166
expResult.add("test2");
167
Set<String> result = instance.getSections();
168
assertEquals(expResult, result);
172
public void testQuotedValue() throws Exception
174
INIConfiguration config = new INIConfiguration();
175
config.load(new StringReader(INI_DATA2));
177
assertEquals("value", "quoted value", config.getString("section4.var1"));
181
public void testQuotedValueWithQuotes() throws Exception
183
INIConfiguration config = new INIConfiguration();
184
config.load(new StringReader(INI_DATA2));
186
assertEquals("value", "quoted value\\nwith \"quotes\"", config.getString("section4.var2"));
190
public void testValueWithComment() throws Exception
192
INIConfiguration config = new INIConfiguration();
193
config.load(new StringReader(INI_DATA2));
195
assertEquals("value", "123", config.getString("section4.var3"));
199
public void testQuotedValueWithComment() throws Exception
201
INIConfiguration config = new INIConfiguration();
202
config.load(new StringReader(INI_DATA2));
204
assertEquals("value", "1;2;3", config.getString("section4.var4"));
208
public void testQuotedValueWithSingleQuotes() throws Exception
210
INIConfiguration config = new INIConfiguration();
211
config.load(new StringReader(INI_DATA2));
213
assertEquals("value", "'quoted' \"value\"", config.getString("section4.var5"));
217
public void testWriteValueWithCommentChar() throws Exception
219
INIConfiguration config = new INIConfiguration();
220
config.setProperty("section.key1", "1;2;3");
222
StringWriter writer = new StringWriter();
225
INIConfiguration config2 = new INIConfiguration();
226
config2.load(new StringReader(writer.toString()));
228
assertEquals("value", "1;2;3", config2.getString("section.key1"));
232
* Tests whether whitespace is left unchanged for quoted values.
235
public void testQuotedValueWithWhitespace() throws Exception
237
final String content = "CmdPrompt = \" [test@cmd ~]$ \"";
238
INIConfiguration config = new INIConfiguration();
239
config.load(new StringReader(content));
240
assertEquals("Wrong propert value", " [test@cmd ~]$ ", config
241
.getString("CmdPrompt"));
245
* Tests a quoted value with space and a comment.
248
public void testQuotedValueWithWhitespaceAndComment() throws Exception
250
final String content = "CmdPrompt = \" [test@cmd ~]$ \" ; a comment";
251
INIConfiguration config = new INIConfiguration();
252
config.load(new StringReader(content));
253
assertEquals("Wrong propert value", " [test@cmd ~]$ ", config
254
.getString("CmdPrompt"));