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 */ 016package org.ini4j.spi; 017 018import org.ini4j.Config; 019import org.ini4j.Ini; 020import org.ini4j.Ini4jCase; 021import org.ini4j.InvalidFileFormatException; 022 023import org.junit.Test; 024 025import java.nio.charset.Charset; 026 027public class UnicodeInputStreamReaderTest extends Ini4jCase 028{ 029 @Test public void _testUTF32BE() throws Exception 030 { 031 test("UTF-32BE.ini", "UTF-32BE"); 032 } 033 034 @Test public void _testUTF32BE_BOM() throws Exception 035 { 036 test("UTF-32BE-BOM.ini", null); 037 test("UTF-32BE-BOM.ini", "UTF-8"); 038 test("UTF-32BE-BOM.ini", "UTF-16"); 039 } 040 041 @Test public void _testUTF32BE_fail() throws Exception 042 { 043 try 044 { 045 test("UTF-32BE.ini", "ISO-8859-1"); 046 missing(IllegalStateException.class); 047 } 048 catch (IllegalStateException x) 049 { 050 // 051 } 052 } 053 054 @Test public void _testUTF32LE() throws Exception 055 { 056 test("UTF-32LE.ini", "UTF-32LE"); 057 } 058 059 @Test public void _testUTF32LE_BOM() throws Exception 060 { 061 test("UTF-32LE-BOM.ini", null); 062 test("UTF-32LE-BOM.ini", "UTF-8"); 063 test("UTF-32LE-BOM.ini", "UTF-16"); 064 } 065 066 @Test public void _testUTF32LE_fail() throws Exception 067 { 068 try 069 { 070 test("UTF-32LE.ini", "ISO-8859-1"); 071 missing(IllegalStateException.class); 072 } 073 catch (IllegalStateException x) 074 { 075 // 076 } 077 } 078 079 @Test public void t_e_s_tUTF16BE_fail() throws Exception 080 { 081 try 082 { 083 test("UTF-16BE.ini", "ISO-8859-1"); 084 missing(IllegalStateException.class); 085 } 086 catch (IllegalStateException x) 087 { 088 // 089 } 090 } 091 092 @Test public void t_e_s_tUTF16LE_fail() throws Exception 093 { 094 try 095 { 096 test("UTF-16LE.ini", "ISO-8859-1"); 097 missing(IllegalStateException.class); 098 } 099 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}