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.Dwarf;
019    
020    import org.ini4j.test.DwarfsData;
021    import org.ini4j.test.Helper;
022    
023    import static org.junit.Assert.assertEquals;
024    import static org.junit.Assert.assertFalse;
025    import static org.junit.Assert.assertNull;
026    import static org.junit.Assert.assertSame;
027    import static org.junit.Assert.assertTrue;
028    import static org.junit.Assert.fail;
029    
030    import org.junit.Test;
031    
032    import java.io.ByteArrayInputStream;
033    import java.io.ByteArrayOutputStream;
034    import java.io.File;
035    import java.io.FileNotFoundException;
036    import java.io.InputStreamReader;
037    import java.io.OutputStreamWriter;
038    import java.io.StringReader;
039    
040    public class OptionsTest extends Ini4jCase
041    {
042        private static final String[] _badOptions = { "=value\n", "\\u000d\\u000d=value\n" };
043        private static final String COMMENT_ONLY = "# first line\n# second line\n";
044        private static final String COMMENT_ONLY_VALUE = " first line\n second line";
045        private static final String OPTIONS_ONE_HEADER = COMMENT_ONLY + "\n\nkey=value\n";
046        private static final String MULTI = "option=value\noption=value2\noption=value3\noption=value4\noption=value5\n";
047    
048        @Test public void testCommentOnly() throws Exception
049        {
050            Options opt = new Options(new StringReader(COMMENT_ONLY));
051    
052            assertEquals(COMMENT_ONLY_VALUE, opt.getComment());
053        }
054    
055        @Test public void testConfig()
056        {
057            Options opts = new Options();
058            Config conf = opts.getConfig();
059    
060            assertTrue(conf.isEmptyOption());
061            assertTrue(conf.isEscape());
062            assertFalse(conf.isInclude());
063            assertTrue(conf.isMultiOption());
064            conf = new Config();
065            opts.setConfig(conf);
066            assertSame(conf, opts.getConfig());
067        }
068    
069        @Test public void testDwarfs() throws Exception
070        {
071            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
072            Options happy = new Options();
073    
074            happy.from(DwarfsData.happy);
075            happy.store(buffer);
076            Options dup = new Options(new ByteArrayInputStream(buffer.toByteArray()));
077    
078            Helper.assertEquals(DwarfsData.happy, dup.as(Dwarf.class));
079            buffer = new ByteArrayOutputStream();
080            happy.store(new OutputStreamWriter(buffer));
081            dup = new Options(new ByteArrayInputStream(buffer.toByteArray()));
082            Helper.assertEquals(DwarfsData.happy, dup.as(Dwarf.class));
083            File file = File.createTempFile("test", ".opt");
084    
085            file.deleteOnExit();
086            happy.setFile(file);
087            happy.store();
088            dup = new Options();
089            dup.setFile(file);
090            assertEquals(file, dup.getFile());
091            dup.load();
092            Helper.assertEquals(DwarfsData.happy, dup.as(Dwarf.class));
093            file.delete();
094        }
095    
096        @Test public void testLoad() throws Exception
097        {
098            Options o1 = new Options(Helper.getResourceURL(Helper.DWARFS_OPT));
099            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    }