2 * License : The MIT License
3 * Copyright(c) 2009 olyutorskii
6 package jp.osdn.jindolf.parser.content;
8 import io.bitbucket.olyutorskii.jiocema.DecodeBreakException;
9 import io.bitbucket.olyutorskii.jiocema.DecodeNotifier;
10 import java.io.ByteArrayInputStream;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import org.junit.After;
14 import org.junit.AfterClass;
15 import org.junit.Before;
16 import org.junit.BeforeClass;
17 import org.junit.Test;
19 import static org.junit.Assert.*;
23 public class ContentBuilderSJTest {
25 public ContentBuilderSJTest() {
29 public static void setUpClass() throws Exception{
33 public static void tearDownClass() throws Exception{
41 public void tearDown() {
46 * Test of SjisDecoder & ContentBuilder.
47 * @throws java.io.IOException
48 * @throws DecodeBreakException
51 public void testDecoding() throws IOException, DecodeBreakException{
52 System.out.println("Decoding");
54 DecodeNotifier decoder;
55 ContentBuilderSJ builder;
58 DecodedContent content;
60 decoder = new SjisNotifier();
61 builder = new ContentBuilderSJ();
62 decoder.setCharDecodeListener(builder);
64 bdata = Bseq.byteArray("20:41:42:43:7e");
65 istream = new ByteArrayInputStream(bdata);
66 decoder.decode(istream);
67 content = builder.getContent();
68 assertEquals(" ABC~", content.toString());
69 assertFalse(content.hasDecodeError());
71 bdata = Bseq.byteArray("");
72 istream = new ByteArrayInputStream(bdata);
73 decoder.decode(istream);
74 content = builder.getContent();
75 assertEquals("", content.toString());
76 assertFalse(content.hasDecodeError());
78 bdata = Bseq.byteArray("00:0A:0D:1F");
79 istream = new ByteArrayInputStream(bdata);
80 decoder.decode(istream);
81 content = builder.getContent();
82 assertEquals("\u0000\n\r\u001f", content.toString());
83 assertFalse(content.hasDecodeError());
85 bdata = Bseq.byteArray("A1:B1:B2:B3:DF");
86 istream = new ByteArrayInputStream(bdata);
87 decoder.decode(istream);
88 content = builder.getContent();
89 assertEquals("。アイウ゚", content.toString());
90 assertFalse(content.hasDecodeError());
92 bdata = Bseq.byteArray("8140:82A0:82A2:82A4:889F:EAA4");
93 istream = new ByteArrayInputStream(bdata);
94 decoder.decode(istream);
95 content = builder.getContent();
96 assertEquals("\u3000あいう亜熙", content.toString());
97 assertFalse(content.hasDecodeError());
99 bdata = Bseq.byteArray("5c");
100 istream = new ByteArrayInputStream(bdata);
101 decoder.decode(istream);
102 content = builder.getContent();
103 assertEquals("\\", content.toString());
104 assertNotSame("\u00a5", content.toString());
105 assertFalse(content.hasDecodeError());
107 bdata = Bseq.byteArray("8d5c");
108 istream = new ByteArrayInputStream(bdata);
109 decoder.decode(istream);
110 content = builder.getContent();
111 assertEquals("構", content.toString());
112 assertFalse(content.hasDecodeError());
117 private void assertUnmapError(DecodeErrorInfo einfo,
120 assertEquals(charPos, einfo.getCharPosition());
121 assertTrue(einfo.has2nd());
122 assertEquals((byte)b1, einfo.getRawByte1st());
123 assertEquals((byte)b2, einfo.getRawByte2nd());
128 * Test of unmappable character.
129 * @throws java.io.IOException
130 * @throws DecodeBreakException
133 public void testUnmap() throws IOException, DecodeBreakException{
134 System.out.println("Unmap");
136 SjisNotifier decoder;
137 ContentBuilderSJ builder;
140 DecodedContent content;
141 DecodeErrorInfo einfo;
143 decoder = new SjisNotifier();
144 builder = new ContentBuilderSJ();
145 decoder.setCharDecodeListener(builder);
147 bdata = Bseq.byteArray("41:8540:42"); // 9区
148 istream = new ByteArrayInputStream(bdata);
149 decoder.decode(istream);
150 content = builder.getContent();
151 assertEquals("A?B", content.toString());
152 assertTrue(content.hasDecodeError());
153 assertEquals(1, content.getDecodeErrorList().size());
154 einfo = content.getDecodeErrorList().get(0);
155 assertUnmapError(einfo, 1, 0x85, 0x40);
157 bdata = Bseq.byteArray("41:8740:42"); // 13区
158 istream = new ByteArrayInputStream(bdata);
159 decoder.decode(istream);
160 content = builder.getContent();
161 assertEquals("A?B", content.toString());
162 assertTrue(content.hasDecodeError());
163 assertEquals(1, content.getDecodeErrorList().size());
164 einfo = content.getDecodeErrorList().get(0);
165 assertUnmapError(einfo, 1, 0x87, 0x40);
167 bdata = Bseq.byteArray("41:8840:42"); // 15区
168 istream = new ByteArrayInputStream(bdata);
169 decoder.decode(istream);
170 content = builder.getContent();
171 assertEquals("A?B", content.toString());
172 assertTrue(content.hasDecodeError());
173 assertEquals(1, content.getDecodeErrorList().size());
174 einfo = content.getDecodeErrorList().get(0);
175 assertUnmapError(einfo, 1, 0x88, 0x40);
177 bdata = Bseq.byteArray("41:EB40:42"); // 85区
178 istream = new ByteArrayInputStream(bdata);
179 decoder.decode(istream);
180 content = builder.getContent();
181 assertEquals("A?B", content.toString());
182 assertTrue(content.hasDecodeError());
183 assertEquals(1, content.getDecodeErrorList().size());
184 einfo = content.getDecodeErrorList().get(0);
185 assertUnmapError(einfo, 1, 0xEB, 0x40);
187 bdata = Bseq.byteArray("41:ED40:42"); // 89区
188 istream = new ByteArrayInputStream(bdata);
189 decoder.decode(istream);
190 content = builder.getContent();
191 assertEquals("A?B", content.toString());
192 assertTrue(content.hasDecodeError());
193 assertEquals(1, content.getDecodeErrorList().size());
194 einfo = content.getDecodeErrorList().get(0);
195 assertUnmapError(einfo, 1, 0xED, 0x40);
197 bdata = Bseq.byteArray("41:EEFC:42"); // 92区
198 istream = new ByteArrayInputStream(bdata);
199 decoder.decode(istream);
200 content = builder.getContent();
201 assertEquals("A?B", content.toString());
202 assertTrue(content.hasDecodeError());
203 assertEquals(1, content.getDecodeErrorList().size());
204 einfo = content.getDecodeErrorList().get(0);
205 assertUnmapError(einfo, 1, 0xEE, 0xFC);
207 bdata = Bseq.byteArray("41:EF9F:42"); // 94区
208 istream = new ByteArrayInputStream(bdata);
209 decoder.decode(istream);
210 content = builder.getContent();
211 assertEquals("A?B", content.toString());
212 assertTrue(content.hasDecodeError());
213 assertEquals(1, content.getDecodeErrorList().size());
214 einfo = content.getDecodeErrorList().get(0);
215 assertUnmapError(einfo, 1, 0xEF, 0x9F);
220 private void assertMalformError(DecodeErrorInfo einfo,
223 assertEquals(charPos, einfo.getCharPosition());
224 assertFalse(einfo.has2nd());
225 assertEquals((byte)b1, einfo.getRawByte1st());
230 * Test of malformed character.
231 * @throws java.io.IOException
232 * @throws DecodeBreakException
235 public void testMalform() throws IOException, DecodeBreakException{
236 System.out.println("Malform");
238 SjisNotifier decoder;
239 ContentBuilderSJ builder;
242 DecodedContent content;
243 DecodeErrorInfo einfo;
245 decoder = new SjisNotifier();
246 builder = new ContentBuilderSJ();
247 decoder.setCharDecodeListener(builder);
249 bdata = Bseq.byteArray("31:FD:FE:FF:32");
250 istream = new ByteArrayInputStream(bdata);
251 decoder.decode(istream);
252 content = builder.getContent();
253 assertEquals("1???2", content.toString());
254 assertTrue(content.hasDecodeError());
255 assertEquals(3, content.getDecodeErrorList().size());
256 einfo = content.getDecodeErrorList().get(0);
257 assertMalformError(einfo, 1, 0xfd);
258 einfo = content.getDecodeErrorList().get(1);
259 assertMalformError(einfo, 2, 0xfe);
260 einfo = content.getDecodeErrorList().get(2);
261 assertMalformError(einfo, 3, 0xff);
263 bdata = Bseq.byteArray("31:82:32:33");
264 istream = new ByteArrayInputStream(bdata);
265 decoder.decode(istream);
266 content = builder.getContent();
267 assertEquals("1?23", content.toString());
268 assertTrue(content.hasDecodeError());
269 assertEquals(1, content.getDecodeErrorList().size());
270 einfo = content.getDecodeErrorList().get(0);
271 assertMalformError(einfo, 1, 0x82);
273 bdata = Bseq.byteArray("31:32:33:82");
274 istream = new ByteArrayInputStream(bdata);
275 decoder.decode(istream);
276 content = builder.getContent();
277 assertEquals("123?", content.toString());
278 assertTrue(content.hasDecodeError());
279 assertEquals(1, content.getDecodeErrorList().size());
280 einfo = content.getDecodeErrorList().get(0);
281 assertMalformError(einfo, 3, 0x82);
287 * Test of Bounds buffering.
288 * @throws java.io.IOException
289 * @throws DecodeBreakException
292 public void testBounds() throws IOException, DecodeBreakException{
293 System.out.println("Bounds");
295 SjisNotifier decoder;
296 ContentBuilderSJ builder;
299 DecodedContent content;
300 DecodeErrorInfo einfo;
302 decoder = new SjisNotifier(5, 5);
303 builder = new ContentBuilderSJ();
304 decoder.setCharDecodeListener(builder);
306 bdata = Bseq.byteArray("31:32:33:34:88" + "9F:35");
307 istream = new ByteArrayInputStream(bdata);
308 decoder.decode(istream);
309 content = builder.getContent();
310 assertEquals("1234亜5", content.toString());
311 assertFalse(content.hasDecodeError());
312 assertEquals(0, content.getDecodeErrorList().size());
314 bdata = Bseq.byteArray("31:32:33:34:82" + "35:36");
315 istream = new ByteArrayInputStream(bdata);
316 decoder.decode(istream);
317 content = builder.getContent();
318 assertEquals("1234?56", content.toString());
319 assertTrue(content.hasDecodeError());
320 assertEquals(1, content.getDecodeErrorList().size());
321 einfo = content.getDecodeErrorList().get(0);
322 assertMalformError(einfo, 4, 0x82);
324 bdata = Bseq.byteArray("31:32:33:34:87" + "40:35");
325 istream = new ByteArrayInputStream(bdata);
326 decoder.decode(istream);
327 content = builder.getContent();
328 assertEquals("1234?5", content.toString());
329 assertTrue(content.hasDecodeError());
330 assertEquals(1, content.getDecodeErrorList().size());
331 einfo = content.getDecodeErrorList().get(0);
332 assertUnmapError(einfo, 4, 0x87, 0x40);
334 decoder = new SjisNotifier(5, 3);
335 builder = new ContentBuilderSJ();
336 decoder.setCharDecodeListener(builder);
338 bdata = Bseq.byteArray("31:32:33:34:35:36");
339 istream = new ByteArrayInputStream(bdata);
340 decoder.decode(istream);
341 content = builder.getContent();
342 assertEquals("123456", content.toString());
343 assertFalse(content.hasDecodeError());
344 assertEquals(0, content.getDecodeErrorList().size());