4 package jp.sourceforge.mikutoga.parser;
6 import java.io.ByteArrayInputStream;
7 import java.io.InputStream;
8 import java.nio.CharBuffer;
9 import java.nio.charset.Charset;
10 import java.util.ArrayList;
11 import java.util.List;
12 import org.junit.After;
13 import org.junit.AfterClass;
14 import org.junit.Before;
15 import org.junit.BeforeClass;
16 import org.junit.Test;
17 import static org.junit.Assert.*;
22 public class TextDecoderTest {
24 private static final Charset CS_WIN31J = Charset.forName("windows-31j");
25 private static final Charset CS_UTF8 = Charset.forName("UTF-8");
26 private static final Charset CS_UTF16LE = Charset.forName("UTF-16LE");
28 public TextDecoderTest() {
32 public static void setUpClass() throws Exception {
36 public static void tearDownClass() throws Exception {
44 public void tearDown() {
47 public static byte[] byteArray(CharSequence seq){
50 List<Byte> byteList = new ArrayList<Byte>();
52 int length = seq.length();
53 for(int pos = 0; pos < length; pos++){
56 char ch = seq.charAt(pos);
58 if('0' <= ch && ch <= '9'){
60 }else if('a' <= ch && ch <= 'f'){
62 }else if('A' <= ch && ch <= 'F'){
69 if(pos >= length) break;
74 if('0' <= ch && ch <= '9'){
76 }else if('a' <= ch && ch <= 'f'){
78 }else if('A' <= ch && ch <= 'F'){
84 byteList.add((byte)val);
87 result = new byte[byteList.size()];
89 for(int pos = 0; pos < result.length; pos++){
90 result[pos] = byteList.get(pos);
97 * Test of prepareBuffer method, of class TextDecoder.
100 public void testPrepareBuffer() {
101 System.out.println("prepareBuffer");
106 * Test of parseString method, of class TextDecoder.
109 public void testParseString() throws Exception {
110 System.out.println("parseString");
118 decoder = new TextDecoder(CS_WIN31J);
120 bdata = byteArray("41:42");
121 istream = new ByteArrayInputStream(bdata);
122 source = new MmdSource(istream);
123 cb =decoder.parseString(source, 2);
124 assertEquals("AB", cb.toString());
126 istream = new ByteArrayInputStream(bdata);
127 source = new MmdSource(istream);
128 cb =decoder.parseString(source, 1);
129 assertEquals("A", cb.toString());
131 bdata = byteArray("88:9F");
132 istream = new ByteArrayInputStream(bdata);
133 source = new MmdSource(istream);
134 cb =decoder.parseString(source, 2);
135 assertEquals("亜", cb.toString());
137 bdata = byteArray("88:9F:88:A0");
138 istream = new ByteArrayInputStream(bdata);
139 source = new MmdSource(istream);
140 cb =decoder.parseString(source, 4);
141 assertEquals("亜唖", cb.toString());
143 bdata = byteArray("88:9F:41:88:A0");
144 istream = new ByteArrayInputStream(bdata);
145 source = new MmdSource(istream);
146 cb =decoder.parseString(source, 5);
147 assertEquals("亜A唖", cb.toString());
149 bdata = byteArray("88:9F:88:A0");
150 istream = new ByteArrayInputStream(bdata);
151 source = new MmdSource(istream);
153 cb =decoder.parseString(source, 5);
155 }catch(MmdEofException e){
159 bdata = byteArray("88:9F:88:A0");
160 istream = new ByteArrayInputStream(bdata);
161 source = new MmdSource(istream);
163 cb =decoder.parseString(source, 3);
165 }catch(MmdFormatException e){