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.test;
017    
018    import org.ini4j.Config;
019    import org.ini4j.Ini;
020    import org.ini4j.OptionMap;
021    import org.ini4j.Options;
022    import org.ini4j.Profile;
023    import org.ini4j.Reg;
024    import org.ini4j.Registry;
025    
026    import org.ini4j.sample.Dwarf;
027    import org.ini4j.sample.Dwarfs;
028    
029    import org.ini4j.spi.IniFormatter;
030    import org.ini4j.spi.IniParser;
031    
032    import org.ini4j.test.DwarfsData.DwarfData;
033    
034    import org.junit.Assert;
035    
036    import java.io.File;
037    import java.io.InputStream;
038    import java.io.InputStreamReader;
039    import java.io.Reader;
040    
041    import java.net.URL;
042    
043    import java.util.Properties;
044    
045    public class Helper
046    {
047        private static final String RESOURCE_PREFIX = "org/ini4j/sample/";
048        private static final File _sourceDir = new File(System.getProperty("basedir") + "/src/test/java/");
049        private static final File _targetDir = new File(System.getProperty("basedir") + "/target");
050        public static final String DWARFS_INI = RESOURCE_PREFIX + "dwarfs.ini";
051        public static final String TALE_INI = RESOURCE_PREFIX + "tale.ini";
052        public static final String DWARFS_OPT = RESOURCE_PREFIX + "dwarfs.opt";
053        public static final String DWARFS_REG = RESOURCE_PREFIX + "dwarfs.reg";
054        public static final String TEST_REG = "org/ini4j/mozilla.reg";
055        public static final String DWARFS_REG_PATH = Reg.Hive.HKEY_CURRENT_USER + "\\Software\\ini4j-test";
056        public static final float DELTA = 0.00000001f;
057        private static final String[] CONFIG_PROPERTIES =
058            {
059                Config.PROP_EMPTY_OPTION, Config.PROP_GLOBAL_SECTION, Config.PROP_GLOBAL_SECTION_NAME, Config.PROP_INCLUDE,
060                Config.PROP_LOWER_CASE_OPTION, Config.PROP_LOWER_CASE_SECTION, Config.PROP_MULTI_OPTION, Config.PROP_MULTI_SECTION,
061                Config.PROP_STRICT_OPERATOR, Config.PROP_UNNAMED_SECTION, Config.PROP_ESCAPE
062            };
063        private static final String[] FACTORY_PROPERTIES = { IniFormatter.class.getName(), IniParser.class.getName() };
064        public static final String HEADER_COMMENT = " Copyright 2005,2009 Ivan SZKIBA\n" + "\n"
065            + " Licensed under the Apache License, Version 2.0 (the \"License\");\n"
066            + " you may not use this file except in compliance with the License.\n" + " You may obtain a copy of the License at\n" + "\n"
067            + "      http://www.apache.org/licenses/LICENSE-2.0\n" + "\n"
068            + " Unless required by applicable law or agreed to in writing, software\n"
069            + " distributed under the License is distributed on an \"AS IS\" BASIS,\n"
070            + " WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"
071            + " See the License for the specific language governing permissions and\n" + " limitations under the License.";
072    
073        private Helper()
074        {
075        }
076    
077        public static File getBuildDirectory()
078        {
079            return _targetDir;
080        }
081    
082        public static Reader getResourceReader(String path) throws Exception
083        {
084            return new InputStreamReader(getResourceURL(path).openStream());
085        }
086    
087        public static InputStream getResourceStream(String path) throws Exception
088        {
089            return getResourceURL(path).openStream();
090        }
091    
092        public static URL getResourceURL(String path) throws Exception
093        {
094            return Helper.class.getClassLoader().getResource(path);
095        }
096    
097        public static File getSourceFile(String path) throws Exception
098        {
099            return new File(_sourceDir, path).getCanonicalFile();
100        }
101    
102        public static void addDwarf(OptionMap opts, DwarfData dwarf)
103        {
104            addDwarf(opts, dwarf, true);
105        }
106    
107        public static Profile.Section addDwarf(Profile prof, DwarfData dwarf)
108        {
109            Profile.Section s = prof.add(dwarf.name);
110    
111            inject(s, dwarf, "");
112            if (dwarf.name.equals(Dwarfs.PROP_DOPEY))
113            {
114                s.put(Dwarf.PROP_WEIGHT, DwarfsData.INI_DOPEY_WEIGHT, 0);
115                s.put(Dwarf.PROP_HEIGHT, DwarfsData.INI_DOPEY_HEIGHT, 0);
116            }
117            else if (dwarf.name.equals(Dwarfs.PROP_GRUMPY))
118            {
119                s.put(Dwarf.PROP_HEIGHT, DwarfsData.INI_GRUMPY_HEIGHT, 0);
120            }
121            else if (dwarf.name.equals(Dwarfs.PROP_SLEEPY))
122            {
123                s.put(Dwarf.PROP_HEIGHT, DwarfsData.INI_SLEEPY_HEIGHT, 0);
124            }
125            else if (dwarf.name.equals(Dwarfs.PROP_SNEEZY))
126            {
127                s.put(Dwarf.PROP_HOME_PAGE, DwarfsData.INI_SNEEZY_HOME_PAGE, 0);
128            }
129    
130            return s;
131        }
132    
133        public static Ini.Section addDwarf(Ini ini, DwarfData dwarf)
134        {
135            Ini.Section s = addDwarf((Profile) ini, dwarf);
136    
137            ini.putComment(dwarf.name, " " + dwarf.name);
138    
139            return s;
140        }
141    
142        public static void addDwarf(OptionMap opts, DwarfData dwarf, boolean addNamePrefix)
143        {
144            String prefix = addNamePrefix ? (dwarf.name + '.') : "";
145    
146            opts.putComment(prefix + Dwarf.PROP_WEIGHT, " " + dwarf.name);
147            inject(opts, dwarf, prefix);
148            if (dwarf.name.equals(Dwarfs.PROP_DOPEY))
149            {
150                opts.put(prefix + Dwarf.PROP_WEIGHT, DwarfsData.OPT_DOPEY_WEIGHT, 0);
151                opts.put(prefix + Dwarf.PROP_HEIGHT, DwarfsData.OPT_DOPEY_HEIGHT, 0);
152            }
153            else if (dwarf.name.equals(Dwarfs.PROP_GRUMPY))
154            {
155                opts.put(prefix + Dwarf.PROP_HEIGHT, DwarfsData.OPT_GRUMPY_HEIGHT, 0);
156            }
157            else if (dwarf.name.equals(Dwarfs.PROP_SLEEPY))
158            {
159                opts.put(prefix + Dwarf.PROP_HEIGHT, DwarfsData.OPT_SLEEPY_HEIGHT, 0);
160            }
161            else if (dwarf.name.equals(Dwarfs.PROP_SNEEZY))
162            {
163                opts.put(prefix + Dwarf.PROP_HOME_PAGE, DwarfsData.OPT_SNEEZY_HOME_PAGE, 0);
164            }
165        }
166    
167        public static void addDwarfs(Profile prof)
168        {
169            addDwarf(prof, DwarfsData.bashful);
170            addDwarf(prof, DwarfsData.doc);
171            addDwarf(prof, DwarfsData.dopey);
172            addDwarf(prof, DwarfsData.grumpy);
173            addDwarf(prof, DwarfsData.happy);
174            addDwarf(prof, DwarfsData.sleepy);
175            addDwarf(prof, DwarfsData.sneezy);
176        }
177    
178        public static void assertEquals(Registry.Key exp, Registry.Key act)
179        {
180            Assert.assertNotNull(exp);
181            Assert.assertEquals(exp.size(), act.size());
182            for (String child : exp.childrenNames())
183            {
184                assertEquals(exp.getChild(child), act.getChild(child));
185            }
186    
187            for (String name : exp.keySet())
188            {
189                Assert.assertEquals(exp.get(name), act.get(name));
190            }
191        }
192    
193        public static void assertEquals(Dwarfs expected, Dwarfs actual)
194        {
195            assertEquals(expected.getBashful(), actual.getBashful());
196            assertEquals(expected.getDoc(), actual.getDoc());
197            assertEquals(expected.getDopey(), actual.getDopey());
198            assertEquals(expected.getGrumpy(), actual.getGrumpy());
199            assertEquals(expected.getHappy(), actual.getHappy());
200            assertEquals(expected.getSleepy(), actual.getSleepy());
201            assertEquals(expected.getSneezy(), actual.getSneezy());
202        }
203    
204        public static void assertEquals(Dwarf expected, Dwarf actual)
205        {
206            Assert.assertEquals(expected.getAge(), actual.getAge());
207            Assert.assertEquals(expected.getHeight(), actual.getHeight(), DELTA);
208            Assert.assertEquals(expected.getWeight(), actual.getWeight(), DELTA);
209            Assert.assertEquals(expected.getHomePage().toString(), actual.getHomePage().toString());
210            Assert.assertEquals(expected.getHomeDir().toString(), actual.getHomeDir().toString());
211            Assert.assertEquals(expected.hasAge(), actual.hasAge());
212            Assert.assertEquals(expected.hasHeight(), actual.hasHeight());
213            Assert.assertEquals(expected.hasWeight(), actual.hasWeight());
214            Assert.assertEquals(expected.hasHomePage(), actual.hasHomePage());
215        }
216    
217        public static Ini loadDwarfsIni() throws Exception
218        {
219            return new Ini(Helper.class.getClassLoader().getResourceAsStream(DWARFS_INI));
220        }
221    
222        public static Ini loadDwarfsIni(Config config) throws Exception
223        {
224            Ini ini = new Ini();
225    
226            ini.setConfig(config);
227            ini.load(Helper.class.getClassLoader().getResourceAsStream(DWARFS_INI));
228    
229            return ini;
230        }
231    
232        public static Options loadDwarfsOpt() throws Exception
233        {
234            return new Options(Helper.class.getClassLoader().getResourceAsStream(DWARFS_OPT));
235        }
236    
237        public static Options loadDwarfsOpt(Config config) throws Exception
238        {
239            Options opt = new Options();
240    
241            opt.setConfig(config);
242            opt.load(Helper.class.getClassLoader().getResourceAsStream(DWARFS_OPT));
243    
244            return opt;
245        }
246    
247        public static Reg loadDwarfsReg() throws Exception
248        {
249            return new Reg(Helper.class.getClassLoader().getResourceAsStream(DWARFS_REG));
250        }
251    
252        public static Ini loadTaleIni() throws Exception
253        {
254            return new Ini(Helper.class.getClassLoader().getResourceAsStream(TALE_INI));
255        }
256    
257        public static Ini loadTaleIni(Config config) throws Exception
258        {
259            Ini ini = new Ini();
260    
261            ini.setConfig(config);
262            ini.load(Helper.class.getClassLoader().getResourceAsStream(TALE_INI));
263    
264            return ini;
265        }
266    
267        public static Ini newDwarfsIni()
268        {
269            Ini ini = new Ini();
270    
271            ini.setComment(HEADER_COMMENT);
272            addDwarf(ini, DwarfsData.bashful);
273            addDwarf(ini, DwarfsData.doc);
274            addDwarf(ini, DwarfsData.dopey);
275            addDwarf(ini, DwarfsData.grumpy);
276            addDwarf(ini, DwarfsData.happy);
277            addDwarf(ini, DwarfsData.sleepy);
278            addDwarf(ini, DwarfsData.sneezy);
279    
280            return ini;
281        }
282    
283        public static Options newDwarfsOpt()
284        {
285            Options opts = new Options();
286    
287            opts.setComment(HEADER_COMMENT);
288            addDwarf(opts, DwarfsData.dopey, false);
289            addDwarf(opts, DwarfsData.bashful);
290            addDwarf(opts, DwarfsData.doc);
291            addDwarf(opts, DwarfsData.dopey);
292            addDwarf(opts, DwarfsData.grumpy);
293            addDwarf(opts, DwarfsData.happy);
294            addDwarf(opts, DwarfsData.sleepy);
295            addDwarf(opts, DwarfsData.sneezy);
296    
297            return opts;
298        }
299    
300        public static Ini newTaleIni()
301        {
302            Ini ini = new Ini();
303    
304            ini.setComment(HEADER_COMMENT);
305            ini.add(TaleData.PROP_DWARFS);
306            addDwarf(ini, TaleData.bashful);
307            addDwarf(ini, TaleData.doc);
308            addDwarf(ini, TaleData.dopey);
309            addDwarf(ini, TaleData.grumpy);
310            addDwarf(ini, TaleData.happy);
311            addDwarf(ini, TaleData.sleepy);
312            addDwarf(ini, TaleData.sneezy);
313    
314            return ini;
315        }
316    
317        public static void resetConfig() throws Exception
318        {
319            Properties props = System.getProperties();
320    
321            for (String name : CONFIG_PROPERTIES)
322            {
323                props.remove(Config.KEY_PREFIX + name);
324            }
325    
326            for (String name : FACTORY_PROPERTIES)
327            {
328                props.remove(name);
329            }
330        }
331    
332        private static void inject(OptionMap map, Dwarf dwarf, String prefix)
333        {
334            map.put(prefix + Dwarf.PROP_WEIGHT, String.valueOf(dwarf.getWeight()));
335            map.put(prefix + Dwarf.PROP_HEIGHT, String.valueOf(dwarf.getHeight()));
336            map.put(prefix + Dwarf.PROP_AGE, String.valueOf(dwarf.getAge()));
337            map.put(prefix + Dwarf.PROP_HOME_PAGE, dwarf.getHomePage().toString());
338            map.put(prefix + Dwarf.PROP_HOME_DIR, dwarf.getHomeDir());
339            int[] numbers = dwarf.getFortuneNumber();
340    
341            if ((numbers != null) && (numbers.length > 0))
342            {
343                for (int i = 0; i < numbers.length; i++)
344                {
345                    map.add(prefix + Dwarf.PROP_FORTUNE_NUMBER, String.valueOf(numbers[i]));
346                }
347            }
348        }
349    }