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.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertNotNull;
022import static org.junit.jupiter.api.Assertions.assertNull;
023import static org.junit.jupiter.api.Assertions.assertTrue;
024
025import java.util.Collection;
026import java.util.Set;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.junit.jupiter.api.BeforeEach;
029import org.junit.jupiter.api.Tag;
030import org.junit.jupiter.api.Test;
031
032import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
033
034@Tag(SmallTests.TAG)
035public class TestRefCountingMap {
036
037  private RefCountingMap<String, String> map;
038
039  @BeforeEach
040  public void setUp() {
041    map = new RefCountingMap<>();
042  }
043
044  @Test
045  public void testPutGet() {
046    map.put("foo", () -> "foovalue");
047
048    String v = map.get("foo");
049    assertNotNull(v);
050    assertEquals("foovalue", v);
051  }
052
053  @Test
054  public void testPutMulti() {
055    String v1 = map.put("foo", () -> "foovalue");
056    String v2 = map.put("foo", () -> "foovalue2");
057    String v3 = map.put("foo", () -> "foovalue3");
058
059    String v = map.get("foo");
060    assertEquals("foovalue", v);
061    assertEquals(v, v1);
062    assertEquals(v, v2);
063    assertEquals(v, v3);
064  }
065
066  @Test
067  public void testPutRemove() {
068    map.put("foo", () -> "foovalue");
069    String v = map.remove("foo");
070    assertNull(v);
071    v = map.get("foo");
072    assertNull(v);
073  }
074
075  @Test
076  public void testPutRemoveMulti() {
077    map.put("foo", () -> "foovalue");
078    map.put("foo", () -> "foovalue2");
079    map.put("foo", () -> "foovalue3");
080
081    // remove 1
082    String v = map.remove("foo");
083    assertEquals("foovalue", v);
084
085    // remove 2
086    v = map.remove("foo");
087    assertEquals("foovalue", v);
088
089    // remove 3
090    v = map.remove("foo");
091    assertNull(v);
092    v = map.get("foo");
093    assertNull(v);
094  }
095
096  @Test
097  public void testSize() {
098    assertEquals(0, map.size());
099
100    // put a key
101    map.put("foo", () -> "foovalue");
102    assertEquals(1, map.size());
103
104    // put a different key
105    map.put("bar", () -> "foovalue2");
106    assertEquals(2, map.size());
107
108    // put the same key again
109    map.put("bar", () -> "foovalue3");
110    assertEquals(2, map.size()); // map should be same size
111  }
112
113  @Test
114  public void testClear() {
115    map.put("foo", () -> "foovalue");
116    map.put("bar", () -> "foovalue2");
117    map.put("baz", () -> "foovalue3");
118
119    map.clear();
120
121    assertEquals(0, map.size());
122  }
123
124  @Test
125  public void testKeySet() {
126    map.put("foo", () -> "foovalue");
127    map.put("bar", () -> "foovalue2");
128    map.put("baz", () -> "foovalue3");
129
130    Set<String> keys = map.keySet();
131    assertEquals(3, keys.size());
132
133    Lists.newArrayList("foo", "bar", "baz").stream().forEach(v -> assertTrue(keys.contains(v)));
134  }
135
136  @Test
137  public void testValues() {
138    map.put("foo", () -> "foovalue");
139    map.put("foo", () -> "foovalue2");
140    map.put("bar", () -> "foovalue3");
141    map.put("baz", () -> "foovalue4");
142
143    Collection<String> values = map.values();
144    assertEquals(3, values.size());
145
146    Lists.newArrayList("foovalue", "foovalue3", "foovalue4").stream()
147      .forEach(v -> assertTrue(values.contains(v)));
148  }
149}