OSDN Git Service

depend jiocema
[jindolf/JinParser.git] / src / test / java / jp / sourceforge / jindolf / parser / StreamDecoderTest.java
1 /*
2  */
3
4 package jp.sourceforge.jindolf.parser;
5
6 import java.io.ByteArrayInputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.nio.charset.Charset;
10 import java.nio.charset.CharsetDecoder;
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
17 import static org.junit.Assert.*;
18
19 /**
20  *
21  */
22 public class StreamDecoderTest {
23
24     public StreamDecoderTest() {
25     }
26
27     @BeforeClass
28     public static void setUpClass() {
29     }
30
31     @AfterClass
32     public static void tearDownClass() {
33     }
34
35     @Before
36     public void setUp() {
37     }
38
39     @After
40     public void tearDown() {
41     }
42
43     /**
44      * Test of constructor method, of class StreamDecoder.
45      * @throws IOException
46      * @throws DecodeException
47      */
48     @Test
49     public void testConstructor() throws IOException, DecodeException {
50         System.out.println("constructor");
51
52         Charset cs;
53         CharsetDecoder decoder;
54         StreamDecoder sd;
55
56         try{
57             sd = new StreamDecoder(null);
58             fail();
59         }catch(NullPointerException e){
60             // GOOD
61         }
62
63         cs = Charset.forName("US-ASCII");
64         decoder = cs.newDecoder();
65
66         try{
67             sd = new StreamDecoder(decoder, 0, 100);
68             fail();
69         }catch(IllegalArgumentException e){
70             // GOOD
71         }
72
73         try{
74             sd = new StreamDecoder(decoder, 100, 0);
75             fail();
76         }catch(IllegalArgumentException e){
77             // GOOD
78         }
79
80         return;
81     }
82
83     /**
84      * Test of decode method, of class StreamDecoder.
85      * @throws IOException
86      * @throws DecodeException
87      */
88     @Test
89     public void testDecode() throws IOException, DecodeException {
90         System.out.println("decode");
91
92         Charset cs;
93         CharsetDecoder decoder;
94
95         StreamDecoder sd;
96         InputStream is;
97         TestHandler handler;
98
99         cs = Charset.forName("US-ASCII");
100
101         decoder = cs.newDecoder();
102         sd = new StreamDecoder(decoder);
103         is = new ByteArrayInputStream(new byte[]{});
104
105         try{
106             sd.decode(is);
107             fail();
108         }catch(NullPointerException e){
109             // GOOD
110         }
111
112         handler = new TestHandler();
113         sd.setDecodeHandler(handler);
114
115         is = byteStream();
116         handler.clear();
117         sd.decode(is);
118         assertEquals("[ST][EN]", handler.toString());
119
120         is = byteStream(0x41, 0x42, 0x43);
121         handler.clear();
122         sd.decode(is);
123         assertEquals("[ST][CH]ABC[EN]", handler.toString());
124
125         is = byteStream(0x0d, 0x0a, 0x7f);
126         handler.clear();
127         sd.decode(is);
128         assertEquals("[ST][CH]\r\n\u007f[EN]", handler.toString());
129
130         is = byteStream(0x7e, 0x7f, 0x80, 0xfe, 0xff);
131         handler.clear();
132         sd.decode(is);
133         assertEquals("[ST][CH]\u007e\u007f[ER]80[ER]fe[ER]ff[EN]", handler.toString());
134
135         is = byteStream(0x41, 0x42, 0x80, 0x43);
136         handler.clear();
137         sd.decode(is);
138         assertEquals("[ST][CH]AB[ER]80[CH]C[EN]", handler.toString());
139
140         decoder = cs.newDecoder();
141         sd = new StreamDecoder(decoder, 4, 100);
142         sd.setDecodeHandler(handler);
143         is = byteStream(0x41, 0x42, 0x43, 0x44, 0x45);
144         handler.clear();
145         sd.decode(is);
146         assertEquals("[ST][CH]ABCDE[EN]", handler.toString());
147
148         decoder = cs.newDecoder();
149         sd = new StreamDecoder(decoder, 100, 4);
150         sd.setDecodeHandler(handler);
151         is = byteStream(0x41, 0x42, 0x43, 0x44, 0x45);
152         handler.clear();
153         sd.decode(is);
154         assertEquals("[ST][CH]ABCDE[EN]", handler.toString());
155
156         decoder = cs.newDecoder();
157         sd = new StreamDecoder(decoder, 4, 4);
158         sd.setDecodeHandler(handler);
159         is = byteStream(0x41, 0x42, 0x43, 0x44, 0x45);
160         handler.clear();
161         sd.decode(is);
162         assertEquals("[ST][CH]ABCDE[EN]", handler.toString());
163
164         return;
165     }
166
167     /**
168      * Test of decode method, of class StreamDecoder.
169      * @throws IOException
170      * @throws DecodeException
171      */
172     @Test
173     public void testDecodeSJ() throws IOException, DecodeException {
174         System.out.println("decode");
175
176         Charset cs;
177         CharsetDecoder decoder;
178
179         StreamDecoder sd;
180         InputStream is;
181         TestHandler handler;
182
183         cs = ShiftJis.CHARSET;
184
185         handler = new TestHandler();
186
187         decoder = cs.newDecoder();
188         sd = new StreamDecoder(decoder, 4, 4);
189         sd.setDecodeHandler(handler);
190         is = byteStream(0x41, 0x82, 0xa0, 0x44, 0x45);
191         handler.clear();
192         sd.decode(is);
193         assertEquals("[ST][CH]AあDE[EN]", handler.toString());
194
195         is = byteStream(0x41, 0x82, 0xf2, 0x44, 0x45);
196         handler.clear();
197         sd.decode(is);
198         assertEquals("[ST][CH]A[ER]82[ER]f2[CH]DE[EN]", handler.toString());
199
200         // malform error
201         // WARNING: some JDK 1.6 implements make 2byte error 0xff32
202         is = byteStream(0x41, 0xff, 0x32, 0x44, 0x45);
203         handler.clear();
204         sd.decode(is);
205         assertEquals("[ST][CH]A[ER]ff[CH]2DE[EN]", handler.toString());
206
207         // malform error
208         is = byteStream(0x41, 0x81, 0xfd, 0x44, 0x45);
209         handler.clear();
210         sd.decode(is);
211         assertEquals("[ST][CH]A[ER]81[ER]fd[CH]DE[EN]", handler.toString());
212
213         // malform error
214         is = byteStream(0x41, 0xa0, 0x80, 0x44, 0x45);
215         handler.clear();
216         sd.decode(is);
217         assertEquals("[ST][CH]A[ER]a0[ER]80[CH]DE[EN]", handler.toString());
218
219         is = byteStream(0x41, 0x82, 0xa0, 0x82, 0xa2, 0x82, 0xa4);
220         handler.clear();
221         sd.decode(is);
222         assertEquals("[ST][CH]Aあいう[EN]", handler.toString());
223
224         // unmap error
225         is = byteStream(0x41, 0x82, 0xa0, 0x82, 0xf2, 0x82, 0xa4);
226         handler.clear();
227         sd.decode(is);
228         assertEquals("[ST][CH]Aあ[ER]82[ER]f2[CH]う[EN]", handler.toString());
229
230         // flush & error
231         is = byteStream(0x41, 0x42, 0x43, 0x44, 0x45, 0x82, 0xf2);
232         handler.clear();
233         sd.decode(is);
234         assertEquals("[ST][CH]ABCDE[ER]82[ER]f2[EN]", handler.toString());
235
236     }
237
238     /**
239      * Test of decode method, of class StreamDecoder.
240      * @throws IOException
241      * @throws DecodeException
242      */
243     @Test
244     public void testDecodeUCS4() throws IOException, DecodeException {
245         System.out.println("decode");
246
247         Charset cs;
248         CharsetDecoder decoder;
249
250         StreamDecoder sd;
251         InputStream is;
252         TestHandler handler;
253
254         cs = Charset.forName("UTF-8");
255
256         handler = new TestHandler();
257
258         decoder = cs.newDecoder();
259         sd = new StreamDecoder(decoder, 4, 4);
260         sd.setDecodeHandler(handler);
261         is = byteStream(0x41, 0x42, 0xe3, 0x81, 0x82, 0x46);
262         handler.clear();
263         sd.decode(is);
264         assertEquals("[ST][CH]ABあF[EN]", handler.toString());
265
266         // malformed
267         is = byteStream(0x41, 0x42, 0xc2, 0xc0, 0x45);
268         handler.clear();
269         sd.decode(is);
270         assertEquals("[ST][CH]AB[ER]c2[ER]c0[CH]E[EN]", handler.toString());
271
272         // SMP character U+1F411 [SHEEP]
273         is = byteStream(0x41, 0x42, 0xf0, 0x9f, 0x90, 0x91, 0x47);
274         handler.clear();
275         sd.decode(is);
276         assertEquals("[ST][CH]AB\ud83d\udc11G[EN]", handler.toString());
277
278         sd = new StreamDecoder(decoder, 3, 4);
279         sd.setDecodeHandler(handler);
280         is = byteStream(0x41, 0x42, 0xf0, 0x9f, 0x90, 0x91, 0x47);
281         handler.clear();
282         try{
283             sd.decode(is);
284             fail();
285         }catch(DecodeException e){
286             assertEquals("too small input buffer (3bytes) for UTF-8 bytePos=-1 charPos=-1", e.getMessage());
287         }
288
289     }
290
291     static ByteArrayInputStream byteStream(int... array){
292         byte[] ba = new byte[array.length];
293
294         int idx = 0;
295         for(int iVal : array){
296             byte bVal = (byte)(iVal & 0xff);
297             ba[idx++] = bVal;
298         }
299
300         return new ByteArrayInputStream(ba);
301     }
302
303     static class TestHandler implements DecodeHandler{
304
305         private final StringBuilder text = new StringBuilder();
306         private boolean notch = true;
307
308         @Override
309         public void startDecoding(CharsetDecoder decoder) throws DecodeException {
310             this.text.append("[ST]");
311             this.notch = true;
312         }
313
314         @Override
315         public void endDecoding() throws DecodeException {
316             this.text.append("[EN]");
317             this.notch = true;
318         }
319
320         @Override
321         public void charContent(CharSequence seq) throws DecodeException {
322             if(this.notch){
323                 this.text.append("[CH]");
324             }
325             this.text.append(seq);
326             this.notch = false;
327         }
328
329         @Override
330         public void decodingError(byte[] errorArray, int offset, int length) throws DecodeException {
331             for(int ct = 0; ct < length;ct++){
332                 this.text.append("[ER]");
333                 int val = errorArray[offset + ct] & 0xff;
334                 if(val <= 0xf) this.text.append('0');
335                 this.text.append(Integer.toHexString(val));
336             }
337             this.notch = true;
338         }
339
340         public void clear(){
341             text.setLength(0);
342             this.notch = true;
343         }
344
345         @Override
346         public String toString(){
347             return text.toString();
348         }
349
350     }
351
352 }