1   /***
2    * Redistribution  and use  in source  and binary  forms, with  or without
3    * modification, are permitted provided  that the following conditions are
4    * met :
5    *
6    * . Redistributions  of  source  code  must  retain  the  above copyright
7    *   notice, this list of conditions and the following disclaimer.
8    *
9    * . Redistributions in  binary form  must reproduce  the above  copyright
10   *   notice, this list of conditions  and the following disclaimer in  the
11   *   documentation and/or other materials provided with the distribution.
12   *
13   * . The name of the author may not be used to endorse or promote products
14   *   derived from this software without specific prior written permission.
15   *
16   * THIS SOFTWARE IS  PROVIDED BY THE  AUTHOR ``AS IS''  AND ANY EXPRESS  OR
17   * IMPLIED  WARRANTIES,  INCLUDING,  BUT   NOT  LIMITED  TO,  THE   IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE ARE
19   * DISCLAIMED.  IN NO  EVENT SHALL  THE AUTHOR  BE LIABLE  FOR ANY  DIRECT,
20   * INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR  CONSEQUENTIAL  DAMAGES
21   * (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT OF  SUBSTITUTE  GOODS OR
22   * SERVICES;  LOSS  OF USE,  DATA,  OR PROFITS;  OR  BUSINESS INTERRUPTION)
23   * HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN  CONTRACT,
24   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25   * ANY  WAY  OUT OF  THE  USE OF  THIS  SOFTWARE, EVEN  IF  ADVISED OF  THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   *
28   * $Id$
29   */
30  
31  package palmed.edit.text;
32  
33  import java.io.ByteArrayInputStream;
34  import java.io.IOException;
35  import junit.framework.TestCase;
36  import org.easymock.MockControl;
37  
38  /***
39   * MultiInputStream test case.
40   *
41   * @author Mathieu Champlon
42   * @version $Revision$ $Date$
43   */
44  public class MultiInputStreamTest extends TestCase
45  {
46      /***
47       * Tested object.
48       */
49      private MultiInputStream stream;
50  
51      protected void setUp()
52      {
53      }
54  
55      private void assertArrayEquals( final byte[] array1, final byte[] array2 )
56      {
57          final MockControl controlArrayComparator = MockControl.createControl( IArrayComparator.class );
58          final IArrayComparator mockArrayComparator = (IArrayComparator)controlArrayComparator.getMock();
59          mockArrayComparator.compare( array1 );
60          controlArrayComparator.setMatcher( MockControl.ARRAY_MATCHER );
61          controlArrayComparator.replay();
62          mockArrayComparator.compare( array2 );
63          controlArrayComparator.verify();
64      }
65  
66      private void assertArrayEquals( final byte[] array1, final byte[] array2, final int length )
67      {
68          assertArrayEquals( new String( array1, 0, length ).getBytes(), new String( array2, 0, length ).getBytes() );
69      }
70  
71      private ByteArrayInputStream createInputStreamStub( final String string )
72      {
73          return new ByteArrayInputStream( string.getBytes() );
74      }
75  
76      public void testStreamSmallerThanThresholdReadInOneChunk() throws IOException
77      {
78          final String string = "this is my string";
79          stream = new MultiInputStream( createInputStreamStub( string ), string.length() + 1 );
80          final byte[] output = new byte[string.length()];
81          assertEquals( string.length(), stream.available() );
82          assertEquals( string.length(), stream.read( output ) );
83          assertEquals( 0, stream.available() );
84          assertArrayEquals( string.getBytes(), output );
85          assertEquals( -1, stream.read( output ) );
86      }
87  
88      public void testStreamLargerThanThresholdReadInTwoChunks() throws IOException
89      {
90          final String string = "this is my string";
91          final int threshold = string.length() - 2;
92          stream = new MultiInputStream( createInputStreamStub( string ), threshold );
93          final byte[] output = new byte[string.length()];
94          assertEquals( threshold, stream.available() );
95          assertEquals( threshold, stream.read( output ) );
96          assertEquals( 0, stream.available() );
97          assertArrayEquals( string.substring( 0, threshold ).getBytes(), output, threshold );
98          assertEquals( -1, stream.read( output ) );
99          stream.next();
100         assertEquals( string.length() - threshold, stream.available() );
101         assertEquals( string.length() - threshold, stream.read( output ) );
102         assertEquals( 0, stream.available() );
103         assertArrayEquals( string.substring( threshold ).getBytes(), output, string.length() - threshold );
104         assertEquals( -1, stream.read( output ) );
105     }
106 }