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