4 package jp.sourceforge.jindolf.parser;
6 import java.io.ByteArrayInputStream;
7 import java.io.InputStream;
8 import java.nio.charset.CharsetDecoder;
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;
17 import static org.junit.Assert.*;
22 public class SjisDecoderTest {
24 public SjisDecoderTest() {
28 public static void setUpClass() {
32 public static void tearDownClass() {
40 public void tearDown() {
43 public static ByteArrayInputStream byteIs(CharSequence seq){
44 byte[] bs = byteArray(seq);
45 ByteArrayInputStream result = new ByteArrayInputStream(bs);
49 public static byte[] byteArray(CharSequence seq){
52 List<Byte> byteList = new ArrayList<>();
54 int length = seq.length();
55 for(int pos = 0; pos < length; pos++){
58 char ch = seq.charAt(pos);
60 if('0' <= ch && ch <= '9'){
62 }else if('a' <= ch && ch <= 'f'){
64 }else if('A' <= ch && ch <= 'F'){
71 if(pos >= length) break;
76 if('0' <= ch && ch <= '9'){
78 }else if('a' <= ch && ch <= 'f'){
80 }else if('A' <= ch && ch <= 'F'){
86 byteList.add((byte)val);
89 result = new byte[byteList.size()];
91 for(int pos = 0; pos < result.length; pos++){
92 result[pos] = byteList.get(pos);
99 * Test of class SjisDecoder.
103 public void testSjisDecoder() throws Exception {
108 handler = new TestHandler();
110 sjd = new SjisDecoder(10, 10);
111 sjd.setDecodeHandler(handler);
112 is = byteIs("414243");
115 assertEquals("[ST][CH]ABC[EN]", handler.toString());
117 sjd = new SjisDecoder(10, 2);
118 sjd.setDecodeHandler(handler);
119 is = byteIs("414243");
122 assertEquals("[ST][CH]AB[CH]C[EN]", handler.toString());
124 sjd = new SjisDecoder(2, 10);
125 sjd.setDecodeHandler(handler);
126 is = byteIs("82a0:82a1");
129 assertEquals("[ST][CH]あぃ[EN]", handler.toString());
131 sjd = new SjisDecoder(2, 10);
132 sjd.setDecodeHandler(handler);
133 is = byteIs("41:82a0:82a1");
136 assertEquals("[ST][CH]Aあぃ[EN]", handler.toString());
138 sjd = new SjisDecoder(1, 10);
139 sjd.setDecodeHandler(handler);
140 is = byteIs("82a0:82a1");
145 }catch(DecodeException e){
152 static class TestHandler implements DecodeHandler{
154 private final StringBuilder text = new StringBuilder();
157 public void startDecoding(CharsetDecoder decoder) throws DecodeException {
158 this.text.append("[ST]");
162 public void endDecoding() throws DecodeException {
163 this.text.append("[EN]");
167 public void charContent(CharSequence seq) throws DecodeException {
168 this.text.append("[CH]");
169 this.text.append(seq);
173 public void decodingError(byte[] errorArray, int offset, int length) throws DecodeException {
174 this.text.append("[ER]");
175 for(int ct = 0; ct < length;ct++){
176 int val = errorArray[offset + ct] & 0xff;
177 if(val <= 0xf) this.text.append('0');
178 this.text.append(Integer.toHexString(val));
187 public String toString(){
188 return text.toString();