View Javadoc

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.testclassification.ClientTests;
32  import org.apache.hadoop.hbase.testclassification.SmallTests;
33  import org.apache.hadoop.hbase.util.Bytes;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  @Category({ ClientTests.class, SmallTests.class })
38  public class TestHBaseRpcControllerImpl {
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      HBaseRpcController controller = new HBaseRpcControllerImpl(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 the index of the cell to use as its value
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                  return null;
77                }
78  
79                @Override
80                public int getRowOffset() {
81                  return 0;
82                }
83  
84                @Override
85                public short getRowLength() {
86                  return 0;
87                }
88  
89                @Override
90                public byte[] getFamilyArray() {
91                  return null;
92                }
93  
94                @Override
95                public int getFamilyOffset() {
96                  return 0;
97                }
98  
99                @Override
100               public byte getFamilyLength() {
101                 return 0;
102               }
103 
104               @Override
105               public byte[] getQualifierArray() {
106                 return null;
107               }
108 
109               @Override
110               public int getQualifierOffset() {
111                 return 0;
112               }
113 
114               @Override
115               public int getQualifierLength() {
116                 return 0;
117               }
118 
119               @Override
120               public long getTimestamp() {
121                 return 0;
122               }
123 
124               @Override
125               public byte getTypeByte() {
126                 return 0;
127               }
128 
129               @Override
130               public long getSequenceId() {
131                 return 0;
132               }
133 
134               @Override
135               public byte[] getValueArray() {
136                 return Bytes.toBytes(this.i);
137               }
138 
139               @Override
140               public int getValueOffset() {
141                 return 0;
142               }
143 
144               @Override
145               public int getValueLength() {
146                 return Bytes.SIZEOF_INT;
147               }
148 
149               @Override
150               public int getTagsOffset() {
151                 return 0;
152               }
153 
154               @Override
155               public int getTagsLength() {
156                 return 0;
157               }
158 
159               @Override
160               public byte[] getTagsArray() {
161                 return null;
162               }
163 
164               @Override
165               public long getMvccVersion() {
166                 return 0;
167               }
168 
169               @Override
170               public byte[] getValue() {
171                 return Bytes.toBytes(this.i);
172               }
173 
174               @Override
175               public byte[] getFamily() {
176                 return null;
177               }
178 
179               @Override
180               public byte[] getQualifier() {
181                 return null;
182               }
183 
184               @Override
185               public byte[] getRow() {
186                 return null;
187               }
188             };
189           }
190 
191           private boolean hasCell = true;
192 
193           @Override
194           public boolean advance() {
195             // We have one Cell only so return true first time then false ever after.
196             if (!hasCell) {
197               return hasCell;
198             }
199 
200             hasCell = false;
201             return true;
202           }
203         };
204       }
205     };
206   }
207 }