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.Dwarf;
19  
20  import org.ini4j.test.DwarfsData;
21  import org.ini4j.test.Helper;
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertFalse;
25  import static org.junit.Assert.assertNull;
26  import static org.junit.Assert.assertSame;
27  import static org.junit.Assert.assertTrue;
28  import static org.junit.Assert.fail;
29  
30  import org.junit.Test;
31  
32  import java.io.ByteArrayInputStream;
33  import java.io.ByteArrayOutputStream;
34  import java.io.File;
35  import java.io.FileNotFoundException;
36  import java.io.InputStreamReader;
37  import java.io.OutputStreamWriter;
38  import java.io.StringReader;
39  
40  public class OptionsTest extends Ini4jCase
41  {
42      private static final String[] _badOptions = { "=value\n", "\\u000d\\u000d=value\n" };
43      private static final String COMMENT_ONLY = "# first line\n# second line\n";
44      private static final String COMMENT_ONLY_VALUE = " first line\n second line";
45      private static final String OPTIONS_ONE_HEADER = COMMENT_ONLY + "\n\nkey=value\n";
46      private static final String MULTI = "option=value\noption=value2\noption=value3\noption=value4\noption=value5\n";
47  
48      @Test public void testCommentOnly() throws Exception
49      {
50          Options opt = new Options(new StringReader(COMMENT_ONLY));
51  
52          assertEquals(COMMENT_ONLY_VALUE, opt.getComment());
53      }
54  
55      @Test public void testConfig()
56      {
57          Options opts = new Options();
58          Config conf = opts.getConfig();
59  
60          assertTrue(conf.isEmptyOption());
61          assertTrue(conf.isEscape());
62          assertFalse(conf.isInclude());
63          assertTrue(conf.isMultiOption());
64          conf = new Config();
65          opts.setConfig(conf);
66          assertSame(conf, opts.getConfig());
67      }
68  
69      @Test public void testDwarfs() throws Exception
70      {
71          ByteArrayOutputStream buffer = new ByteArrayOutputStream();
72          Options happy = new Options();
73  
74          happy.from(DwarfsData.happy);
75          happy.store(buffer);
76          Options dup = new Options(new ByteArrayInputStream(buffer.toByteArray()));
77  
78          Helper.assertEquals(DwarfsData.happy, dup.as(Dwarf.class));
79          buffer = new ByteArrayOutputStream();
80          happy.store(new OutputStreamWriter(buffer));
81          dup = new Options(new ByteArrayInputStream(buffer.toByteArray()));
82          Helper.assertEquals(DwarfsData.happy, dup.as(Dwarf.class));
83          File file = File.createTempFile("test", ".opt");
84  
85          file.deleteOnExit();
86          happy.setFile(file);
87          happy.store();
88          dup = new Options();
89          dup.setFile(file);
90          assertEquals(file, dup.getFile());
91          dup.load();
92          Helper.assertEquals(DwarfsData.happy, dup.as(Dwarf.class));
93          file.delete();
94      }
95  
96      @Test public void testLoad() throws Exception
97      {
98          Options o1 = new Options(Helper.getResourceURL(Helper.DWARFS_OPT));
99          Options o2 = new Options(Helper.getResourceURL(Helper.DWARFS_OPT).openStream());
100         Options o3 = new Options(new InputStreamReader(Helper.getResourceURL(Helper.DWARFS_OPT).openStream()));
101         Options o4 = new Options(Helper.getResourceURL(Helper.DWARFS_OPT));
102         Options o5 = new Options(Helper.getSourceFile(Helper.DWARFS_OPT));
103         Options o6 = new Options();
104 
105         o6.setFile(Helper.getSourceFile(Helper.DWARFS_OPT));
106         o6.load();
107         Helper.assertEquals(DwarfsData.dopey, o1.as(Dwarf.class));
108         Helper.assertEquals(DwarfsData.dopey, o2.as(Dwarf.class));
109         Helper.assertEquals(DwarfsData.dopey, o3.as(Dwarf.class));
110         Helper.assertEquals(DwarfsData.dopey, o4.as(Dwarf.class));
111         Helper.assertEquals(DwarfsData.dopey, o5.as(Dwarf.class));
112         Helper.assertEquals(DwarfsData.dopey, o6.as(Dwarf.class));
113     }
114 
115     @Test public void testLoadException() throws Exception
116     {
117         Options opt = new Options();
118 
119         try
120         {
121             opt.load();
122             missing(FileNotFoundException.class);
123         }
124         catch (FileNotFoundException x)
125         {
126             //
127         }
128     }
129 
130     @Test public void testLowerCase() throws Exception
131     {
132         Config cfg = new Config();
133         Options opts = new Options();
134 
135         cfg.setLowerCaseOption(true);
136         opts.setConfig(cfg);
137         opts.load(new StringReader("OptIon=value\n"));
138         assertTrue(opts.containsKey("option"));
139     }
140 
141     @Test public void testMultiOption() throws Exception
142     {
143         Options opts = new Options(new StringReader(MULTI));
144 
145         assertEquals(5, opts.length("option"));
146         opts.clear();
147         Config cfg = new Config();
148 
149         cfg.setMultiOption(false);
150         opts.setConfig(cfg);
151         opts.load(new StringReader(MULTI));
152         assertEquals(1, opts.length("option"));
153     }
154 
155     @Test public void testNoEmptyOption() throws Exception
156     {
157         Config cfg = new Config();
158         Options opts = new Options();
159 
160         opts.setConfig(cfg);
161         try
162         {
163             opts.load(new StringReader("foo\n"));
164             missing(InvalidFileFormatException.class);
165         }
166         catch (InvalidFileFormatException x)
167         {
168             //
169         }
170 
171         cfg.setEmptyOption(true);
172         opts.load(new StringReader("dummy\n"));
173         assertTrue(opts.containsKey("dummy"));
174         assertNull(opts.get("dummy"));
175     }
176 
177     @Test public void testOneHeaderOnly() throws Exception
178     {
179         Options opt = new Options(new StringReader(OPTIONS_ONE_HEADER));
180 
181         assertEquals(COMMENT_ONLY_VALUE, opt.getComment());
182     }
183 
184     @Test
185     @SuppressWarnings("empty-statement")
186     public void testParseError() throws Exception
187     {
188         for (String s : _badOptions)
189         {
190             try
191             {
192                 new Options(new ByteArrayInputStream(s.getBytes()));
193                 fail("expected InvalidIniFormatException: " + s);
194             }
195             catch (InvalidFileFormatException x)
196             {
197                 ;
198             }
199         }
200     }
201 
202     @Test public void testStoreException() throws Exception
203     {
204         Options opt = new Options();
205 
206         try
207         {
208             opt.store();
209             missing(FileNotFoundException.class);
210         }
211         catch (FileNotFoundException x)
212         {
213             //
214         }
215     }
216 
217     @Test public void testWithComment() throws Exception
218     {
219         Options opts = new Options();
220 
221         opts.load(Helper.getResourceStream(Helper.DWARFS_OPT));
222         assertNotNull(opts.getComment());
223     }
224 
225     @Test public void testWithoutComment() throws Exception
226     {
227         Options opts = new Options();
228         Config cfg = new Config();
229 
230         cfg.setComment(false);
231         opts.setConfig(cfg);
232         opts.load(Helper.getResourceStream(Helper.DWARFS_OPT));
233         assertNull(opts.getComment());
234     }
235 
236     @Test public void testWithoutHeaderComment() throws Exception
237     {
238         Options opts = new Options();
239         Config cfg = new Config();
240 
241         cfg.setComment(true);
242         cfg.setHeaderComment(false);
243         opts.setConfig(cfg);
244         opts.load(Helper.getResourceStream(Helper.DWARFS_OPT));
245         assertNull(opts.getComment());
246     }
247 }