001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with this
004: * work for additional information regarding copyright ownership. The ASF
005: * licenses this file to You under the Apache License, Version 2.0 (the
006: * "License"); you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017:
018: package org.apache.commons.configuration;
019:
020: import java.io.IOException;
021: import java.io.Reader;
022: import java.io.StringReader;
023: import java.io.StringWriter;
024: import java.io.Writer;
025: import java.util.HashSet;
026: import java.util.Set;
027:
028: import junit.framework.TestCase;
029:
030: /**
031: * Test class for INIConfiguration.
032: *
033: * @author trevor.miller
034: * @version $Id: TestHierarchicalConfiguration.java 439648 2006-09-02 20:42:10Z
035: * oheger $
036: */
037: public class TestINIConfiguration extends TestCase {
038: /** Constant for the content of an ini file. */
039: private static final String INI_DATA = "[section1]\r\nvar1 = foo\r\n"
040: + "var2 = 451\r\n\r\n[section2]\r\nvar1 = 123.45\r\nvar2 = bar\r\n\r\n"
041: + "[section3]\r\nvar1 = true\r\n\r\n";
042:
043: /**
044: * Test of save method, of class
045: * org.apache.commons.configuration.INIConfiguration.
046: */
047: public void testSave() throws Exception {
048: Writer writer = new StringWriter();
049: INIConfiguration instance = new INIConfiguration();
050: instance.addProperty("section1.var1", "foo");
051: instance.addProperty("section1.var2", "451");
052: instance.addProperty("section2.var1", "123.45");
053: instance.addProperty("section2.var2", "bar");
054: instance.addProperty("section3.var1", "true");
055: instance.save(writer);
056: assertEquals("Wrong content of ini file", INI_DATA, writer
057: .toString());
058: }
059:
060: /**
061: * Test of load method, of class
062: * org.apache.commons.configuration.INIConfiguration.
063: */
064: public void testLoad() throws Exception {
065: checkLoad(INI_DATA);
066: }
067:
068: /**
069: * Tests the load() method when the alternative value separator is used (a
070: * ':' for '=').
071: */
072: public void testLoadAlternativeSeparator() throws Exception {
073: checkLoad(INI_DATA.replace('=', ':'));
074: }
075:
076: /**
077: * Helper method for testing the load operation. Loads the specified content
078: * into a configuration and then checks some properties.
079: *
080: * @param data the data to load
081: */
082: private void checkLoad(String data) throws ConfigurationException,
083: IOException {
084: Reader reader = new StringReader(data);
085: INIConfiguration instance = new INIConfiguration();
086: instance.load(reader);
087: reader.close();
088: assertTrue(instance.getString("section1.var1").equals("foo"));
089: assertTrue(instance.getInt("section1.var2") == 451);
090: assertTrue(instance.getDouble("section2.var1") == 123.45);
091: assertTrue(instance.getString("section2.var2").equals("bar"));
092: assertTrue(instance.getBoolean("section3.var1"));
093: assertTrue(instance.getSections().size() == 3);
094: }
095:
096: /**
097: * Test of isCommentLine method, of class
098: * org.apache.commons.configuration.INIConfiguration.
099: */
100: public void testIsCommentLine() {
101: INIConfiguration instance = new INIConfiguration();
102: assertTrue(instance.isCommentLine("#comment1"));
103: assertTrue(instance.isCommentLine(";comment1"));
104: assertFalse(instance.isCommentLine("nocomment=true"));
105: assertFalse(instance.isCommentLine(null));
106: }
107:
108: /**
109: * Test of isSectionLine method, of class
110: * org.apache.commons.configuration.INIConfiguration.
111: */
112: public void testIsSectionLine() {
113: INIConfiguration instance = new INIConfiguration();
114: assertTrue(instance.isSectionLine("[section]"));
115: assertFalse(instance.isSectionLine("nosection=true"));
116: assertFalse(instance.isSectionLine(null));
117: }
118:
119: /**
120: * Test of getSections method, of class
121: * org.apache.commons.configuration.INIConfiguration.
122: */
123: public void testGetSections() {
124: INIConfiguration instance = new INIConfiguration();
125: instance.addProperty("test1.foo", "bar");
126: instance.addProperty("test2.foo", "abc");
127: Set expResult = new HashSet();
128: expResult.add("test1");
129: expResult.add("test2");
130: Set result = instance.getSections();
131: assertEquals(expResult, result);
132: }
133: }
|