View Javadoc

1   
2   package org.yajul.io;
3   
4   import java.io.ByteArrayInputStream;
5   import java.io.ByteArrayOutputStream;
6   import java.io.IOException;
7   import java.io.InputStream;
8   import java.io.OutputStream;
9   import java.util.Arrays;
10  
11  import junit.framework.TestCase;
12  
13  import org.yajul.io.ByteCountingInputStream;
14  import org.yajul.io.ByteCountingOutputStream;
15  import org.yajul.io.EchoInputStream;
16  import org.yajul.io.HexDumpOutputStream;
17  import org.yajul.io.StreamCopier;
18  import org.yajul.io.TeeOutputStream;
19  
20  /***
21   * Tests org.yajul.io classes:
22   * <ul>
23   * <li>StreamCopier</li>
24   * <li>EchoInputStream</li>
25   * <li>TeeOutputStream</li>
26   * <li>ByteCountingInputStream</li>
27   * <li>ByteCountingOutputStream</li>
28   * </ul>
29   * User: josh
30   * Date: Sep 22, 2002
31   * Time: 12:38:27 AM
32   */
33  public class StreamCopierTest extends TestCase
34  {
35      private static final byte[] BYTES = "12345678901234567890".getBytes();
36  
37      public StreamCopierTest(String name)
38      {
39          super(name);
40      }
41  
42      public void testStreamCopy()
43      {
44          byte[] a = BYTES;
45          InputStream in = new ByteArrayInputStream(a);
46          ByteArrayOutputStream out = new ByteArrayOutputStream();
47          StreamCopier copier = new StreamCopier(in,out);
48          copier.run();
49          Arrays.equals(a,out.toByteArray());
50      }
51  
52      public void testEchoInputStream() throws IOException
53      {
54          byte[] bytes = BYTES;
55  
56          ByteArrayInputStream input = new ByteArrayInputStream(bytes);
57          ByteArrayOutputStream echo = new ByteArrayOutputStream();
58          ByteArrayOutputStream output = new ByteArrayOutputStream();
59          InputStream in = new EchoInputStream(input,echo);
60          StreamCopier.unsyncCopy(in,output,8);
61          assertTrue(Arrays.equals(bytes,output.toByteArray()));
62          assertTrue(Arrays.equals(bytes,echo.toByteArray()));
63      }
64  
65     public void test2OutputStreams() throws IOException
66      {
67          byte[] bytes = BYTES;
68  
69          ByteArrayInputStream input = new ByteArrayInputStream(bytes);
70          ByteArrayOutputStream echo = new ByteArrayOutputStream();
71          ByteArrayOutputStream output = new ByteArrayOutputStream();
72          OutputStream out = new TeeOutputStream(output,echo);
73          StreamCopier.unsyncCopy(input,out,8);
74          byte[] outputbytes = output.toByteArray();
75          assertTrue(Arrays.equals(bytes,outputbytes));
76          byte[] echobytes = echo.toByteArray();
77          assertTrue(Arrays.equals(bytes,echobytes));
78      }
79  
80      public void test3OutputStreams() throws IOException
81      {
82          byte[] bytes = BYTES;
83  
84          ByteArrayInputStream input = new ByteArrayInputStream(bytes);
85          ByteArrayOutputStream[] streams = new ByteArrayOutputStream[3];
86          for (int i = 0; i < streams.length; i++)
87          {
88              streams[i] = new ByteArrayOutputStream();
89          }
90  
91          OutputStream out = new TeeOutputStream(streams);
92          StreamCopier.unsyncCopy(input,out,8);
93  
94          for (int i = 0; i < streams.length; i++)
95          {
96              ByteArrayOutputStream stream = streams[i];
97              byte[] outputbytes = stream.toByteArray();
98              assertTrue(Arrays.equals(bytes,outputbytes));
99          }
100     }
101 
102     public void testByteCountingInputStream() throws IOException
103     {
104         byte[] bytes = BYTES;
105 
106         ByteArrayInputStream input = new ByteArrayInputStream(bytes);
107         ByteArrayOutputStream output = new ByteArrayOutputStream();
108         ByteCountingInputStream in = new ByteCountingInputStream(input);
109         StreamCopier.unsyncCopy(in,output,8);
110         assertTrue(Arrays.equals(bytes,output.toByteArray()));
111         assertEquals(bytes.length,in.getByteCount());
112     }
113 
114     public void testByteCountingOutputStream() throws IOException
115     {
116         byte[] bytes = BYTES;
117 
118         ByteArrayInputStream input = new ByteArrayInputStream(bytes);
119         ByteArrayOutputStream output = new ByteArrayOutputStream();
120         ByteCountingOutputStream out = new ByteCountingOutputStream(output);
121         StreamCopier.unsyncCopy(input,out,8);
122         assertTrue(Arrays.equals(bytes,output.toByteArray()));
123         assertEquals(bytes.length,out.getByteCount());
124     }
125 
126     public void testHexDumpOutputStream() throws IOException
127     {
128         byte[] bytes = new byte[50];
129         for (int i = 0; i < bytes.length; i++)
130             bytes[i] = (byte)i;
131 
132         ByteArrayInputStream input = new ByteArrayInputStream(bytes);
133         ByteArrayOutputStream output = new ByteArrayOutputStream();
134         HexDumpOutputStream out = new HexDumpOutputStream(output,16);
135         StreamCopier.unsyncCopy(input,out,8);
136         out.flush();
137         // TODO: Test the result.
138     }
139 
140 }