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  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041    HBaseClassTestRule.forClass(TestHBaseRpcControllerImpl.class);
042
043  @Test
044  public void testListOfCellScannerables() throws IOException {
045    final int count = 10;
046    List<CellScannable> cells = new ArrayList<>(count);
047
048    for (int i = 0; i < count; i++) {
049      cells.add(createCell(i));
050    }
051    HBaseRpcController controller = new HBaseRpcControllerImpl(null, cells);
052    CellScanner cellScanner = controller.cellScanner();
053    int index = 0;
054    for (; cellScanner.advance(); index++) {
055      Cell cell = cellScanner.current();
056      byte[] indexBytes = Bytes.toBytes(index);
057      assertTrue("" + index, Bytes.equals(indexBytes, 0, indexBytes.length, cell.getValueArray(),
058        cell.getValueOffset(), cell.getValueLength()));
059    }
060    assertEquals(count, index);
061  }
062
063  /**
064   * @param index the index of the cell to use as its value
065   * @return A faked out 'Cell' that does nothing but return index as its value
066   */
067  static CellScannable createCell(final int index) {
068    return new CellScannable() {
069      @Override
070      public CellScanner cellScanner() {
071        return new CellScanner() {
072          @Override
073          public Cell current() {
074            // Fake out a Cell. All this Cell has is a value that is an int in size and equal
075            // to the above 'index' param serialized as an int.
076            return new Cell() {
077              @Override
078              public long heapSize() {
079                return 0;
080              }
081
082              private final int i = index;
083
084              @Override
085              public byte[] getRowArray() {
086                return null;
087              }
088
089              @Override
090              public int getRowOffset() {
091                return 0;
092              }
093
094              @Override
095              public short getRowLength() {
096                return 0;
097              }
098
099              @Override
100              public byte[] getFamilyArray() {
101                return null;
102              }
103
104              @Override
105              public int getFamilyOffset() {
106                return 0;
107              }
108
109              @Override
110              public byte getFamilyLength() {
111                return 0;
112              }
113
114              @Override
115              public byte[] getQualifierArray() {
116                return null;
117              }
118
119              @Override
120              public int getQualifierOffset() {
121                return 0;
122              }
123
124              @Override
125              public int getQualifierLength() {
126                return 0;
127              }
128
129              @Override
130              public long getTimestamp() {
131                return 0;
132              }
133
134              @Override
135              public byte getTypeByte() {
136                return 0;
137              }
138
139              @Override
140              public long getSequenceId() {
141                return 0;
142              }
143
144              @Override
145              public byte[] getValueArray() {
146                return Bytes.toBytes(this.i);
147              }
148
149              @Override
150              public int getValueOffset() {
151                return 0;
152              }
153
154              @Override
155              public int getValueLength() {
156                return Bytes.SIZEOF_INT;
157              }
158
159              @Override
160              public int getSerializedSize() {
161                return 0;
162              }
163
164              @Override
165              public int getTagsOffset() {
166                return 0;
167              }
168
169              @Override
170              public int getTagsLength() {
171                return 0;
172              }
173
174              @Override
175              public byte[] getTagsArray() {
176                return null;
177              }
178
179              @Override
180              public Type getType() {
181                return null;
182              }
183            };
184          }
185
186          private boolean hasCell = true;
187
188          @Override
189          public boolean advance() {
190            // We have one Cell only so return true first time then false ever after.
191            if (!hasCell) {
192              return hasCell;
193            }
194
195            hasCell = false;
196            return true;
197          }
198        };
199      }
200    };
201  }
202}