View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.client;
21  
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.fail;
24  
25  import java.io.File;
26  import java.io.FileOutputStream;
27  import java.io.IOException;
28  import java.util.Arrays;
29  import java.util.Set;
30  
31  import org.apache.hadoop.conf.Configuration;
32  import org.apache.hadoop.hbase.HBaseConfiguration;
33  import org.apache.hadoop.hbase.SmallTests;
34  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
35  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
36  import org.apache.hadoop.hbase.util.Base64;
37  import org.apache.hadoop.hbase.util.Bytes;
38  import org.junit.Assert;
39  import org.junit.Test;
40  import org.junit.experimental.categories.Category;
41  
42  // TODO: cover more test cases
43  @Category(SmallTests.class)
44  public class TestGet {
45    private static final byte [] ROW = new byte [] {'r'};
46  
47    private static final String PB_GET = "CgNyb3ciEwoPdGVzdC5Nb2NrRmlsdGVyEgAwATgB";
48  
49    private static final String MOCK_FILTER_JAR =
50      "UEsDBBQACAgIACqBiEIAAAAAAAAAAAAAAAAJAAQATUVUQS1JTkYv/soAAAMAUEsHCAAAAAACAAAA" +
51      "AAAAAFBLAwQUAAgICAAqgYhCAAAAAAAAAAAAAAAAFAAAAE1FVEEtSU5GL01BTklGRVNULk1G803M" +
52      "y0xLLS7RDUstKs7Mz7NSMNQz4OVyLkpNLElN0XWqBAmY6xnEG1gqaPgXJSbnpCo45xcV5BcllgCV" +
53      "a/Jy8XIBAFBLBwgxyqRbQwAAAEQAAABQSwMECgAACAAAz4CIQgAAAAAAAAAAAAAAAAUAAAB0ZXN0" +
54      "L1BLAwQUAAgICACPgIhCAAAAAAAAAAAAAAAAFQAAAHRlc3QvTW9ja0ZpbHRlci5jbGFzc41Qy0rD" +
55      "QBQ9k6RNG6N9aH2uXAhWwUC3FRdRC0J1oxSkq0k6mmjaCUkq6lfpqqLgB/hR4k1aqlQEs7j3zLnn" +
56      "3Ec+Pl/fATSwoUNhKCUiTqxT6d62/CARkQ6NoS6ja4uH3PWE5fGelKHlOTwW1lWmscZSmxiG/L4/" +
57      "8JMDBnW73mHQDmVPGFBRNJFDnga0/YE4G/YdEV1wJyBHtS1dHnR45KfvCaklnh8zVNoz+zQZiiGP" +
58      "YtGKZJ+htt216780BkjFoIeO/UA1BqVrM+xm2n+dQlOM43tXhIkvB7GOZYbmX0Yx1VlHIhZ0ReA/" +
59      "8pSYdkj3WTWxgBL1PZfDyBU0h64sfS+9d8PvODZJqSL9VEL0wyjq9LIoM8q5nREKzwQUGBTzYxJz" +
60      "FM0JNjFPuZhOm5gbpE5rhTewyxHKTzN+/Ye/gAqqQPmE/IukWiJOo0ot67Q1XeMFK7NtWNZGydBa" +
61      "hta/AFBLBwjdsJqTXwEAAF0CAABQSwECFAAUAAgICAAqgYhCAAAAAAIAAAAAAAAACQAEAAAAAAAA" +
62      "AAAAAAAAAAAATUVUQS1JTkYv/soAAFBLAQIUABQACAgIACqBiEIxyqRbQwAAAEQAAAAUAAAAAAAA" +
63      "AAAAAAAAAD0AAABNRVRBLUlORi9NQU5JRkVTVC5NRlBLAQIKAAoAAAgAAM+AiEIAAAAAAAAAAAAA" +
64      "AAAFAAAAAAAAAAAAAAAAAMIAAAB0ZXN0L1BLAQIUABQACAgIAI+AiELdsJqTXwEAAF0CAAAVAAAA" +
65      "AAAAAAAAAAAAAOUAAAB0ZXN0L01vY2tGaWx0ZXIuY2xhc3NQSwUGAAAAAAQABADzAAAAhwIAAAAA";
66  
67    @Test
68    public void testAttributesSerialization() throws IOException {
69      Get get = new Get(Bytes.toBytes("row"));
70      get.setAttribute("attribute1", Bytes.toBytes("value1"));
71      get.setAttribute("attribute2", Bytes.toBytes("value2"));
72      get.setAttribute("attribute3", Bytes.toBytes("value3"));
73  
74      ClientProtos.Get getProto = ProtobufUtil.toGet(get);
75  
76      Get get2 = ProtobufUtil.toGet(getProto);
77      Assert.assertNull(get2.getAttribute("absent"));
78      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), get2.getAttribute("attribute1")));
79      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), get2.getAttribute("attribute2")));
80      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value3"), get2.getAttribute("attribute3")));
81      Assert.assertEquals(3, get2.getAttributesMap().size());
82    }
83  
84    @Test
85    public void testGetAttributes() {
86      Get get = new Get(ROW);
87      Assert.assertTrue(get.getAttributesMap().isEmpty());
88      Assert.assertNull(get.getAttribute("absent"));
89  
90      get.setAttribute("absent", null);
91      Assert.assertTrue(get.getAttributesMap().isEmpty());
92      Assert.assertNull(get.getAttribute("absent"));
93  
94      // adding attribute
95      get.setAttribute("attribute1", Bytes.toBytes("value1"));
96      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), get.getAttribute("attribute1")));
97      Assert.assertEquals(1, get.getAttributesMap().size());
98      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), get.getAttributesMap().get("attribute1")));
99  
100     // overriding attribute value
101     get.setAttribute("attribute1", Bytes.toBytes("value12"));
102     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), get.getAttribute("attribute1")));
103     Assert.assertEquals(1, get.getAttributesMap().size());
104     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), get.getAttributesMap().get("attribute1")));
105 
106     // adding another attribute
107     get.setAttribute("attribute2", Bytes.toBytes("value2"));
108     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), get.getAttribute("attribute2")));
109     Assert.assertEquals(2, get.getAttributesMap().size());
110     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), get.getAttributesMap().get("attribute2")));
111 
112     // removing attribute
113     get.setAttribute("attribute2", null);
114     Assert.assertNull(get.getAttribute("attribute2"));
115     Assert.assertEquals(1, get.getAttributesMap().size());
116     Assert.assertNull(get.getAttributesMap().get("attribute2"));
117 
118     // removing non-existed attribute
119     get.setAttribute("attribute2", null);
120     Assert.assertNull(get.getAttribute("attribute2"));
121     Assert.assertEquals(1, get.getAttributesMap().size());
122     Assert.assertNull(get.getAttributesMap().get("attribute2"));
123 
124     // removing another attribute
125     get.setAttribute("attribute1", null);
126     Assert.assertNull(get.getAttribute("attribute1"));
127     Assert.assertTrue(get.getAttributesMap().isEmpty());
128     Assert.assertNull(get.getAttributesMap().get("attribute1"));
129   }
130 
131   @Test
132   public void testNullQualifier() {
133     Get get = new Get(ROW);
134     byte[] family = Bytes.toBytes("family");
135     get.addColumn(family, null);
136     Set<byte[]> qualifiers = get.getFamilyMap().get(family);
137     Assert.assertEquals(1, qualifiers.size());
138   }
139 
140   @Test
141   public void testDynamicFilter() throws Exception {
142     Configuration conf = HBaseConfiguration.create();
143     String localPath = conf.get("hbase.local.dir")
144       + File.separator + "jars" + File.separator;
145     File jarFile = new File(localPath, "MockFilter.jar");
146     jarFile.delete();
147     assertFalse("Should be deleted: " + jarFile.getPath(), jarFile.exists());
148 
149     ClientProtos.Get getProto = ClientProtos.Get.parseFrom(Base64.decode(PB_GET));
150     try {
151       ProtobufUtil.toGet(getProto);
152       fail("Should not be able to load the filter class");
153     } catch (IOException ioe) {
154       Assert.assertTrue(ioe.getCause() instanceof ClassNotFoundException);
155     }
156     FileOutputStream fos = new FileOutputStream(jarFile);
157     fos.write(Base64.decode(MOCK_FILTER_JAR));
158     fos.close();
159 
160     Get get = ProtobufUtil.toGet(getProto);
161     Assert.assertEquals("test.MockFilter",
162       get.getFilter().getClass().getName());
163   }
164 }