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.addon;
17  
18  import org.easymock.EasyMock;
19  
20  import org.ini4j.Config;
21  import org.ini4j.DwarfsData;
22  import org.ini4j.Helper;
23  import org.ini4j.Ini;
24  import org.ini4j.IniHandler;
25  import org.ini4j.IniParser;
26  import org.ini4j.InvalidIniFormatException;
27  
28  import org.ini4j.sample.Dwarfs;
29  
30  import org.junit.AfterClass;
31  
32  import static org.junit.Assert.*;
33  
34  import org.junit.Test;
35  
36  import java.io.ByteArrayInputStream;
37  
38  import java.net.MalformedURLException;
39  
40  @SuppressWarnings("deprecation")
41  public class FancyIniParserTest
42  {
43      private static final String[] _extraIni = { "[section]\noption\n", "[]\noption=value\n", "option=value\n" };
44      private static final String[] _badIni = { "[section\noption=value\n", "[section]\n=value\n" };
45      private static final String MIXEDCASE = "[SectioN]\n\nOptioN=ValuE\n";
46      private static final String MORESECTIONS = "[Section]\noption=value\n\n[second]\noption=value\n";
47      private static final String UNNAMED = "option=value\n";
48      private static final String ANONYMOUS = "?";
49      private static final String EMPTY = "";
50      private static final String INCLUDE = "org/ini4j/addon/dwarfs-include.ini";
51      private static final String NESTED = "org/ini4j/addon/dwarfs-nested.ini";
52  
53      @AfterClass public static void tearDownClass() throws Exception
54      {
55          Helper.resetConfig();
56      }
57  
58      @Test public void testConvertCase() throws Exception
59      {
60          class Handler implements IniHandler
61          {
62              boolean sectionOK;
63              boolean optionOK;
64  
65              @SuppressWarnings("empty-statement")
66              @Override public void startIni()
67              {
68                  ;
69              }
70  
71              @Override
72              @SuppressWarnings("empty-statement")
73              public void endIni()
74              {
75                  ;
76              }
77  
78              @Override public void handleOption(String name, String value)
79              {
80                  assertEquals(name.toLowerCase(), name);
81                  assertFalse(value.equals(value.toLowerCase()));
82                  optionOK = true;
83              }
84  
85              @Override public void startSection(String sectionName)
86              {
87                  assertEquals(sectionName.toLowerCase(), sectionName);
88                  sectionOK = true;
89              }
90  
91              @Override
92              @SuppressWarnings("empty-statement")
93              public void endSection()
94              {
95                  ;
96              }
97          }
98  
99          FancyIniParser parser = new FancyIniParser();
100 
101         parser.setAllowSectionCaseConversion(true);
102         parser.setAllowOptionCaseConversion(true);
103         Handler handler = new Handler();
104 
105         parser.parse(new ByteArrayInputStream(MIXEDCASE.getBytes()), handler);
106         assertTrue(handler.sectionOK);
107         assertTrue(handler.optionOK);
108     }
109 
110     @Test public void testDefaults() throws Exception
111     {
112         FancyIniParser parser = new FancyIniParser();
113 
114         parser.setConfig(new Config());
115         assertTrue(parser.isAllowEmptyOption());
116         assertTrue(parser.isAllowMissingSection());
117         assertFalse(parser.isAllowOptionCaseConversion());
118         assertFalse(parser.isAllowSectionCaseConversion());
119         assertTrue(parser.isAllowUnnamedSection());
120     }
121 
122     @SuppressWarnings("empty-statement")
123     @Test public void testInclude() throws Exception
124     {
125         System.setProperty(IniParser.class.getName(), FancyIniParser.class.getName());
126         Ini ini = new Ini(getClass().getClassLoader().getResource(INCLUDE));
127 
128         Helper.assertEquals(DwarfsData.dwarfs, ini.to(Dwarfs.class));
129         try
130         {
131             ini = new Ini(getClass().getClassLoader().getResourceAsStream(INCLUDE));
132             fail();
133         }
134         catch (MalformedURLException x)
135         {
136             ;
137         }
138 
139         ini = new Ini(getClass().getClassLoader().getResource(NESTED));
140         Helper.assertEquals(DwarfsData.dwarfs, ini.to(Dwarfs.class));
141         FancyIniParser parser = (FancyIniParser) IniParser.newInstance();
142 
143         assertTrue(parser.isAllowInclude());
144         parser.setAllowInclude(false);
145         assertFalse(parser.isAllowInclude());
146     }
147 
148     /**
149      * Test of newInstance method.
150      *
151      * @throws Exception on error
152      */
153     @Test public void testNewInstance() throws Exception
154     {
155         System.setProperty(IniParser.class.getName(), FancyIniParser.class.getName());
156         FancyIniParser parser = (FancyIniParser) IniParser.newInstance();
157 
158         assertNotNull(parser);
159     }
160 
161     /**
162      * Test of parse method.
163      *
164      * @throws Exception on error
165      */
166     @SuppressWarnings("empty-statement")
167     @Test public void testParse() throws Exception
168     {
169         FancyIniParser parser = new FancyIniParser();
170         IniHandler handler = EasyMock.createNiceMock(IniHandler.class);
171 
172         // add-on extension should work by default
173         for (String s : _extraIni)
174         {
175             parser.parse(new ByteArrayInputStream(s.getBytes()), handler);
176         }
177 
178         // disable add-on features and expect error
179         parser.setAllowEmptyOption(false);
180         parser.setAllowMissingSection(false);
181         parser.setAllowUnnamedSection(false);
182         for (String s : _extraIni)
183         {
184             try
185             {
186                 parser.parse(new ByteArrayInputStream(s.getBytes()), handler);
187                 fail("expected InvalidIniFormatException: " + s);
188             }
189             catch (InvalidIniFormatException x)
190             {
191                 ;
192             }
193         }
194 
195         for (String s : _badIni)
196         {
197             try
198             {
199                 parser.parse(new ByteArrayInputStream(s.getBytes()), handler);
200                 fail("expected InvalidIniFormatException: " + s);
201             }
202             catch (InvalidIniFormatException x)
203             {
204                 ;
205             }
206         }
207 
208         parser.parse(new ByteArrayInputStream(EMPTY.getBytes()), handler);
209         parser.parse(new ByteArrayInputStream(MORESECTIONS.getBytes()), handler);
210         parser.setMissingSectionName(ANONYMOUS);
211         assertEquals(ANONYMOUS, parser.getMissingSectionName());
212         System.setProperty(IniParser.class.getName(), FancyIniParser.class.getName());
213         Ini ini = new Ini(new ByteArrayInputStream(UNNAMED.getBytes()));
214 
215         assertNotNull(ini);
216         assertNotNull(ini.get(ANONYMOUS));
217         assertEquals(1, ini.size());
218     }
219 }