View Javadoc

1   /*
2    * Copyright 2005,2009 Ivan SZKIBA
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.ini4j;
17  
18  import org.ini4j.sample.Dwarfs;
19  
20  import org.ini4j.test.DwarfsData;
21  import org.ini4j.test.Helper;
22  
23  import static org.junit.Assert.assertEquals;
24  
25  import org.junit.Test;
26  
27  import java.io.ByteArrayInputStream;
28  import java.io.ByteArrayOutputStream;
29  import java.io.File;
30  import java.io.FileNotFoundException;
31  import java.io.InputStreamReader;
32  import java.io.OutputStreamWriter;
33  import java.io.StringReader;
34  import java.io.StringWriter;
35  
36  public class IniTest extends Ini4jCase
37  {
38      private static final String COMMENT_ONLY = "# first line\n# second line\n";
39      private static final String COMMENT_ONLY_VALUE = " first line\n second line";
40      private static final String INI_ONE_HEADER = COMMENT_ONLY + "\n\n[section]\nkey=value\n";
41      private static final String COMMENTED_OPTION = COMMENT_ONLY + "\n\n[section]\n;comment\nkey=value\n";
42      private static final String MULTI = "[section]\noption=value\noption=value2\n[section]\noption=value3\noption=value4\noption=value5\n";
43  
44      @Test public void testCommentedOption() throws Exception
45      {
46          Ini ini = new Ini(new StringReader(COMMENTED_OPTION));
47  
48          assertEquals("comment", ini.get("section").getComment("key"));
49      }
50  
51      @Test public void testCommentOnly() throws Exception
52      {
53          Ini ini = new Ini(new StringReader(COMMENT_ONLY));
54  
55          assertEquals(COMMENT_ONLY_VALUE, ini.getComment());
56      }
57  
58      @Test public void testLoad() throws Exception
59      {
60          Ini ini;
61  
62          ini = new Ini(Helper.getResourceURL(Helper.DWARFS_INI));
63          Helper.assertEquals(DwarfsData.dwarfs, ini.as(Dwarfs.class));
64          ini = new Ini(Helper.getResourceStream(Helper.DWARFS_INI));
65          Helper.assertEquals(DwarfsData.dwarfs, ini.as(Dwarfs.class));
66          ini = new Ini(Helper.getResourceReader(Helper.DWARFS_INI));
67          Helper.assertEquals(DwarfsData.dwarfs, ini.as(Dwarfs.class));
68          ini = new Ini(Helper.getSourceFile(Helper.DWARFS_INI));
69          Helper.assertEquals(DwarfsData.dwarfs, ini.as(Dwarfs.class));
70          ini = new Ini();
71          ini.setFile(Helper.getSourceFile(Helper.DWARFS_INI));
72          ini.load();
73          Helper.assertEquals(DwarfsData.dwarfs, ini.as(Dwarfs.class));
74      }
75  
76      @Test public void testLoadException() throws Exception
77      {
78          Ini ini = new Ini();
79  
80          try
81          {
82              ini.load();
83              missing(FileNotFoundException.class);
84          }
85          catch (FileNotFoundException x)
86          {
87              //
88          }
89      }
90  
91      @Test public void testMulti() throws Exception
92      {
93          Ini ini = new Ini(new StringReader(MULTI));
94          Ini.Section sec;
95  
96          assertEquals(1, ini.length("section"));
97          assertEquals(5, ini.get("section", 0).length("option"));
98          ini.clear();
99          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 }