4 package jp.sourceforge.mikutoga.binio;
6 import java.io.ByteArrayOutputStream;
7 import java.io.FilterOutputStream;
8 import java.io.IOException;
9 import org.junit.After;
10 import org.junit.AfterClass;
11 import org.junit.Before;
12 import org.junit.BeforeClass;
13 import org.junit.Test;
14 import static org.junit.Assert.*;
19 public class BinaryExporterTest {
21 public BinaryExporterTest() {
25 public static void setUpClass() {
29 public static void tearDownClass() {
37 public void tearDown() {
41 * Test of close method, of class BinaryExporter.
44 public void testClose() throws Exception {
45 System.out.println("close");
47 TestOutputStream os = new TestOutputStream();
49 bex = new BinaryExporter(os);
51 assertFalse(os.closed);
53 assertTrue(os.closed);
59 * Test of flush method, of class BinaryExporter.
62 public void testFlush() throws Exception {
63 System.out.println("flush");
65 TestOutputStream os = new TestOutputStream();
67 bex = new BinaryExporter(os);
69 assertFalse(os.flushed);
71 assertTrue(os.flushed);
77 * Test of dumpByte method, of class BinaryExporter.
80 public void testDumpByte_byte() throws Exception {
81 System.out.println("dumpByte");
83 ByteArrayOutputStream bos;
87 bos = new ByteArrayOutputStream();
88 bex = new BinaryExporter(bos);
90 bex.dumpByte((byte)10);
93 barr = bos.toByteArray();
94 assertEquals(1, barr.length);
95 assertEquals((byte)10, barr[0]);
101 * Test of dumpByte method, of class BinaryExporter.
104 public void testDumpByte_int() throws Exception {
105 System.out.println("dumpByte");
107 ByteArrayOutputStream bos;
111 bos = new ByteArrayOutputStream();
112 bex = new BinaryExporter(bos);
116 barr = bos.toByteArray();
117 assertEquals(1, barr.length);
118 assertEquals((byte)10, barr[0]);
123 barr = bos.toByteArray();
124 assertEquals(1, barr.length);
125 assertEquals((byte)1, barr[0]);
132 * Test of dumpByteArray method, of class BinaryExporter.
135 public void testDumpByteArray_byteArr() throws Exception {
136 System.out.println("dumpByteArray");
138 ByteArrayOutputStream bos;
142 bos = new ByteArrayOutputStream();
143 bex = new BinaryExporter(bos);
145 bex.dumpByteArray(new byte[]{0x01, 0x02, 0x03});
148 barr = bos.toByteArray();
149 assertEquals(3, barr.length);
150 assertEquals((byte)0x01, barr[0]);
151 assertEquals((byte)0x02, barr[1]);
152 assertEquals((byte)0x03, barr[2]);
158 * Test of dumpByteArray method, of class BinaryExporter.
161 public void testDumpByteArray_3args() throws Exception {
162 System.out.println("dumpByteArray");
164 ByteArrayOutputStream bos;
168 bos = new ByteArrayOutputStream();
169 bex = new BinaryExporter(bos);
171 bex.dumpByteArray(new byte[]{0x01, 0x02, 0x03, 0x04, 0x05},
175 barr = bos.toByteArray();
176 assertEquals(3, barr.length);
177 assertEquals((byte)0x02, barr[0]);
178 assertEquals((byte)0x03, barr[1]);
179 assertEquals((byte)0x04, barr[2]);
185 * Test of dumpLeShort method, of class BinaryExporter.
188 public void testDumpLeShort_short() throws Exception {
189 System.out.println("dumpLeShort");
191 ByteArrayOutputStream bos;
195 bos = new ByteArrayOutputStream();
196 bex = new BinaryExporter(bos);
199 bex.dumpLeShort((short)( 256 * 2 + 1));
201 barr = bos.toByteArray();
202 assertEquals(2, barr.length);
203 assertEquals((byte)1, barr[0]);
204 assertEquals((byte)2, barr[1]);
207 bex.dumpLeShort((short)-2);
209 barr = bos.toByteArray();
210 assertEquals(2, barr.length);
211 assertEquals((byte)0xfe, barr[0]);
212 assertEquals((byte)0xff, barr[1]);
218 * Test of dumpLeShort method, of class BinaryExporter.
221 public void testDumpLeShort_int() throws Exception {
222 System.out.println("dumpLeShort");
224 ByteArrayOutputStream bos;
228 bos = new ByteArrayOutputStream();
229 bex = new BinaryExporter(bos);
232 bex.dumpLeShort(256 * 2 + 1);
234 barr = bos.toByteArray();
235 assertEquals(2, barr.length);
236 assertEquals((byte)1, barr[0]);
237 assertEquals((byte)2, barr[1]);
240 bex.dumpLeShort(0xff1234);
242 barr = bos.toByteArray();
243 assertEquals(2, barr.length);
244 assertEquals((byte)0x34, barr[0]);
245 assertEquals((byte)0x12, barr[1]);
250 barr = bos.toByteArray();
251 assertEquals(2, barr.length);
252 assertEquals((byte)0xfe, barr[0]);
253 assertEquals((byte)0xff, barr[1]);
259 * Test of dumpLeInt method, of class BinaryExporter.
262 public void testDumpLeInt() throws Exception {
263 System.out.println("dumpLeInt");
265 ByteArrayOutputStream bos;
269 bos = new ByteArrayOutputStream();
270 bex = new BinaryExporter(bos);
273 bex.dumpLeInt(0x12345678);
275 barr = bos.toByteArray();
276 assertEquals(4, barr.length);
277 assertEquals((byte)0x78, barr[0]);
278 assertEquals((byte)0x56, barr[1]);
279 assertEquals((byte)0x34, barr[2]);
280 assertEquals((byte)0x12, barr[3]);
285 barr = bos.toByteArray();
286 assertEquals(4, barr.length);
287 assertEquals((byte)0xfe, barr[0]);
288 assertEquals((byte)0xff, barr[1]);
289 assertEquals((byte)0xff, barr[2]);
290 assertEquals((byte)0xff, barr[3]);
296 * Test of dumpLeLong method, of class BinaryExporter.
299 public void testDumpLeLong() throws Exception {
300 System.out.println("dumpLeLong");
302 ByteArrayOutputStream bos;
306 bos = new ByteArrayOutputStream();
307 bex = new BinaryExporter(bos);
310 bex.dumpLeLong(0x12345678abcdef00L);
312 barr = bos.toByteArray();
313 assertEquals(8, barr.length);
314 assertEquals((byte)0x00, barr[0]);
315 assertEquals((byte)0xef, barr[1]);
316 assertEquals((byte)0xcd, barr[2]);
317 assertEquals((byte)0xab, barr[3]);
318 assertEquals((byte)0x78, barr[4]);
319 assertEquals((byte)0x56, barr[5]);
320 assertEquals((byte)0x34, barr[6]);
321 assertEquals((byte)0x12, barr[7]);
326 barr = bos.toByteArray();
327 assertEquals(8, barr.length);
328 assertEquals((byte)0xfe, barr[0]);
329 assertEquals((byte)0xff, barr[1]);
330 assertEquals((byte)0xff, barr[2]);
331 assertEquals((byte)0xff, barr[3]);
332 assertEquals((byte)0xff, barr[4]);
333 assertEquals((byte)0xff, barr[5]);
334 assertEquals((byte)0xff, barr[6]);
335 assertEquals((byte)0xff, barr[7]);
341 * Test of dumpLeFloat method, of class BinaryExporter.
344 public void testDumpLeFloat() throws Exception {
345 System.out.println("dumpLeFloat");
347 ByteArrayOutputStream bos;
351 bos = new ByteArrayOutputStream();
352 bex = new BinaryExporter(bos);
355 bex.dumpLeFloat(Float.intBitsToFloat(0x12345678));
357 barr = bos.toByteArray();
358 assertEquals(4, barr.length);
359 assertEquals((byte)0x78, barr[0]);
360 assertEquals((byte)0x56, barr[1]);
361 assertEquals((byte)0x34, barr[2]);
362 assertEquals((byte)0x12, barr[3]);
365 bex.dumpLeFloat(-1.5f);
367 barr = bos.toByteArray();
368 assertEquals(4, barr.length);
369 assertEquals((byte)0x00, barr[0]);
370 assertEquals((byte)0x00, barr[1]);
371 assertEquals((byte)0xc0, barr[2]);
372 assertEquals((byte)0xbf, barr[3]);
378 * Test of dumpLeDouble method, of class BinaryExporter.
381 public void testDumpLeDouble() throws Exception {
382 System.out.println("dumpLeDouble");
384 ByteArrayOutputStream bos;
388 bos = new ByteArrayOutputStream();
389 bex = new BinaryExporter(bos);
392 bex.dumpLeDouble(Double.longBitsToDouble(0x12345678abcdef00L));
394 barr = bos.toByteArray();
395 assertEquals(8, barr.length);
396 assertEquals((byte)0x00, barr[0]);
397 assertEquals((byte)0xef, barr[1]);
398 assertEquals((byte)0xcd, barr[2]);
399 assertEquals((byte)0xab, barr[3]);
400 assertEquals((byte)0x78, barr[4]);
401 assertEquals((byte)0x56, barr[5]);
402 assertEquals((byte)0x34, barr[6]);
403 assertEquals((byte)0x12, barr[7]);
406 bex.dumpLeDouble(-1.5);
408 barr = bos.toByteArray();
409 assertEquals(8, barr.length);
410 assertEquals((byte)0x00, barr[0]);
411 assertEquals((byte)0x00, barr[1]);
412 assertEquals((byte)0x00, barr[2]);
413 assertEquals((byte)0x00, barr[3]);
414 assertEquals((byte)0x00, barr[4]);
415 assertEquals((byte)0x00, barr[5]);
416 assertEquals((byte)0xf8, barr[6]);
417 assertEquals((byte)0xbf, barr[7]);
423 * Test of dumpFiller method, of class BinaryExporter.
426 public void testDumpFiller() throws Exception {
427 System.out.println("dumpFiller");
429 ByteArrayOutputStream bos;
433 bos = new ByteArrayOutputStream();
434 bex = new BinaryExporter(bos);
436 bex.dumpFiller(new byte[]{}, 1);
438 barr = bos.toByteArray();
439 assertEquals(0, barr.length);
442 bex.dumpFiller(new byte[]{0x01}, 3);
444 barr = bos.toByteArray();
445 assertEquals(3, barr.length);
446 assertEquals((byte)0x01, barr[0]);
447 assertEquals((byte)0x01, barr[1]);
448 assertEquals((byte)0x01, barr[2]);
451 bex.dumpFiller(new byte[]{0x01, 0x02}, 3);
453 barr = bos.toByteArray();
454 assertEquals(3, barr.length);
455 assertEquals((byte)0x01, barr[0]);
456 assertEquals((byte)0x02, barr[1]);
457 assertEquals((byte)0x02, barr[2]);
460 bex.dumpFiller(new byte[]{0x01, 0x02, 0x03}, 2);
462 barr = bos.toByteArray();
463 assertEquals(2, barr.length);
464 assertEquals((byte)0x01, barr[0]);
465 assertEquals((byte)0x02, barr[1]);
471 * Test of dumpFixedW31j method, of class BinaryExporter.
474 public void testDumpFixedW31j() throws Exception {
475 System.out.println("dumpFixedW31j");
477 ByteArrayOutputStream bos;
481 bos = new ByteArrayOutputStream();
482 bex = new BinaryExporter(bos);
485 bex.dumpFixedW31j("あい", 7, new byte[]{0x01, 0x02});
487 barr = bos.toByteArray();
488 assertEquals(7, barr.length);
489 assertEquals((byte)0x82, barr[0]);
490 assertEquals((byte)0xA0, barr[1]);
491 assertEquals((byte)0x82, barr[2]);
492 assertEquals((byte)0xA2, barr[3]);
493 assertEquals((byte)0x01, barr[4]);
494 assertEquals((byte)0x02, barr[5]);
495 assertEquals((byte)0x02, barr[6]);
499 bex.dumpFixedW31j("あい", 3, new byte[]{0x00});
501 }catch(IllegalTextExportException e){
507 bex.dumpFixedW31j("¤", 10, new byte[]{0x00});
509 }catch(IllegalTextExportException e){
517 * Test of dumpHollerithUtf16LE method, of class BinaryExporter.
520 public void testDumpHollerithUtf16LE() throws Exception {
521 System.out.println("dumpHollerithUtf16LE");
523 ByteArrayOutputStream bos;
527 bos = new ByteArrayOutputStream();
528 bex = new BinaryExporter(bos);
530 bex.dumpHollerithUtf16LE("あい");
532 barr = bos.toByteArray();
533 assertEquals(8, barr.length);
534 assertEquals((byte)0x04, barr[0]);
535 assertEquals((byte)0x00, barr[1]);
536 assertEquals((byte)0x00, barr[2]);
537 assertEquals((byte)0x00, barr[3]);
538 assertEquals((byte)0x42, barr[4]);
539 assertEquals((byte)0x30, barr[5]);
540 assertEquals((byte)0x44, barr[6]);
541 assertEquals((byte)0x30, barr[7]);
544 bex.dumpHollerithUtf16LE("");
546 barr = bos.toByteArray();
547 assertEquals(4, barr.length);
548 assertEquals((byte)0x00, barr[0]);
549 assertEquals((byte)0x00, barr[1]);
550 assertEquals((byte)0x00, barr[2]);
551 assertEquals((byte)0x00, barr[3]);
556 private static class TestOutputStream extends FilterOutputStream{
557 public boolean closed = false;
558 public boolean flushed = false;
561 super(new ByteArrayOutputStream());
566 public void flush() throws IOException {
573 public void close() throws IOException {