|
| 1 | +package com.baeldung.java.concurrentmap; |
| 2 | + |
| 3 | +import org.junit.Before; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.concurrent.ConcurrentHashMap; |
| 9 | +import java.util.concurrent.ConcurrentMap; |
| 10 | + |
| 11 | +import static org.junit.Assert.assertNull; |
| 12 | + |
| 13 | +public class ConcurrentMapNullKeyValueTest { |
| 14 | + |
| 15 | + ConcurrentMap<String, Object> concurrentMap; |
| 16 | + |
| 17 | + @Before |
| 18 | + public void setup() { |
| 19 | + concurrentMap = new ConcurrentHashMap<>(); |
| 20 | + } |
| 21 | + |
| 22 | + @Test(expected = NullPointerException.class) |
| 23 | + public void givenConcurrentHashMap_whenGetWithNullKey_thenThrowsNPE() { |
| 24 | + concurrentMap.get(null); |
| 25 | + } |
| 26 | + |
| 27 | + @Test(expected = NullPointerException.class) |
| 28 | + public void givenConcurrentHashMap_whenGetOrDefaultWithNullKey_thenThrowsNPE() { |
| 29 | + concurrentMap.getOrDefault(null, new Object()); |
| 30 | + } |
| 31 | + |
| 32 | + @Test(expected = NullPointerException.class) |
| 33 | + public void givenConcurrentHashMap_whenPutWithNullKey_thenThrowsNPE() { |
| 34 | + concurrentMap.put(null, new Object()); |
| 35 | + } |
| 36 | + |
| 37 | + @Test(expected = NullPointerException.class) |
| 38 | + public void givenConcurrentHashMap_whenPutNullValue_thenThrowsNPE() { |
| 39 | + concurrentMap.put("test", null); |
| 40 | + } |
| 41 | + |
| 42 | + @Test(expected = NullPointerException.class) |
| 43 | + public void givenConcurrentHashMapAndKeyAbsent_whenPutWithNullKey_thenThrowsNPE() { |
| 44 | + concurrentMap.putIfAbsent(null, new Object()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test(expected = NullPointerException.class) |
| 48 | + public void givenConcurrentHashMapAndMapWithNullKey_whenPutNullKeyMap_thenThrowsNPE() { |
| 49 | + Map<String, Object> nullKeyMap = new HashMap<>(); |
| 50 | + nullKeyMap.put(null, new Object()); |
| 51 | + concurrentMap.putAll(nullKeyMap); |
| 52 | + } |
| 53 | + |
| 54 | + @Test(expected = NullPointerException.class) |
| 55 | + public void givenConcurrentHashMapAndMapWithNullValue_whenPutNullValueMap_thenThrowsNPE() { |
| 56 | + Map<String, Object> nullValueMap = new HashMap<>(); |
| 57 | + nullValueMap.put("test", null); |
| 58 | + concurrentMap.putAll(nullValueMap); |
| 59 | + } |
| 60 | + |
| 61 | + @Test(expected = NullPointerException.class) |
| 62 | + public void givenConcurrentHashMap_whenReplaceNullKeyWithValues_thenThrowsNPE() { |
| 63 | + concurrentMap.replace(null, new Object(), new Object()); |
| 64 | + } |
| 65 | + |
| 66 | + @Test(expected = NullPointerException.class) |
| 67 | + public void givenConcurrentHashMap_whenReplaceWithNullNewValue_thenThrowsNPE() { |
| 68 | + Object o = new Object(); |
| 69 | + concurrentMap.replace("test", o, null); |
| 70 | + } |
| 71 | + |
| 72 | + @Test(expected = NullPointerException.class) |
| 73 | + public void givenConcurrentHashMap_whenReplaceOldNullValue_thenThrowsNPE() { |
| 74 | + Object o = new Object(); |
| 75 | + concurrentMap.replace("test", null, o); |
| 76 | + } |
| 77 | + |
| 78 | + @Test(expected = NullPointerException.class) |
| 79 | + public void givenConcurrentHashMap_whenReplaceWithNullValue_thenThrowsNPE() { |
| 80 | + concurrentMap.replace("test", null); |
| 81 | + } |
| 82 | + |
| 83 | + @Test(expected = NullPointerException.class) |
| 84 | + public void givenConcurrentHashMap_whenReplaceNullKey_thenThrowsNPE() { |
| 85 | + concurrentMap.replace(null, "test"); |
| 86 | + } |
| 87 | + |
| 88 | + @Test(expected = NullPointerException.class) |
| 89 | + public void givenConcurrentHashMap_whenReplaceAllMappingNull_thenThrowsNPE() { |
| 90 | + concurrentMap.put("test", new Object()); |
| 91 | + concurrentMap.replaceAll((s, o) -> null); |
| 92 | + } |
| 93 | + |
| 94 | + @Test(expected = NullPointerException.class) |
| 95 | + public void givenConcurrentHashMap_whenRemoveNullKey_thenThrowsNPE() { |
| 96 | + concurrentMap.remove(null); |
| 97 | + } |
| 98 | + |
| 99 | + @Test(expected = NullPointerException.class) |
| 100 | + public void givenConcurrentHashMap_whenRemoveNullKeyWithValue_thenThrowsNPE() { |
| 101 | + concurrentMap.remove(null, new Object()); |
| 102 | + } |
| 103 | + |
| 104 | + @Test(expected = NullPointerException.class) |
| 105 | + public void givenConcurrentHashMap_whenMergeNullKeyWithValue_thenThrowsNPE() { |
| 106 | + concurrentMap.merge(null, new Object(), (o, o2) -> o2); |
| 107 | + } |
| 108 | + |
| 109 | + @Test(expected = NullPointerException.class) |
| 110 | + public void givenConcurrentHashMap_whenMergeKeyWithNullValue_thenThrowsNPE() { |
| 111 | + concurrentMap.put("test", new Object()); |
| 112 | + concurrentMap.merge("test", null, (o, o2) -> o2); |
| 113 | + } |
| 114 | + |
| 115 | + @Test(expected = NullPointerException.class) |
| 116 | + public void givenConcurrentHashMapAndAssumeKeyAbsent_whenComputeWithNullKey_thenThrowsNPE() { |
| 117 | + concurrentMap.computeIfAbsent(null, s -> s); |
| 118 | + } |
| 119 | + |
| 120 | + @Test(expected = NullPointerException.class) |
| 121 | + public void givenConcurrentHashMapAndAssumeKeyPresent_whenComputeWithNullKey_thenThrowsNPE() { |
| 122 | + concurrentMap.computeIfPresent(null, (s, o) -> o); |
| 123 | + } |
| 124 | + |
| 125 | + @Test(expected = NullPointerException.class) |
| 126 | + public void givenConcurrentHashMap_whenComputeWithNullKey_thenThrowsNPE() { |
| 127 | + concurrentMap.compute(null, (s, o) -> o); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void givenConcurrentHashMap_whenMergeKeyRemappingNull_thenRemovesMapping() { |
| 132 | + Object oldValue = new Object(); |
| 133 | + concurrentMap.put("test", oldValue); |
| 134 | + concurrentMap.merge("test", new Object(), (o, o2) -> null); |
| 135 | + assertNull(concurrentMap.get("test")); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void givenConcurrentHashMapAndKeyAbsent_whenComputeWithKeyRemappingNull_thenRemainsAbsent() { |
| 140 | + concurrentMap.computeIfPresent("test", (s, o) -> null); |
| 141 | + assertNull(concurrentMap.get("test")); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void givenKeyPresent_whenComputeIfPresentRemappingNull_thenMappingRemoved() { |
| 146 | + Object oldValue = new Object(); |
| 147 | + concurrentMap.put("test", oldValue); |
| 148 | + concurrentMap.computeIfPresent("test", (s, o) -> null); |
| 149 | + assertNull(concurrentMap.get("test")); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void givenKeyPresent_whenComputeRemappingNull_thenMappingRemoved() { |
| 154 | + Object oldValue = new Object(); |
| 155 | + concurrentMap.put("test", oldValue); |
| 156 | + concurrentMap.compute("test", (s, o) -> null); |
| 157 | + assertNull(concurrentMap.get("test")); |
| 158 | + } |
| 159 | + |
| 160 | +} |
0 commit comments