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.io.hfile;
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import org.junit.Test;
30  
31  /**
32   * Tests the BlockCacheColumnFamilySummary class
33   * 
34   */
35  public class TestBlockCacheColumnFamilySummary {
36  
37  
38    /**
39     * 
40     */
41    @Test
42    public void testEquals()  {
43  
44      BlockCacheColumnFamilySummary e1 = new BlockCacheColumnFamilySummary();
45      e1.setTable("table1");
46      e1.setColumnFamily("cf1");
47      
48      BlockCacheColumnFamilySummary e2 = new BlockCacheColumnFamilySummary();
49      e2.setTable("table1");
50      e2.setColumnFamily("cf1");
51  
52      assertEquals("bcse", e1, e2);
53    }
54  
55    /**
56     * 
57     */
58    @Test
59    public void testNotEquals() {
60  
61      BlockCacheColumnFamilySummary e1 = new BlockCacheColumnFamilySummary();
62      e1.setTable("table1");
63      e1.setColumnFamily("cf1");
64      
65      BlockCacheColumnFamilySummary e2 = new BlockCacheColumnFamilySummary();
66      e2.setTable("tablexxxxxx");
67      e2.setColumnFamily("cf1");
68  
69      assertTrue("bcse", ! e1.equals(e2));
70    }
71  
72    /**
73     * 
74     */
75    @Test
76    public void testMapLookup() {
77      
78      Map<BlockCacheColumnFamilySummary, BlockCacheColumnFamilySummary> bcs = 
79        new HashMap<BlockCacheColumnFamilySummary, BlockCacheColumnFamilySummary>();
80  
81      BlockCacheColumnFamilySummary e1 = new BlockCacheColumnFamilySummary("table1","cf1");
82  
83      BlockCacheColumnFamilySummary lookup = bcs.get(e1);
84  
85      if (lookup == null) {
86        lookup = BlockCacheColumnFamilySummary.create(e1);
87        bcs.put(e1,lookup);
88        lookup.incrementBlocks();
89        lookup.incrementHeapSize(100L);
90      }
91  
92      BlockCacheColumnFamilySummary e2 = new BlockCacheColumnFamilySummary("table1","cf1");
93  
94      BlockCacheColumnFamilySummary l2 = bcs.get(e2);
95      assertEquals("blocks",1,l2.getBlocks());
96      assertEquals("heap",100L,l2.getHeapSize());
97    }
98  
99    /**
100    * 
101    */
102   @Test
103   public void testMapEntry() {
104     
105     Map<BlockCacheColumnFamilySummary, BlockCacheColumnFamilySummary> bcs = 
106       new HashMap<BlockCacheColumnFamilySummary, BlockCacheColumnFamilySummary>();
107 
108     BlockCacheColumnFamilySummary e1 = new BlockCacheColumnFamilySummary("table1","cf1");
109     bcs.put(e1, e1);
110     
111     BlockCacheColumnFamilySummary e2 = new BlockCacheColumnFamilySummary("table1","cf1");
112     bcs.put(e2, e2);
113     
114     BlockCacheColumnFamilySummary e3 = new BlockCacheColumnFamilySummary("table1","cf1");
115     bcs.put(e3, e3);
116     
117     assertEquals("mapSize",1,bcs.size());
118   }
119 
120 }