1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 package org.apache.thrift.transport;
20 
21 import java.util.Arrays;
22 
23 import junit.framework.TestCase;
24 
25 public class TestTMemoryInputTransport extends TestCase {
testFresh()26   public void testFresh() throws Exception {
27     byte[] input_buf = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
28     TMemoryInputTransport trans = new TMemoryInputTransport(input_buf);
29     assertEquals(0, trans.getBufferPosition());
30     assertEquals(input_buf, trans.getBuffer());
31     assertEquals(10, trans.getBytesRemainingInBuffer());
32 
33     byte[] buf1 = new byte[4];
34     trans.readAll(buf1, 0, 4);
35     assertTrue(Arrays.equals(new byte[]{1, 2, 3, 4}, buf1));
36     assertEquals(4, trans.getBufferPosition());
37     assertEquals(6, trans.getBytesRemainingInBuffer());
38 
39     trans.consumeBuffer(2);
40 
41     assertEquals(6, trans.getBufferPosition());
42     assertEquals(4, trans.getBytesRemainingInBuffer());
43 
44     trans.readAll(buf1, 0, 4);
45     assertTrue(Arrays.equals(new byte[]{7, 8, 9, 10}, buf1));
46     assertEquals(10, trans.getBufferPosition());
47     assertEquals(0, trans.getBytesRemainingInBuffer());
48   }
49 
testReused()50   public void testReused() throws Exception {
51     byte[] input_buf = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
52     TMemoryInputTransport trans = new TMemoryInputTransport(input_buf);
53     assertEquals(0, trans.getBufferPosition());
54     assertEquals(input_buf, trans.getBuffer());
55     assertEquals(10, trans.getBytesRemainingInBuffer());
56 
57     byte[] new_buf = new byte[]{10, 9, 8};
58     trans.reset(new_buf);
59     assertEquals(0, trans.getBufferPosition());
60     assertEquals(new_buf, trans.getBuffer());
61     assertEquals(3, trans.getBytesRemainingInBuffer());
62   }
63 
testWithOffsetAndLength()64   public void testWithOffsetAndLength() throws Exception {
65     byte[] input_buf = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
66     TMemoryInputTransport trans = new TMemoryInputTransport(input_buf, 1, 3);
67     assertEquals(1, trans.getBufferPosition());
68     assertEquals(3, trans.getBytesRemainingInBuffer());
69     byte[] readBuffer = new byte[3];
70     trans.readAll(readBuffer, 0, 3);
71     assertTrue(Arrays.equals(new byte[]{2, 3, 4}, readBuffer));
72 
73     try {
74       assertEquals(0, trans.readAll(readBuffer, 0, 3));
75       fail("should have thrown an exception");
76     } catch (Exception e) {
77       // yay
78     }
79 
80     trans.reset(input_buf, 3, 4);
81     readBuffer = new byte[4];
82     trans.readAll(readBuffer, 0, 4);
83     assertTrue(Arrays.equals(new byte[]{4, 5, 6, 7}, readBuffer));
84   }
85 }
86