1   /**
2    * Copyright 2011 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 static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.fail;
25  
26  import java.io.ByteArrayInputStream;
27  import java.io.ByteArrayOutputStream;
28  import java.io.DataInput;
29  import java.io.DataInputStream;
30  import java.io.DataOutput;
31  import java.io.DataOutputStream;
32  import java.io.File;
33  import java.io.FileOutputStream;
34  import java.io.IOException;
35  import java.util.Arrays;
36  
37  import org.apache.hadoop.conf.Configuration;
38  import org.apache.hadoop.hbase.HBaseConfiguration;
39  import org.apache.hadoop.hbase.SmallTests;
40  import org.apache.hadoop.hbase.util.Base64;
41  import org.apache.hadoop.hbase.util.Bytes;
42  import org.junit.Assert;
43  import org.junit.Test;
44  import org.junit.experimental.categories.Category;
45  
46  import com.google.common.io.ByteStreams;
47  
48  // TODO: cover more test cases
49  @Category(SmallTests.class)
50  public class TestGet {
51  
52    private static final String WRITABLE_GET =
53      "AgD//////////wAAAAEBD3Rlc3QuTW9ja0ZpbHRlcgEAAAAAAAAAAH//////////AQAAAAAAAAAA";
54  
55    private static final String MOCK_FILTER_JAR =
56      "UEsDBBQACAgIACmBi0IAAAAAAAAAAAAAAAAJAAQATUVUQS1JTkYv/soAAAMAUEsHCAAAAAACAAAA" +
57      "AAAAAFBLAwQUAAgICAApgYtCAAAAAAAAAAAAAAAAFAAAAE1FVEEtSU5GL01BTklGRVNULk1G803M" +
58      "y0xLLS7RDUstKs7Mz7NSMNQz4OVyLkpNLElN0XWqBAmY6xnEG1gqaPgXJSbnpCo45xcV5BcllgCV" +
59      "a/Jy8XIBAFBLBwgxyqRbQwAAAEQAAABQSwMECgAACAAAbICLQgAAAAAAAAAAAAAAAAUAAAB0ZXN0" +
60      "L1BLAwQUAAgICAAcgItCAAAAAAAAAAAAAAAAFQAAAHRlc3QvTW9ja0ZpbHRlci5jbGFzc41Qy07C" +
61      "QBS9A4VKBZGHoO7cgQvHmLjCuPBBQlJloWE/tCMdLZ1mOlV/y5WJCz/AjzLeDqCRYOIs7uuce87N" +
62      "fHy+vQPAEezakCNQ1TzR9Ep6D30Raq5ssAh0pZpQFjMv4DRgvpQxDcYs4fTOcOiMeoYTAsUTEQl9" +
63      "SiDf6Y4IWOfS5w7koVSGAhTRwBURv06nY65u2TjEjborPRaOmBJZPx9aOhAJgZq7dE+PgKM48/uC" +
64      "hz4SWh33nj0yKiS9YJoNojjVvczYuXz2eKyFjBIb6gQaC9pg+I2gDVOTQwRXiBAoPCmh8Zb2b49h" +
65      "qhcmzVUAet/IVHkcL8bt6s/xBxkb9gA/B7KXxwo/BaONHcVMMBf2X2HtBYscOBiLZliCdYzlGQFz" +
66      "BTOBDagiaxNrC7uakTk2m4guS1SMRGsGziWyqgFN47xlsH+K1f4UaxuxbcPf+QJQSwcI8UIYqlEB" +
67      "AABeAgAAUEsBAhQAFAAICAgAKYGLQgAAAAACAAAAAAAAAAkABAAAAAAAAAAAAAAAAAAAAE1FVEEt" +
68      "SU5GL/7KAABQSwECFAAUAAgICAApgYtCMcqkW0MAAABEAAAAFAAAAAAAAAAAAAAAAAA9AAAATUVU" +
69      "QS1JTkYvTUFOSUZFU1QuTUZQSwECCgAKAAAIAABsgItCAAAAAAAAAAAAAAAABQAAAAAAAAAAAAAA" +
70      "AADCAAAAdGVzdC9QSwECFAAUAAgICAAcgItC8UIYqlEBAABeAgAAFQAAAAAAAAAAAAAAAADlAAAA" +
71      "dGVzdC9Nb2NrRmlsdGVyLmNsYXNzUEsFBgAAAAAEAAQA8wAAAHkCAAAAAA==";
72  
73    @Test
74    public void testAttributesSerialization() throws IOException {
75      Get get = new Get();
76      get.setAttribute("attribute1", Bytes.toBytes("value1"));
77      get.setAttribute("attribute2", Bytes.toBytes("value2"));
78      get.setAttribute("attribute3", Bytes.toBytes("value3"));
79  
80      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
81      DataOutput out = new DataOutputStream(byteArrayOutputStream);
82      get.write(out);
83  
84      Get get2 = new Get();
85      Assert.assertTrue(get2.getAttributesMap().isEmpty());
86  
87      get2.readFields(new DataInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())));
88  
89      Assert.assertNull(get2.getAttribute("absent"));
90      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), get2.getAttribute("attribute1")));
91      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), get2.getAttribute("attribute2")));
92      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value3"), get2.getAttribute("attribute3")));
93      Assert.assertEquals(3, get2.getAttributesMap().size());
94    }
95  
96    @Test
97    public void testGetAttributes() {
98      Get get = new Get();
99      Assert.assertTrue(get.getAttributesMap().isEmpty());
100     Assert.assertNull(get.getAttribute("absent"));
101 
102     get.setAttribute("absent", null);
103     Assert.assertTrue(get.getAttributesMap().isEmpty());
104     Assert.assertNull(get.getAttribute("absent"));
105 
106     // adding attribute
107     get.setAttribute("attribute1", Bytes.toBytes("value1"));
108     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), get.getAttribute("attribute1")));
109     Assert.assertEquals(1, get.getAttributesMap().size());
110     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), get.getAttributesMap().get("attribute1")));
111 
112     // overriding attribute value
113     get.setAttribute("attribute1", Bytes.toBytes("value12"));
114     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), get.getAttribute("attribute1")));
115     Assert.assertEquals(1, get.getAttributesMap().size());
116     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), get.getAttributesMap().get("attribute1")));
117 
118     // adding another attribute
119     get.setAttribute("attribute2", Bytes.toBytes("value2"));
120     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), get.getAttribute("attribute2")));
121     Assert.assertEquals(2, get.getAttributesMap().size());
122     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), get.getAttributesMap().get("attribute2")));
123 
124     // removing attribute
125     get.setAttribute("attribute2", null);
126     Assert.assertNull(get.getAttribute("attribute2"));
127     Assert.assertEquals(1, get.getAttributesMap().size());
128     Assert.assertNull(get.getAttributesMap().get("attribute2"));
129 
130     // removing non-existed attribute
131     get.setAttribute("attribute2", null);
132     Assert.assertNull(get.getAttribute("attribute2"));
133     Assert.assertEquals(1, get.getAttributesMap().size());
134     Assert.assertNull(get.getAttributesMap().get("attribute2"));
135 
136     // removing another attribute
137     get.setAttribute("attribute1", null);
138     Assert.assertNull(get.getAttribute("attribute1"));
139     Assert.assertTrue(get.getAttributesMap().isEmpty());
140     Assert.assertNull(get.getAttributesMap().get("attribute1"));
141   }
142 
143   @Test
144   public void testDynamicFilter() throws Exception {
145     Configuration conf = HBaseConfiguration.create();
146     String localPath = conf.get("hbase.local.dir")
147       + File.separator + "jars" + File.separator;
148     File jarFile = new File(localPath, "MockFilter.jar");
149     jarFile.delete();
150     assertFalse("Should be deleted: " + jarFile.getPath(), jarFile.exists());
151 
152     DataInput dis = ByteStreams.newDataInput(Base64.decode(WRITABLE_GET));
153     Get get = new Get();
154     try {
155       get.readFields(dis);
156       fail("Should not be able to load the filter class");
157     } catch (RuntimeException re) {
158       String msg = re.getMessage();
159       Assert.assertTrue(msg != null
160         && msg.contains("Can't find class test.MockFilter"));
161     }
162 
163     FileOutputStream fos = new FileOutputStream(jarFile);
164     fos.write(Base64.decode(MOCK_FILTER_JAR));
165     fos.close();
166 
167     dis = ByteStreams.newDataInput(Base64.decode(WRITABLE_GET));
168     get.readFields(dis);
169     Assert.assertEquals("test.MockFilter",
170       get.getFilter().getClass().getName());
171   }
172 
173   @org.junit.Rule
174   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
175     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
176 }
177