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.SmallTests;
26  import org.apache.hadoop.hbase.util.Bytes;
27  import org.junit.experimental.categories.Category;
28  
29  import static org.apache.hadoop.hbase.HBaseTestCase.assertByteEquals;
30  
31  import java.util.Arrays;
32  import java.util.List;
33  import java.util.Map;
34  import java.util.NavigableMap;
35  
36  @Category(SmallTests.class)
37  public class TestResult extends TestCase {
38  
39    static KeyValue[] genKVs(final byte[] row, final byte[] family,
40                             final byte[] value,
41                      final long timestamp,
42                      final int cols) {
43      KeyValue [] kvs = new KeyValue[cols];
44  
45      for (int i = 0; i < cols ; i++) {
46        kvs[i] = new KeyValue(
47            row, family, Bytes.toBytes(i),
48            timestamp,
49            Bytes.add(value, Bytes.toBytes(i)));
50      }
51      return kvs;
52    }
53  
54    static final byte [] row = Bytes.toBytes("row");
55    static final byte [] family = Bytes.toBytes("family");
56    static final byte [] value = Bytes.toBytes("value");
57  
58    public void testBasic() throws Exception {
59      KeyValue [] kvs = genKVs(row, family, value, 1, 100);
60  
61      Arrays.sort(kvs, KeyValue.COMPARATOR);
62  
63      Result r = new Result(kvs);
64  
65      for (int i = 0; i < 100; ++i) {
66        final byte[] qf = Bytes.toBytes(i);
67  
68        List<KeyValue> ks = r.getColumn(family, qf);
69        assertEquals(1, ks.size());
70        assertByteEquals(qf, ks.get(0).getQualifier());
71  
72        assertEquals(ks.get(0), r.getColumnLatest(family, qf));
73        assertByteEquals(Bytes.add(value, Bytes.toBytes(i)), r.getValue(family, qf));
74        assertTrue(r.containsColumn(family, qf));
75      }
76    }
77    public void testMultiVersion() throws Exception {
78      KeyValue [] kvs1 = genKVs(row, family, value, 1, 100);
79      KeyValue [] kvs2 = genKVs(row, family, value, 200, 100);
80  
81      KeyValue [] kvs = new KeyValue[kvs1.length+kvs2.length];
82      System.arraycopy(kvs1, 0, kvs, 0, kvs1.length);
83      System.arraycopy(kvs2, 0, kvs, kvs1.length, kvs2.length);
84  
85      Arrays.sort(kvs, KeyValue.COMPARATOR);
86  
87      Result r = new Result(kvs);
88      for (int i = 0; i < 100; ++i) {
89        final byte[] qf = Bytes.toBytes(i);
90  
91        List<KeyValue> ks = r.getColumn(family, qf);
92        assertEquals(2, ks.size());
93        assertByteEquals(qf, ks.get(0).getQualifier());
94        assertEquals(200, ks.get(0).getTimestamp());
95  
96        assertEquals(ks.get(0), r.getColumnLatest(family, qf));
97        assertByteEquals(Bytes.add(value, Bytes.toBytes(i)), r.getValue(family, qf));
98        assertTrue(r.containsColumn(family, qf));
99      }
100   }
101 
102   /**
103    * Verify that Result.compareResults(...) behaves correctly.
104    */
105   public void testCompareResults() throws Exception {
106     byte [] value1 = Bytes.toBytes("value1");
107     byte [] qual = Bytes.toBytes("qual");
108 
109     KeyValue kv1 = new KeyValue(row, family, qual, value);
110     KeyValue kv2 = new KeyValue(row, family, qual, value1);
111 
112     Result r1 = new Result(new KeyValue[] {kv1});
113     Result r2 = new Result(new KeyValue[] {kv2});
114     // no exception thrown
115     Result.compareResults(r1, r1);
116     try {
117       // these are different (HBASE-4800)
118       Result.compareResults(r1, r2);
119       fail();
120     } catch (Exception x) {
121       assertTrue(x.getMessage().startsWith("This result was different:"));
122     }
123   }
124 
125   @org.junit.Rule
126   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
127     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
128 }
129