OSDN Git Service

2.102.3-SNAPSHOT 開発開始
[mikutoga/TogaGem.git] / src / test / java / jp / sourceforge / mikutoga / binio / TextExporterTest.java
1 /*
2  */
3
4 package jp.sourceforge.mikutoga.binio;
5
6 import java.io.ByteArrayOutputStream;
7 import java.io.IOException;
8 import java.io.PipedOutputStream;
9 import java.nio.charset.CharacterCodingException;
10 import java.nio.charset.Charset;
11 import java.nio.charset.CharsetEncoder;
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 TextExporterTest {
23
24     private static Charset CS_ASCII = Charset.forName("US-ASCII");
25     private static Charset CS_UTF8  = Charset.forName("UTF-8");
26
27     public TextExporterTest() {
28     }
29
30     @BeforeClass
31     public static void setUpClass() {
32     }
33
34     @AfterClass
35     public static void tearDownClass() {
36     }
37
38     @Before
39     public void setUp() {
40     }
41
42     @After
43     public void tearDown() {
44     }
45
46     /**
47      * Test of getEncoder method, of class TextExporter.
48      */
49     @Test
50     public void testGetEncoder() {
51         System.out.println("getEncoder");
52
53         Charset cs;
54         cs = Charset.forName("US-ASCII");
55         CharsetEncoder usenc = cs.newEncoder();
56
57         TextExporter exporter;
58         CharsetEncoder enc;
59
60         exporter = new TextExporter(cs);
61         enc = exporter.getEncoder();
62         assertEquals(usenc.charset(), enc.charset());
63
64         exporter = new TextExporter(usenc);
65         enc = exporter.getEncoder();
66         assertEquals(usenc.charset(), enc.charset());
67
68         return;
69     }
70
71     /**
72      * Test of setCharBufSize method, of class TextExporter.
73      */
74     @Test
75     public void testSetBufSize() {
76         System.out.println("setCharBufSize");
77
78         TextExporter exporter;
79         ByteArrayOutputStream bout = new ByteArrayOutputStream();
80
81         exporter = new TextExporter(CS_UTF8);
82         exporter.setCharBufSize(1);
83         exporter.setByteBufSize(4);
84         bout.reset();
85         try{
86             exporter.encodeToByteStream("あいう", bout);
87         }catch(CharacterCodingException e){
88             fail();
89         }
90         assertEquals(9, bout.size());
91
92         try{
93             exporter.setByteBufSize(3);
94             fail();
95         }catch(IllegalArgumentException e){
96             // GOOD
97         }
98
99         return;
100     }
101
102     /**
103      * Test of dumpText method, of class TextExporter.
104      */
105     @Test
106     public void testDumpText() throws Exception {
107         System.out.println("dumpText");
108
109         TextExporter exporter;
110         ByteArrayOutputStream bout;
111         byte[] barr;
112
113         exporter = new TextExporter(CS_ASCII);
114
115         bout = new ByteArrayOutputStream();
116         exporter.dumpText("ABC", bout);
117         assertEquals(3, bout.size());
118         barr = bout.toByteArray();
119         assertEquals(3, barr.length);
120         assertEquals((byte)0x41, barr[0]);
121         assertEquals((byte)0x42, barr[1]);
122         assertEquals((byte)0x43, barr[2]);
123
124         PipedOutputStream pout;
125         pout = new PipedOutputStream();
126         try{
127             exporter.dumpText("ABC", pout);
128             fail();
129         }catch(IOException e){
130             // GOOD
131         }
132
133         return;
134     }
135
136     /**
137      * Test of encodeToByteStream method, of class TextExporter.
138      */
139     @Test
140     public void testEncodeToByteStream() throws Exception {
141         System.out.println("encodeToByteStream");
142
143         TextExporter exporter;
144         ByteArrayOutputStream bout = new ByteArrayOutputStream();
145         byte[] barr;
146
147         exporter = new TextExporter(CS_ASCII);
148
149         bout.reset();
150         exporter.encodeToByteStream("ABC", bout);
151         assertEquals(3, bout.size());
152         barr = bout.toByteArray();
153         assertEquals(3, barr.length);
154         assertEquals((byte)0x41, barr[0]);
155         assertEquals((byte)0x42, barr[1]);
156         assertEquals((byte)0x43, barr[2]);
157
158         bout.reset();
159         exporter.encodeToByteStream("", bout);
160         assertEquals(0, bout.size());
161
162         bout.reset();
163         try{
164             exporter.encodeToByteStream("あ", bout);
165             fail();
166         }catch(CharacterCodingException e){
167             // GOOD
168         }
169
170         exporter = new TextExporter(CS_UTF8);
171         bout.reset();
172         exporter.encodeToByteStream("あ", bout);
173         assertEquals(3, bout.size());
174         barr = bout.toByteArray();
175         assertEquals(3, barr.length);
176         assertEquals((byte)0xe3, barr[0]);
177         assertEquals((byte)0x81, barr[1]);
178         assertEquals((byte)0x82, barr[2]);
179
180         return;
181     }
182
183 }