OSDN Git Service

3074561ceba72fe3e583594a73b0a4bd6ed9cc74
[jindolf/JinParser.git] / src / test / java / jp / sourceforge / jindolf / parser / SjisDecoderTest.java
1 /*
2  */
3
4 package jp.sourceforge.jindolf.parser;
5
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;
16
17 import static org.junit.Assert.*;
18
19 /**
20  *
21  */
22 public class SjisDecoderTest {
23
24     public SjisDecoderTest() {
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     public static ByteArrayInputStream byteIs(CharSequence seq){
44         byte[] bs = byteArray(seq);
45         ByteArrayInputStream result = new ByteArrayInputStream(bs);
46         return result;
47     }
48
49     public static byte[] byteArray(CharSequence seq){
50         byte[] result;
51
52         List<Byte> byteList = new ArrayList<>();
53
54         int length = seq.length();
55         for(int pos = 0; pos < length; pos++){
56             int val = 0;
57
58             char ch = seq.charAt(pos);
59
60             if('0' <= ch && ch <= '9'){
61                 val += ch - '0';
62             }else if('a' <= ch && ch <= 'f'){
63                 val += ch - 'a' + 10;
64             }else if('A' <= ch && ch <= 'F'){
65                 val += ch - 'A' + 10;
66             }else{
67                 continue;
68             }
69
70             pos++;
71             if(pos >= length) break;
72
73             val *= 16;
74             ch = seq.charAt(pos);
75
76             if('0' <= ch && ch <= '9'){
77                 val += ch - '0';
78             }else if('a' <= ch && ch <= 'f'){
79                 val += ch - 'a' + 10;
80             }else if('A' <= ch && ch <= 'F'){
81                 val += ch - 'A' + 10;
82             }else{
83                 continue;
84             }
85
86             byteList.add((byte)val);
87         }
88
89         result = new byte[byteList.size()];
90
91         for(int pos = 0; pos < result.length; pos++){
92             result[pos] = byteList.get(pos);
93         }
94
95         return result;
96     }
97
98     /**
99      * Test of class SjisDecoder.
100      * @throws Exception
101      */
102     @Test
103     public void testSjisDecoder() throws Exception {
104         SjisDecoder sjd;
105         InputStream is;
106         TestHandler handler;
107
108         handler = new TestHandler();
109
110         sjd = new SjisDecoder(10, 10);
111         sjd.setDecodeHandler(handler);
112         is = byteIs("414243");
113         handler.clear();
114         sjd.decode(is);
115         assertEquals("[ST][CH]ABC[EN]", handler.toString());
116
117         sjd = new SjisDecoder(10, 2);
118         sjd.setDecodeHandler(handler);
119         is = byteIs("414243");
120         handler.clear();
121         sjd.decode(is);
122         assertEquals("[ST][CH]AB[CH]C[EN]", handler.toString());
123
124         sjd = new SjisDecoder(2, 10);
125         sjd.setDecodeHandler(handler);
126         is = byteIs("82a0:82a1");
127         handler.clear();
128         sjd.decode(is);
129         assertEquals("[ST][CH]あぃ[EN]", handler.toString());
130
131         sjd = new SjisDecoder(2, 10);
132         sjd.setDecodeHandler(handler);
133         is = byteIs("41:82a0:82a1");
134         handler.clear();
135         sjd.decode(is);
136         assertEquals("[ST][CH]Aあぃ[EN]", handler.toString());
137
138         sjd = new SjisDecoder(1, 10);
139         sjd.setDecodeHandler(handler);
140         is = byteIs("82a0:82a1");
141         handler.clear();
142         try{
143             sjd.decode(is);
144             fail();
145         }catch(DecodeException e){
146             // GOOD
147         }
148
149         return;
150     }
151
152     static class TestHandler implements DecodeHandler{
153
154         private final StringBuilder text = new StringBuilder();
155
156         @Override
157         public void startDecoding(CharsetDecoder decoder) throws DecodeException {
158             this.text.append("[ST]");
159         }
160
161         @Override
162         public void endDecoding() throws DecodeException {
163             this.text.append("[EN]");
164         }
165
166         @Override
167         public void charContent(CharSequence seq) throws DecodeException {
168             this.text.append("[CH]");
169             this.text.append(seq);
170         }
171
172         @Override
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));
179             }
180         }
181
182         public void clear(){
183             text.setLength(0);
184         }
185
186         @Override
187         public String toString(){
188             return text.toString();
189         }
190
191     }
192
193 }