OSDN Git Service

2.102.3-SNAPSHOT 開発開始
[mikutoga/TogaGem.git] / src / test / java / jp / sfjp / mikutoga / bin / export / BinaryExporterTest.java
1 /*
2  */
3
4 package jp.sfjp.mikutoga.bin.export;
5
6 import java.io.ByteArrayOutputStream;
7 import java.io.FilterOutputStream;
8 import java.io.IOException;
9 import java.io.OutputStream;
10 import org.junit.After;
11 import org.junit.AfterClass;
12 import org.junit.Before;
13 import org.junit.BeforeClass;
14 import org.junit.Test;
15 import static org.junit.Assert.*;
16
17 /**
18  *
19  */
20 public class BinaryExporterTest {
21
22     public BinaryExporterTest() {
23     }
24
25     @BeforeClass
26     public static void setUpClass() {
27     }
28
29     @AfterClass
30     public static void tearDownClass() {
31     }
32
33     @Before
34     public void setUp() {
35     }
36
37     @After
38     public void tearDown() {
39     }
40
41     /**
42      * Test of constructor, of class BinaryExporter.
43      * @throws Exception
44      */
45     @Test
46     public void testCons() throws Exception {
47         System.out.println("constructor");
48
49         BinaryExporter ex;
50         OutputStream stream;
51
52         stream = new ByteArrayOutputStream();
53         ex = new BinaryExporter(stream);
54
55         try{
56             ex = new BinaryExporter(null);
57             fail();
58         }catch(NullPointerException e){
59             // GOOD
60         }
61
62         return;
63     }
64
65     /**
66      * Test of close method, of class BinaryExporter.
67      * @throws Exception
68      */
69     @Test
70     public void testClose() throws Exception {
71         System.out.println("close");
72
73         TestOutputStream os = new TestOutputStream();
74         BinaryExporter bex;
75         bex = new BinaryExporter(os);
76
77         assertFalse(os.closed);
78         bex.close();
79         assertTrue(os.closed);
80
81         return;
82     }
83
84     /**
85      * Test of flush method, of class BinaryExporter.
86      * @throws Exception
87      */
88     @Test
89     public void testFlush() throws Exception {
90         System.out.println("flush");
91
92         TestOutputStream os = new TestOutputStream();
93         BinaryExporter bex;
94         bex = new BinaryExporter(os);
95
96         assertFalse(os.flushed);
97         bex.flush();
98         assertTrue(os.flushed);
99
100         return;
101     }
102
103     /**
104      * Test of dumpByte method, of class BinaryExporter.
105      * @throws Exception
106      */
107     @Test
108     public void testDumpByte_byte() throws Exception {
109         System.out.println("dumpByte");
110
111         ByteArrayOutputStream bos;
112         BinaryExporter bex;
113         byte[] barr;
114
115         bos = new ByteArrayOutputStream();
116         bex = new BinaryExporter(bos);
117
118         bex.dumpByte((byte)10);
119         bex.flush();
120
121         barr = bos.toByteArray();
122         assertEquals(1, barr.length);
123         assertEquals((byte)10, barr[0]);
124
125         return;
126     }
127
128     /**
129      * Test of dumpByte method, of class BinaryExporter.
130      * @throws Exception
131      */
132     @Test
133     public void testDumpByte_int() throws Exception {
134         System.out.println("dumpByte");
135
136         ByteArrayOutputStream bos;
137         BinaryExporter bex;
138         byte[] barr;
139
140         bos = new ByteArrayOutputStream();
141         bex = new BinaryExporter(bos);
142
143         bex.dumpByte(10);
144         bex.flush();
145         barr = bos.toByteArray();
146         assertEquals(1, barr.length);
147         assertEquals((byte)10, barr[0]);
148
149         bos.reset();
150         bex.dumpByte(257);
151         bex.flush();
152         barr = bos.toByteArray();
153         assertEquals(1, barr.length);
154         assertEquals((byte)1, barr[0]);
155
156
157         return;
158     }
159
160     /**
161      * Test of dumpByteArray method, of class BinaryExporter.
162      * @throws Exception
163      */
164     @Test
165     public void testDumpByteArray_byteArr() throws Exception {
166         System.out.println("dumpByteArray");
167
168         ByteArrayOutputStream bos;
169         BinaryExporter bex;
170         byte[] barr;
171
172         bos = new ByteArrayOutputStream();
173         bex = new BinaryExporter(bos);
174
175         bex.dumpByteArray(new byte[]{0x01, 0x02, 0x03});
176         bex.flush();
177
178         barr = bos.toByteArray();
179         assertEquals(3, barr.length);
180         assertEquals((byte)0x01, barr[0]);
181         assertEquals((byte)0x02, barr[1]);
182         assertEquals((byte)0x03, barr[2]);
183
184         return;
185     }
186
187     /**
188      * Test of dumpByteArray method, of class BinaryExporter.
189      * @throws Exception
190      */
191     @Test
192     public void testDumpByteArray_3args() throws Exception {
193         System.out.println("dumpByteArray");
194
195         ByteArrayOutputStream bos;
196         BinaryExporter bex;
197         byte[] barr;
198
199         bos = new ByteArrayOutputStream();
200         bex = new BinaryExporter(bos);
201
202         bex.dumpByteArray(new byte[]{0x01, 0x02, 0x03, 0x04, 0x05},
203                           1, 3);
204         bex.flush();
205
206         barr = bos.toByteArray();
207         assertEquals(3, barr.length);
208         assertEquals((byte)0x02, barr[0]);
209         assertEquals((byte)0x03, barr[1]);
210         assertEquals((byte)0x04, barr[2]);
211
212         return;
213     }
214
215     /**
216      * Test of dumpLeShort method, of class BinaryExporter.
217      * @throws Exception
218      */
219     @Test
220     public void testDumpLeShort_short() throws Exception {
221         System.out.println("dumpLeShort");
222
223         ByteArrayOutputStream bos;
224         BinaryExporter bex;
225         byte[] barr;
226
227         bos = new ByteArrayOutputStream();
228         bex = new BinaryExporter(bos);
229
230         bos.reset();
231         bex.dumpLeShort((short)( 256 * 2 + 1));
232         bex.flush();
233         barr = bos.toByteArray();
234         assertEquals(2, barr.length);
235         assertEquals((byte)1, barr[0]);
236         assertEquals((byte)2, barr[1]);
237
238         bos.reset();
239         bex.dumpLeShort((short)-2);
240         bex.flush();
241         barr = bos.toByteArray();
242         assertEquals(2, barr.length);
243         assertEquals((byte)0xfe, barr[0]);
244         assertEquals((byte)0xff, barr[1]);
245
246         return;
247     }
248
249     /**
250      * Test of dumpLeShort method, of class BinaryExporter.
251      * @throws Exception
252      */
253     @Test
254     public void testDumpLeShort_int() throws Exception {
255         System.out.println("dumpLeShort");
256
257         ByteArrayOutputStream bos;
258         BinaryExporter bex;
259         byte[] barr;
260
261         bos = new ByteArrayOutputStream();
262         bex = new BinaryExporter(bos);
263
264         bos.reset();
265         bex.dumpLeShort(256 * 2 + 1);
266         bex.flush();
267         barr = bos.toByteArray();
268         assertEquals(2, barr.length);
269         assertEquals((byte)1, barr[0]);
270         assertEquals((byte)2, barr[1]);
271
272         bos.reset();
273         bex.dumpLeShort(0xff1234);
274         bex.flush();
275         barr = bos.toByteArray();
276         assertEquals(2, barr.length);
277         assertEquals((byte)0x34, barr[0]);
278         assertEquals((byte)0x12, barr[1]);
279
280         bos.reset();
281         bex.dumpLeShort(-2);
282         bex.flush();
283         barr = bos.toByteArray();
284         assertEquals(2, barr.length);
285         assertEquals((byte)0xfe, barr[0]);
286         assertEquals((byte)0xff, barr[1]);
287
288         return;
289     }
290
291     /**
292      * Test of dumpLeInt method, of class BinaryExporter.
293      * @throws Exception
294      */
295     @Test
296     public void testDumpLeInt() throws Exception {
297         System.out.println("dumpLeInt");
298
299         ByteArrayOutputStream bos;
300         BinaryExporter bex;
301         byte[] barr;
302
303         bos = new ByteArrayOutputStream();
304         bex = new BinaryExporter(bos);
305
306         bos.reset();
307         bex.dumpLeInt(0x12345678);
308         bex.flush();
309         barr = bos.toByteArray();
310         assertEquals(4, barr.length);
311         assertEquals((byte)0x78, barr[0]);
312         assertEquals((byte)0x56, barr[1]);
313         assertEquals((byte)0x34, barr[2]);
314         assertEquals((byte)0x12, barr[3]);
315
316         bos.reset();
317         bex.dumpLeInt(-2);
318         bex.flush();
319         barr = bos.toByteArray();
320         assertEquals(4, barr.length);
321         assertEquals((byte)0xfe, barr[0]);
322         assertEquals((byte)0xff, barr[1]);
323         assertEquals((byte)0xff, barr[2]);
324         assertEquals((byte)0xff, barr[3]);
325
326         return;
327     }
328
329     /**
330      * Test of dumpLeLong method, of class BinaryExporter.
331      * @throws Exception
332      */
333     @Test
334     public void testDumpLeLong() throws Exception {
335         System.out.println("dumpLeLong");
336
337         ByteArrayOutputStream bos;
338         BinaryExporter bex;
339         byte[] barr;
340
341         bos = new ByteArrayOutputStream();
342         bex = new BinaryExporter(bos);
343
344         bos.reset();
345         bex.dumpLeLong(0x12345678abcdef00L);
346         bex.flush();
347         barr = bos.toByteArray();
348         assertEquals(8, barr.length);
349         assertEquals((byte)0x00, barr[0]);
350         assertEquals((byte)0xef, barr[1]);
351         assertEquals((byte)0xcd, barr[2]);
352         assertEquals((byte)0xab, barr[3]);
353         assertEquals((byte)0x78, barr[4]);
354         assertEquals((byte)0x56, barr[5]);
355         assertEquals((byte)0x34, barr[6]);
356         assertEquals((byte)0x12, barr[7]);
357
358         bos.reset();
359         bex.dumpLeLong(-2L);
360         bex.flush();
361         barr = bos.toByteArray();
362         assertEquals(8, barr.length);
363         assertEquals((byte)0xfe, barr[0]);
364         assertEquals((byte)0xff, barr[1]);
365         assertEquals((byte)0xff, barr[2]);
366         assertEquals((byte)0xff, barr[3]);
367         assertEquals((byte)0xff, barr[4]);
368         assertEquals((byte)0xff, barr[5]);
369         assertEquals((byte)0xff, barr[6]);
370         assertEquals((byte)0xff, barr[7]);
371
372         return;
373     }
374
375     /**
376      * Test of dumpLeFloat method, of class BinaryExporter.
377      * @throws Exception
378      */
379     @Test
380     public void testDumpLeFloat() throws Exception {
381         System.out.println("dumpLeFloat");
382
383         ByteArrayOutputStream bos;
384         BinaryExporter bex;
385         byte[] barr;
386
387         bos = new ByteArrayOutputStream();
388         bex = new BinaryExporter(bos);
389
390         bos.reset();
391         bex.dumpLeFloat(Float.intBitsToFloat(0x12345678));
392         bex.flush();
393         barr = bos.toByteArray();
394         assertEquals(4, barr.length);
395         assertEquals((byte)0x78, barr[0]);
396         assertEquals((byte)0x56, barr[1]);
397         assertEquals((byte)0x34, barr[2]);
398         assertEquals((byte)0x12, barr[3]);
399
400         bos.reset();
401         bex.dumpLeFloat(-1.5f);
402         bex.flush();
403         barr = bos.toByteArray();
404         assertEquals(4, barr.length);
405         assertEquals((byte)0x00, barr[0]);
406         assertEquals((byte)0x00, barr[1]);
407         assertEquals((byte)0xc0, barr[2]);
408         assertEquals((byte)0xbf, barr[3]);
409
410         return;
411     }
412
413     /**
414      * Test of dumpLeDouble method, of class BinaryExporter.
415      * @throws Exception
416      */
417     @Test
418     public void testDumpLeDouble() throws Exception {
419         System.out.println("dumpLeDouble");
420
421         ByteArrayOutputStream bos;
422         BinaryExporter bex;
423         byte[] barr;
424
425         bos = new ByteArrayOutputStream();
426         bex = new BinaryExporter(bos);
427
428         bos.reset();
429         bex.dumpLeDouble(Double.longBitsToDouble(0x12345678abcdef00L));
430         bex.flush();
431         barr = bos.toByteArray();
432         assertEquals(8, barr.length);
433         assertEquals((byte)0x00, barr[0]);
434         assertEquals((byte)0xef, barr[1]);
435         assertEquals((byte)0xcd, barr[2]);
436         assertEquals((byte)0xab, barr[3]);
437         assertEquals((byte)0x78, barr[4]);
438         assertEquals((byte)0x56, barr[5]);
439         assertEquals((byte)0x34, barr[6]);
440         assertEquals((byte)0x12, barr[7]);
441
442         bos.reset();
443         bex.dumpLeDouble(-1.5);
444         bex.flush();
445         barr = bos.toByteArray();
446         assertEquals(8, barr.length);
447         assertEquals((byte)0x00, barr[0]);
448         assertEquals((byte)0x00, barr[1]);
449         assertEquals((byte)0x00, barr[2]);
450         assertEquals((byte)0x00, barr[3]);
451         assertEquals((byte)0x00, barr[4]);
452         assertEquals((byte)0x00, barr[5]);
453         assertEquals((byte)0xf8, barr[6]);
454         assertEquals((byte)0xbf, barr[7]);
455
456         return;
457     }
458
459     /**
460      * Test of dumpFiller method, of class BinaryExporter.
461      * @throws Exception
462      */
463     @Test
464     public void testDumpFiller() throws Exception {
465         System.out.println("dumpFiller");
466
467         ByteArrayOutputStream bos;
468         BinaryExporter bex;
469         byte[] barr;
470
471         bos = new ByteArrayOutputStream();
472         bex = new BinaryExporter(bos);
473
474         bex.dumpFiller(new byte[]{}, 1);
475         bex.flush();
476         barr = bos.toByteArray();
477         assertEquals(0, barr.length);
478
479         bos.reset();
480         bex.dumpFiller(new byte[]{0x01}, 3);
481         bex.flush();
482         barr = bos.toByteArray();
483         assertEquals(3, barr.length);
484         assertEquals((byte)0x01, barr[0]);
485         assertEquals((byte)0x01, barr[1]);
486         assertEquals((byte)0x01, barr[2]);
487
488         bos.reset();
489         bex.dumpFiller(new byte[]{0x01, 0x02}, 3);
490         bex.flush();
491         barr = bos.toByteArray();
492         assertEquals(3, barr.length);
493         assertEquals((byte)0x01, barr[0]);
494         assertEquals((byte)0x02, barr[1]);
495         assertEquals((byte)0x02, barr[2]);
496
497         bos.reset();
498         bex.dumpFiller(new byte[]{0x01, 0x02, 0x03}, 2);
499         bex.flush();
500         barr = bos.toByteArray();
501         assertEquals(2, barr.length);
502         assertEquals((byte)0x01, barr[0]);
503         assertEquals((byte)0x02, barr[1]);
504
505         bos.reset();
506         bex.dumpFiller(new byte[]{}, 3);
507         bex.flush();
508         assertEquals(0, bos.size());
509
510         bos.reset();
511         bex.dumpFiller(new byte[]{0x01, 0x02, 0x03}, 0);
512         bex.flush();
513         assertEquals(0, bos.size());
514
515         return;
516     }
517
518     /**
519      * Test of dumpFixedW31j method, of class BinaryExporter.
520      * @throws Exception
521      */
522     @Test
523     public void testDumpFixedW31j() throws Exception {
524         System.out.println("dumpFixedW31j");
525
526         ByteArrayOutputStream bos;
527         BinaryExporter bex;
528         byte[] barr;
529
530         bos = new ByteArrayOutputStream();
531         bex = new BinaryExporter(bos);
532
533         bos.reset();
534         bex.dumpFixedW31j("あい", 7, new byte[]{0x01, 0x02});
535         bex.flush();
536         barr = bos.toByteArray();
537         assertEquals(7, barr.length);
538         assertEquals((byte)0x82, barr[0]);
539         assertEquals((byte)0xA0, barr[1]);
540         assertEquals((byte)0x82, barr[2]);
541         assertEquals((byte)0xA2, barr[3]);
542         assertEquals((byte)0x01, barr[4]);
543         assertEquals((byte)0x02, barr[5]);
544         assertEquals((byte)0x02, barr[6]);
545
546         bos.reset();
547         bex.dumpFixedW31j("あい", 4, new byte[]{0x01, 0x02});
548         bex.flush();
549         barr = bos.toByteArray();
550         assertEquals(4, barr.length);
551         assertEquals((byte)0x82, barr[0]);
552         assertEquals((byte)0xA0, barr[1]);
553         assertEquals((byte)0x82, barr[2]);
554         assertEquals((byte)0xA2, barr[3]);
555
556         bos.reset();
557         bex.dumpFixedW31j("あい", 0, new byte[]{0x01, 0x02});
558         bex.flush();
559         barr = bos.toByteArray();
560         assertEquals(4, barr.length);
561         assertEquals((byte)0x82, barr[0]);
562         assertEquals((byte)0xA0, barr[1]);
563         assertEquals((byte)0x82, barr[2]);
564         assertEquals((byte)0xA2, barr[3]);
565
566         bos.reset();
567         try{
568             bex.dumpFixedW31j("あい", 3, new byte[]{0x00});
569             fail();
570         }catch(IllegalTextExportException e){
571             // GOOD
572         }
573
574         bos.reset();
575         try{
576             bex.dumpFixedW31j("¤", 10, new byte[]{0x00});
577             fail();
578         }catch(IllegalTextExportException e){
579             // GOOD
580         }
581
582         return;
583     }
584
585     /**
586      * Test of dumpHollerithUtf16LE method, of class BinaryExporter.
587      * @throws Exception
588      */
589     @Test
590     public void testDumpHollerithUtf16LE() throws Exception {
591         System.out.println("dumpHollerithUtf16LE");
592
593         ByteArrayOutputStream bos;
594         BinaryExporter bex;
595         byte[] barr;
596
597         bos = new ByteArrayOutputStream();
598         bex = new BinaryExporter(bos);
599
600         bex.dumpHollerithUtf16LE("あい");
601         bex.flush();
602         barr = bos.toByteArray();
603         assertEquals(8, barr.length);
604         assertEquals((byte)0x04, barr[0]);
605         assertEquals((byte)0x00, barr[1]);
606         assertEquals((byte)0x00, barr[2]);
607         assertEquals((byte)0x00, barr[3]);
608         assertEquals((byte)0x42, barr[4]);
609         assertEquals((byte)0x30, barr[5]);
610         assertEquals((byte)0x44, barr[6]);
611         assertEquals((byte)0x30, barr[7]);
612
613         bos.reset();
614         bex.dumpHollerithUtf16LE("");
615         bex.flush();
616         barr = bos.toByteArray();
617         assertEquals(4, barr.length);
618         assertEquals((byte)0x00, barr[0]);
619         assertEquals((byte)0x00, barr[1]);
620         assertEquals((byte)0x00, barr[2]);
621         assertEquals((byte)0x00, barr[3]);
622
623         return;
624     }
625
626     private static class TestOutputStream extends FilterOutputStream{
627         public boolean closed = false;
628         public boolean flushed = false;
629
630         TestOutputStream(){
631             super(new ByteArrayOutputStream());
632             return;
633         }
634
635         @Override
636         public void flush() throws IOException {
637             super.flush();
638             this.flushed = true;
639             return;
640         }
641
642         @Override
643         public void close() throws IOException {
644             super.close();
645             this.closed = true;
646             return;
647         }
648
649     }
650
651 }