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.IOException;
21  import java.io.Reader;
22  import java.io.StringReader;
23  import java.io.StringWriter;
24  import java.io.Writer;
25  import java.util.HashSet;
26  import java.util.Set;
27  
28  import junit.framework.TestCase;
29  
30  /***
31   * Test class for INIConfiguration.
32   *
33   * @author Trevor Miller
34   * @version $Id: TestINIConfiguration.java 620146 2008-02-09 16:33:45Z oheger $
35   */
36  public class TestINIConfiguration extends TestCase
37  {
38      private static String LINE_SEPARATOR = System.getProperty("line.separator");
39  
40      /*** Constant for the content of an ini file. */
41  	private static final String INI_DATA =
42              "[section1]" + LINE_SEPARATOR
43              + "var1 = foo" + LINE_SEPARATOR
44              + "var2 = 451" + LINE_SEPARATOR
45              + LINE_SEPARATOR
46              + "[section2]" + LINE_SEPARATOR
47              + "var1 = 123.45" + LINE_SEPARATOR
48              + "var2 = bar" + LINE_SEPARATOR
49              + LINE_SEPARATOR
50              + "[section3]" + LINE_SEPARATOR
51              + "var1 = true" + LINE_SEPARATOR
52              + "interpolated = ${section3.var1}" + LINE_SEPARATOR
53              + "multi = foo" + LINE_SEPARATOR
54              + "multi = bar" + LINE_SEPARATOR
55              + LINE_SEPARATOR;
56  
57  	private static final String INI_DATA2 =
58              "[section4]" + LINE_SEPARATOR
59              + "var1 = \"quoted value\"" + LINE_SEPARATOR
60              + "var2 = \"quoted value//nwith //\"quotes//\"\"" + LINE_SEPARATOR
61              + "var3 = 123 ; comment" + LINE_SEPARATOR
62              + "var4 = \"1;2;3\" ; comment" + LINE_SEPARATOR
63              + "var5 = '//'quoted//' \"value\"' ; comment";
64  
65      /***
66       * Test of save method, of class {@link INIConfiguration}.
67       */
68  	public void testSave() throws Exception
69  	{
70  		Writer writer = new StringWriter();
71  		INIConfiguration instance = new INIConfiguration();
72  		instance.addProperty("section1.var1", "foo");
73  		instance.addProperty("section1.var2", "451");
74  		instance.addProperty("section2.var1", "123.45");
75  		instance.addProperty("section2.var2", "bar");
76  		instance.addProperty("section3.var1", "true");
77  		instance.addProperty("section3.interpolated", "${section3.var1}");
78  		instance.addProperty("section3.multi", "foo");
79  		instance.addProperty("section3.multi", "bar");
80  		instance.save(writer);
81  
82          assertEquals("Wrong content of ini file", INI_DATA, writer.toString());
83  	}
84  
85  	/***
86       * Test of load method, of class {@link INIConfiguration}.
87       */
88  	public void testLoad() throws Exception
89  	{
90  		checkLoad(INI_DATA);
91  	}
92  
93  	/***
94       * Tests the load() method when the alternative value separator is used (a
95       * ':' for '=').
96       */
97  	public void testLoadAlternativeSeparator() throws Exception
98  	{
99  		checkLoad(INI_DATA.replace('=', ':'));
100 	}
101 
102 	/***
103      * Helper method for testing the load operation. Loads the specified content
104      * into a configuration and then checks some properties.
105      *
106      * @param data the data to load
107      */
108 	private void checkLoad(String data) throws ConfigurationException, IOException
109 	{
110 		Reader reader = new StringReader(data);
111 		INIConfiguration instance = new INIConfiguration();
112 		instance.load(reader);
113 		reader.close();
114 		assertTrue(instance.getString("section1.var1").equals("foo"));
115 		assertTrue(instance.getInt("section1.var2") == 451);
116 		assertTrue(instance.getDouble("section2.var1") == 123.45);
117 		assertTrue(instance.getString("section2.var2").equals("bar"));
118 		assertTrue(instance.getBoolean("section3.var1"));
119 		assertTrue(instance.getSections().size() == 3);
120 	}
121 
122 	/***
123      * Test of isCommentLine method, of class {@link INIConfiguration}.
124      */
125 	public void testIsCommentLine()
126 	{
127 		INIConfiguration instance = new INIConfiguration();
128 		assertTrue(instance.isCommentLine("#comment1"));
129 		assertTrue(instance.isCommentLine(";comment1"));
130 		assertFalse(instance.isCommentLine("nocomment=true"));
131 		assertFalse(instance.isCommentLine(null));
132 	}
133 
134 	/***
135      * Test of isSectionLine method, of class {@link INIConfiguration}.
136      */
137 	public void testIsSectionLine()
138 	{
139 		INIConfiguration instance = new INIConfiguration();
140 		assertTrue(instance.isSectionLine("[section]"));
141 		assertFalse(instance.isSectionLine("nosection=true"));
142 		assertFalse(instance.isSectionLine(null));
143 	}
144 
145 	/***
146      * Test of getSections method, of class {@link INIConfiguration}.
147      */
148 	public void testGetSections()
149 	{
150 		INIConfiguration instance = new INIConfiguration();
151 		instance.addProperty("test1.foo", "bar");
152 		instance.addProperty("test2.foo", "abc");
153 		Set expResult = new HashSet();
154 		expResult.add("test1");
155 		expResult.add("test2");
156 		Set result = instance.getSections();
157 		assertEquals(expResult, result);
158 	}
159 
160     public void testQuotedValue() throws Exception
161     {
162         INIConfiguration config = new INIConfiguration();
163         config.load(new StringReader(INI_DATA2));
164 
165         assertEquals("value", "quoted value", config.getString("section4.var1"));
166     }
167 
168     public void testQuotedValueWithQuotes() throws Exception
169     {
170         INIConfiguration config = new INIConfiguration();
171         config.load(new StringReader(INI_DATA2));
172 
173         assertEquals("value", "quoted value//nwith \"quotes\"", config.getString("section4.var2"));
174     }
175 
176     public void testValueWithComment() throws Exception
177     {
178         INIConfiguration config = new INIConfiguration();
179         config.load(new StringReader(INI_DATA2));
180 
181         assertEquals("value", "123", config.getString("section4.var3"));
182     }
183 
184     public void testQuotedValueWithComment() throws Exception
185     {
186         INIConfiguration config = new INIConfiguration();
187         config.load(new StringReader(INI_DATA2));
188 
189         assertEquals("value", "1;2;3", config.getString("section4.var4"));
190     }
191 
192     public void testQuotedValueWithSingleQuotes() throws Exception
193     {
194         INIConfiguration config = new INIConfiguration();
195         config.load(new StringReader(INI_DATA2));
196 
197         assertEquals("value", "'quoted' \"value\"", config.getString("section4.var5"));
198     }
199 
200     public void testWriteValueWithCommentChar() throws Exception
201     {
202         INIConfiguration config = new INIConfiguration();
203         config.setProperty("section.key1", "1;2;3");
204 
205         StringWriter writer = new StringWriter();
206         config.save(writer);
207 
208         INIConfiguration config2 = new INIConfiguration();
209         config2.load(new StringReader(writer.toString()));
210 
211         assertEquals("value", "1;2;3", config2.getString("section.key1"));
212     }
213 
214     /***
215      * Tests whether whitespace is left unchanged for quoted values.
216      */
217     public void testQuotedValueWithWhitespace() throws Exception
218     {
219         final String content = "CmdPrompt = \" [test@cmd ~]$ \"";
220         INIConfiguration config = new INIConfiguration();
221         config.load(new StringReader(content));
222         assertEquals("Wrong propert value", " [test@cmd ~]$ ", config
223                 .getString("CmdPrompt"));
224     }
225 
226     /***
227      * Tests a quoted value with space and a comment.
228      */
229     public void testQuotedValueWithWhitespaceAndComment() throws Exception
230     {
231         final String content = "CmdPrompt = \" [test@cmd ~]$ \" ; a comment";
232         INIConfiguration config = new INIConfiguration();
233         config.load(new StringReader(content));
234         assertEquals("Wrong propert value", " [test@cmd ~]$ ", config
235                 .getString("CmdPrompt"));
236     }
237 }