4 package jp.sourceforge.mikutoga.parser;
6 import java.io.ByteArrayInputStream;
7 import java.io.InputStream;
8 import java.nio.CharBuffer;
9 import java.nio.charset.Charset;
10 import java.util.ArrayList;
11 import java.util.List;
12 import org.junit.After;
13 import org.junit.AfterClass;
14 import org.junit.Before;
15 import org.junit.BeforeClass;
16 import org.junit.Test;
17 import static org.junit.Assert.*;
22 public class TextDecoderTest {
24 private static final Charset CS_WIN31J = Charset.forName("windows-31j");
25 private static final Charset CS_UTF8 = Charset.forName("UTF-8");
26 private static final Charset CS_UTF16LE = Charset.forName("UTF-16LE");
28 public TextDecoderTest() {
32 public static void setUpClass() throws Exception {
36 public static void tearDownClass() throws Exception {
44 public void tearDown() {
47 public static byte[] byteArray(CharSequence seq){
50 List<Byte> byteList = new ArrayList<Byte>();
52 int length = seq.length();
53 for(int pos = 0; pos < length; pos++){
56 char ch = seq.charAt(pos);
58 if('0' <= ch && ch <= '9'){
60 }else if('a' <= ch && ch <= 'f'){
62 }else if('A' <= ch && ch <= 'F'){
69 if(pos >= length) break;
74 if('0' <= ch && ch <= '9'){
76 }else if('a' <= ch && ch <= 'f'){
78 }else if('A' <= ch && ch <= 'F'){
84 byteList.add((byte)val);
87 result = new byte[byteList.size()];
89 for(int pos = 0; pos < result.length; pos++){
90 result[pos] = byteList.get(pos);
97 * Test of prepareBuffer method, of class TextDecoder.
100 public void testPrepareBuffer() {
101 System.out.println("prepareBuffer");
106 * Test of setChopMode, getChopMode method, of class TextDecoder.
109 public void testChopMode() throws Exception {
110 System.out.println("chopMode");
114 decoder = new TextDecoder(CS_WIN31J);
115 assertFalse(decoder.isZeroChopMode());
117 decoder.setZeroChopMode(true);
118 assertTrue(decoder.isZeroChopMode());
120 decoder.setZeroChopMode(false);
121 assertFalse(decoder.isZeroChopMode());
127 * Test of parseString method, of class TextDecoder.
130 public void testParseStringChop() throws Exception {
131 System.out.println("parseString(Chop)");
139 decoder = new TextDecoder(CS_WIN31J);
140 decoder.setZeroChopMode(true);
142 assertDecoded("41:42:00", "AB", decoder);
143 assertDecoded("41:00:42", "A", decoder);
144 assertDecoded("00:41:42", "", decoder);
145 assertDecoded("41:00:88", "A", decoder);
147 bdata = byteArray("41:00:42:43");
148 istream = new ByteArrayInputStream(bdata);
149 source = new MmdSource(istream);
150 cb =decoder.parseString(source, 3);
151 assertEquals("A", cb.toString());
152 cb =decoder.parseString(source, 1);
153 assertEquals("C", cb.toString());
159 * Test of parseString method, of class TextDecoder.
162 public void testParseStringWin31J() throws Exception {
163 System.out.println("parseString(Win31J)");
167 decoder = new TextDecoder(CS_WIN31J);
169 assertDecoded("41:42", "AB", decoder);
170 assertDecoded("41:42", "A", decoder, 1);
171 assertDecoded("88:9F", "亜", decoder);
172 assertDecoded("88:9F:88:A0", "亜唖", decoder);
173 assertDecoded("88:9F:41:88:A0", "亜A唖", decoder);
174 assertDecoded("00", "\u0000", decoder);
176 assertFormatError("88:9F:88:A0", decoder, 3);
184 bdata = byteArray("88:9F:88:A0");
185 istream = new ByteArrayInputStream(bdata);
186 source = new MmdSource(istream);
188 cb =decoder.parseString(source, 5);
190 }catch(MmdEofException e){
198 * Test of parseString method, of class TextDecoder.
201 public void testParseStringUTF8() throws Exception {
202 System.out.println("parseString(UTF8)");
206 decoder = new TextDecoder(CS_UTF8);
208 assertDecoded("41:42", "AB", decoder);
209 assertDecoded("41:42", "A", decoder, 1);
210 assertDecoded("E4:BA:9C", "亜", decoder);
211 assertDecoded("E4:BA:9C:E5:94:96", "亜唖", decoder);
212 assertDecoded("E4:BA:9C:41:E5:94:96", "亜A唖", decoder);
213 assertDecoded("00", "\u0000", decoder);
214 assertDecoded("EF:BF:BF", "\uffff", decoder);
216 assertFormatError("E4:BA:9C:E5:94:96", decoder, 5);
224 bdata = byteArray("E4:BA:9C:E5:94:96");
225 istream = new ByteArrayInputStream(bdata);
226 source = new MmdSource(istream);
228 cb =decoder.parseString(source, 7);
230 }catch(MmdEofException e){
238 * Test of parseString method, of class TextDecoder.
241 public void testParseStringUTF16LE() throws Exception {
242 System.out.println("parseString(UTF16LE)");
246 decoder = new TextDecoder(CS_UTF16LE);
248 assertDecoded("41:00:42:00", "AB", decoder);
249 assertDecoded("41:00:42:00", "A", decoder, 2);
250 assertDecoded("9C:4E", "亜", decoder);
251 assertDecoded("9C:4E:16:55", "亜唖", decoder);
252 assertDecoded("9C:4E:41:00:16:55", "亜A唖", decoder);
253 assertDecoded("00:00", "\u0000", decoder);
254 assertDecoded("FF:FF", "\uffff", decoder);
256 assertDecoded("60:08", "\u0860", decoder);
258 assertDecoded("FF:FE:9C:4E", "\ufeff亜", decoder);
259 // not BOM, ZERO WIDTH NO-BREAK SPACE
261 assertFormatError("9C:4E:16:55", decoder, 3);
268 bdata = byteArray("9C:4E:16:55");
269 istream = new ByteArrayInputStream(bdata);
270 source = new MmdSource(istream);
272 cb =decoder.parseString(source, 5);
274 }catch(MmdEofException e){
282 * Test of Yen(U+00A5) & Backslash(U+005C) encoding, of class TextDecoder.
285 public void testYenAndBackslash() throws Exception {
286 System.out.println("Yen & Backslash");
290 decoder = new TextDecoder(CS_WIN31J);
291 assertDecoded("5C", "\u005c\u005c", decoder);
293 decoder = new TextDecoder(CS_UTF8);
294 assertDecoded("5C", "\u005c\u005c", decoder);
295 assertDecoded("C2:A5", "\u00a5", decoder);
297 decoder = new TextDecoder(CS_UTF16LE);
298 assertDecoded("5C:00", "\u005c\u005c", decoder);
299 assertDecoded("A5:00", "\u00a5", decoder);
305 * Test of unmapped char, of class TextDecoder.
308 public void testUnmapChar() throws Exception {
309 System.out.println("unmap char");
313 decoder = new TextDecoder(CS_WIN31J);
314 assertFormatError("FF:FF", decoder, 2);
317 // Unicode2.0の時点でU+0860は未定義文字
319 decoder = new TextDecoder(CS_UTF8);
320 assertFormatError("FF:FF:FF", decoder, 3);
321 assertDecoded("E0:A1:A0", "\u0860", decoder);
323 decoder = new TextDecoder(CS_UTF16LE);
324 assertDecoded("60:08", "\u0860", decoder);
329 public void assertDecoded(String bin, String desired,
332 byte[] bdata = byteArray(bin);
333 assertDecoded(bin, desired, decoder, bdata.length);
337 public void assertDecoded(String bin, String desired,
338 TextDecoder decoder, int len)
345 bdata = byteArray(bin);
346 istream = new ByteArrayInputStream(bdata);
347 source = new MmdSource(istream);
349 assertDecoded(source, desired, decoder, len);
354 public void assertDecoded(MmdSource source, String desired,
355 TextDecoder decoder, int len)
358 cb =decoder.parseString(source, len);
359 assertEquals(desired, cb.toString());
363 public void assertFormatError(String bin,
364 TextDecoder decoder, int len)
370 bdata = byteArray(bin);
371 istream = new ByteArrayInputStream(bdata);
372 source = new MmdSource(istream);
375 decoder.parseString(source, len);
377 }catch(MmdFormatException e){