1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.ini4j;
17
18 import org.ini4j.sample.Dwarf;
19 import org.ini4j.sample.Dwarfs;
20
21 import org.ini4j.spi.BeanAccess;
22 import org.ini4j.spi.BeanTool;
23
24 import org.ini4j.test.DwarfsData;
25 import org.ini4j.test.Helper;
26 import org.ini4j.test.TaleData;
27
28 import static org.junit.Assert.assertArrayEquals;
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertNotNull;
31 import static org.junit.Assert.assertNull;
32 import static org.junit.Assert.assertSame;
33 import static org.junit.Assert.fail;
34
35 import org.junit.Test;
36
37 import java.util.prefs.Preferences;
38
39 public class IniPreferencesTest extends Ini4jCase
40 {
41 private static final String DUMMY = "dummy";
42
43 @Test public void testConstructor() throws Exception
44 {
45 Ini ini = Helper.newDwarfsIni();
46 IniPreferences prefs = new IniPreferences(ini);
47
48 assertSame(ini, prefs.getIni());
49 Helper.assertEquals(DwarfsData.dwarfs, ini.as(Dwarfs.class));
50 prefs = new IniPreferences(Helper.getResourceStream(Helper.DWARFS_INI));
51 Helper.assertEquals(DwarfsData.doc, newDwarf(prefs.node(Dwarfs.PROP_DOC)));
52 prefs = new IniPreferences(Helper.getResourceReader(Helper.DWARFS_INI));
53 Helper.assertEquals(DwarfsData.happy, newDwarf(prefs.node(Dwarfs.PROP_HAPPY)));
54 prefs = new IniPreferences(Helper.getResourceURL(Helper.DWARFS_INI));
55 Helper.assertEquals(DwarfsData.sleepy, newDwarf(prefs.node(Dwarfs.PROP_SLEEPY)));
56 }
57
58 @Test public void testMisc() throws Exception
59 {
60 Ini ini = new Ini();
61 IniPreferences prefs = new IniPreferences(ini);
62
63
64 prefs.sync();
65 prefs.flush();
66
67
68 assertEquals(0, prefs.keysSpi().length);
69 assertEquals(0, prefs.childrenNamesSpi().length);
70
71
72 assertNotNull(prefs.node(Dwarfs.PROP_DOC));
73 assertEquals(1, prefs.childrenNamesSpi().length);
74 ini.add(Dwarfs.PROP_HAPPY);
75 assertNotNull(prefs.node(Dwarfs.PROP_HAPPY));
76 assertEquals(2, prefs.childrenNamesSpi().length);
77
78
79 IniPreferences.SectionPreferences sec = (IniPreferences.SectionPreferences) prefs.node(Dwarfs.PROP_DOC);
80
81 assertEquals(0, sec.childrenNamesSpi().length);
82
83
84 sec.sync();
85 sec.syncSpi();
86 sec.flush();
87 sec.flushSpi();
88
89
90 assertEquals(0, sec.keysSpi().length);
91
92
93 sec.put(Dwarf.PROP_AGE, "87");
94 sec.flush();
95 assertEquals("87", sec.getSpi(Dwarf.PROP_AGE));
96
97
98 assertEquals(1, sec.keysSpi().length);
99
100
101 sec.remove(Dwarf.PROP_AGE);
102 sec.flush();
103
104
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 }