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.spi;
17  
18  import org.ini4j.Config;
19  import org.ini4j.Ini;
20  import org.ini4j.Ini4jCase;
21  import org.ini4j.InvalidFileFormatException;
22  
23  import org.junit.Test;
24  
25  import java.nio.charset.Charset;
26  
27  public class UnicodeInputStreamReaderTest extends Ini4jCase
28  {
29      @Test public void _testUTF32BE() throws Exception
30      {
31          test("UTF-32BE.ini", "UTF-32BE");
32      }
33  
34      @Test public void _testUTF32BE_BOM() throws Exception
35      {
36          test("UTF-32BE-BOM.ini", null);
37          test("UTF-32BE-BOM.ini", "UTF-8");
38          test("UTF-32BE-BOM.ini", "UTF-16");
39      }
40  
41      @Test public void _testUTF32BE_fail() throws Exception
42      {
43          try
44          {
45              test("UTF-32BE.ini", "ISO-8859-1");
46              missing(IllegalStateException.class);
47          }
48          catch (IllegalStateException x)
49          {
50              //
51          }
52      }
53  
54      @Test public void _testUTF32LE() throws Exception
55      {
56          test("UTF-32LE.ini", "UTF-32LE");
57      }
58  
59      @Test public void _testUTF32LE_BOM() throws Exception
60      {
61          test("UTF-32LE-BOM.ini", null);
62          test("UTF-32LE-BOM.ini", "UTF-8");
63          test("UTF-32LE-BOM.ini", "UTF-16");
64      }
65  
66      @Test public void _testUTF32LE_fail() throws Exception
67      {
68          try
69          {
70              test("UTF-32LE.ini", "ISO-8859-1");
71              missing(IllegalStateException.class);
72          }
73          catch (IllegalStateException x)
74          {
75              //
76          }
77      }
78  
79      @Test public void t_e_s_tUTF16BE_fail() throws Exception
80      {
81          try
82          {
83              test("UTF-16BE.ini", "ISO-8859-1");
84              missing(IllegalStateException.class);
85          }
86          catch (IllegalStateException x)
87          {
88              //
89          }
90      }
91  
92      @Test public void t_e_s_tUTF16LE_fail() throws Exception
93      {
94          try
95          {
96              test("UTF-16LE.ini", "ISO-8859-1");
97              missing(IllegalStateException.class);
98          }
99          catch (IllegalStateException x)
100         {
101             //
102         }
103     }
104 
105     @Test public void testUTF16BE() throws Exception
106     {
107         test("UTF-16BE.ini", "UTF-16BE");
108     }
109 
110     @Test public void testUTF16BE_BOM() throws Exception
111     {
112         test("UTF-16BE-BOM.ini", null);
113         test("UTF-16BE-BOM.ini", "UTF-8");
114         test("UTF-16BE-BOM.ini", "UTF-16");
115     }
116 
117     @Test public void testUTF16LE() throws Exception
118     {
119         test("UTF-16LE.ini", "UTF-16LE");
120     }
121 
122     @Test public void testUTF16LE_BOM() throws Exception
123     {
124         test("UTF-16LE-BOM.ini", null);
125         test("UTF-16LE-BOM.ini", "UTF-8");
126         test("UTF-16LE-BOM.ini", "UTF-16");
127     }
128 
129     @Test public void testUTF8() throws Exception
130     {
131         test("UTF-8.ini", null);
132         test("UTF-8.ini", "UTF-8");
133     }
134 
135     @Test public void testUTF8_BOM() throws Exception
136     {
137         test("UTF-8-BOM.ini", null);
138         test("UTF-8-BOM.ini", "UTF-8");
139         test("UTF-8-BOM.ini", "UTF-16");
140     }
141 
142     @Test public void testUTF8_fail() throws Exception
143     {
144         try
145         {
146             test("UTF-8.ini", "UTF-16");
147             missing(InvalidFileFormatException.class);
148         }
149         catch (InvalidFileFormatException x)
150         {
151             //
152         }
153     }
154 
155     private UnicodeInputStreamReader instantiate(String filename, String defaultEncoding)
156     {
157         Charset charset = (defaultEncoding == null) ? Charset.defaultCharset() : Charset.forName(defaultEncoding);
158 
159         return new UnicodeInputStreamReader(getClass().getResourceAsStream(filename), charset);
160     }
161 
162     private void test(String filename, String defaultEncoding) throws Exception
163     {
164         Charset charset = (defaultEncoding == null) ? Config.DEFAULT_FILE_ENCODING : Charset.forName(defaultEncoding);
165         UnicodeInputStreamReader reader = new UnicodeInputStreamReader(getClass().getResourceAsStream(filename), charset);
166         Ini ini = new Ini();
167 
168         ini.setConfig(Config.getGlobal().clone());
169         ini.getConfig().setFileEncoding(charset);
170         ini.load(reader);
171         Ini.Section sec = ini.get("section");
172 
173         if (sec == null)
174         {
175             throw new IllegalStateException("Missing section: section");
176         }
177 
178         if (!"value".equals(sec.get("option")))
179         {
180             throw new IllegalStateException("Missing option: option");
181         }
182     }
183 }