View Javadoc
1   /**
2    * 
3    */
4   package org.melati.util.test;
5   
6   import java.io.ByteArrayInputStream;
7   
8   import org.melati.util.DelimitedBufferedInputStream;
9   
10  import junit.framework.TestCase;
11  
12  /**
13   * @author timp
14   * @since 28 Jan 2008
15   *
16   */
17  public class DelimitedBufferedInputStreamTest extends TestCase {
18  
19    byte[] sink = new byte[10];
20    byte[] arr1 = {30, 31, 32, 33};
21    byte[] arr2 = {32, 33};
22    byte[] arr3 = {32, 34, 33};
23    byte[] arr4 = {32, 33, 34};
24  
25    /**
26     * @param name
27     */
28    public DelimitedBufferedInputStreamTest(String name) {
29      super(name);
30    }
31  
32    /** 
33     * {@inheritDoc}
34     * @see junit.framework.TestCase#setUp()
35     */
36    protected void setUp() throws Exception {
37      super.setUp();
38    }
39  
40    /** 
41     * {@inheritDoc}
42     * @see junit.framework.TestCase#tearDown()
43     */
44    protected void tearDown() throws Exception {
45      super.tearDown();
46    }
47  
48    /**
49     * Test method for {@link org.melati.util.DelimitedBufferedInputStream#DelimitedBufferedInputStream(java.io.InputStream)}.
50     */
51    public void testDelimitedBufferedInputStreamInputStream() throws Exception {
52      DelimitedBufferedInputStream testDBIS =
53        new DelimitedBufferedInputStream(
54                new ByteArrayInputStream(arr1));
55      assertTrue("arr2 in arr1 (expect 2): " ,
56              testDBIS.indexOf(arr1, arr2, 0) == 2);
57      assertTrue("potentialMatch (expect -1): " ,
58              testDBIS.getPotentialMatch() == -1);
59      assertTrue("arr3 in arr1 (expect -1): " ,
60              testDBIS.indexOf(arr1, arr3, 0) == -1);
61      assertTrue("potentialMatch (expect -1): " ,
62              testDBIS.getPotentialMatch() == -1);
63      assertTrue("arr4 in arr1 (expect -1): ",
64              testDBIS.indexOf(arr1, arr4, 0) == -1);
65      assertTrue("potentialMatch (expect 2): ",
66              testDBIS.getPotentialMatch() == 2);
67      testDBIS.close();
68    }
69  
70    /**
71     * Test method for {@link org.melati.util.DelimitedBufferedInputStream#DelimitedBufferedInputStream(java.io.InputStream, int)}.
72     */
73    public void testDelimitedBufferedInputStreamInputStreamInt() throws Exception {
74      DelimitedBufferedInputStream testDBIS =
75          new DelimitedBufferedInputStream(
76              new ByteArrayInputStream(arr1), 1);
77       assertTrue("reading upto arr3 (expect 4): " ,
78               testDBIS.readToDelimiter(sink, 0, 10, arr3) == 4);
79       testDBIS.close();
80    }
81  
82    /**
83     * Test method for {@link org.melati.util.DelimitedBufferedInputStream#readToDelimiter(byte[], int, int, byte[])}.
84     */
85    public void testReadToDelimiter() throws Exception {
86      DelimitedBufferedInputStream testDBIS =
87        new DelimitedBufferedInputStream(
88                new ByteArrayInputStream(arr1));
89      assertTrue("reading upto arr2 (expect 2): ",
90              testDBIS.readToDelimiter(sink, 0, 10, arr2) == 2);
91      assertTrue("reading upto arr2 again (expect 0): " + 
92              testDBIS.readToDelimiter(sink, 0, 10, arr2),
93              testDBIS.readToDelimiter(sink, 0, 10, arr2) == 0);
94      testDBIS = new DelimitedBufferedInputStream(
95              new ByteArrayInputStream(arr1), 1);
96      assertTrue("reading upto arr4 (expect 4): " , 
97              testDBIS.readToDelimiter(sink, 0, 10, arr4) == 4);
98      testDBIS = new DelimitedBufferedInputStream(
99              new ByteArrayInputStream(arr1), 1);
100     assertTrue("reading upto arr4 (expect 3): ", 
101             testDBIS.readToDelimiter(sink, 0, 3, arr4) == 3);
102     assertTrue("buf.length is now (expect 3): ", 
103             testDBIS.getBufferLength() == 3);
104 
105   }
106 
107   /**
108    * Test method for {@link org.melati.util.DelimitedBufferedInputStream#indexOf(byte[], byte[], int)}.
109    */
110   public void testIndexOfByteArrayByteArrayInt() {
111   }
112 
113   /**
114    * Test method for {@link org.melati.util.DelimitedBufferedInputStream#indexOf(byte[], byte[], int, int)}.
115    */
116   public void testIndexOfByteArrayByteArrayIntInt() {
117   }
118 
119 }