View Javadoc

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 static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertTrue;
23  
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;
30  import java.util.Set;
31  
32  import org.junit.Test;
33  
34  /**
35   * Test class for INIConfiguration.
36   *
37   * @author Trevor Miller
38   * @version $Id: TestINIConfiguration.java 1224770 2011-12-26 17:18:36Z oheger $
39   */
40  @SuppressWarnings("deprecation")
41  public class TestINIConfiguration
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 =
47              "[section1]" + LINE_SEPARATOR
48              + "var1 = foo" + LINE_SEPARATOR
49              + "var2 = 451" + LINE_SEPARATOR
50              + LINE_SEPARATOR
51              + "[section2]" + LINE_SEPARATOR
52              + "var1 = 123.45" + LINE_SEPARATOR
53              + "var2 = bar" + LINE_SEPARATOR
54              + 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
60              + LINE_SEPARATOR;
61  
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";
69  
70      /**
71       * Test of save method, of class {@link INIConfiguration}.
72       */
73      @Test
74      public void testSave() throws Exception
75      {
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);
87  
88          assertEquals("Wrong content of ini file", INI_DATA, writer.toString());
89      }
90  
91      /**
92       * Test of load method, of class {@link INIConfiguration}.
93       */
94      @Test
95      public void testLoad() throws Exception
96      {
97          checkLoad(INI_DATA);
98      }
99  
100     /**
101      * Tests the load() method when the alternative value separator is used (a
102      * ':' for '=').
103      */
104     @Test
105     public void testLoadAlternativeSeparator() throws Exception
106     {
107         checkLoad(INI_DATA.replace('=', ':'));
108     }
109 
110     /**
111      * Helper method for testing the load operation. Loads the specified content
112      * into a configuration and then checks some properties.
113      *
114      * @param data the data to load
115      */
116     private void checkLoad(String data) throws ConfigurationException, IOException
117     {
118         Reader reader = new StringReader(data);
119         INIConfiguration instance = new INIConfiguration();
120         instance.load(reader);
121         reader.close();
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);
128     }
129 
130     /**
131      * Test of isCommentLine method, of class {@link INIConfiguration}.
132      */
133     @Test
134     public void testIsCommentLine()
135     {
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));
141     }
142 
143     /**
144      * Test of isSectionLine method, of class {@link INIConfiguration}.
145      */
146     @Test
147     public void testIsSectionLine()
148     {
149         INIConfiguration instance = new INIConfiguration();
150         assertTrue(instance.isSectionLine("[section]"));
151         assertFalse(instance.isSectionLine("nosection=true"));
152         assertFalse(instance.isSectionLine(null));
153     }
154 
155     /**
156      * Test of getSections method, of class {@link INIConfiguration}.
157      */
158     @Test
159     public void testGetSections()
160     {
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);
169     }
170 
171     @Test
172     public void testQuotedValue() throws Exception
173     {
174         INIConfiguration config = new INIConfiguration();
175         config.load(new StringReader(INI_DATA2));
176 
177         assertEquals("value", "quoted value", config.getString("section4.var1"));
178     }
179 
180     @Test
181     public void testQuotedValueWithQuotes() throws Exception
182     {
183         INIConfiguration config = new INIConfiguration();
184         config.load(new StringReader(INI_DATA2));
185 
186         assertEquals("value", "quoted value\\nwith \"quotes\"", config.getString("section4.var2"));
187     }
188 
189     @Test
190     public void testValueWithComment() throws Exception
191     {
192         INIConfiguration config = new INIConfiguration();
193         config.load(new StringReader(INI_DATA2));
194 
195         assertEquals("value", "123", config.getString("section4.var3"));
196     }
197 
198     @Test
199     public void testQuotedValueWithComment() throws Exception
200     {
201         INIConfiguration config = new INIConfiguration();
202         config.load(new StringReader(INI_DATA2));
203 
204         assertEquals("value", "1;2;3", config.getString("section4.var4"));
205     }
206 
207     @Test
208     public void testQuotedValueWithSingleQuotes() throws Exception
209     {
210         INIConfiguration config = new INIConfiguration();
211         config.load(new StringReader(INI_DATA2));
212 
213         assertEquals("value", "'quoted' \"value\"", config.getString("section4.var5"));
214     }
215 
216     @Test
217     public void testWriteValueWithCommentChar() throws Exception
218     {
219         INIConfiguration config = new INIConfiguration();
220         config.setProperty("section.key1", "1;2;3");
221 
222         StringWriter writer = new StringWriter();
223         config.save(writer);
224 
225         INIConfiguration config2 = new INIConfiguration();
226         config2.load(new StringReader(writer.toString()));
227 
228         assertEquals("value", "1;2;3", config2.getString("section.key1"));
229     }
230 
231     /**
232      * Tests whether whitespace is left unchanged for quoted values.
233      */
234     @Test
235     public void testQuotedValueWithWhitespace() throws Exception
236     {
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"));
242     }
243 
244     /**
245      * Tests a quoted value with space and a comment.
246      */
247     @Test
248     public void testQuotedValueWithWhitespaceAndComment() throws Exception
249     {
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"));
255     }
256 }