OSDN Git Service

JRE1.6と1.7の差を吸収
[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         // JRE 1.6 と 1.7 でびみょーに違う
110         float maxb = CS_UTF8.newEncoder().maxBytesPerChar();
111
112         exporter.setCharBufSize(1);
113         int minb = (int)( StrictMath.floor(maxb) );
114         exporter.setByteBufSize(minb);
115         bout.reset();
116         try{
117             exporter.encodeToByteStream("あいう", bout);
118         }catch(CharacterCodingException e){
119             fail();
120         }
121         assertEquals(9, bout.size());
122
123         try{
124             int failSize = minb - 1;
125             exporter.setByteBufSize(failSize);
126             fail();
127         }catch(IllegalArgumentException e){
128             // GOOD
129         }
130
131         try{
132             exporter.setCharBufSize(0);
133             fail();
134         }catch(IllegalArgumentException e){
135             // GOOD
136         }
137
138         return;
139     }
140
141     /**
142      * Test of dumpText method, of class TextExporter.
143      * @throws Exception
144      */
145     @Test
146     public void testDumpText() throws Exception {
147         System.out.println("dumpText");
148
149         TextExporter exporter;
150         ByteArrayOutputStream bout;
151         byte[] barr;
152
153         exporter = new TextExporter(CS_ASCII);
154
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]);
163
164         PipedOutputStream pout;
165         pout = new PipedOutputStream();
166         try{
167             exporter.dumpText("ABC", pout);
168             fail();
169         }catch(IOException e){
170             // GOOD
171         }
172
173         return;
174     }
175
176     /**
177      * Test of encodeToByteStream method, of class TextExporter.
178      * @throws Exception
179      */
180     @Test
181     public void testEncodeToByteStream() throws Exception {
182         System.out.println("encodeToByteStream");
183
184         TextExporter exporter;
185         ByteArrayOutputStream bout = new ByteArrayOutputStream();
186         byte[] barr;
187
188         exporter = new TextExporter(CS_ASCII);
189
190         bout.reset();
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]);
198
199         bout.reset();
200         exporter.encodeToByteStream("", bout);
201         assertEquals(0, bout.size());
202
203         bout.reset();
204         try{
205             exporter.encodeToByteStream("あ", bout);
206             fail();
207         }catch(CharacterCodingException e){
208             // GOOD
209         }
210
211         exporter = new TextExporter(CS_UTF8);
212         bout.reset();
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]);
220
221         return;
222     }
223
224 }