OSDN Git Service

3fa25701181cd1f6ebd35aeaa8432dc47a15597b
[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 decode method, of class StreamDecoder.
45      * @throws IOException
46      * @throws DecodeException
47      */
48     @Test
49     public void testDecode() throws IOException, DecodeException {
50         System.out.println("decode");
51
52         Charset cs;
53         CharsetDecoder decoder;
54
55         StreamDecoder sd;
56         InputStream is;
57         TestHandler handler;
58
59         cs = Charset.forName("US-ASCII");
60
61         decoder = cs.newDecoder();
62         sd = new StreamDecoder(decoder);
63         is = new ByteArrayInputStream(new byte[]{});
64
65         try{
66             sd.decode(is);
67             fail();
68         }catch(NullPointerException e){
69             // GOOD
70         }
71
72         handler = new TestHandler();
73         sd.setDecodeHandler(handler);
74
75         is = byteStream();
76         handler.clear();
77         sd.decode(is);
78         assertEquals("[ST][EN]", handler.toString());
79
80         is = byteStream(0x41, 0x42, 0x43);
81         handler.clear();
82         sd.decode(is);
83         assertEquals("[ST][CH]ABC[EN]", handler.toString());
84
85         is = byteStream(0x0d, 0x0a, 0x7f);
86         handler.clear();
87         sd.decode(is);
88         assertEquals("[ST][CH]\r\n\u007f[EN]", handler.toString());
89
90         is = byteStream(0x7e, 0x7f, 0x80, 0xfe, 0xff);
91         handler.clear();
92         sd.decode(is);
93         assertEquals("[ST][CH]\u007e\u007f[ER]80[ER]fe[ER]ff[EN]", handler.toString());
94
95         is = byteStream(0x41, 0x42, 0x80, 0x43);
96         handler.clear();
97         sd.decode(is);
98         assertEquals("[ST][CH]AB[ER]80[CH]C[EN]", handler.toString());
99
100         decoder = cs.newDecoder();
101         sd = new StreamDecoder(decoder, 4, 100);
102         sd.setDecodeHandler(handler);
103         is = byteStream(0x41, 0x42, 0x43, 0x44, 0x45);
104         handler.clear();
105         sd.decode(is);
106         assertEquals("[ST][CH]ABCDE[EN]", handler.toString());
107
108         decoder = cs.newDecoder();
109         sd = new StreamDecoder(decoder, 100, 4);
110         sd.setDecodeHandler(handler);
111         is = byteStream(0x41, 0x42, 0x43, 0x44, 0x45);
112         handler.clear();
113         sd.decode(is);
114         assertEquals("[ST][CH]ABCDE[EN]", handler.toString());
115
116         return;
117     }
118
119     static ByteArrayInputStream byteStream(int... array){
120         byte[] ba = new byte[array.length];
121
122         int idx = 0;
123         for(int iVal : array){
124             byte bVal = (byte)(iVal & 0xff);
125             ba[idx++] = bVal;
126         }
127
128         return new ByteArrayInputStream(ba);
129     }
130
131     static class TestHandler implements DecodeHandler{
132
133         private final StringBuilder text = new StringBuilder();
134         private boolean notch = true;
135
136         @Override
137         public void startDecoding(CharsetDecoder decoder) throws DecodeException {
138             this.text.append("[ST]");
139             this.notch = true;
140         }
141
142         @Override
143         public void endDecoding() throws DecodeException {
144             this.text.append("[EN]");
145             this.notch = true;
146         }
147
148         @Override
149         public void charContent(CharSequence seq) throws DecodeException {
150             if(this.notch){
151                 this.text.append("[CH]");
152             }
153             this.text.append(seq);
154             this.notch = false;
155         }
156
157         @Override
158         public void decodingError(byte[] errorArray, int offset, int length) throws DecodeException {
159             for(int ct = 0; ct < length;ct++){
160                 this.text.append("[ER]");
161                 int val = errorArray[offset + ct] & 0xff;
162                 if(val <= 0xf) this.text.append('0');
163                 this.text.append(Integer.toHexString(val));
164             }
165             this.notch = true;
166         }
167
168         public void clear(){
169             text.setLength(0);
170             this.notch = true;
171         }
172
173         @Override
174         public String toString(){
175             return text.toString();
176         }
177
178     }
179
180 }