001/* 002 * Copyright 2005,2009 Ivan SZKIBA 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ini4j; 017 018import org.ini4j.sample.Dwarf; 019import org.ini4j.sample.DwarfBean; 020 021import org.ini4j.test.DwarfsData; 022import org.ini4j.test.DwarfsData.DwarfData; 023import org.ini4j.test.Helper; 024 025import static org.junit.Assert.assertArrayEquals; 026import static org.junit.Assert.assertEquals; 027import static org.junit.Assert.assertNotNull; 028import static org.junit.Assert.assertNull; 029import static org.junit.Assert.assertTrue; 030 031import org.junit.Test; 032 033import java.net.URI; 034 035public class BasicOptionMapTest extends Ini4jCase 036{ 037 private static BasicOptionMap _map; 038 039 static 040 { 041 _map = new BasicOptionMap(); 042 _map.putAll(Helper.newDwarfsOpt()); 043 } 044 045 @Test public void testAddPutNullAndString() 046 { 047 OptionMap map = new BasicOptionMap(); 048 Object o; 049 050 // null 051 o = null; 052 map.add(Dwarf.PROP_AGE, o); 053 assertNull(map.get(Dwarf.PROP_AGE)); 054 map.put(Dwarf.PROP_AGE, new Integer(DwarfsData.doc.age)); 055 assertNotNull(map.get(Dwarf.PROP_AGE)); 056 map.add(Dwarf.PROP_AGE, o, 0); 057 assertNull(map.get(Dwarf.PROP_AGE, 0)); 058 map.put(Dwarf.PROP_AGE, new Integer(DwarfsData.doc.age), 0); 059 assertNotNull(map.get(Dwarf.PROP_AGE, 0)); 060 map.put(Dwarf.PROP_AGE, o, 0); 061 assertNull(map.get(Dwarf.PROP_AGE, 0)); 062 map.remove(Dwarf.PROP_AGE); 063 map.put(Dwarf.PROP_AGE, o); 064 assertNull(map.get(Dwarf.PROP_AGE)); 065 066 // str 067 map.remove(Dwarf.PROP_AGE); 068 o = String.valueOf(DwarfsData.doc.age); 069 map.add(Dwarf.PROP_AGE, o); 070 assertEquals(o, map.get(Dwarf.PROP_AGE)); 071 map.remove(Dwarf.PROP_AGE); 072 map.put(Dwarf.PROP_AGE, o); 073 assertEquals(o, map.get(Dwarf.PROP_AGE)); 074 o = String.valueOf(DwarfsData.happy.age); 075 map.add(Dwarf.PROP_AGE, o, 0); 076 assertEquals(new Integer(DwarfsData.happy.age), (Integer) map.get(Dwarf.PROP_AGE, 0, int.class)); 077 o = String.valueOf(DwarfsData.doc.age); 078 map.put(Dwarf.PROP_AGE, o, 0); 079 assertEquals(DwarfsData.doc.age, (int) map.get(Dwarf.PROP_AGE, 0, int.class)); 080 } 081 082 @Test public void testFetch() 083 { 084 OptionMap map = new BasicOptionMap(); 085 086 Helper.addDwarf(map, DwarfsData.dopey, false); 087 Helper.addDwarf(map, DwarfsData.bashful); 088 Helper.addDwarf(map, DwarfsData.doc); 089 090 // dopey 091 assertEquals(DwarfsData.dopey.weight, map.fetch(Dwarf.PROP_WEIGHT, double.class), Helper.DELTA); 092 map.add(Dwarf.PROP_HEIGHT, map.get(Dwarf.PROP_HEIGHT)); 093 assertEquals(DwarfsData.dopey.height, map.fetch(Dwarf.PROP_HEIGHT, 1, double.class), Helper.DELTA); 094 095 // sneezy 096 map.clear(); 097 Helper.addDwarf(map, DwarfsData.happy); 098 Helper.addDwarf(map, DwarfsData.sneezy, false); 099 assertEquals(DwarfsData.sneezy.homePage, map.fetch(Dwarf.PROP_HOME_PAGE, URI.class)); 100 101 // null 102 map = new BasicOptionMap(); 103 map.add(Dwarf.PROP_AGE, null); 104 assertNull(map.fetch(Dwarf.PROP_AGE, 0)); 105 } 106 107 @Test public void testFetchAllException() 108 { 109 OptionMap map = new BasicOptionMap(); 110 111 try 112 { 113 map.fetchAll(Dwarf.PROP_FORTUNE_NUMBER, String.class); 114 missing(IllegalArgumentException.class); 115 } 116 catch (IllegalArgumentException x) 117 { 118 // 119 } 120 } 121 122 @Test public void testFromToAs() throws Exception 123 { 124 DwarfBean bean = new DwarfBean(); 125 126 _map.to(bean); 127 Helper.assertEquals(DwarfsData.dopey, bean); 128 OptionMap map = new BasicOptionMap(); 129 130 map.from(bean); 131 bean = new DwarfBean(); 132 map.to(bean); 133 Helper.assertEquals(DwarfsData.dopey, bean); 134 Dwarf proxy = map.as(Dwarf.class); 135 136 Helper.assertEquals(DwarfsData.dopey, proxy); 137 map.clear(); 138 _map.to(proxy); 139 Helper.assertEquals(DwarfsData.dopey, proxy); 140 } 141 142 @Test public void testFromToAsPrefixed() throws Exception 143 { 144 fromToAs(DwarfsData.bashful); 145 fromToAs(DwarfsData.doc); 146 fromToAs(DwarfsData.dopey); 147 fromToAs(DwarfsData.grumpy); 148 fromToAs(DwarfsData.happy); 149 fromToAs(DwarfsData.sleepy); 150 fromToAs(DwarfsData.sneezy); 151 } 152 153 @Test public void testGet() 154 { 155 OptionMap map = new BasicOptionMap(); 156 157 // bashful 158 Helper.addDwarf(map, DwarfsData.bashful, false); 159 assertEquals(DwarfsData.bashful.weight, map.get(Dwarf.PROP_WEIGHT, double.class), Helper.DELTA); 160 map.add(Dwarf.PROP_HEIGHT, map.get(Dwarf.PROP_HEIGHT)); 161 assertEquals(DwarfsData.bashful.height, map.get(Dwarf.PROP_HEIGHT, 1, double.class), Helper.DELTA); 162 assertEquals(DwarfsData.bashful.homePage, map.fetch(Dwarf.PROP_HOME_PAGE, URI.class)); 163 } 164 165 @Test public void testGetAllException() 166 { 167 OptionMap map = new BasicOptionMap(); 168 169 try 170 { 171 map.getAll(Dwarf.PROP_FORTUNE_NUMBER, String.class); 172 missing(IllegalArgumentException.class); 173 } 174 catch (IllegalArgumentException x) 175 { 176 // 177 } 178 } 179 180 @Test public void testPropertyFirstUpper() 181 { 182 DwarfBean bean; 183 OptionMap map = new BasicOptionMap(true); 184 185 map.from(DwarfsData.bashful); 186 assertTrue(map.containsKey("Age")); 187 assertTrue(map.containsKey("Height")); 188 assertTrue(map.containsKey("Weight")); 189 assertTrue(map.containsKey("HomePage")); 190 assertTrue(map.containsKey("HomeDir")); 191 bean = new DwarfBean(); 192 map.to(bean); 193 Helper.assertEquals(DwarfsData.bashful, bean); 194 Helper.assertEquals(DwarfsData.bashful, map.as(Dwarf.class)); 195 } 196 197 @Test public void testPut() 198 { 199 OptionMap map = new BasicOptionMap(); 200 201 map.add(Dwarf.PROP_AGE, new Integer(DwarfsData.sneezy.age)); 202 map.put(Dwarf.PROP_HEIGHT, new Double(DwarfsData.sneezy.height)); 203 map.add(Dwarf.PROP_HOME_DIR, DwarfsData.sneezy.homeDir); 204 map.add(Dwarf.PROP_WEIGHT, new Double(DwarfsData.sneezy.weight), 0); 205 map.put(Dwarf.PROP_HOME_PAGE, null); 206 map.put(Dwarf.PROP_HOME_PAGE, DwarfsData.sneezy.homePage); 207 map.add(Dwarf.PROP_FORTUNE_NUMBER, new Integer(DwarfsData.sneezy.fortuneNumber[1])); 208 map.add(Dwarf.PROP_FORTUNE_NUMBER, new Integer(DwarfsData.sneezy.fortuneNumber[2])); 209 map.add(Dwarf.PROP_FORTUNE_NUMBER, new Integer(0)); 210 map.put(Dwarf.PROP_FORTUNE_NUMBER, new Integer(DwarfsData.sneezy.fortuneNumber[3]), 2); 211 map.add(Dwarf.PROP_FORTUNE_NUMBER, new Integer(DwarfsData.sneezy.fortuneNumber[0]), 0); 212 Helper.assertEquals(DwarfsData.sneezy, map.as(Dwarf.class)); 213 } 214 215 @Test public void testPutAllException() 216 { 217 OptionMap map = new BasicOptionMap(); 218 219 try 220 { 221 map.putAll(Dwarf.PROP_FORTUNE_NUMBER, new Integer(0)); 222 missing(IllegalArgumentException.class); 223 } 224 catch (IllegalArgumentException x) 225 { 226 // 227 } 228 } 229 230 @Test public void testPutGetFetchAll() 231 { 232 OptionMap map = new BasicOptionMap(); 233 234 map.putAll(Dwarf.PROP_FORTUNE_NUMBER, DwarfsData.sneezy.fortuneNumber); 235 assertEquals(DwarfsData.sneezy.fortuneNumber.length, map.length(Dwarf.PROP_FORTUNE_NUMBER)); 236 assertArrayEquals(DwarfsData.sneezy.fortuneNumber, map.getAll(Dwarf.PROP_FORTUNE_NUMBER, int[].class)); 237 assertArrayEquals(DwarfsData.sneezy.fortuneNumber, map.fetchAll(Dwarf.PROP_FORTUNE_NUMBER, int[].class)); 238 map.putAll(Dwarf.PROP_FORTUNE_NUMBER, (int[]) null); 239 assertEquals(0, map.length(Dwarf.PROP_FORTUNE_NUMBER)); 240 assertEquals(0, map.getAll(Dwarf.PROP_FORTUNE_NUMBER, int[].class).length); 241 assertEquals(0, map.fetchAll(Dwarf.PROP_FORTUNE_NUMBER, int[].class).length); 242 } 243 244 @Test public void testResolve() throws Exception 245 { 246 StringBuilder buffer; 247 String input; 248 249 // simple value 250 input = "${height}"; 251 buffer = new StringBuilder(input); 252 253 _map.resolve(buffer); 254 assertEquals("" + DwarfsData.dopey.getHeight(), buffer.toString()); 255 256 // system property 257 input = "${@prop/user.home}"; 258 buffer = new StringBuilder(input); 259 260 _map.resolve(buffer); 261 assertEquals(System.getProperty("user.home"), buffer.toString()); 262 263 // system environment 264 input = "${@env/PATH}"; 265 buffer = new StringBuilder(input); 266 try 267 { 268 _map.resolve(buffer); 269 assertEquals(System.getenv("PATH"), buffer.toString()); 270 } 271 catch (Error e) 272 { 273 // retroweaver + JDK 1.4 throws Error on getenv 274 } 275 276 // unknown variable 277 input = "${no such name}"; 278 buffer = new StringBuilder(input); 279 280 _map.resolve(buffer); 281 assertEquals(input, buffer.toString()); 282 283 // small input 284 input = "${"; 285 buffer = new StringBuilder(input); 286 287 _map.resolve(buffer); 288 assertEquals(input, buffer.toString()); 289 290 // incorrect references 291 input = "${weight"; 292 buffer = new StringBuilder(input); 293 294 _map.resolve(buffer); 295 assertEquals(input, buffer.toString()); 296 297 // empty references 298 input = "jim${}"; 299 buffer = new StringBuilder(input); 300 301 _map.resolve(buffer); 302 assertEquals(input, buffer.toString()); 303 304 // escaped references 305 input = "${weight}"; 306 buffer = new StringBuilder(input); 307 308 _map.resolve(buffer); 309 assertEquals("" + DwarfsData.dopey.getWeight(), buffer.toString()); 310 input = "\\" + input; 311 buffer = new StringBuilder(input); 312 313 assertEquals(input, buffer.toString()); 314 } 315 316 private void fromToAs(DwarfData dwarf) 317 { 318 String prefix = dwarf.name + '.'; 319 DwarfBean bean = new DwarfBean(); 320 321 _map.to(bean, prefix); 322 Helper.assertEquals(dwarf, bean); 323 OptionMap map = new BasicOptionMap(); 324 325 map.from(bean, prefix); 326 bean = new DwarfBean(); 327 map.to(bean, prefix); 328 Helper.assertEquals(dwarf, bean); 329 Dwarf proxy = map.as(Dwarf.class, prefix); 330 331 Helper.assertEquals(dwarf, proxy); 332 map.clear(); 333 _map.to(proxy, prefix); 334 Helper.assertEquals(dwarf, proxy); 335 } 336}