OSDN Git Service

2.102.3-SNAPSHOT 開発開始
[mikutoga/TogaGem.git] / src / test / java / jp / sfjp / mikutoga / bin / export / TextExporterTest.java
1 /*
2  */
3
4 package jp.sfjp.mikutoga.bin.export;
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 constructor, of class TextExporter.
48      * @throws Exception
49      */
50     @Test
51     public void testCons() throws Exception {
52         System.out.println("constructor");
53
54         TextExporter exp;
55
56         try{
57             exp = new TextExporter((Charset)null);
58             fail();
59         }catch(NullPointerException e){
60             // GOOD
61         }
62
63         try{
64             exp = new TextExporter((CharsetEncoder)null);
65             fail();
66         }catch(NullPointerException e){
67             // GOOD
68         }
69
70         return;
71     }
72
73     /**
74      * Test of getEncoder method, of class TextExporter.
75      */
76     @Test
77     public void testGetEncoder() {
78         System.out.println("getEncoder");
79
80         Charset cs;
81         cs = Charset.forName("US-ASCII");
82         CharsetEncoder usenc = cs.newEncoder();
83
84         TextExporter exporter;
85         CharsetEncoder enc;
86
87         exporter = new TextExporter(cs);
88         enc = exporter.getEncoder();
89         assertEquals(usenc.charset(), enc.charset());
90
91         exporter = new TextExporter(usenc);
92         enc = exporter.getEncoder();
93         assertEquals(usenc.charset(), enc.charset());
94
95         return;
96     }
97
98     /**
99      * Test of setCharBufSize method, of class TextExporter.
100      */
101     @Test
102     public void testSetBufSize() {
103         System.out.println("setCharBufSize");
104
105         TextExporter exporter;
106         ByteArrayOutputStream bout = new ByteArrayOutputStream();
107
108         exporter = new TextExporter(CS_UTF8);
109         exporter.setCharBufSize(1);
110         exporter.setByteBufSize(4);
111         bout.reset();
112         try{
113             exporter.encodeToByteStream("あいう", bout);
114         }catch(CharacterCodingException e){
115             fail();
116         }
117         assertEquals(9, bout.size());
118
119         try{
120             exporter.setByteBufSize(3);
121             fail();
122         }catch(IllegalArgumentException e){
123             // GOOD
124         }
125
126         try{
127             exporter.setCharBufSize(0);
128             fail();
129         }catch(IllegalArgumentException e){
130             // GOOD
131         }
132
133         return;
134     }
135
136     /**
137      * Test of dumpText method, of class TextExporter.
138      * @throws Exception
139      */
140     @Test
141     public void testDumpText() throws Exception {
142         System.out.println("dumpText");
143
144         TextExporter exporter;
145         ByteArrayOutputStream bout;
146         byte[] barr;
147
148         exporter = new TextExporter(CS_ASCII);
149
150         bout = new ByteArrayOutputStream();
151         exporter.dumpText("ABC", bout);
152         assertEquals(3, bout.size());
153         barr = bout.toByteArray();
154         assertEquals(3, barr.length);
155         assertEquals((byte)0x41, barr[0]);
156         assertEquals((byte)0x42, barr[1]);
157         assertEquals((byte)0x43, barr[2]);
158
159         PipedOutputStream pout;
160         pout = new PipedOutputStream();
161         try{
162             exporter.dumpText("ABC", pout);
163             fail();
164         }catch(IOException e){
165             // GOOD
166         }
167
168         return;
169     }
170
171     /**
172      * Test of encodeToByteStream method, of class TextExporter.
173      * @throws Exception
174      */
175     @Test
176     public void testEncodeToByteStream() throws Exception {
177         System.out.println("encodeToByteStream");
178
179         TextExporter exporter;
180         ByteArrayOutputStream bout = new ByteArrayOutputStream();
181         byte[] barr;
182
183         exporter = new TextExporter(CS_ASCII);
184
185         bout.reset();
186         exporter.encodeToByteStream("ABC", bout);
187         assertEquals(3, bout.size());
188         barr = bout.toByteArray();
189         assertEquals(3, barr.length);
190         assertEquals((byte)0x41, barr[0]);
191         assertEquals((byte)0x42, barr[1]);
192         assertEquals((byte)0x43, barr[2]);
193
194         bout.reset();
195         exporter.encodeToByteStream("", bout);
196         assertEquals(0, bout.size());
197
198         bout.reset();
199         try{
200             exporter.encodeToByteStream("あ", bout);
201             fail();
202         }catch(CharacterCodingException e){
203             // GOOD
204         }
205
206         exporter = new TextExporter(CS_UTF8);
207         bout.reset();
208         exporter.encodeToByteStream("あ", bout);
209         assertEquals(3, bout.size());
210         barr = bout.toByteArray();
211         assertEquals(3, barr.length);
212         assertEquals((byte)0xe3, barr[0]);
213         assertEquals((byte)0x81, barr[1]);
214         assertEquals((byte)0x82, barr[2]);
215
216         return;
217     }
218
219 }