001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.ipc;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024import java.util.ArrayList;
025import java.util.List;
026import org.apache.hadoop.hbase.Cell;
027import org.apache.hadoop.hbase.CellScannable;
028import org.apache.hadoop.hbase.CellScanner;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.testclassification.ClientTests;
031import org.apache.hadoop.hbase.testclassification.SmallTests;
032import org.apache.hadoop.hbase.util.Bytes;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036
037@Category({ ClientTests.class, SmallTests.class })
038public class TestHBaseRpcControllerImpl {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042      HBaseClassTestRule.forClass(TestHBaseRpcControllerImpl.class);
043
044  @Test
045  public void testListOfCellScannerables() throws IOException {
046    final int count = 10;
047    List<CellScannable> cells = new ArrayList<>(count);
048
049    for (int i = 0; i < count; i++) {
050      cells.add(createCell(i));
051    }
052    HBaseRpcController controller = new HBaseRpcControllerImpl(cells);
053    CellScanner cellScanner = controller.cellScanner();
054    int index = 0;
055    for (; cellScanner.advance(); index++) {
056      Cell cell = cellScanner.current();
057      byte[] indexBytes = Bytes.toBytes(index);
058      assertTrue("" + index, Bytes.equals(indexBytes, 0, indexBytes.length, cell.getValueArray(),
059        cell.getValueOffset(), cell.getValueLength()));
060    }
061    assertEquals(count, index);
062  }
063
064  /**
065   * @param index
066   * @return A faked out 'Cell' that does nothing but return index as its value
067   */
068  static CellScannable createCell(final int index) {
069    return new CellScannable() {
070      @Override
071      public CellScanner cellScanner() {
072        return new CellScanner() {
073          @Override
074          public Cell current() {
075            // Fake out a Cell. All this Cell has is a value that is an int in size and equal
076            // to the above 'index' param serialized as an int.
077            return new Cell() {
078              private final int i = index;
079
080              @Override
081              public byte[] getRowArray() {
082                // unused
083                return null;
084              }
085
086              @Override
087              public int getRowOffset() {
088                // unused
089                return 0;
090              }
091
092              @Override
093              public short getRowLength() {
094                // unused
095                return 0;
096              }
097
098              @Override
099              public byte[] getFamilyArray() {
100                // unused
101                return null;
102              }
103
104              @Override
105              public int getFamilyOffset() {
106                // unused
107                return 0;
108              }
109
110              @Override
111              public byte getFamilyLength() {
112                // unused
113                return 0;
114              }
115
116              @Override
117              public byte[] getQualifierArray() {
118                // unused
119                return null;
120              }
121
122              @Override
123              public int getQualifierOffset() {
124                // unused
125                return 0;
126              }
127
128              @Override
129              public int getQualifierLength() {
130                // unused
131                return 0;
132              }
133
134              @Override
135              public long getTimestamp() {
136                // unused
137                return 0;
138              }
139
140              @Override
141              public byte getTypeByte() {
142                // unused
143                return 0;
144              }
145
146              @Override
147              public long getSequenceId() {
148                // unused
149                return 0;
150              }
151
152              @Override
153              public byte[] getValueArray() {
154                return Bytes.toBytes(this.i);
155              }
156
157              @Override
158              public int getValueOffset() {
159                return 0;
160              }
161
162              @Override
163              public int getValueLength() {
164                return Bytes.SIZEOF_INT;
165              }
166
167              @Override
168              public int getTagsOffset() {
169                // unused
170                return 0;
171              }
172
173              @Override
174              public int getTagsLength() {
175                // unused
176                return 0;
177              }
178
179              @Override
180              public byte[] getTagsArray() {
181                // unused
182                return null;
183              }
184
185              @Override
186              public Type getType() {
187                // unused
188                return null;
189              }
190            };
191          }
192
193          private boolean hasCell = true;
194
195          @Override
196          public boolean advance() {
197            // We have one Cell only so return true first time then false ever after.
198            if (!hasCell) return hasCell;
199            hasCell = false;
200            return true;
201          }
202        };
203      }
204    };
205  }
206}