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 decode method, of class StreamDecoder.
46 * @throws DecodeException
49 public void testDecode() throws IOException, DecodeException {
50 System.out.println("decode");
53 CharsetDecoder decoder;
59 cs = Charset.forName("US-ASCII");
61 decoder = cs.newDecoder();
62 sd = new StreamDecoder(decoder);
63 is = new ByteArrayInputStream(new byte[]{});
68 }catch(NullPointerException e){
72 handler = new TestHandler();
73 sd.setDecodeHandler(handler);
78 assertEquals("[ST][EN]", handler.toString());
80 is = byteStream(0x41, 0x42, 0x43);
83 assertEquals("[ST][CH]ABC[EN]", handler.toString());
85 is = byteStream(0x0d, 0x0a, 0x7f);
88 assertEquals("[ST][CH]\r\n\u007f[EN]", handler.toString());
90 is = byteStream(0x7e, 0x7f, 0x80, 0xfe, 0xff);
93 assertEquals("[ST][CH]\u007e\u007f[ER]80[ER]fe[ER]ff[EN]", handler.toString());
95 is = byteStream(0x41, 0x42, 0x80, 0x43);
98 assertEquals("[ST][CH]AB[ER]80[CH]C[EN]", handler.toString());
100 decoder = cs.newDecoder();
101 sd = new StreamDecoder(decoder, 4, 100);
102 sd.setDecodeHandler(handler);
103 is = byteStream(0x41, 0x42, 0x43, 0x44, 0x45);
106 assertEquals("[ST][CH]ABCDE[EN]", handler.toString());
108 decoder = cs.newDecoder();
109 sd = new StreamDecoder(decoder, 100, 4);
110 sd.setDecodeHandler(handler);
111 is = byteStream(0x41, 0x42, 0x43, 0x44, 0x45);
114 assertEquals("[ST][CH]ABCDE[EN]", handler.toString());
119 static ByteArrayInputStream byteStream(int... array){
120 byte[] ba = new byte[array.length];
123 for(int iVal : array){
124 byte bVal = (byte)(iVal & 0xff);
128 return new ByteArrayInputStream(ba);
131 static class TestHandler implements DecodeHandler{
133 private final StringBuilder text = new StringBuilder();
134 private boolean notch = true;
137 public void startDecoding(CharsetDecoder decoder) throws DecodeException {
138 this.text.append("[ST]");
143 public void endDecoding() throws DecodeException {
144 this.text.append("[EN]");
149 public void charContent(CharSequence seq) throws DecodeException {
151 this.text.append("[CH]");
153 this.text.append(seq);
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));
174 public String toString(){
175 return text.toString();