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: TestHierarchicalConfiguration.java 439648 2006-09-02 20:42:10Z
35   * oheger $
36   */
37  public class TestINIConfiguration extends TestCase
38  {
39  	/*** Constant for the content of an ini file. */
40  	private static final String INI_DATA = "[section1]\r\nvar1 = foo\r\n"
41  			+ "var2 = 451\r\n\r\n[section2]\r\nvar1 = 123.45\r\nvar2 = bar\r\n\r\n"
42  			+ "[section3]\r\nvar1 = true\r\n\r\n";
43  
44  	/***
45       * Test of save method, of class
46       * org.apache.commons.configuration.INIConfiguration.
47       */
48  	public void testSave() throws Exception
49  	{
50  		Writer writer = new StringWriter();
51  		INIConfiguration instance = new INIConfiguration();
52  		instance.addProperty("section1.var1", "foo");
53  		instance.addProperty("section1.var2", "451");
54  		instance.addProperty("section2.var1", "123.45");
55  		instance.addProperty("section2.var2", "bar");
56  		instance.addProperty("section3.var1", "true");
57  		instance.save(writer);
58  		assertEquals("Wrong content of ini file", INI_DATA, writer.toString());
59  	}
60  
61  	/***
62       * Test of load method, of class
63       * org.apache.commons.configuration.INIConfiguration.
64       */
65  	public void testLoad() throws Exception
66  	{
67  		checkLoad(INI_DATA);
68  	}
69  
70  	/***
71       * Tests the load() method when the alternative value separator is used (a
72       * ':' for '=').
73       */
74  	public void testLoadAlternativeSeparator() throws Exception
75  	{
76  		checkLoad(INI_DATA.replace('=', ':'));
77  	}
78  
79  	/***
80       * Helper method for testing the load operation. Loads the specified content
81       * into a configuration and then checks some properties.
82       *
83       * @param data the data to load
84       */
85  	private void checkLoad(String data) throws ConfigurationException,
86  			IOException
87  	{
88  		Reader reader = new StringReader(data);
89  		INIConfiguration instance = new INIConfiguration();
90  		instance.load(reader);
91  		reader.close();
92  		assertTrue(instance.getString("section1.var1").equals("foo"));
93  		assertTrue(instance.getInt("section1.var2") == 451);
94  		assertTrue(instance.getDouble("section2.var1") == 123.45);
95  		assertTrue(instance.getString("section2.var2").equals("bar"));
96  		assertTrue(instance.getBoolean("section3.var1"));
97  		assertTrue(instance.getSections().size() == 3);
98  	}
99  
100 	/***
101      * Test of isCommentLine method, of class
102      * org.apache.commons.configuration.INIConfiguration.
103      */
104 	public void testIsCommentLine()
105 	{
106 		INIConfiguration instance = new INIConfiguration();
107 		assertTrue(instance.isCommentLine("#comment1"));
108 		assertTrue(instance.isCommentLine(";comment1"));
109 		assertFalse(instance.isCommentLine("nocomment=true"));
110 		assertFalse(instance.isCommentLine(null));
111 	}
112 
113 	/***
114      * Test of isSectionLine method, of class
115      * org.apache.commons.configuration.INIConfiguration.
116      */
117 	public void testIsSectionLine()
118 	{
119 		INIConfiguration instance = new INIConfiguration();
120 		assertTrue(instance.isSectionLine("[section]"));
121 		assertFalse(instance.isSectionLine("nosection=true"));
122 		assertFalse(instance.isSectionLine(null));
123 	}
124 
125 	/***
126      * Test of getSections method, of class
127      * org.apache.commons.configuration.INIConfiguration.
128      */
129 	public void testGetSections()
130 	{
131 		INIConfiguration instance = new INIConfiguration();
132 		instance.addProperty("test1.foo", "bar");
133 		instance.addProperty("test2.foo", "abc");
134 		Set expResult = new HashSet();
135 		expResult.add("test1");
136 		expResult.add("test2");
137 		Set result = instance.getSections();
138 		assertEquals(expResult, result);
139 	}
140 }