OSDN Git Service

da4709aa8d5ca68c05a09a71860ab96024aab6c0
[mikutoga/TogaGem.git] / src / test / java / jp / sourceforge / mikutoga / parser / TextDecoderTest.java
1 /*
2  */
3
4 package jp.sourceforge.mikutoga.parser;
5
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.*;
18
19 /**
20  *
21  */
22 public class TextDecoderTest {
23
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");
27
28     public TextDecoderTest() {
29     }
30
31     @BeforeClass
32     public static void setUpClass() throws Exception {
33     }
34
35     @AfterClass
36     public static void tearDownClass() throws Exception {
37     }
38
39     @Before
40     public void setUp() {
41     }
42
43     @After
44     public void tearDown() {
45     }
46
47     public static byte[] byteArray(CharSequence seq){
48         byte[] result;
49
50         List<Byte> byteList = new ArrayList<Byte>();
51
52         int length = seq.length();
53         for(int pos = 0; pos < length; pos++){
54             int val = 0;
55
56             char ch = seq.charAt(pos);
57
58             if('0' <= ch && ch <= '9'){
59                 val += ch - '0';
60             }else if('a' <= ch && ch <= 'f'){
61                 val += ch - 'a' + 10;
62             }else if('A' <= ch && ch <= 'F'){
63                 val += ch - 'A' + 10;
64             }else{
65                 continue;
66             }
67
68             pos++;
69             if(pos >= length) break;
70
71             val *= 16;
72             ch = seq.charAt(pos);
73
74             if('0' <= ch && ch <= '9'){
75                 val += ch - '0';
76             }else if('a' <= ch && ch <= 'f'){
77                 val += ch - 'a' + 10;
78             }else if('A' <= ch && ch <= 'F'){
79                 val += ch - 'A' + 10;
80             }else{
81                 continue;
82             }
83
84             byteList.add((byte)val);
85         }
86
87         result = new byte[byteList.size()];
88
89         for(int pos = 0; pos < result.length; pos++){
90             result[pos] = byteList.get(pos);
91         }
92
93         return result;
94     }
95
96     /**
97      * Test of prepareBuffer method, of class TextDecoder.
98      */
99     @Test
100     public void testPrepareBuffer() {
101         System.out.println("prepareBuffer");
102         return;
103     }
104
105     /**
106      * Test of setChopMode, getChopMode method, of class TextDecoder.
107      */
108     @Test
109     public void testChopMode() throws Exception {
110         System.out.println("chopMode");
111
112         TextDecoder decoder;
113
114         decoder = new TextDecoder(CS_WIN31J);
115         assertFalse(decoder.isZeroChopMode());
116
117         decoder.setZeroChopMode(true);
118         assertTrue(decoder.isZeroChopMode());
119
120         decoder.setZeroChopMode(false);
121         assertFalse(decoder.isZeroChopMode());
122
123         return;
124     }
125
126     /**
127      * Test of parseString method, of class TextDecoder.
128      */
129     @Test
130     public void testParseStringChop() throws Exception {
131         System.out.println("parseString(Chop)");
132
133         TextDecoder decoder;
134         byte[] bdata;
135         InputStream istream;
136         MmdSource source;
137         CharBuffer cb;
138
139         decoder = new TextDecoder(CS_WIN31J);
140         decoder.setZeroChopMode(true);
141
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);
146
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());
154
155         return;
156     }
157
158     /**
159      * Test of parseString method, of class TextDecoder.
160      */
161     @Test
162     public void testParseStringWin31J() throws Exception {
163         System.out.println("parseString(Win31J)");
164
165         TextDecoder decoder;
166
167         decoder = new TextDecoder(CS_WIN31J);
168
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);
175
176         assertFormatError("88:9F:88:A0", decoder, 3);
177
178
179         byte[] bdata;
180         InputStream istream;
181         MmdSource source;
182         CharBuffer cb;
183
184         bdata = byteArray("88:9F:88:A0");
185         istream = new ByteArrayInputStream(bdata);
186         source = new MmdSource(istream);
187         try{
188             cb =decoder.parseString(source, 5);
189             fail();
190         }catch(MmdEofException e){
191             // OK
192         }
193
194         return;
195     }
196
197     /**
198      * Test of parseString method, of class TextDecoder.
199      */
200     @Test
201     public void testParseStringUTF8() throws Exception {
202         System.out.println("parseString(UTF8)");
203
204         TextDecoder decoder;
205
206         decoder = new TextDecoder(CS_UTF8);
207
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);
215
216         assertFormatError("E4:BA:9C:E5:94:96", decoder, 5);
217
218
219         byte[] bdata;
220         InputStream istream;
221         MmdSource source;
222         CharBuffer cb;
223
224         bdata = byteArray("E4:BA:9C:E5:94:96");
225         istream = new ByteArrayInputStream(bdata);
226         source = new MmdSource(istream);
227         try{
228             cb =decoder.parseString(source, 7);
229             fail();
230         }catch(MmdEofException e){
231             // OK
232         }
233
234         return;
235     }
236
237     /**
238      * Test of parseString method, of class TextDecoder.
239      */
240     @Test
241     public void testParseStringUTF16LE() throws Exception {
242         System.out.println("parseString(UTF16LE)");
243
244         TextDecoder decoder;
245
246         decoder = new TextDecoder(CS_UTF16LE);
247
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);
255
256         assertDecoded("60:08", "\u0860", decoder);
257
258         assertDecoded("FF:FE:9C:4E", "\ufeff亜", decoder);
259         // not BOM, ZERO WIDTH NO-BREAK SPACE
260
261         assertFormatError("9C:4E:16:55", decoder, 3);
262
263
264         byte[] bdata;
265         InputStream istream;
266         MmdSource source;
267         CharBuffer cb;
268         bdata = byteArray("9C:4E:16:55");
269         istream = new ByteArrayInputStream(bdata);
270         source = new MmdSource(istream);
271         try{
272             cb =decoder.parseString(source, 5);
273             fail();
274         }catch(MmdEofException e){
275             // OK
276         }
277
278         return;
279     }
280
281     /**
282      * Test of Yen(U+00A5) & Backslash(U+005C) encoding, of class TextDecoder.
283      */
284     @Test
285     public void testYenAndBackslash() throws Exception {
286         System.out.println("Yen & Backslash");
287
288         TextDecoder decoder;
289
290         decoder = new TextDecoder(CS_WIN31J);
291         assertDecoded("5C", "\u005c\u005c", decoder);
292
293         decoder = new TextDecoder(CS_UTF8);
294         assertDecoded("5C", "\u005c\u005c", decoder);
295         assertDecoded("C2:A5", "\u00a5", decoder);
296
297         decoder = new TextDecoder(CS_UTF16LE);
298         assertDecoded("5C:00", "\u005c\u005c", decoder);
299         assertDecoded("A5:00", "\u00a5", decoder);
300
301         return;
302     }
303
304     /**
305      * Test of unmapped char, of class TextDecoder.
306      */
307     @Test
308     public void testUnmapChar() throws Exception {
309         System.out.println("unmap char");
310
311         TextDecoder decoder;
312
313         decoder = new TextDecoder(CS_WIN31J);
314         assertFormatError("FF:FF", decoder, 2);
315
316
317         // Unicode2.0の時点でU+0860は未定義文字
318
319         decoder = new TextDecoder(CS_UTF8);
320         assertFormatError("FF:FF:FF", decoder, 3);
321         assertDecoded("E0:A1:A0", "\u0860", decoder);
322
323         decoder = new TextDecoder(CS_UTF16LE);
324         assertDecoded("60:08", "\u0860", decoder);
325
326         return;
327     }
328
329     public void assertDecoded(String bin, String desired,
330                                 TextDecoder decoder)
331             throws Exception{
332         byte[] bdata = byteArray(bin);
333         assertDecoded(bin, desired, decoder, bdata.length);
334         return;
335     }
336
337     public void assertDecoded(String bin, String desired,
338                                 TextDecoder decoder, int len)
339             throws Exception{
340         byte[] bdata;
341         InputStream istream;
342         MmdSource source;
343         CharBuffer cb;
344
345         bdata = byteArray(bin);
346         istream = new ByteArrayInputStream(bdata);
347         source = new MmdSource(istream);
348
349         assertDecoded(source, desired, decoder, len);
350
351         return;
352     }
353
354     public void assertDecoded(MmdSource source, String desired,
355                                 TextDecoder decoder, int len)
356             throws Exception{
357         CharBuffer cb;
358         cb =decoder.parseString(source, len);
359         assertEquals(desired, cb.toString());
360         return;
361     }
362
363     public void assertFormatError(String bin,
364                                     TextDecoder decoder, int len)
365             throws Exception{
366         byte[] bdata;
367         InputStream istream;
368         MmdSource source;
369
370         bdata = byteArray(bin);
371         istream = new ByteArrayInputStream(bdata);
372         source = new MmdSource(istream);
373
374         try{
375             decoder.parseString(source, len);
376             fail();
377         }catch(MmdFormatException e){
378             // OK
379         }
380
381         return;
382     }
383
384 }