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