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, 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.
17 */
18
19 package org.apache.hadoop.hbase.ipc;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.apache.hadoop.hbase.Cell;
29 import org.apache.hadoop.hbase.CellScannable;
30 import org.apache.hadoop.hbase.CellScanner;
31 import org.apache.hadoop.hbase.SmallTests;
32 import org.apache.hadoop.hbase.util.Bytes;
33 import org.junit.Test;
34 import org.junit.experimental.categories.Category;
35
36
37 @Category(SmallTests.class)
38 public class TestPayloadCarryingRpcController {
39 @Test
40 public void testListOfCellScannerables() throws IOException {
41 List<CellScannable> cells = new ArrayList<CellScannable>();
42 final int count = 10;
43 for (int i = 0; i < count; i++) {
44 cells.add(createCell(i));
45 }
46 PayloadCarryingRpcController controller = new PayloadCarryingRpcController(cells);
47 CellScanner cellScanner = controller.cellScanner();
48 int index = 0;
49 for (; cellScanner.advance(); index++) {
50 Cell cell = cellScanner.current();
51 byte [] indexBytes = Bytes.toBytes(index);
52 assertTrue("" + index, Bytes.equals(indexBytes, 0, indexBytes.length, cell.getValueArray(),
53 cell.getValueOffset(), cell.getValueLength()));
54 }
55 assertEquals(count, index);
56 }
57
58 /**
59 * @param index
60 * @return A faked out 'Cell' that does nothing but return index as its value
61 */
62 static CellScannable createCell(final int index) {
63 return new CellScannable() {
64 @Override
65 public CellScanner cellScanner() {
66 return new CellScanner() {
67 @Override
68 public Cell current() {
69 // Fake out a Cell. All this Cell has is a value that is an int in size and equal
70 // to the above 'index' param serialized as an int.
71 return new Cell() {
72 private final int i = index;
73
74 @Override
75 public byte[] getRowArray() {
76 // TODO Auto-generated method stub
77 return null;
78 }
79
80 @Override
81 public int getRowOffset() {
82 // TODO Auto-generated method stub
83 return 0;
84 }
85
86 @Override
87 public short getRowLength() {
88 // TODO Auto-generated method stub
89 return 0;
90 }
91
92 @Override
93 public byte[] getFamilyArray() {
94 // TODO Auto-generated method stub
95 return null;
96 }
97
98 @Override
99 public int getFamilyOffset() {
100 // TODO Auto-generated method stub
101 return 0;
102 }
103
104 @Override
105 public byte getFamilyLength() {
106 // TODO Auto-generated method stub
107 return 0;
108 }
109
110 @Override
111 public byte[] getQualifierArray() {
112 // TODO Auto-generated method stub
113 return null;
114 }
115
116 @Override
117 public int getQualifierOffset() {
118 // TODO Auto-generated method stub
119 return 0;
120 }
121
122 @Override
123 public int getQualifierLength() {
124 // TODO Auto-generated method stub
125 return 0;
126 }
127
128 @Override
129 public long getTimestamp() {
130 // TODO Auto-generated method stub
131 return 0;
132 }
133
134 @Override
135 public byte getTypeByte() {
136 // TODO Auto-generated method stub
137 return 0;
138 }
139
140 @Override
141 public long getMvccVersion() {
142 // TODO Auto-generated method stub
143 return 0;
144 }
145
146 @Override
147 public byte[] getValueArray() {
148 return Bytes.toBytes(this.i);
149 }
150
151 @Override
152 public int getValueOffset() {
153 return 0;
154 }
155
156 @Override
157 public int getValueLength() {
158 return Bytes.SIZEOF_INT;
159 }
160
161 @Override
162 public int getTagsOffset() {
163 // TODO Auto-generated method stub
164 return 0;
165 }
166
167 @Override
168 public int getTagsLength() {
169 // TODO Auto-generated method stub
170 return 0;
171 }
172
173 @Override
174 public byte[] getTagsArray() {
175 // TODO Auto-generated method stub
176 return null;
177 }
178 };
179 }
180
181 private boolean hasCell = true;
182 @Override
183 public boolean advance() {
184 // We have one Cell only so return true first time then false ever after.
185 if (!hasCell) return hasCell;
186 hasCell = false;
187 return true;
188 }
189 };
190 }
191 };
192 }
193 }