1   /**
2    * Copyright 2009 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 java.io.ByteArrayInputStream;
24  import java.io.ByteArrayOutputStream;
25  import java.io.DataInputStream;
26  import java.io.DataOutput;
27  import java.io.DataOutputStream;
28  import java.io.IOException;
29  import java.util.Arrays;
30  
31  import org.apache.hadoop.hbase.util.Bytes;
32  import org.junit.Assert;
33  import org.junit.Test;
34  
35  // TODO: cover more test cases
36  public class TestScan {
37    @Test
38    public void testAttributesSerialization() throws IOException {
39      Scan scan = new Scan();
40      scan.setAttribute("attribute1", Bytes.toBytes("value1"));
41      scan.setAttribute("attribute2", Bytes.toBytes("value2"));
42      scan.setAttribute("attribute3", Bytes.toBytes("value3"));
43  
44      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
45      DataOutput out = new DataOutputStream(byteArrayOutputStream);
46      scan.write(out);
47  
48      Scan scan2 = new Scan();
49      Assert.assertTrue(scan2.getAttributesMap().isEmpty());
50  
51      scan2.readFields(new DataInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())));
52  
53      Assert.assertNull(scan2.getAttribute("absent"));
54      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), scan2.getAttribute("attribute1")));
55      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), scan2.getAttribute("attribute2")));
56      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value3"), scan2.getAttribute("attribute3")));
57      Assert.assertEquals(3, scan2.getAttributesMap().size());
58    }
59  
60    @Test
61    public void testScanAttributes() {
62      Scan scan = new Scan();
63      Assert.assertTrue(scan.getAttributesMap().isEmpty());
64      Assert.assertNull(scan.getAttribute("absent"));
65  
66      scan.setAttribute("absent", null);
67      Assert.assertTrue(scan.getAttributesMap().isEmpty());
68      Assert.assertNull(scan.getAttribute("absent"));
69  
70      // adding attribute
71      scan.setAttribute("attribute1", Bytes.toBytes("value1"));
72      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), scan.getAttribute("attribute1")));
73      Assert.assertEquals(1, scan.getAttributesMap().size());
74      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), scan.getAttributesMap().get("attribute1")));
75  
76      // overriding attribute value
77      scan.setAttribute("attribute1", Bytes.toBytes("value12"));
78      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), scan.getAttribute("attribute1")));
79      Assert.assertEquals(1, scan.getAttributesMap().size());
80      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), scan.getAttributesMap().get("attribute1")));
81  
82      // adding another attribute
83      scan.setAttribute("attribute2", Bytes.toBytes("value2"));
84      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), scan.getAttribute("attribute2")));
85      Assert.assertEquals(2, scan.getAttributesMap().size());
86      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), scan.getAttributesMap().get("attribute2")));
87  
88      // removing attribute
89      scan.setAttribute("attribute2", null);
90      Assert.assertNull(scan.getAttribute("attribute2"));
91      Assert.assertEquals(1, scan.getAttributesMap().size());
92      Assert.assertNull(scan.getAttributesMap().get("attribute2"));
93  
94      // removing non-existed attribute
95      scan.setAttribute("attribute2", null);
96      Assert.assertNull(scan.getAttribute("attribute2"));
97      Assert.assertEquals(1, scan.getAttributesMap().size());
98      Assert.assertNull(scan.getAttributesMap().get("attribute2"));
99  
100     // removing another attribute
101     scan.setAttribute("attribute1", null);
102     Assert.assertNull(scan.getAttribute("attribute1"));
103     Assert.assertTrue(scan.getAttributesMap().isEmpty());
104     Assert.assertNull(scan.getAttributesMap().get("attribute1"));
105   }
106 }