1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.ini4j.test;
17
18 import org.ini4j.Config;
19 import org.ini4j.Ini;
20 import org.ini4j.OptionMap;
21 import org.ini4j.Options;
22 import org.ini4j.Profile;
23 import org.ini4j.Reg;
24 import org.ini4j.Registry;
25
26 import org.ini4j.sample.Dwarf;
27 import org.ini4j.sample.Dwarfs;
28
29 import org.ini4j.spi.IniFormatter;
30 import org.ini4j.spi.IniParser;
31
32 import org.ini4j.test.DwarfsData.DwarfData;
33
34 import org.junit.Assert;
35
36 import java.io.File;
37 import java.io.InputStream;
38 import java.io.InputStreamReader;
39 import java.io.Reader;
40
41 import java.net.URL;
42
43 import java.util.Properties;
44
45 public class Helper
46 {
47 private static final String RESOURCE_PREFIX = "org/ini4j/sample/";
48 private static final File _sourceDir = new File(System.getProperty("basedir") + "/src/test/java/");
49 private static final File _targetDir = new File(System.getProperty("basedir") + "/target");
50 public static final String DWARFS_INI = RESOURCE_PREFIX + "dwarfs.ini";
51 public static final String TALE_INI = RESOURCE_PREFIX + "tale.ini";
52 public static final String DWARFS_OPT = RESOURCE_PREFIX + "dwarfs.opt";
53 public static final String DWARFS_REG = RESOURCE_PREFIX + "dwarfs.reg";
54 public static final String TEST_REG = "org/ini4j/mozilla.reg";
55 public static final String DWARFS_REG_PATH = Reg.Hive.HKEY_CURRENT_USER + "\\Software\\ini4j-test";
56 public static final float DELTA = 0.00000001f;
57 private static final String[] CONFIG_PROPERTIES =
58 {
59 Config.PROP_EMPTY_OPTION, Config.PROP_GLOBAL_SECTION, Config.PROP_GLOBAL_SECTION_NAME, Config.PROP_INCLUDE, Config.PROP_LOWER_CASE_OPTION,
60 Config.PROP_LOWER_CASE_SECTION, Config.PROP_MULTI_OPTION, Config.PROP_MULTI_SECTION, Config.PROP_STRICT_OPERATOR,
61 Config.PROP_UNNAMED_SECTION, Config.PROP_ESCAPE, Config.PROP_ESCAPE_NEWLINE
62 };
63 private static final String[] FACTORY_PROPERTIES = { IniFormatter.class.getName(), IniParser.class.getName() };
64 public static final String HEADER_COMMENT = " Copyright 2005,2009 Ivan SZKIBA\n" + "\n"
65 + " Licensed under the Apache License, Version 2.0 (the \"License\");\n"
66 + " you may not use this file except in compliance with the License.\n" + " You may obtain a copy of the License at\n" + "\n"
67 + " http://www.apache.org/licenses/LICENSE-2.0\n" + "\n" + " Unless required by applicable law or agreed to in writing, software\n"
68 + " distributed under the License is distributed on an \"AS IS\" BASIS,\n"
69 + " WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"
70 + " See the License for the specific language governing permissions and\n" + " limitations under the License.";
71
72 private Helper()
73 {
74 }
75
76 public static File getBuildDirectory()
77 {
78 return _targetDir;
79 }
80
81 public static Reader getResourceReader(String path) throws Exception
82 {
83 return new InputStreamReader(getResourceURL(path).openStream());
84 }
85
86 public static InputStream getResourceStream(String path) throws Exception
87 {
88 return getResourceURL(path).openStream();
89 }
90
91 public static URL getResourceURL(String path) throws Exception
92 {
93 return Helper.class.getClassLoader().getResource(path);
94 }
95
96 public static File getSourceFile(String path) throws Exception
97 {
98 return new File(_sourceDir, path).getCanonicalFile();
99 }
100
101 public static void addDwarf(OptionMap opts, DwarfData dwarf)
102 {
103 addDwarf(opts, dwarf, true);
104 }
105
106 public static Profile.Section addDwarf(Profile prof, DwarfData dwarf)
107 {
108 Profile.Section s = prof.add(dwarf.name);
109
110 inject(s, dwarf, "");
111 if (dwarf.name.equals(Dwarfs.PROP_DOPEY))
112 {
113 s.put(Dwarf.PROP_WEIGHT, DwarfsData.INI_DOPEY_WEIGHT, 0);
114 s.put(Dwarf.PROP_HEIGHT, DwarfsData.INI_DOPEY_HEIGHT, 0);
115 }
116 else if (dwarf.name.equals(Dwarfs.PROP_GRUMPY))
117 {
118 s.put(Dwarf.PROP_HEIGHT, DwarfsData.INI_GRUMPY_HEIGHT, 0);
119 }
120 else if (dwarf.name.equals(Dwarfs.PROP_SLEEPY))
121 {
122 s.put(Dwarf.PROP_HEIGHT, DwarfsData.INI_SLEEPY_HEIGHT, 0);
123 }
124 else if (dwarf.name.equals(Dwarfs.PROP_SNEEZY))
125 {
126 s.put(Dwarf.PROP_HOME_PAGE, DwarfsData.INI_SNEEZY_HOME_PAGE, 0);
127 }
128
129 return s;
130 }
131
132 public static Ini.Section addDwarf(Ini ini, DwarfData dwarf)
133 {
134 Ini.Section s = addDwarf((Profile) ini, dwarf);
135
136 ini.putComment(dwarf.name, " " + dwarf.name);
137
138 return s;
139 }
140
141 public static void addDwarf(OptionMap opts, DwarfData dwarf, boolean addNamePrefix)
142 {
143 String prefix = addNamePrefix ? (dwarf.name + '.') : "";
144
145 opts.putComment(prefix + Dwarf.PROP_WEIGHT, " " + dwarf.name);
146 inject(opts, dwarf, prefix);
147 if (dwarf.name.equals(Dwarfs.PROP_DOPEY))
148 {
149 opts.put(prefix + Dwarf.PROP_WEIGHT, DwarfsData.OPT_DOPEY_WEIGHT, 0);
150 opts.put(prefix + Dwarf.PROP_HEIGHT, DwarfsData.OPT_DOPEY_HEIGHT, 0);
151 }
152 else if (dwarf.name.equals(Dwarfs.PROP_GRUMPY))
153 {
154 opts.put(prefix + Dwarf.PROP_HEIGHT, DwarfsData.OPT_GRUMPY_HEIGHT, 0);
155 }
156 else if (dwarf.name.equals(Dwarfs.PROP_SLEEPY))
157 {
158 opts.put(prefix + Dwarf.PROP_HEIGHT, DwarfsData.OPT_SLEEPY_HEIGHT, 0);
159 }
160 else if (dwarf.name.equals(Dwarfs.PROP_SNEEZY))
161 {
162 opts.put(prefix + Dwarf.PROP_HOME_PAGE, DwarfsData.OPT_SNEEZY_HOME_PAGE, 0);
163 }
164 }
165
166 public static void addDwarfs(Profile prof)
167 {
168 addDwarf(prof, DwarfsData.bashful);
169 addDwarf(prof, DwarfsData.doc);
170 addDwarf(prof, DwarfsData.dopey);
171 addDwarf(prof, DwarfsData.grumpy);
172 addDwarf(prof, DwarfsData.happy);
173 addDwarf(prof, DwarfsData.sleepy);
174 addDwarf(prof, DwarfsData.sneezy);
175 }
176
177 public static void assertEquals(Registry.Key exp, Registry.Key act)
178 {
179 Assert.assertNotNull(exp);
180 Assert.assertEquals(exp.size(), act.size());
181 for (String child : exp.childrenNames())
182 {
183 assertEquals(exp.getChild(child), act.getChild(child));
184 }
185
186 for (String name : exp.keySet())
187 {
188 Assert.assertEquals(exp.get(name), act.get(name));
189 }
190 }
191
192 public static void assertEquals(Dwarfs expected, Dwarfs actual)
193 {
194 assertEquals(expected.getBashful(), actual.getBashful());
195 assertEquals(expected.getDoc(), actual.getDoc());
196 assertEquals(expected.getDopey(), actual.getDopey());
197 assertEquals(expected.getGrumpy(), actual.getGrumpy());
198 assertEquals(expected.getHappy(), actual.getHappy());
199 assertEquals(expected.getSleepy(), actual.getSleepy());
200 assertEquals(expected.getSneezy(), actual.getSneezy());
201 }
202
203 public static void assertEquals(Dwarf expected, Dwarf actual)
204 {
205 Assert.assertEquals(expected.getAge(), actual.getAge());
206 Assert.assertEquals(expected.getHeight(), actual.getHeight(), DELTA);
207 Assert.assertEquals(expected.getWeight(), actual.getWeight(), DELTA);
208 Assert.assertEquals(expected.getHomePage().toString(), actual.getHomePage().toString());
209 Assert.assertEquals(expected.getHomeDir().toString(), actual.getHomeDir().toString());
210 Assert.assertEquals(expected.hasAge(), actual.hasAge());
211 Assert.assertEquals(expected.hasHeight(), actual.hasHeight());
212 Assert.assertEquals(expected.hasWeight(), actual.hasWeight());
213 Assert.assertEquals(expected.hasHomePage(), actual.hasHomePage());
214 }
215
216 public static Ini loadDwarfsIni() throws Exception
217 {
218 return new Ini(Helper.class.getClassLoader().getResourceAsStream(DWARFS_INI));
219 }
220
221 public static Ini loadDwarfsIni(Config config) throws Exception
222 {
223 Ini ini = new Ini();
224
225 ini.setConfig(config);
226 ini.load(Helper.class.getClassLoader().getResourceAsStream(DWARFS_INI));
227
228 return ini;
229 }
230
231 public static Options loadDwarfsOpt() throws Exception
232 {
233 return new Options(Helper.class.getClassLoader().getResourceAsStream(DWARFS_OPT));
234 }
235
236 public static Options loadDwarfsOpt(Config config) throws Exception
237 {
238 Options opt = new Options();
239
240 opt.setConfig(config);
241 opt.load(Helper.class.getClassLoader().getResourceAsStream(DWARFS_OPT));
242
243 return opt;
244 }
245
246 public static Reg loadDwarfsReg() throws Exception
247 {
248 return new Reg(Helper.class.getClassLoader().getResourceAsStream(DWARFS_REG));
249 }
250
251 public static Ini loadTaleIni() throws Exception
252 {
253 return new Ini(Helper.class.getClassLoader().getResourceAsStream(TALE_INI));
254 }
255
256 public static Ini loadTaleIni(Config config) throws Exception
257 {
258 Ini ini = new Ini();
259
260 ini.setConfig(config);
261 ini.load(Helper.class.getClassLoader().getResourceAsStream(TALE_INI));
262
263 return ini;
264 }
265
266 public static Ini newDwarfsIni()
267 {
268 Ini ini = new Ini();
269
270 ini.setComment(HEADER_COMMENT);
271 addDwarf(ini, DwarfsData.bashful);
272 addDwarf(ini, DwarfsData.doc);
273 addDwarf(ini, DwarfsData.dopey);
274 addDwarf(ini, DwarfsData.grumpy);
275 addDwarf(ini, DwarfsData.happy);
276 addDwarf(ini, DwarfsData.sleepy);
277 addDwarf(ini, DwarfsData.sneezy);
278
279 return ini;
280 }
281
282 public static Options newDwarfsOpt()
283 {
284 Options opts = new Options();
285
286 opts.setComment(HEADER_COMMENT);
287 addDwarf(opts, DwarfsData.dopey, false);
288 addDwarf(opts, DwarfsData.bashful);
289 addDwarf(opts, DwarfsData.doc);
290 addDwarf(opts, DwarfsData.dopey);
291 addDwarf(opts, DwarfsData.grumpy);
292 addDwarf(opts, DwarfsData.happy);
293 addDwarf(opts, DwarfsData.sleepy);
294 addDwarf(opts, DwarfsData.sneezy);
295
296 return opts;
297 }
298
299 public static Ini newTaleIni()
300 {
301 Ini ini = new Ini();
302
303 ini.setComment(HEADER_COMMENT);
304 ini.add(TaleData.PROP_DWARFS);
305 addDwarf(ini, TaleData.bashful);
306 addDwarf(ini, TaleData.doc);
307 addDwarf(ini, TaleData.dopey);
308 addDwarf(ini, TaleData.grumpy);
309 addDwarf(ini, TaleData.happy);
310 addDwarf(ini, TaleData.sleepy);
311 addDwarf(ini, TaleData.sneezy);
312
313 return ini;
314 }
315
316 public static void resetConfig() throws Exception
317 {
318 Properties props = System.getProperties();
319
320 for (String name : CONFIG_PROPERTIES)
321 {
322 props.remove(Config.KEY_PREFIX + name);
323 }
324
325 for (String name : FACTORY_PROPERTIES)
326 {
327 props.remove(name);
328 }
329 }
330
331 private static void inject(OptionMap map, Dwarf dwarf, String prefix)
332 {
333 map.put(prefix + Dwarf.PROP_WEIGHT, String.valueOf(dwarf.getWeight()));
334 map.put(prefix + Dwarf.PROP_HEIGHT, String.valueOf(dwarf.getHeight()));
335 map.put(prefix + Dwarf.PROP_AGE, String.valueOf(dwarf.getAge()));
336 map.put(prefix + Dwarf.PROP_HOME_PAGE, dwarf.getHomePage().toString());
337 map.put(prefix + Dwarf.PROP_HOME_DIR, dwarf.getHomeDir());
338 int[] numbers = dwarf.getFortuneNumber();
339
340 if ((numbers != null) && (numbers.length > 0))
341 {
342 for (int i = 0; i < numbers.length; i++)
343 {
344 map.add(prefix + Dwarf.PROP_FORTUNE_NUMBER, String.valueOf(numbers[i]));
345 }
346 }
347 }
348 }