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  
21  package org.apache.hadoop.hbase.metrics.impl;
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertNull;
26  import static org.junit.Assert.assertTrue;
27  
28  import java.util.Collection;
29  import java.util.Set;
30  
31  import org.apache.hadoop.hbase.testclassification.SmallTests;
32  import org.junit.Before;
33  import org.junit.Test;
34  import org.junit.experimental.categories.Category;
35  
36  import com.google.common.base.Supplier;
37  import com.google.common.collect.Lists;
38  
39  @Category(SmallTests.class)
40  public class TestRefCountingMap {
41  
42    private RefCountingMap<String, String> map;
43  
44    @Before
45    public void setUp() {
46      map = new RefCountingMap<>();
47    }
48  
49    @Test
50    public void testPutGet() {
51      map.put("foo", new Supplier<String>() {
52  
53        @Override
54        public String get() {
55          return "foovalue";
56        }
57      });
58  
59      String v = map.get("foo");
60      assertNotNull(v);
61      assertEquals("foovalue", v);
62    }
63  
64    @Test
65    public void testPutMulti() {
66      String v1 = map.put("foo", new Supplier<String>() {
67  
68        @Override
69        public String get() {
70          return "foovalue";
71        }
72      });
73      String v2 = map.put("foo", new Supplier<String>() {
74  
75        @Override
76        public String get() {
77          return "foovalue2";
78        }
79      });
80      String v3 = map.put("foo", new Supplier<String>() {
81  
82        @Override
83        public String get() {
84          return "foovalue3";
85        }
86      });
87  
88      String v = map.get("foo");
89      assertEquals("foovalue", v);
90      assertEquals(v, v1);
91      assertEquals(v, v2);
92      assertEquals(v, v3);
93    }
94  
95    @Test
96    public void testPutRemove() {
97      map.put("foo", new Supplier<String>() {
98  
99        @Override
100       public String get() {
101         return "foovalue";
102       }
103     });
104     String v = map.remove("foo");
105     assertNull(v);
106     v = map.get("foo");
107     assertNull(v);
108   }
109 
110   @Test
111   public void testPutRemoveMulti() {
112     map.put("foo", new Supplier<String>() {
113 
114       @Override
115       public String get() {
116         return "foovalue";
117       }
118     });
119     map.put("foo", new Supplier<String>() {
120 
121       @Override
122       public String get() {
123         return "foovalue2";
124       }
125     });
126     map.put("foo", new Supplier<String>() {
127 
128       @Override
129       public String get() {
130         return "foovalue3";
131       }
132     });
133 
134     // remove 1
135     String v = map.remove("foo");
136     assertEquals("foovalue", v);
137 
138     // remove 2
139     v = map.remove("foo");
140     assertEquals("foovalue", v);
141 
142     // remove 3
143     v = map.remove("foo");
144     assertNull(v);
145     v = map.get("foo");
146     assertNull(v);
147   }
148 
149   @Test
150   public void testSize() {
151     assertEquals(0, map.size());
152 
153     // put a key
154     map.put("foo", new Supplier<String>() {
155 
156       @Override
157       public String get() {
158         return "foovalue";
159       }
160     });
161     assertEquals(1, map.size());
162 
163     // put a different key
164     map.put("bar", new Supplier<String>() {
165 
166       @Override
167       public String get() {
168         return "foovalue2";
169       }
170     });
171     assertEquals(2, map.size());
172 
173     // put the same key again
174     map.put("bar", new Supplier<String>() {
175 
176       @Override
177       public String get() {
178         return "foovalue3";
179       }
180     });
181     assertEquals(2, map.size()); // map should be same size
182   }
183 
184   @Test
185   public void testClear() {
186     map.put("foo", new Supplier<String>() {
187 
188       @Override
189       public String get() {
190         return "foovalue";
191       }
192     });
193     map.put("bar", new Supplier<String>() {
194 
195       @Override
196       public String get() {
197         return "foovalue2";
198       }
199     });
200     map.put("baz", new Supplier<String>() {
201 
202       @Override
203       public String get() {
204         return "foovalue3";
205       }
206     });
207 
208     map.clear();
209 
210     assertEquals(0, map.size());
211   }
212 
213 
214   @Test
215   public void testKeySet() {
216     map.put("foo", new Supplier<String>() {
217 
218       @Override
219       public String get() {
220         return "foovalue";
221       }
222     });
223     map.put("bar", new Supplier<String>() {
224 
225       @Override
226       public String get() {
227         return "foovalue2";
228       }
229     });
230     map.put("baz", new Supplier<String>() {
231 
232       @Override
233       public String get() {
234         return "foovalue3";
235       }
236     });
237 
238     Set<String> keys = map.keySet();
239     assertEquals(3, keys.size());
240 
241     for (String v : Lists.newArrayList("foo", "bar", "baz")) {
242       assertTrue(keys.contains(v));
243     }
244   }
245 
246   @Test
247   public void testValues() {
248     map.put("foo", new Supplier<String>() {
249 
250       @Override
251       public String get() {
252         return "foovalue";
253       }
254     });
255     map.put("foo", new Supplier<String>() {
256 
257       @Override
258       public String get() {
259         return "foovalue2";
260       }
261     });
262     map.put("bar", new Supplier<String>() {
263 
264       @Override
265       public String get() {
266         return "foovalue3";
267       }
268     });
269     map.put("baz", new Supplier<String>() {
270 
271       @Override
272       public String get() {
273         return "foovalue4";
274       }
275     });
276 
277     Collection<String> values = map.values();
278     assertEquals(3, values.size());
279 
280     for (String v : Lists.newArrayList("foovalue", "foovalue3", "foovalue4")) {
281       assertTrue(values.contains(v));
282     }
283   }
284 }