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 */ 016 package org.ini4j; 017 018 import org.ini4j.sample.Dwarf; 019 import org.ini4j.sample.Dwarfs; 020 021 import org.ini4j.spi.BeanAccess; 022 import org.ini4j.spi.BeanTool; 023 024 import org.ini4j.test.DwarfsData; 025 import org.ini4j.test.Helper; 026 import org.ini4j.test.TaleData; 027 028 import static org.junit.Assert.assertArrayEquals; 029 import static org.junit.Assert.assertEquals; 030 import static org.junit.Assert.assertNotNull; 031 import static org.junit.Assert.assertNull; 032 import static org.junit.Assert.assertSame; 033 import static org.junit.Assert.fail; 034 035 import org.junit.Test; 036 037 import java.util.prefs.Preferences; 038 039 public class IniPreferencesTest extends Ini4jCase 040 { 041 private static final String DUMMY = "dummy"; 042 043 @Test public void testConstructor() throws Exception 044 { 045 Ini ini = Helper.newDwarfsIni(); 046 IniPreferences prefs = new IniPreferences(ini); 047 048 assertSame(ini, prefs.getIni()); 049 Helper.assertEquals(DwarfsData.dwarfs, ini.as(Dwarfs.class)); 050 prefs = new IniPreferences(Helper.getResourceStream(Helper.DWARFS_INI)); 051 Helper.assertEquals(DwarfsData.doc, newDwarf(prefs.node(Dwarfs.PROP_DOC))); 052 prefs = new IniPreferences(Helper.getResourceReader(Helper.DWARFS_INI)); 053 Helper.assertEquals(DwarfsData.happy, newDwarf(prefs.node(Dwarfs.PROP_HAPPY))); 054 prefs = new IniPreferences(Helper.getResourceURL(Helper.DWARFS_INI)); 055 Helper.assertEquals(DwarfsData.sleepy, newDwarf(prefs.node(Dwarfs.PROP_SLEEPY))); 056 } 057 058 @Test public void testMisc() throws Exception 059 { 060 Ini ini = new Ini(); 061 IniPreferences prefs = new IniPreferences(ini); 062 063 // do nothing, but doesn't throw exception 064 prefs.sync(); 065 prefs.flush(); 066 067 // node & key count 068 assertEquals(0, prefs.keysSpi().length); 069 assertEquals(0, prefs.childrenNamesSpi().length); 070 071 // childNode for new and for existing section 072 assertNotNull(prefs.node(Dwarfs.PROP_DOC)); 073 assertEquals(1, prefs.childrenNamesSpi().length); 074 ini.add(Dwarfs.PROP_HAPPY); 075 assertNotNull(prefs.node(Dwarfs.PROP_HAPPY)); 076 assertEquals(2, prefs.childrenNamesSpi().length); 077 078 // SectionPreferences 079 IniPreferences.SectionPreferences sec = (IniPreferences.SectionPreferences) prefs.node(Dwarfs.PROP_DOC); 080 081 assertEquals(0, sec.childrenNamesSpi().length); 082 083 // do nothing, but doesn't throw exception 084 sec.sync(); 085 sec.syncSpi(); 086 sec.flush(); 087 sec.flushSpi(); 088 089 // empty 090 assertEquals(0, sec.keysSpi().length); 091 092 // add one key 093 sec.put(Dwarf.PROP_AGE, "87"); 094 sec.flush(); 095 assertEquals("87", sec.getSpi(Dwarf.PROP_AGE)); 096 097 // has one key 098 assertEquals(1, sec.keysSpi().length); 099 100 // remove key 101 sec.remove(Dwarf.PROP_AGE); 102 sec.flush(); 103 104 // has 0 key 105 assertEquals(0, sec.keysSpi().length); 106 sec.removeNode(); 107 prefs.flush(); 108 assertNull(ini.get(Dwarfs.PROP_DOC)); 109 } 110 111 @Test public void testTaleTree() throws Exception 112 { 113 Ini ini = Helper.newTaleIni(); 114 IniPreferences prefs = new IniPreferences(ini); 115 Preferences dwarfs = prefs.node(TaleData.PROP_DWARFS); 116 117 Helper.assertEquals(DwarfsData.doc, newDwarf(dwarfs.node(Dwarfs.PROP_DOC))); 118 assertArrayEquals(DwarfsData.dwarfNames, dwarfs.childrenNames()); 119 assertEquals(1, prefs.childrenNames().length); 120 } 121 122 @Test public void testTree() throws Exception 123 { 124 Ini ini = new Ini(); 125 IniPreferences prefs = new IniPreferences(ini); 126 IniPreferences.SectionPreferences sec = (IniPreferences.SectionPreferences) prefs.node(Dwarfs.PROP_DOC); 127 Preferences child = sec.node(DUMMY); 128 129 assertNotNull(child); 130 assertNotNull(sec.node(DUMMY)); 131 assertNotNull(ini.get(Dwarfs.PROP_DOC).getChild(DUMMY)); 132 assertEquals(1, prefs.childrenNames().length); 133 } 134 135 @SuppressWarnings("empty-statement") 136 @Test public void testUnsupported() throws Exception 137 { 138 Ini ini = new Ini(); 139 IniPreferences prefs = new IniPreferences(ini); 140 141 try 142 { 143 prefs.getSpi(DUMMY); 144 fail(); 145 } 146 catch (UnsupportedOperationException x) 147 { 148 ; 149 } 150 151 try 152 { 153 prefs.putSpi(DUMMY, DUMMY); 154 fail(); 155 } 156 catch (UnsupportedOperationException x) 157 { 158 ; 159 } 160 161 try 162 { 163 prefs.removeNodeSpi(); 164 fail(); 165 } 166 catch (UnsupportedOperationException x) 167 { 168 ; 169 } 170 171 try 172 { 173 prefs.removeSpi(DUMMY); 174 fail(); 175 } 176 catch (UnsupportedOperationException x) 177 { 178 ; 179 } 180 } 181 182 private Dwarf newDwarf(Preferences node) 183 { 184 return BeanTool.getInstance().proxy(Dwarf.class, new Access(node)); 185 } 186 187 public static class Access implements BeanAccess 188 { 189 private final Preferences _node; 190 191 public Access(Preferences node) 192 { 193 _node = node; 194 } 195 196 public void propAdd(String propertyName, String value) 197 { 198 throw new UnsupportedOperationException("Not supported yet."); 199 } 200 201 public String propDel(String propertyName) 202 { 203 throw new UnsupportedOperationException("Not supported yet."); 204 } 205 206 public String propGet(String propertyName) 207 { 208 return _node.get(propertyName, null); 209 } 210 211 public String propGet(String propertyName, int index) 212 { 213 return (index == 0) ? propGet(propertyName) : null; 214 } 215 216 public int propLength(String propertyName) 217 { 218 return (propGet(propertyName) == null) ? 0 : 1; 219 } 220 221 public String propSet(String propertyName, String value) 222 { 223 throw new UnsupportedOperationException("Not supported yet."); 224 } 225 226 public String propSet(String propertyName, String value, int index) 227 { 228 throw new UnsupportedOperationException("Not supported yet."); 229 } 230 } 231 }