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