4 package jp.sfjp.mikutoga.bin.export;
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.*;
20 public class BinaryExporterTest {
22 public BinaryExporterTest() {
26 public static void setUpClass() {
30 public static void tearDownClass() {
38 public void tearDown() {
42 * Test of constructor, of class BinaryExporter.
46 public void testCons() throws Exception {
47 System.out.println("constructor");
52 stream = new ByteArrayOutputStream();
53 ex = new BinaryExporter(stream);
56 ex = new BinaryExporter(null);
58 }catch(NullPointerException e){
66 * Test of close method, of class BinaryExporter.
70 public void testClose() throws Exception {
71 System.out.println("close");
73 TestOutputStream os = new TestOutputStream();
75 bex = new BinaryExporter(os);
77 assertFalse(os.closed);
79 assertTrue(os.closed);
85 * Test of flush method, of class BinaryExporter.
89 public void testFlush() throws Exception {
90 System.out.println("flush");
92 TestOutputStream os = new TestOutputStream();
94 bex = new BinaryExporter(os);
96 assertFalse(os.flushed);
98 assertTrue(os.flushed);
104 * Test of dumpByte method, of class BinaryExporter.
108 public void testDumpByte_byte() throws Exception {
109 System.out.println("dumpByte");
111 ByteArrayOutputStream bos;
115 bos = new ByteArrayOutputStream();
116 bex = new BinaryExporter(bos);
118 bex.dumpByte((byte)10);
121 barr = bos.toByteArray();
122 assertEquals(1, barr.length);
123 assertEquals((byte)10, barr[0]);
129 * Test of dumpByte method, of class BinaryExporter.
133 public void testDumpByte_int() throws Exception {
134 System.out.println("dumpByte");
136 ByteArrayOutputStream bos;
140 bos = new ByteArrayOutputStream();
141 bex = new BinaryExporter(bos);
145 barr = bos.toByteArray();
146 assertEquals(1, barr.length);
147 assertEquals((byte)10, barr[0]);
152 barr = bos.toByteArray();
153 assertEquals(1, barr.length);
154 assertEquals((byte)1, barr[0]);
161 * Test of dumpByteArray method, of class BinaryExporter.
165 public void testDumpByteArray_byteArr() throws Exception {
166 System.out.println("dumpByteArray");
168 ByteArrayOutputStream bos;
172 bos = new ByteArrayOutputStream();
173 bex = new BinaryExporter(bos);
175 bex.dumpByteArray(new byte[]{0x01, 0x02, 0x03});
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]);
188 * Test of dumpByteArray method, of class BinaryExporter.
192 public void testDumpByteArray_3args() throws Exception {
193 System.out.println("dumpByteArray");
195 ByteArrayOutputStream bos;
199 bos = new ByteArrayOutputStream();
200 bex = new BinaryExporter(bos);
202 bex.dumpByteArray(new byte[]{0x01, 0x02, 0x03, 0x04, 0x05},
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]);
216 * Test of dumpLeShort method, of class BinaryExporter.
220 public void testDumpLeShort_short() throws Exception {
221 System.out.println("dumpLeShort");
223 ByteArrayOutputStream bos;
227 bos = new ByteArrayOutputStream();
228 bex = new BinaryExporter(bos);
231 bex.dumpLeShort((short)( 256 * 2 + 1));
233 barr = bos.toByteArray();
234 assertEquals(2, barr.length);
235 assertEquals((byte)1, barr[0]);
236 assertEquals((byte)2, barr[1]);
239 bex.dumpLeShort((short)-2);
241 barr = bos.toByteArray();
242 assertEquals(2, barr.length);
243 assertEquals((byte)0xfe, barr[0]);
244 assertEquals((byte)0xff, barr[1]);
250 * Test of dumpLeShort method, of class BinaryExporter.
254 public void testDumpLeShort_int() throws Exception {
255 System.out.println("dumpLeShort");
257 ByteArrayOutputStream bos;
261 bos = new ByteArrayOutputStream();
262 bex = new BinaryExporter(bos);
265 bex.dumpLeShort(256 * 2 + 1);
267 barr = bos.toByteArray();
268 assertEquals(2, barr.length);
269 assertEquals((byte)1, barr[0]);
270 assertEquals((byte)2, barr[1]);
273 bex.dumpLeShort(0xff1234);
275 barr = bos.toByteArray();
276 assertEquals(2, barr.length);
277 assertEquals((byte)0x34, barr[0]);
278 assertEquals((byte)0x12, barr[1]);
283 barr = bos.toByteArray();
284 assertEquals(2, barr.length);
285 assertEquals((byte)0xfe, barr[0]);
286 assertEquals((byte)0xff, barr[1]);
292 * Test of dumpLeInt method, of class BinaryExporter.
296 public void testDumpLeInt() throws Exception {
297 System.out.println("dumpLeInt");
299 ByteArrayOutputStream bos;
303 bos = new ByteArrayOutputStream();
304 bex = new BinaryExporter(bos);
307 bex.dumpLeInt(0x12345678);
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]);
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]);
330 * Test of dumpLeLong method, of class BinaryExporter.
334 public void testDumpLeLong() throws Exception {
335 System.out.println("dumpLeLong");
337 ByteArrayOutputStream bos;
341 bos = new ByteArrayOutputStream();
342 bex = new BinaryExporter(bos);
345 bex.dumpLeLong(0x12345678abcdef00L);
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]);
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]);
376 * Test of dumpLeFloat method, of class BinaryExporter.
380 public void testDumpLeFloat() throws Exception {
381 System.out.println("dumpLeFloat");
383 ByteArrayOutputStream bos;
387 bos = new ByteArrayOutputStream();
388 bex = new BinaryExporter(bos);
391 bex.dumpLeFloat(Float.intBitsToFloat(0x12345678));
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]);
401 bex.dumpLeFloat(-1.5f);
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]);
414 * Test of dumpLeDouble method, of class BinaryExporter.
418 public void testDumpLeDouble() throws Exception {
419 System.out.println("dumpLeDouble");
421 ByteArrayOutputStream bos;
425 bos = new ByteArrayOutputStream();
426 bex = new BinaryExporter(bos);
429 bex.dumpLeDouble(Double.longBitsToDouble(0x12345678abcdef00L));
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]);
443 bex.dumpLeDouble(-1.5);
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]);
460 * Test of dumpFiller method, of class BinaryExporter.
464 public void testDumpFiller() throws Exception {
465 System.out.println("dumpFiller");
467 ByteArrayOutputStream bos;
471 bos = new ByteArrayOutputStream();
472 bex = new BinaryExporter(bos);
474 bex.dumpFiller(new byte[]{}, 1);
476 barr = bos.toByteArray();
477 assertEquals(0, barr.length);
480 bex.dumpFiller(new byte[]{0x01}, 3);
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]);
489 bex.dumpFiller(new byte[]{0x01, 0x02}, 3);
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]);
498 bex.dumpFiller(new byte[]{0x01, 0x02, 0x03}, 2);
500 barr = bos.toByteArray();
501 assertEquals(2, barr.length);
502 assertEquals((byte)0x01, barr[0]);
503 assertEquals((byte)0x02, barr[1]);
506 bex.dumpFiller(new byte[]{}, 3);
508 assertEquals(0, bos.size());
511 bex.dumpFiller(new byte[]{0x01, 0x02, 0x03}, 0);
513 assertEquals(0, bos.size());
519 * Test of dumpFixedW31j method, of class BinaryExporter.
523 public void testDumpFixedW31j() throws Exception {
524 System.out.println("dumpFixedW31j");
526 ByteArrayOutputStream bos;
530 bos = new ByteArrayOutputStream();
531 bex = new BinaryExporter(bos);
534 bex.dumpFixedW31j("あい", 7, new byte[]{0x01, 0x02});
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]);
547 bex.dumpFixedW31j("あい", 4, new byte[]{0x01, 0x02});
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]);
557 bex.dumpFixedW31j("あい", 0, new byte[]{0x01, 0x02});
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]);
568 bex.dumpFixedW31j("あい", 3, new byte[]{0x00});
570 }catch(IllegalTextExportException e){
576 bex.dumpFixedW31j("¤", 10, new byte[]{0x00});
578 }catch(IllegalTextExportException e){
586 * Test of dumpHollerithUtf16LE method, of class BinaryExporter.
590 public void testDumpHollerithUtf16LE() throws Exception {
591 System.out.println("dumpHollerithUtf16LE");
593 ByteArrayOutputStream bos;
597 bos = new ByteArrayOutputStream();
598 bex = new BinaryExporter(bos);
600 bex.dumpHollerithUtf16LE("あい");
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]);
614 bex.dumpHollerithUtf16LE("");
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]);
626 private static class TestOutputStream extends FilterOutputStream{
627 public boolean closed = false;
628 public boolean flushed = false;
631 super(new ByteArrayOutputStream());
636 public void flush() throws IOException {
643 public void close() throws IOException {