1 /* 2 * Copyright (C) 2016 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.graph; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import com.google.common.collect.Ordering; 22 import java.util.Arrays; 23 import java.util.Collection; 24 import java.util.Comparator; 25 import java.util.HashMap; 26 import java.util.TreeMap; 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.junit.runners.Parameterized; 31 import org.junit.runners.Parameterized.Parameters; 32 33 /** Tests for {@link MapIteratorCache} and {@link MapRetrievalCache}. */ 34 @AndroidIncompatible 35 // TODO(cpovirk): Figure out Android JUnit 4 support. Does it work with Gingerbread? @RunWith? 36 @RunWith(Parameterized.class) 37 public final class MapCacheTest { 38 private final MapIteratorCache<String, String> mapCache; 39 MapCacheTest(MapIteratorCache<String, String> mapCache)40 public MapCacheTest(MapIteratorCache<String, String> mapCache) { 41 this.mapCache = mapCache; 42 } 43 44 @Parameters parameters()45 public static Collection<Object[]> parameters() { 46 Comparator<String> nullsLast = Ordering.natural().nullsLast(); 47 48 return Arrays.asList( 49 new Object[][] { 50 {new MapIteratorCache<String, String>(new HashMap<String, String>())}, 51 {new MapIteratorCache<String, String>(new TreeMap<String, String>(nullsLast))}, 52 {new MapRetrievalCache<String, String>(new HashMap<String, String>())}, 53 {new MapRetrievalCache<String, String>(new TreeMap<String, String>(nullsLast))} 54 }); 55 } 56 57 @Before init()58 public void init() { 59 mapCache.clear(); 60 } 61 62 @Test testKeySetIterator()63 public void testKeySetIterator() { 64 mapCache.put("A", "A_value"); 65 mapCache.put("B", "B_value"); 66 mapCache.put("C", "C_value"); 67 68 assertThat(mapCache.unmodifiableKeySet()).hasSize(3); 69 for (String key : mapCache.unmodifiableKeySet()) { 70 assertThat(mapCache.get(key)).isEqualTo(key + "_value"); 71 } 72 } 73 74 @Test testPutNewValue()75 public void testPutNewValue() { 76 assertThat(mapCache.put("key", "value")).isNull(); 77 assertThat(mapCache.get("key")).isEqualTo("value"); // ensure key/value is cached 78 assertThat(mapCache.put("key", "new value")).isEqualTo("value"); 79 assertThat(mapCache.get("key")).isEqualTo("new value"); 80 } 81 82 @Test testRemoveEqualKeyWithDifferentReference()83 public void testRemoveEqualKeyWithDifferentReference() { 84 String fooReference1 = new String("foo"); 85 String fooReference2 = new String("foo"); 86 assertThat(fooReference1).isNotSameInstanceAs(fooReference2); 87 88 assertThat(mapCache.put(fooReference1, "bar")).isNull(); 89 assertThat(mapCache.get(fooReference1)).isEqualTo("bar"); // ensure first reference is cached 90 assertThat(mapCache.remove(fooReference2)).isEqualTo("bar"); 91 assertThat(mapCache.get(fooReference1)).isNull(); 92 } 93 94 @Test testHandleNulls()95 public void testHandleNulls() { 96 mapCache.put("foo", "bar"); 97 mapCache.put("non-null key", null); 98 mapCache.put(null, "non-null value"); 99 100 assertThat(mapCache.get("foo")).isEqualTo("bar"); 101 assertThat(mapCache.get("non-null key")).isNull(); 102 assertThat(mapCache.get(null)).isEqualTo("non-null value"); 103 104 assertThat(mapCache.containsKey("foo")).isTrue(); 105 assertThat(mapCache.containsKey("bar")).isFalse(); 106 assertThat(mapCache.containsKey("non-null key")).isTrue(); 107 assertThat(mapCache.containsKey(null)).isTrue(); 108 109 // Test again - in reverse order. 110 assertThat(mapCache.get(null)).isEqualTo("non-null value"); 111 assertThat(mapCache.get("non-null key")).isNull(); 112 assertThat(mapCache.get("foo")).isEqualTo("bar"); 113 } 114 } 115