001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.metrics.impl; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertNotNull; 022import static org.junit.Assert.assertNull; 023import static org.junit.Assert.assertTrue; 024 025import java.util.Collection; 026import java.util.Set; 027import org.apache.hadoop.hbase.HBaseClassTestRule; 028import org.apache.hadoop.hbase.testclassification.SmallTests; 029import org.junit.Before; 030import org.junit.ClassRule; 031import org.junit.Test; 032import org.junit.experimental.categories.Category; 033 034import org.apache.hbase.thirdparty.com.google.common.collect.Lists; 035 036@Category(SmallTests.class) 037public class TestRefCountingMap { 038 039 @ClassRule 040 public static final HBaseClassTestRule CLASS_RULE = 041 HBaseClassTestRule.forClass(TestRefCountingMap.class); 042 043 private RefCountingMap<String, String> map; 044 045 @Before 046 public void setUp() { 047 map = new RefCountingMap<>(); 048 } 049 050 @Test 051 public void testPutGet() { 052 map.put("foo", () -> "foovalue"); 053 054 String v = map.get("foo"); 055 assertNotNull(v); 056 assertEquals("foovalue", v); 057 } 058 059 @Test 060 public void testPutMulti() { 061 String v1 = map.put("foo", () -> "foovalue"); 062 String v2 = map.put("foo", () -> "foovalue2"); 063 String v3 = map.put("foo", () -> "foovalue3"); 064 065 String v = map.get("foo"); 066 assertEquals("foovalue", v); 067 assertEquals(v, v1); 068 assertEquals(v, v2); 069 assertEquals(v, v3); 070 } 071 072 @Test 073 public void testPutRemove() { 074 map.put("foo", () -> "foovalue"); 075 String v = map.remove("foo"); 076 assertNull(v); 077 v = map.get("foo"); 078 assertNull(v); 079 } 080 081 @Test 082 public void testPutRemoveMulti() { 083 map.put("foo", () -> "foovalue"); 084 map.put("foo", () -> "foovalue2"); 085 map.put("foo", () -> "foovalue3"); 086 087 // remove 1 088 String v = map.remove("foo"); 089 assertEquals("foovalue", v); 090 091 // remove 2 092 v = map.remove("foo"); 093 assertEquals("foovalue", v); 094 095 // remove 3 096 v = map.remove("foo"); 097 assertNull(v); 098 v = map.get("foo"); 099 assertNull(v); 100 } 101 102 @Test 103 public void testSize() { 104 assertEquals(0, map.size()); 105 106 // put a key 107 map.put("foo", () -> "foovalue"); 108 assertEquals(1, map.size()); 109 110 // put a different key 111 map.put("bar", () -> "foovalue2"); 112 assertEquals(2, map.size()); 113 114 // put the same key again 115 map.put("bar", () -> "foovalue3"); 116 assertEquals(2, map.size()); // map should be same size 117 } 118 119 @Test 120 public void testClear() { 121 map.put("foo", () -> "foovalue"); 122 map.put("bar", () -> "foovalue2"); 123 map.put("baz", () -> "foovalue3"); 124 125 map.clear(); 126 127 assertEquals(0, map.size()); 128 } 129 130 131 @Test 132 public void testKeySet() { 133 map.put("foo", () -> "foovalue"); 134 map.put("bar", () -> "foovalue2"); 135 map.put("baz", () -> "foovalue3"); 136 137 Set<String> keys = map.keySet(); 138 assertEquals(3, keys.size()); 139 140 Lists.newArrayList("foo", "bar", "baz").stream().forEach(v -> assertTrue(keys.contains(v))); 141 } 142 143 @Test 144 public void testValues() { 145 map.put("foo", () -> "foovalue"); 146 map.put("foo", () -> "foovalue2"); 147 map.put("bar", () -> "foovalue3"); 148 map.put("baz", () -> "foovalue4"); 149 150 Collection<String> values = map.values(); 151 assertEquals(3, values.size()); 152 153 Lists.newArrayList("foovalue", "foovalue3", "foovalue4").stream() 154 .forEach(v -> assertTrue(values.contains(v))); 155 } 156}