4 package jp.sfjp.mikutoga.bin.parser;
6 import java.nio.ByteBuffer;
7 import java.nio.charset.CharacterCodingException;
8 import java.nio.charset.Charset;
9 import java.util.ArrayList;
10 import java.util.List;
11 import org.junit.After;
12 import org.junit.AfterClass;
13 import org.junit.Before;
14 import org.junit.BeforeClass;
15 import org.junit.Test;
16 import static org.junit.Assert.*;
21 public class TextDecoderTest {
23 private static final Charset CS_WIN31J = Charset.forName("windows-31j");
24 private static final Charset CS_UTF8 = Charset.forName("UTF-8");
25 private static final Charset CS_UTF16LE = Charset.forName("UTF-16LE");
27 public TextDecoderTest() {
31 public static void setUpClass() throws Exception {
35 public static void tearDownClass() throws Exception {
43 public void tearDown() {
46 public static byte[] byteArray(CharSequence seq){
49 List<Byte> byteList = new ArrayList<Byte>();
51 int length = seq.length();
52 for(int pos = 0; pos < length; pos++){
55 char ch = seq.charAt(pos);
57 if('0' <= ch && ch <= '9'){
59 }else if('a' <= ch && ch <= 'f'){
61 }else if('A' <= ch && ch <= 'F'){
68 if(pos >= length) break;
73 if('0' <= ch && ch <= '9'){
75 }else if('a' <= ch && ch <= 'f'){
77 }else if('A' <= ch && ch <= 'F'){
83 byteList.add((byte)val);
86 result = new byte[byteList.size()];
88 for(int pos = 0; pos < result.length; pos++){
89 result[pos] = byteList.get(pos);
96 * Test of setChopMode, getChopMode method, of class TextDecoder.
100 public void testChopMode() throws Exception {
101 System.out.println("chopMode");
105 decoder = new TextDecoder(CS_WIN31J);
106 assertFalse(decoder.isZeroChopMode());
108 decoder.setZeroChopMode(true);
109 assertTrue(decoder.isZeroChopMode());
111 decoder.setZeroChopMode(false);
112 assertFalse(decoder.isZeroChopMode());
118 * Test of parseString method, of class TextDecoder.
122 public void testParseStringChop() throws Exception {
123 System.out.println("parseString(Chop)");
127 decoder = new TextDecoder(CS_WIN31J);
128 decoder.setZeroChopMode(true);
130 assertDecoded("41:42:00", "AB", decoder);
131 assertDecoded("41:00:42", "A", decoder);
132 assertDecoded("00:41:42", "", decoder);
133 assertDecoded("41:00:88", "A", decoder);
134 assertDecoded("", "", decoder);
136 decoder.setZeroChopMode(false);
137 assertDecoded("41:00:42", "A\u0000B", decoder);
138 assertDecoded("", "", decoder);
144 * Test of parseString method, of class TextDecoder.
148 public void testParseStringWin31J() throws Exception {
149 System.out.println("parseString(Win31J)");
153 decoder = new TextDecoder(CS_WIN31J);
155 assertDecoded("41:42", "AB", decoder);
156 assertDecoded("41:42", "A", decoder, 1);
157 assertDecoded("88:9F", "亜", decoder);
158 assertDecoded("88:9F:88:A0", "亜唖", decoder);
159 assertDecoded("88:9F:41:88:A0", "亜A唖", decoder);
160 assertDecoded("00", "\u0000", decoder);
162 assertFormatError("88:9F:88:A0", decoder, 3);
168 * Test of parseString method, of class TextDecoder.
172 public void testParseStringUTF8() throws Exception {
173 System.out.println("parseString(UTF8)");
177 decoder = new TextDecoder(CS_UTF8);
179 assertDecoded("41:42", "AB", decoder);
180 assertDecoded("41:42", "A", decoder, 1);
181 assertDecoded("E4:BA:9C", "亜", decoder);
182 assertDecoded("E4:BA:9C:E5:94:96", "亜唖", decoder);
183 assertDecoded("E4:BA:9C:41:E5:94:96", "亜A唖", decoder);
184 assertDecoded("00", "\u0000", decoder);
185 assertDecoded("EF:BF:BF", "\uffff", decoder);
187 assertFormatError("E4:BA:9C:E5:94:96", decoder, 5);
193 * Test of parseString method, of class TextDecoder.
197 public void testParseStringUTF16LE() throws Exception {
198 System.out.println("parseString(UTF16LE)");
202 decoder = new TextDecoder(CS_UTF16LE);
204 assertDecoded("41:00:42:00", "AB", decoder);
205 assertDecoded("41:00:42:00", "A", decoder, 2);
206 assertDecoded("9C:4E", "亜", decoder);
207 assertDecoded("9C:4E:16:55", "亜唖", decoder);
208 assertDecoded("9C:4E:41:00:16:55", "亜A唖", decoder);
209 assertDecoded("00:00", "\u0000", decoder);
210 assertDecoded("FF:FF", "\uffff", decoder);
212 assertDecoded("60:08", "\u0860", decoder);
214 assertDecoded("FF:FE:9C:4E", "\ufeff亜", decoder);
215 // not BOM, ZERO WIDTH NO-BREAK SPACE
217 assertFormatError("9C:4E:16:55", decoder, 3);
223 * Test of Yen(U+00A5) & Backslash(U+005C) encoding, of class TextDecoder.
227 public void testYenAndBackslash() throws Exception {
228 System.out.println("Yen & Backslash");
232 decoder = new TextDecoder(CS_WIN31J);
233 assertDecoded("5C", "\u005c\u005c", decoder);
235 decoder = new TextDecoder(CS_UTF8);
236 assertDecoded("5C", "\u005c\u005c", decoder);
237 assertDecoded("C2:A5", "\u00a5", decoder);
239 decoder = new TextDecoder(CS_UTF16LE);
240 assertDecoded("5C:00", "\u005c\u005c", decoder);
241 assertDecoded("A5:00", "\u00a5", decoder);
247 * Test of unmapped char, of class TextDecoder.
251 public void testUnmapChar() throws Exception {
252 System.out.println("unmap char");
256 decoder = new TextDecoder(CS_WIN31J);
257 assertFormatError("FF:FF", decoder, 2);
260 // Unicode2.0の時点でU+0860は未定義文字
262 decoder = new TextDecoder(CS_UTF8);
263 assertFormatError("FF:FF:FF", decoder, 3);
264 assertDecoded("E0:A1:A0", "\u0860", decoder);
266 decoder = new TextDecoder(CS_UTF16LE);
267 assertDecoded("60:08", "\u0860", decoder);
272 public void assertDecoded(String bin, String desired,
275 byte[] bdata = byteArray(bin);
276 ByteBuffer bBuf = ByteBuffer.wrap(bdata);
277 assertDecoded(bBuf, desired, decoder);
281 public void assertDecoded(String bin, String desired,
282 TextDecoder decoder, int len)
284 byte[] bdata = byteArray(bin);
285 ByteBuffer bBuf = ByteBuffer.wrap(bdata, 0, len);
286 assertDecoded(bBuf, desired, decoder);
290 public void assertDecoded(ByteBuffer bBuf, String desired,
294 result = decoder.decode(bBuf);
296 assertEquals(desired, result);
301 public void assertFormatError(String bin,
302 TextDecoder decoder, int len)
304 byte[] bdata = byteArray(bin);
305 ByteBuffer bBuf = ByteBuffer.wrap(bdata, 0, len);
308 decoder.decode(bBuf);
310 }catch(CharacterCodingException e){