|
5 | 5 |
|
6 | 6 | import static org.hamcrest.CoreMatchers.is;
|
7 | 7 | import static org.hamcrest.MatcherAssert.assertThat;
|
| 8 | +import static org.junit.Assert.assertNull; |
8 | 9 | import static org.junit.Assert.assertTrue;
|
9 | 10 |
|
10 | 11 | /**
|
@@ -79,4 +80,80 @@ public void replaceAll() {
|
79 | 80 | assertThat(map.get(2), is("2-updated"));
|
80 | 81 | assertThat(map.get(3), is("3-updated"));
|
81 | 82 | }
|
| 83 | + |
| 84 | + @Test |
| 85 | + public void replace_key_value_newValue_found() { |
| 86 | + Map<Integer, String> map = new HashMap<>(); |
| 87 | + |
| 88 | + map.put(1, "old"); |
| 89 | + |
| 90 | + map.replace(1, "old", "replaced"); |
| 91 | + |
| 92 | + assertThat(map.get(1), is("replaced")); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void replace_key_value_newValue_notFound() { |
| 97 | + Map<Integer, String> map = new HashMap<>(); |
| 98 | + |
| 99 | + map.put(1, "1"); |
| 100 | + |
| 101 | + map.replace(1, "2", "replaced"); |
| 102 | + |
| 103 | + assertThat(map.get(1), is("1")); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void replace_key_value_newValue_oldNull() { |
| 108 | + Map<Integer, String> map = new HashMap<>(); |
| 109 | + |
| 110 | + map.put(1, null); |
| 111 | + |
| 112 | + map.replace(1, null, "replaced"); |
| 113 | + |
| 114 | + assertThat(map.get(1), is("replaced")); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void replace_key_value_newValue_newNull() { |
| 119 | + Map<Integer, String> map = new HashMap<>(); |
| 120 | + |
| 121 | + map.put(1, "1"); |
| 122 | + |
| 123 | + map.replace(1, "1", null); |
| 124 | + |
| 125 | + assertThat(map.size(), is(1)); |
| 126 | + assertNull(map.get(1)); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void replace_key_value_found() { |
| 131 | + Map<Integer, String> map = new HashMap<>(); |
| 132 | + |
| 133 | + map.put(1, "1"); |
| 134 | + |
| 135 | + map.replace(1, "replaced"); |
| 136 | + |
| 137 | + assertThat(map.get(1), is("replaced")); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void replace_key_value_notFound() { |
| 142 | + Map<Integer, String> map = new HashMap<>(); |
| 143 | + |
| 144 | + map.replace(1, "replaced"); |
| 145 | + |
| 146 | + assertTrue(map.isEmpty()); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void replace_key_value_mappedToNull() { |
| 151 | + Map<Integer, String> map = new HashMap<>(); |
| 152 | + |
| 153 | + map.put(1, null); |
| 154 | + |
| 155 | + map.replace(1, "replaced"); |
| 156 | + |
| 157 | + assertThat(map.get(1), is("replaced")); |
| 158 | + } |
82 | 159 | }
|
0 commit comments