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.easymock.EasyMock;
19  
20  import static org.junit.Assert.*;
21  
22  import org.junit.Test;
23  
24  import java.io.ByteArrayInputStream;
25  
26  /**
27   * JUnit test of IniParser class.
28   */
29  public class IniParserTest
30  {
31      private static final String[] _badIni =
32          { "[section\noption=value\n", "[]\noption=value", "section\noption=value", "[section]\noption\n", "[section]\n=value\n", "[section]\n\\u000d\\u000d=value\n" };
33      private static final String[] _badXML =
34          { "[section\noption=value\n", "<?xml version='1.0' encoding='UTF-8'?>\n<ini></ini>", "<?xml version='1.0' encoding='UTF-8'?>\n<ini version='dummy'></ini>", "<?xml version='1.0' encoding='UTF-8'?>\n<ini version='1.0'><unknown-tag key='dummy'/></ini>", "<?xml version='1.0' encoding='UTF-8'?>\n<ini version='1.0'><section/></ini>", "<?xml version='1.0' encoding='UTF-8'?>\n<ini version='1.0'><section key='sec'><option/></section></ini>" };
35  
36      /**
37       * Test of newInstance method.
38       *
39       * @throws Exception on error
40       */
41      @Test public void testNewInstance() throws Exception
42      {
43          assertNotNull(IniParser.newInstance());
44      }
45  
46      /**
47       * Test of parse method.
48       *
49       * @throws Exception on error
50       */
51      @SuppressWarnings("empty-statement")
52      @Test public void testParse() throws Exception
53      {
54          IniParser parser = new IniParser();
55          IniHandler handler = EasyMock.createNiceMock(IniHandler.class);
56  
57          for (String s : _badIni)
58          {
59              try
60              {
61                  parser.parse(new ByteArrayInputStream(s.getBytes()), handler);
62                  fail("expected InvalidIniFormatException: " + s);
63              }
64              catch (InvalidIniFormatException x)
65              {
66                  ;
67              }
68          }
69      }
70  
71      /**
72       * Test of parseXML method.
73       *
74       * @throws Exception on error
75       */
76      @SuppressWarnings("empty-statement")
77      @Test public void testParseXML() throws Exception
78      {
79          IniParser parser = new IniParser();
80          IniHandler handler = EasyMock.createNiceMock(IniHandler.class);
81  
82          for (String s : _badXML)
83          {
84              try
85              {
86                  parser.parseXML(new ByteArrayInputStream(s.getBytes()), handler);
87                  fail("expected InvalidIniFormatException");
88              }
89              catch (InvalidIniFormatException x)
90              {
91                  ;
92              }
93          }
94      }
95  }