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
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.ipc
;
20 import static org
.junit
.Assert
.*;
23 import java
.io
.FileOutputStream
;
24 import java
.io
.IOException
;
25 import java
.nio
.ByteBuffer
;
26 import java
.nio
.channels
.FileChannel
;
27 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
28 import org
.apache
.hadoop
.hbase
.testclassification
.RPCTests
;
29 import org
.apache
.hadoop
.hbase
.testclassification
.SmallTests
;
30 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
31 import org
.junit
.After
;
32 import org
.junit
.Before
;
33 import org
.junit
.ClassRule
;
34 import org
.junit
.Test
;
35 import org
.junit
.experimental
.categories
.Category
;
36 import org
.mockito
.Mockito
;
38 import org
.apache
.hbase
.thirdparty
.com
.google
.common
.base
.Charsets
;
39 import org
.apache
.hbase
.thirdparty
.com
.google
.common
.io
.Files
;
41 @Category({RPCTests
.class, SmallTests
.class})
42 public class TestBufferChain
{
45 public static final HBaseClassTestRule CLASS_RULE
=
46 HBaseClassTestRule
.forClass(TestBufferChain
.class);
50 private static final byte[][] HELLO_WORLD_CHUNKS
= new byte[][] {
51 "hello".getBytes(Charsets
.UTF_8
),
52 " ".getBytes(Charsets
.UTF_8
),
53 "world".getBytes(Charsets
.UTF_8
)
57 public void setup() throws IOException
{
58 tmpFile
= File
.createTempFile("TestBufferChain", "txt");
62 public void teardown() {
67 public void testGetBackBytesWePutIn() {
68 ByteBuffer
[] bufs
= wrapArrays(HELLO_WORLD_CHUNKS
);
69 BufferChain chain
= new BufferChain(bufs
);
70 assertTrue(Bytes
.equals(Bytes
.toBytes("hello world"), chain
.getBytes()));
74 public void testChainChunkBiggerThanWholeArray() throws IOException
{
75 ByteBuffer
[] bufs
= wrapArrays(HELLO_WORLD_CHUNKS
);
76 BufferChain chain
= new BufferChain(bufs
);
77 writeAndVerify(chain
, "hello world", 8192);
78 assertNoRemaining(bufs
);
82 public void testChainChunkBiggerThanSomeArrays() throws IOException
{
83 ByteBuffer
[] bufs
= wrapArrays(HELLO_WORLD_CHUNKS
);
84 BufferChain chain
= new BufferChain(bufs
);
85 writeAndVerify(chain
, "hello world", 3);
86 assertNoRemaining(bufs
);
90 public void testLimitOffset() throws IOException
{
91 ByteBuffer
[] bufs
= new ByteBuffer
[] {
92 stringBuf("XXXhelloYYY", 3, 5),
94 stringBuf("XXXXworldY", 4, 5) };
95 BufferChain chain
= new BufferChain(bufs
);
96 writeAndVerify(chain
, "hello world", 3);
97 assertNoRemaining(bufs
);
101 public void testWithSpy() throws IOException
{
102 ByteBuffer
[] bufs
= new ByteBuffer
[] {
103 stringBuf("XXXhelloYYY", 3, 5),
104 stringBuf(" ", 0, 1),
105 stringBuf("XXXXworldY", 4, 5) };
106 BufferChain chain
= new BufferChain(bufs
);
107 FileOutputStream fos
= new FileOutputStream(tmpFile
);
108 FileChannel ch
= Mockito
.spy(fos
.getChannel());
111 assertEquals("he", Files
.toString(tmpFile
, Charsets
.UTF_8
));
113 assertEquals("hell", Files
.toString(tmpFile
, Charsets
.UTF_8
));
115 assertEquals("hello w", Files
.toString(tmpFile
, Charsets
.UTF_8
));
117 assertEquals("hello world", Files
.toString(tmpFile
, Charsets
.UTF_8
));
124 private ByteBuffer
stringBuf(String string
, int position
, int length
) {
125 ByteBuffer buf
= ByteBuffer
.wrap(string
.getBytes(Charsets
.UTF_8
));
126 buf
.position(position
);
127 buf
.limit(position
+ length
);
128 assertTrue(buf
.hasRemaining());
132 private void assertNoRemaining(ByteBuffer
[] bufs
) {
133 for (ByteBuffer buf
: bufs
) {
134 assertFalse(buf
.hasRemaining());
138 private ByteBuffer
[] wrapArrays(byte[][] arrays
) {
139 ByteBuffer
[] ret
= new ByteBuffer
[arrays
.length
];
140 for (int i
= 0; i
< arrays
.length
; i
++) {
141 ret
[i
] = ByteBuffer
.wrap(arrays
[i
]);
146 private void writeAndVerify(BufferChain chain
, String string
, int chunkSize
)
148 FileOutputStream fos
= new FileOutputStream(tmpFile
);
149 FileChannel ch
= fos
.getChannel();
151 long remaining
= string
.length();
152 while (chain
.hasRemaining()) {
153 long n
= chain
.write(ch
, chunkSize
);
154 assertTrue(n
== chunkSize
|| n
== remaining
);
157 assertEquals(0, remaining
);
161 assertFalse(chain
.hasRemaining());
162 assertEquals(string
, Files
.toString(tmpFile
, Charsets
.UTF_8
));