4 package jp.sfjp.mikutoga.bin.export;
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.*;
22 public class TextExporterTest {
24 private static Charset CS_ASCII = Charset.forName("US-ASCII");
25 private static Charset CS_UTF8 = Charset.forName("UTF-8");
27 public TextExporterTest() {
31 public static void setUpClass() {
35 public static void tearDownClass() {
43 public void tearDown() {
47 * Test of constructor, of class TextExporter.
51 public void testCons() throws Exception {
52 System.out.println("constructor");
57 exp = new TextExporter((Charset)null);
59 }catch(NullPointerException e){
64 exp = new TextExporter((CharsetEncoder)null);
66 }catch(NullPointerException e){
74 * Test of getEncoder method, of class TextExporter.
77 public void testGetEncoder() {
78 System.out.println("getEncoder");
81 cs = Charset.forName("US-ASCII");
82 CharsetEncoder usenc = cs.newEncoder();
84 TextExporter exporter;
87 exporter = new TextExporter(cs);
88 enc = exporter.getEncoder();
89 assertEquals(usenc.charset(), enc.charset());
91 exporter = new TextExporter(usenc);
92 enc = exporter.getEncoder();
93 assertEquals(usenc.charset(), enc.charset());
99 * Test of setCharBufSize method, of class TextExporter.
102 public void testSetBufSize() {
103 System.out.println("setCharBufSize");
105 TextExporter exporter;
106 ByteArrayOutputStream bout = new ByteArrayOutputStream();
108 exporter = new TextExporter(CS_UTF8);
109 // JRE 1.6 と 1.7 でびみょーに違う
110 float maxb = CS_UTF8.newEncoder().maxBytesPerChar();
112 exporter.setCharBufSize(1);
113 int minb = (int)( StrictMath.floor(maxb) );
114 exporter.setByteBufSize(minb);
117 exporter.encodeToByteStream("あいう", bout);
118 }catch(CharacterCodingException e){
121 assertEquals(9, bout.size());
124 int failSize = minb - 1;
125 exporter.setByteBufSize(failSize);
127 }catch(IllegalArgumentException e){
132 exporter.setCharBufSize(0);
134 }catch(IllegalArgumentException e){
142 * Test of dumpText method, of class TextExporter.
146 public void testDumpText() throws Exception {
147 System.out.println("dumpText");
149 TextExporter exporter;
150 ByteArrayOutputStream bout;
153 exporter = new TextExporter(CS_ASCII);
155 bout = new ByteArrayOutputStream();
156 exporter.dumpText("ABC", bout);
157 assertEquals(3, bout.size());
158 barr = bout.toByteArray();
159 assertEquals(3, barr.length);
160 assertEquals((byte)0x41, barr[0]);
161 assertEquals((byte)0x42, barr[1]);
162 assertEquals((byte)0x43, barr[2]);
164 PipedOutputStream pout;
165 pout = new PipedOutputStream();
167 exporter.dumpText("ABC", pout);
169 }catch(IOException e){
177 * Test of encodeToByteStream method, of class TextExporter.
181 public void testEncodeToByteStream() throws Exception {
182 System.out.println("encodeToByteStream");
184 TextExporter exporter;
185 ByteArrayOutputStream bout = new ByteArrayOutputStream();
188 exporter = new TextExporter(CS_ASCII);
191 exporter.encodeToByteStream("ABC", bout);
192 assertEquals(3, bout.size());
193 barr = bout.toByteArray();
194 assertEquals(3, barr.length);
195 assertEquals((byte)0x41, barr[0]);
196 assertEquals((byte)0x42, barr[1]);
197 assertEquals((byte)0x43, barr[2]);
200 exporter.encodeToByteStream("", bout);
201 assertEquals(0, bout.size());
205 exporter.encodeToByteStream("あ", bout);
207 }catch(CharacterCodingException e){
211 exporter = new TextExporter(CS_UTF8);
213 exporter.encodeToByteStream("あ", bout);
214 assertEquals(3, bout.size());
215 barr = bout.toByteArray();
216 assertEquals(3, barr.length);
217 assertEquals((byte)0xe3, barr[0]);
218 assertEquals((byte)0x81, barr[1]);
219 assertEquals((byte)0x82, barr[2]);