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.Dwarfs;
019    
020    import org.ini4j.test.DwarfsData;
021    import org.ini4j.test.Helper;
022    
023    import static org.junit.Assert.assertEquals;
024    
025    import org.junit.Test;
026    
027    import java.io.ByteArrayInputStream;
028    import java.io.ByteArrayOutputStream;
029    import java.io.File;
030    import java.io.FileNotFoundException;
031    import java.io.InputStreamReader;
032    import java.io.OutputStreamWriter;
033    import java.io.StringReader;
034    import java.io.StringWriter;
035    
036    public class IniTest extends Ini4jCase
037    {
038        private static final String COMMENT_ONLY = "# first line\n# second line\n";
039        private static final String COMMENT_ONLY_VALUE = " first line\n second line";
040        private static final String INI_ONE_HEADER = COMMENT_ONLY + "\n\n[section]\nkey=value\n";
041        private static final String COMMENTED_OPTION = COMMENT_ONLY + "\n\n[section]\n;comment\nkey=value\n";
042        private static final String MULTI = "[section]\noption=value\noption=value2\n[section]\noption=value3\noption=value4\noption=value5\n";
043    
044        @Test public void testCommentedOption() throws Exception
045        {
046            Ini ini = new Ini(new StringReader(COMMENTED_OPTION));
047    
048            assertEquals("comment", ini.get("section").getComment("key"));
049        }
050    
051        @Test public void testCommentOnly() throws Exception
052        {
053            Ini ini = new Ini(new StringReader(COMMENT_ONLY));
054    
055            assertEquals(COMMENT_ONLY_VALUE, ini.getComment());
056        }
057    
058        @Test public void testLoad() throws Exception
059        {
060            Ini ini;
061    
062            ini = new Ini(Helper.getResourceURL(Helper.DWARFS_INI));
063            Helper.assertEquals(DwarfsData.dwarfs, ini.as(Dwarfs.class));
064            ini = new Ini(Helper.getResourceStream(Helper.DWARFS_INI));
065            Helper.assertEquals(DwarfsData.dwarfs, ini.as(Dwarfs.class));
066            ini = new Ini(Helper.getResourceReader(Helper.DWARFS_INI));
067            Helper.assertEquals(DwarfsData.dwarfs, ini.as(Dwarfs.class));
068            ini = new Ini(Helper.getSourceFile(Helper.DWARFS_INI));
069            Helper.assertEquals(DwarfsData.dwarfs, ini.as(Dwarfs.class));
070            ini = new Ini();
071            ini.setFile(Helper.getSourceFile(Helper.DWARFS_INI));
072            ini.load();
073            Helper.assertEquals(DwarfsData.dwarfs, ini.as(Dwarfs.class));
074        }
075    
076        @Test public void testLoadException() throws Exception
077        {
078            Ini ini = new Ini();
079    
080            try
081            {
082                ini.load();
083                missing(FileNotFoundException.class);
084            }
085            catch (FileNotFoundException x)
086            {
087                //
088            }
089        }
090    
091        @Test public void testMulti() throws Exception
092        {
093            Ini ini = new Ini(new StringReader(MULTI));
094            Ini.Section sec;
095    
096            assertEquals(1, ini.length("section"));
097            assertEquals(5, ini.get("section", 0).length("option"));
098            ini.clear();
099            Config cfg = new Config();
100    
101            cfg.setMultiSection(true);
102            ini.setConfig(cfg);
103            ini.load(new StringReader(MULTI));
104            assertEquals(2, ini.get("section", 0).length("option"));
105            assertEquals(3, ini.get("section", 1).length("option"));
106    
107            //
108            StringWriter writer = new StringWriter();
109    
110            cfg.setMultiOption(false);
111            ini.store(writer);
112            ini.clear();
113            cfg.setMultiOption(true);
114            ini.load(new StringReader(writer.toString()));
115            assertEquals(1, ini.get("section", 0).length("option"));
116            assertEquals(1, ini.get("section", 1).length("option"));
117            assertEquals("value2", ini.get("section", 0).get("option"));
118            assertEquals("value5", ini.get("section", 1).get("option"));
119    
120            //
121            ini.clear();
122            cfg.setMultiOption(false);
123            ini.load(new StringReader(MULTI));
124            assertEquals(1, ini.get("section", 0).length("option"));
125            assertEquals(1, ini.get("section", 1).length("option"));
126        }
127    
128        @Test public void testOneHeaderOnly() throws Exception
129        {
130            Ini ini = new Ini(new StringReader(INI_ONE_HEADER));
131    
132            assertEquals(COMMENT_ONLY_VALUE, ini.getComment());
133        }
134    
135        @Test public void testStore() throws Exception
136        {
137            Ini ini = Helper.newDwarfsIni();
138            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
139    
140            ini.store(buffer);
141            Ini dup = new Ini();
142    
143            dup.load(new ByteArrayInputStream(buffer.toByteArray()));
144            Helper.assertEquals(DwarfsData.dwarfs, dup.as(Dwarfs.class));
145            buffer = new ByteArrayOutputStream();
146            ini.store(new OutputStreamWriter(buffer));
147            dup = new Ini();
148            dup.load(new InputStreamReader(new ByteArrayInputStream(buffer.toByteArray())));
149            Helper.assertEquals(DwarfsData.dwarfs, dup.as(Dwarfs.class));
150    
151            //
152            File file = File.createTempFile("test", ".ini");
153    
154            file.deleteOnExit();
155            ini.setFile(file);
156            assertEquals(file, ini.getFile());
157            ini.store();
158            dup = new Ini();
159            dup.setFile(file);
160            dup.load();
161            Helper.assertEquals(DwarfsData.dwarfs, dup.as(Dwarfs.class));
162            file.delete();
163        }
164    
165        @Test public void testStoreException() throws Exception
166        {
167            Ini ini = new Ini();
168    
169            try
170            {
171                ini.store();
172                missing(FileNotFoundException.class);
173            }
174            catch (FileNotFoundException x)
175            {
176                //
177            }
178        }
179    
180        @Test public void testWithComment() throws Exception
181        {
182            Ini ini = new Ini();
183    
184            ini.load(Helper.getResourceStream(Helper.DWARFS_INI));
185            assertNotNull(ini.getComment());
186            for (Ini.Section sec : ini.values())
187            {
188                assertNotNull(ini.getComment(sec.getName()));
189            }
190        }
191    
192        @Test public void testWithoutComment() throws Exception
193        {
194            Ini ini = new Ini();
195            Config cfg = new Config();
196    
197            cfg.setComment(false);
198            ini.setConfig(cfg);
199            ini.load(Helper.getResourceStream(Helper.DWARFS_INI));
200            assertNull(ini.getComment());
201            for (Ini.Section sec : ini.values())
202            {
203                assertNull(ini.getComment(sec.getName()));
204            }
205    
206            ini = new Ini();
207            ini.setConfig(cfg);
208            ini.setComment("comment");
209            Ini.Section sec = ini.add("section");
210    
211            sec.add("option", "value");
212            ini.putComment("section", "section-comment");
213            StringWriter writer = new StringWriter();
214    
215            ini.store(writer);
216            assertEquals("[section]\noption = value\n\n", writer.toString());
217        }
218    
219        @Test public void testWithoutHeaderComment() throws Exception
220        {
221            Ini ini = new Ini();
222            Config cfg = new Config();
223    
224            cfg.setHeaderComment(false);
225            cfg.setComment(true);
226            ini.setConfig(cfg);
227            ini.load(Helper.getResourceStream(Helper.DWARFS_INI));
228            assertNull(ini.getComment());
229            for (Ini.Section sec : ini.values())
230            {
231                assertNotNull(ini.getComment(sec.getName()));
232            }
233    
234            ini = new Ini();
235            ini.setConfig(cfg);
236            ini.setComment("comment");
237            Ini.Section sec = ini.add("section");
238    
239            sec.add("option", "value");
240            ini.putComment("section", "section-comment");
241            StringWriter writer = new StringWriter();
242    
243            ini.store(writer);
244            assertEquals("#section-comment\n[section]\noption = value\n\n", writer.toString());
245        }
246    }