OSDN Git Service

文字デコーディング処理の分離に伴うテスト
[mikutoga/TogaGem.git] / src / test / java / jp / sourceforge / mikutoga / parser / TextDecoderTest.java
1 /*
2  */
3
4 package jp.sourceforge.mikutoga.parser;
5
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.*;
18
19 /**
20  *
21  */
22 public class TextDecoderTest {
23
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");
27
28     public TextDecoderTest() {
29     }
30
31     @BeforeClass
32     public static void setUpClass() throws Exception {
33     }
34
35     @AfterClass
36     public static void tearDownClass() throws Exception {
37     }
38
39     @Before
40     public void setUp() {
41     }
42
43     @After
44     public void tearDown() {
45     }
46
47     public static byte[] byteArray(CharSequence seq){
48         byte[] result;
49
50         List<Byte> byteList = new ArrayList<Byte>();
51
52         int length = seq.length();
53         for(int pos = 0; pos < length; pos++){
54             int val = 0;
55
56             char ch = seq.charAt(pos);
57
58             if('0' <= ch && ch <= '9'){
59                 val += ch - '0';
60             }else if('a' <= ch && ch <= 'f'){
61                 val += ch - 'a' + 10;
62             }else if('A' <= ch && ch <= 'F'){
63                 val += ch - 'A' + 10;
64             }else{
65                 continue;
66             }
67
68             pos++;
69             if(pos >= length) break;
70
71             val *= 16;
72             ch = seq.charAt(pos);
73
74             if('0' <= ch && ch <= '9'){
75                 val += ch - '0';
76             }else if('a' <= ch && ch <= 'f'){
77                 val += ch - 'a' + 10;
78             }else if('A' <= ch && ch <= 'F'){
79                 val += ch - 'A' + 10;
80             }else{
81                 continue;
82             }
83
84             byteList.add((byte)val);
85         }
86
87         result = new byte[byteList.size()];
88
89         for(int pos = 0; pos < result.length; pos++){
90             result[pos] = byteList.get(pos);
91         }
92
93         return result;
94     }
95
96     /**
97      * Test of prepareBuffer method, of class TextDecoder.
98      */
99     @Test
100     public void testPrepareBuffer() {
101         System.out.println("prepareBuffer");
102         return;
103     }
104
105     /**
106      * Test of parseString method, of class TextDecoder.
107      */
108     @Test
109     public void testParseString() throws Exception {
110         System.out.println("parseString");
111
112         TextDecoder decoder;
113         byte[] bdata;
114         InputStream istream;
115         MmdSource source;
116         CharBuffer cb;
117
118         decoder = new TextDecoder(CS_WIN31J);
119
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());
125
126         istream = new ByteArrayInputStream(bdata);
127         source = new MmdSource(istream);
128         cb =decoder.parseString(source, 1);
129         assertEquals("A", cb.toString());
130
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());
136
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());
142
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());
148
149         bdata = byteArray("88:9F:88:A0");
150         istream = new ByteArrayInputStream(bdata);
151         source = new MmdSource(istream);
152         try{
153             cb =decoder.parseString(source, 5);
154             fail();
155         }catch(MmdEofException e){
156             // OK
157         }
158
159         bdata = byteArray("88:9F:88:A0");
160         istream = new ByteArrayInputStream(bdata);
161         source = new MmdSource(istream);
162         try{
163             cb =decoder.parseString(source, 3);
164             fail();
165         }catch(MmdFormatException e){
166             // OK
167         }
168
169         return;
170     }
171
172 }