4 package jp.sourceforge.jindolf.parser;
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;
17 import static org.junit.Assert.*;
22 public class StreamDecoderTest {
24 public StreamDecoderTest() {
28 public static void setUpClass() {
32 public static void tearDownClass() {
40 public void tearDown() {
44 * Test of constructor method, of class StreamDecoder.
46 * @throws DecodeException
49 public void testConstructor() throws IOException, DecodeException {
50 System.out.println("constructor");
53 CharsetDecoder decoder;
57 sd = new StreamDecoder(null);
59 }catch(NullPointerException e){
63 cs = Charset.forName("US-ASCII");
64 decoder = cs.newDecoder();
67 sd = new StreamDecoder(decoder, 0, 100);
69 }catch(IllegalArgumentException e){
74 sd = new StreamDecoder(decoder, 100, 0);
76 }catch(IllegalArgumentException e){
84 * Test of decode method, of class StreamDecoder.
86 * @throws DecodeException
89 public void testDecode() throws IOException, DecodeException {
90 System.out.println("decode");
93 CharsetDecoder decoder;
99 cs = Charset.forName("US-ASCII");
101 decoder = cs.newDecoder();
102 sd = new StreamDecoder(decoder);
103 is = new ByteArrayInputStream(new byte[]{});
108 }catch(NullPointerException e){
112 handler = new TestHandler();
113 sd.setDecodeHandler(handler);
118 assertEquals("[ST][EN]", handler.toString());
120 is = byteStream(0x41, 0x42, 0x43);
123 assertEquals("[ST][CH]ABC[EN]", handler.toString());
125 is = byteStream(0x0d, 0x0a, 0x7f);
128 assertEquals("[ST][CH]\r\n\u007f[EN]", handler.toString());
130 is = byteStream(0x7e, 0x7f, 0x80, 0xfe, 0xff);
133 assertEquals("[ST][CH]\u007e\u007f[ER]80[ER]fe[ER]ff[EN]", handler.toString());
135 is = byteStream(0x41, 0x42, 0x80, 0x43);
138 assertEquals("[ST][CH]AB[ER]80[CH]C[EN]", handler.toString());
140 decoder = cs.newDecoder();
141 sd = new StreamDecoder(decoder, 4, 100);
142 sd.setDecodeHandler(handler);
143 is = byteStream(0x41, 0x42, 0x43, 0x44, 0x45);
146 assertEquals("[ST][CH]ABCDE[EN]", handler.toString());
148 decoder = cs.newDecoder();
149 sd = new StreamDecoder(decoder, 100, 4);
150 sd.setDecodeHandler(handler);
151 is = byteStream(0x41, 0x42, 0x43, 0x44, 0x45);
154 assertEquals("[ST][CH]ABCDE[EN]", handler.toString());
156 decoder = cs.newDecoder();
157 sd = new StreamDecoder(decoder, 4, 4);
158 sd.setDecodeHandler(handler);
159 is = byteStream(0x41, 0x42, 0x43, 0x44, 0x45);
162 assertEquals("[ST][CH]ABCDE[EN]", handler.toString());
168 * Test of decode method, of class StreamDecoder.
169 * @throws IOException
170 * @throws DecodeException
173 public void testDecodeSJ() throws IOException, DecodeException {
174 System.out.println("decode");
177 CharsetDecoder decoder;
183 cs = ShiftJis.CHARSET;
185 handler = new TestHandler();
187 decoder = cs.newDecoder();
188 sd = new StreamDecoder(decoder, 4, 4);
189 sd.setDecodeHandler(handler);
190 is = byteStream(0x41, 0x82, 0xa0, 0x44, 0x45);
193 assertEquals("[ST][CH]AあDE[EN]", handler.toString());
195 is = byteStream(0x41, 0x82, 0xf2, 0x44, 0x45);
198 assertEquals("[ST][CH]A[ER]82[ER]f2[CH]DE[EN]", handler.toString());
201 // WARNING: some JDK 1.6 implements make 2byte error 0xff32
202 is = byteStream(0x41, 0xff, 0x32, 0x44, 0x45);
205 assertEquals("[ST][CH]A[ER]ff[CH]2DE[EN]", handler.toString());
208 is = byteStream(0x41, 0x81, 0xfd, 0x44, 0x45);
211 assertEquals("[ST][CH]A[ER]81[ER]fd[CH]DE[EN]", handler.toString());
214 is = byteStream(0x41, 0xa0, 0x80, 0x44, 0x45);
217 assertEquals("[ST][CH]A[ER]a0[ER]80[CH]DE[EN]", handler.toString());
219 is = byteStream(0x41, 0x82, 0xa0, 0x82, 0xa2, 0x82, 0xa4);
222 assertEquals("[ST][CH]Aあいう[EN]", handler.toString());
225 is = byteStream(0x41, 0x82, 0xa0, 0x82, 0xf2, 0x82, 0xa4);
228 assertEquals("[ST][CH]Aあ[ER]82[ER]f2[CH]う[EN]", handler.toString());
231 is = byteStream(0x41, 0x42, 0x43, 0x44, 0x45, 0x82, 0xf2);
234 assertEquals("[ST][CH]ABCDE[ER]82[ER]f2[EN]", handler.toString());
239 * Test of decode method, of class StreamDecoder.
240 * @throws IOException
241 * @throws DecodeException
244 public void testDecodeUCS4() throws IOException, DecodeException {
245 System.out.println("decode");
248 CharsetDecoder decoder;
254 cs = Charset.forName("UTF-8");
256 handler = new TestHandler();
258 decoder = cs.newDecoder();
259 sd = new StreamDecoder(decoder, 4, 4);
260 sd.setDecodeHandler(handler);
261 is = byteStream(0x41, 0x42, 0xe3, 0x81, 0x82, 0x46);
264 assertEquals("[ST][CH]ABあF[EN]", handler.toString());
267 is = byteStream(0x41, 0x42, 0xc2, 0xc0, 0x45);
270 assertEquals("[ST][CH]AB[ER]c2[ER]c0[CH]E[EN]", handler.toString());
272 // SMP character U+1F411 [SHEEP]
273 is = byteStream(0x41, 0x42, 0xf0, 0x9f, 0x90, 0x91, 0x47);
276 assertEquals("[ST][CH]AB\ud83d\udc11G[EN]", handler.toString());
278 sd = new StreamDecoder(decoder, 3, 4);
279 sd.setDecodeHandler(handler);
280 is = byteStream(0x41, 0x42, 0xf0, 0x9f, 0x90, 0x91, 0x47);
285 }catch(DecodeException e){
286 assertEquals("too small input buffer (3bytes) for UTF-8 bytePos=-1 charPos=-1", e.getMessage());
291 static ByteArrayInputStream byteStream(int... array){
292 byte[] ba = new byte[array.length];
295 for(int iVal : array){
296 byte bVal = (byte)(iVal & 0xff);
300 return new ByteArrayInputStream(ba);
303 static class TestHandler implements DecodeHandler{
305 private final StringBuilder text = new StringBuilder();
306 private boolean notch = true;
309 public void startDecoding(CharsetDecoder decoder) throws DecodeException {
310 this.text.append("[ST]");
315 public void endDecoding() throws DecodeException {
316 this.text.append("[EN]");
321 public void charContent(CharSequence seq) throws DecodeException {
323 this.text.append("[CH]");
325 this.text.append(seq);
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));
346 public String toString(){
347 return text.toString();