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 static org.junit.Assert.assertEquals; 021 import static org.junit.Assert.assertFalse; 022 import static org.junit.Assert.assertTrue; 023 024 import java.io.IOException; 025 import java.io.Reader; 026 import java.io.StringReader; 027 import java.io.StringWriter; 028 import java.io.Writer; 029 import java.util.HashSet; 030 import java.util.Set; 031 032 import org.junit.Test; 033 034 /** 035 * Test class for INIConfiguration. 036 * 037 * @author Trevor Miller 038 * @version $Id: TestINIConfiguration.java 1224770 2011-12-26 17:18:36Z oheger $ 039 */ 040 @SuppressWarnings("deprecation") 041 public class TestINIConfiguration 042 { 043 private static String LINE_SEPARATOR = System.getProperty("line.separator"); 044 045 /** Constant for the content of an ini file. */ 046 private static final String INI_DATA = 047 "[section1]" + LINE_SEPARATOR 048 + "var1 = foo" + LINE_SEPARATOR 049 + "var2 = 451" + LINE_SEPARATOR 050 + LINE_SEPARATOR 051 + "[section2]" + LINE_SEPARATOR 052 + "var1 = 123.45" + LINE_SEPARATOR 053 + "var2 = bar" + LINE_SEPARATOR 054 + LINE_SEPARATOR 055 + "[section3]" + LINE_SEPARATOR 056 + "var1 = true" + LINE_SEPARATOR 057 + "interpolated = ${section3.var1}" + LINE_SEPARATOR 058 + "multi = foo" + LINE_SEPARATOR 059 + "multi = bar" + LINE_SEPARATOR 060 + LINE_SEPARATOR; 061 062 private static final String INI_DATA2 = 063 "[section4]" + LINE_SEPARATOR 064 + "var1 = \"quoted value\"" + LINE_SEPARATOR 065 + "var2 = \"quoted value\\nwith \\\"quotes\\\"\"" + LINE_SEPARATOR 066 + "var3 = 123 ; comment" + LINE_SEPARATOR 067 + "var4 = \"1;2;3\" ; comment" + LINE_SEPARATOR 068 + "var5 = '\\'quoted\\' \"value\"' ; comment"; 069 070 /** 071 * Test of save method, of class {@link INIConfiguration}. 072 */ 073 @Test 074 public void testSave() throws Exception 075 { 076 Writer writer = new StringWriter(); 077 INIConfiguration instance = new INIConfiguration(); 078 instance.addProperty("section1.var1", "foo"); 079 instance.addProperty("section1.var2", "451"); 080 instance.addProperty("section2.var1", "123.45"); 081 instance.addProperty("section2.var2", "bar"); 082 instance.addProperty("section3.var1", "true"); 083 instance.addProperty("section3.interpolated", "${section3.var1}"); 084 instance.addProperty("section3.multi", "foo"); 085 instance.addProperty("section3.multi", "bar"); 086 instance.save(writer); 087 088 assertEquals("Wrong content of ini file", INI_DATA, writer.toString()); 089 } 090 091 /** 092 * Test of load method, of class {@link INIConfiguration}. 093 */ 094 @Test 095 public void testLoad() throws Exception 096 { 097 checkLoad(INI_DATA); 098 } 099 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 }