1 16 17 18 package org.apache.commons.io.output; 19 20 21 import java.io.ByteArrayOutputStream ; 22 import java.io.IOException ; 23 24 import junit.framework.TestCase; 25 26 27 31 32 public class CountingOutputStreamTest extends TestCase { 33 34 public CountingOutputStreamTest(String name) { 35 super(name); 36 } 37 38 public void testCounting() throws IOException { 39 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 40 CountingOutputStream cos = new CountingOutputStream(baos); 41 42 for(int i = 0; i < 20; i++) { 43 cos.write(i); 44 } 45 assertByteArrayEquals("CountingOutputStream.write(int)", baos.toByteArray(), 0, 20); 46 assertEquals("CountingOutputStream.getCount()", cos.getCount(), 20); 47 48 byte[] array = new byte[10]; 49 for(int i = 20; i < 30; i++) { 50 array[i-20] = (byte)i; 51 } 52 cos.write(array); 53 assertByteArrayEquals("CountingOutputStream.write(byte[])", baos.toByteArray(), 0, 30); 54 assertEquals("CountingOutputStream.getCount()", cos.getCount(), 30); 55 56 for(int i = 25; i < 35; i++) { 57 array[i-25] = (byte)i; 58 } 59 cos.write(array, 5, 5); 60 assertByteArrayEquals("CountingOutputStream.write(byte[], int, int)", baos.toByteArray(), 0, 35); 61 assertEquals("CountingOutputStream.getCount()", cos.getCount(), 35); 62 } 63 64 private void assertByteArrayEquals(String msg, byte[] array, int start, int end) { 65 assertEquals(msg+": array size mismatch", end-start, 66 array.length ); 67 68 for (int i = start; i < end; i++) { 69 assertEquals(msg+": array[ " + i + "] mismatch", array[i], 70 i); 71 } 72 } 73 74 } 75 | Popular Tags |