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  
40    @Test
41    public void testListOfCellScannerables() throws IOException {
42      List<CellScannable> cells = new ArrayList<CellScannable>();
43      final int count = 10;
44      for (int i = 0; i < count; i++) {
45        cells.add(createCell(i));
46      }
47      HBaseRpcController controller = new HBaseRpcControllerImpl(cells);
48      CellScanner cellScanner = controller.cellScanner();
49      int index = 0;
50      for (; cellScanner.advance(); index++) {
51        Cell cell = cellScanner.current();
52        byte[] indexBytes = Bytes.toBytes(index);
53        assertTrue("" + index, Bytes.equals(indexBytes, 0, indexBytes.length, cell.getValueArray(),
54          cell.getValueOffset(), cell.getValueLength()));
55      }
56      assertEquals(count, index);
57    }
58  
59    /**
60     * @param index
61     * @return A faked out 'Cell' that does nothing but return index as its value
62     */
63    static CellScannable createCell(final int index) {
64      return new CellScannable() {
65        @Override
66        public CellScanner cellScanner() {
67          return new CellScanner() {
68            @Override
69            public Cell current() {
70              // Fake out a Cell. All this Cell has is a value that is an int in size and equal
71              // to the above 'index' param serialized as an int.
72              return new Cell() {
73                private final int i = index;
74  
75                @Override
76                public byte[] getRowArray() {
77                  // unused
78                  return null;
79                }
80  
81                @Override
82                public int getRowOffset() {
83                  // unused
84                  return 0;
85                }
86  
87                @Override
88                public short getRowLength() {
89                  // unused
90                  return 0;
91                }
92  
93                @Override
94                public byte[] getFamilyArray() {
95                  // unused
96                  return null;
97                }
98  
99                @Override
100               public int getFamilyOffset() {
101                 // unused
102                 return 0;
103               }
104 
105               @Override
106               public byte getFamilyLength() {
107                 // unused
108                 return 0;
109               }
110 
111               @Override
112               public byte[] getQualifierArray() {
113                 // unused
114                 return null;
115               }
116 
117               @Override
118               public int getQualifierOffset() {
119                 // unused
120                 return 0;
121               }
122 
123               @Override
124               public int getQualifierLength() {
125                 // unused
126                 return 0;
127               }
128 
129               @Override
130               public long getTimestamp() {
131                 // unused
132                 return 0;
133               }
134 
135               @Override
136               public byte getTypeByte() {
137                 // unused
138                 return 0;
139               }
140 
141               @Override
142               public long getSequenceId() {
143                 // unused
144                 return 0;
145               }
146 
147               @Override
148               public byte[] getValueArray() {
149                 return Bytes.toBytes(this.i);
150               }
151 
152               @Override
153               public int getValueOffset() {
154                 return 0;
155               }
156 
157               @Override
158               public int getValueLength() {
159                 return Bytes.SIZEOF_INT;
160               }
161 
162               @Override
163               public int getTagsOffset() {
164                 // unused
165                 return 0;
166               }
167 
168               @Override
169               public int getTagsLength() {
170                 // unused
171                 return 0;
172               }
173 
174               @Override
175               public byte[] getTagsArray() {
176                 // unused
177                 return null;
178               }
179 
180               @Override
181               public long getMvccVersion() {
182                 return 0;
183               }
184 
185               @Override
186               public byte[] getValue() {
187                 return Bytes.toBytes(this.i);
188               }
189 
190               @Override
191               public byte[] getFamily() {
192                 return null;
193               }
194 
195               @Override
196               public byte[] getQualifier() {
197                 return null;
198               }
199 
200               @Override
201               public byte[] getRow() {
202                 return null;
203               }
204             };
205           }
206 
207           private boolean hasCell = true;
208 
209           @Override
210           public boolean advance() {
211             // We have one Cell only so return true first time then false ever after.
212             if (!hasCell) return hasCell;
213             hasCell = false;
214             return true;
215           }
216         };
217       }
218     };
219   }
220 }