OSDN Git Service

パッケージ変更。テスト整備。
[mikutoga/TogaGem.git] / src / test / java / jp / sfjp / mikutoga / bin / parser / TextDecoderTest.java
1 /*
2  */
3
4 package jp.sfjp.mikutoga.bin.parser;
5
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.*;
17
18 /**
19  *
20  */
21 public class TextDecoderTest {
22
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");
26
27     public TextDecoderTest() {
28     }
29
30     @BeforeClass
31     public static void setUpClass() throws Exception {
32     }
33
34     @AfterClass
35     public static void tearDownClass() throws Exception {
36     }
37
38     @Before
39     public void setUp() {
40     }
41
42     @After
43     public void tearDown() {
44     }
45
46     public static byte[] byteArray(CharSequence seq){
47         byte[] result;
48
49         List<Byte> byteList = new ArrayList<Byte>();
50
51         int length = seq.length();
52         for(int pos = 0; pos < length; pos++){
53             int val = 0;
54
55             char ch = seq.charAt(pos);
56
57             if('0' <= ch && ch <= '9'){
58                 val += ch - '0';
59             }else if('a' <= ch && ch <= 'f'){
60                 val += ch - 'a' + 10;
61             }else if('A' <= ch && ch <= 'F'){
62                 val += ch - 'A' + 10;
63             }else{
64                 continue;
65             }
66
67             pos++;
68             if(pos >= length) break;
69
70             val *= 16;
71             ch = seq.charAt(pos);
72
73             if('0' <= ch && ch <= '9'){
74                 val += ch - '0';
75             }else if('a' <= ch && ch <= 'f'){
76                 val += ch - 'a' + 10;
77             }else if('A' <= ch && ch <= 'F'){
78                 val += ch - 'A' + 10;
79             }else{
80                 continue;
81             }
82
83             byteList.add((byte)val);
84         }
85
86         result = new byte[byteList.size()];
87
88         for(int pos = 0; pos < result.length; pos++){
89             result[pos] = byteList.get(pos);
90         }
91
92         return result;
93     }
94
95     /**
96      * Test of setChopMode, getChopMode method, of class TextDecoder.
97      * @throws Exception
98      */
99     @Test
100     public void testChopMode() throws Exception {
101         System.out.println("chopMode");
102
103         TextDecoder decoder;
104
105         decoder = new TextDecoder(CS_WIN31J);
106         assertFalse(decoder.isZeroChopMode());
107
108         decoder.setZeroChopMode(true);
109         assertTrue(decoder.isZeroChopMode());
110
111         decoder.setZeroChopMode(false);
112         assertFalse(decoder.isZeroChopMode());
113
114         return;
115     }
116
117     /**
118      * Test of parseString method, of class TextDecoder.
119      * @throws Exception
120      */
121     @Test
122     public void testParseStringChop() throws Exception {
123         System.out.println("parseString(Chop)");
124
125         TextDecoder decoder;
126
127         decoder = new TextDecoder(CS_WIN31J);
128         decoder.setZeroChopMode(true);
129
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);
135
136         decoder.setZeroChopMode(false);
137         assertDecoded("41:00:42", "A\u0000B", decoder);
138         assertDecoded("", "", decoder);
139
140         return;
141     }
142
143     /**
144      * Test of parseString method, of class TextDecoder.
145      * @throws Exception
146      */
147     @Test
148     public void testParseStringWin31J() throws Exception {
149         System.out.println("parseString(Win31J)");
150
151         TextDecoder decoder;
152
153         decoder = new TextDecoder(CS_WIN31J);
154
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);
161
162         assertFormatError("88:9F:88:A0", decoder, 3);
163
164         return;
165     }
166
167     /**
168      * Test of parseString method, of class TextDecoder.
169      * @throws Exception
170      */
171     @Test
172     public void testParseStringUTF8() throws Exception {
173         System.out.println("parseString(UTF8)");
174
175         TextDecoder decoder;
176
177         decoder = new TextDecoder(CS_UTF8);
178
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);
186
187         assertFormatError("E4:BA:9C:E5:94:96", decoder, 5);
188
189         return;
190     }
191
192     /**
193      * Test of parseString method, of class TextDecoder.
194      * @throws Exception
195      */
196     @Test
197     public void testParseStringUTF16LE() throws Exception {
198         System.out.println("parseString(UTF16LE)");
199
200         TextDecoder decoder;
201
202         decoder = new TextDecoder(CS_UTF16LE);
203
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);
211
212         assertDecoded("60:08", "\u0860", decoder);
213
214         assertDecoded("FF:FE:9C:4E", "\ufeff亜", decoder);
215         // not BOM, ZERO WIDTH NO-BREAK SPACE
216
217         assertFormatError("9C:4E:16:55", decoder, 3);
218
219         return;
220     }
221
222     /**
223      * Test of Yen(U+00A5) & Backslash(U+005C) encoding, of class TextDecoder.
224      * @throws Exception
225      */
226     @Test
227     public void testYenAndBackslash() throws Exception {
228         System.out.println("Yen & Backslash");
229
230         TextDecoder decoder;
231
232         decoder = new TextDecoder(CS_WIN31J);
233         assertDecoded("5C", "\u005c\u005c", decoder);
234
235         decoder = new TextDecoder(CS_UTF8);
236         assertDecoded("5C", "\u005c\u005c", decoder);
237         assertDecoded("C2:A5", "\u00a5", decoder);
238
239         decoder = new TextDecoder(CS_UTF16LE);
240         assertDecoded("5C:00", "\u005c\u005c", decoder);
241         assertDecoded("A5:00", "\u00a5", decoder);
242
243         return;
244     }
245
246     /**
247      * Test of unmapped char, of class TextDecoder.
248      * @throws Exception
249      */
250     @Test
251     public void testUnmapChar() throws Exception {
252         System.out.println("unmap char");
253
254         TextDecoder decoder;
255
256         decoder = new TextDecoder(CS_WIN31J);
257         assertFormatError("FF:FF", decoder, 2);
258
259
260         // Unicode2.0の時点でU+0860は未定義文字
261
262         decoder = new TextDecoder(CS_UTF8);
263         assertFormatError("FF:FF:FF", decoder, 3);
264         assertDecoded("E0:A1:A0", "\u0860", decoder);
265
266         decoder = new TextDecoder(CS_UTF16LE);
267         assertDecoded("60:08", "\u0860", decoder);
268
269         return;
270     }
271
272     public void assertDecoded(String bin, String desired,
273                                 TextDecoder decoder)
274             throws Exception{
275         byte[] bdata = byteArray(bin);
276         ByteBuffer bBuf = ByteBuffer.wrap(bdata);
277         assertDecoded(bBuf, desired, decoder);
278         return;
279     }
280
281     public void assertDecoded(String bin, String desired,
282                                 TextDecoder decoder, int len)
283             throws Exception{
284         byte[] bdata = byteArray(bin);
285         ByteBuffer bBuf = ByteBuffer.wrap(bdata, 0, len);
286         assertDecoded(bBuf, desired, decoder);
287         return;
288     }
289
290     public void assertDecoded(ByteBuffer bBuf, String desired,
291                                 TextDecoder decoder)
292             throws Exception{
293         String result;
294         result = decoder.decode(bBuf);
295
296         assertEquals(desired, result);
297
298         return;
299     }
300
301     public void assertFormatError(String bin,
302                                     TextDecoder decoder, int len)
303             throws Exception{
304         byte[] bdata = byteArray(bin);
305         ByteBuffer bBuf = ByteBuffer.wrap(bdata, 0, len);
306
307         try{
308             decoder.decode(bBuf);
309             fail();
310         }catch(CharacterCodingException e){
311             // OK
312         }
313
314         return;
315     }
316
317 }