1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.client;
22  
23  import junit.framework.TestCase;
24  import org.apache.hadoop.hbase.KeyValue;
25  import org.apache.hadoop.hbase.util.Bytes;
26  
27  import static org.apache.hadoop.hbase.HBaseTestCase.assertByteEquals;
28  
29  import java.util.Arrays;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.NavigableMap;
33  
34  public class TestResult extends TestCase {
35  
36    static KeyValue[] genKVs(final byte[] row, final byte[] family,
37                             final byte[] value,
38                      final long timestamp,
39                      final int cols) {
40      KeyValue [] kvs = new KeyValue[cols];
41  
42      for (int i = 0; i < cols ; i++) {
43        kvs[i] = new KeyValue(
44            row, family, Bytes.toBytes(i),
45            timestamp,
46            Bytes.add(value, Bytes.toBytes(i)));
47      }
48      return kvs;
49    }
50  
51    static final byte [] row = Bytes.toBytes("row");
52    static final byte [] family = Bytes.toBytes("family");
53    static final byte [] value = Bytes.toBytes("value");
54  
55    public void testBasic() throws Exception {
56      KeyValue [] kvs = genKVs(row, family, value, 1, 100);
57  
58      Arrays.sort(kvs, KeyValue.COMPARATOR);
59  
60      Result r = new Result(kvs);
61  
62      for (int i = 0; i < 100; ++i) {
63        final byte[] qf = Bytes.toBytes(i);
64  
65        List<KeyValue> ks = r.getColumn(family, qf);
66        assertEquals(1, ks.size());
67        assertByteEquals(qf, ks.get(0).getQualifier());
68  
69        assertEquals(ks.get(0), r.getColumnLatest(family, qf));
70        assertByteEquals(Bytes.add(value, Bytes.toBytes(i)), r.getValue(family, qf));
71        assertTrue(r.containsColumn(family, qf));
72      }
73    }
74    public void testMultiVersion() throws Exception {
75      KeyValue [] kvs1 = genKVs(row, family, value, 1, 100);
76      KeyValue [] kvs2 = genKVs(row, family, value, 200, 100);
77  
78      KeyValue [] kvs = new KeyValue[kvs1.length+kvs2.length];
79      System.arraycopy(kvs1, 0, kvs, 0, kvs1.length);
80      System.arraycopy(kvs2, 0, kvs, kvs1.length, kvs2.length);
81  
82      Arrays.sort(kvs, KeyValue.COMPARATOR);
83  
84      Result r = new Result(kvs);
85      for (int i = 0; i < 100; ++i) {
86        final byte[] qf = Bytes.toBytes(i);
87  
88        List<KeyValue> ks = r.getColumn(family, qf);
89        assertEquals(2, ks.size());
90        assertByteEquals(qf, ks.get(0).getQualifier());
91        assertEquals(200, ks.get(0).getTimestamp());
92  
93        assertEquals(ks.get(0), r.getColumnLatest(family, qf));
94        assertByteEquals(Bytes.add(value, Bytes.toBytes(i)), r.getValue(family, qf));
95        assertTrue(r.containsColumn(family, qf));
96      }
97    }
98  }