Add more unit tests for Util.java
* Add new addAllToMap and factor out addKeyToMap.
Change-Id: I0654ff50b53cfc7539794a750f1d082cf63a9e68
diff --git a/src/main/java/com/googlesource/gerrit/plugins/findowners/Util.java b/src/main/java/com/googlesource/gerrit/plugins/findowners/Util.java
index 77eff7f..f75a5b5 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/findowners/Util.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/findowners/Util.java
@@ -94,10 +94,19 @@
return result;
}
- static void addToMap(Map<String, Set<String>> map, String key, String value) {
+ static void addKeyToMap(Map<String, Set<String>> map, String key) {
if (map.get(key) == null) {
map.put(key, new HashSet<>());
}
+ }
+
+ static void addToMap(Map<String, Set<String>> map, String key, String value) {
+ addKeyToMap(map, key);
map.get(key).add(value);
}
+
+ static void addAllToMap(Map<String, Set<String>> map, String key, Set<String> values) {
+ addKeyToMap(map, key);
+ map.get(key).addAll(values);
+ }
}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/findowners/UtilTest.java b/src/test/java/com/googlesource/gerrit/plugins/findowners/UtilTest.java
index 79f4187..1ddd4ec 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/findowners/UtilTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/findowners/UtilTest.java
@@ -175,6 +175,58 @@
}
@Test
+ public void addKeyToMapTest() {
+ Map<String, Set<String>> m = new HashMap<>();
+ String key1 = "key1";
+ String key2 = "key2";
+ assertThat(m).isEmpty();
+ Util.addKeyToMap(m, key1);
+ assertThat(m).hasSize(1);
+ assertThat(m.get(key1)).isEmpty();
+ Util.addKeyToMap(m, key1);
+ assertThat(m).hasSize(1);
+ assertThat(m.get(key1)).isEmpty();
+ Util.addKeyToMap(m, key2);
+ assertThat(m).hasSize(2);
+ assertThat(m.get(key1)).isEmpty();
+ assertThat(m.get(key2)).isEmpty();
+ }
+
+ @Test
+ public void addToMapTest() {
+ Map<String, Set<String>> m = new HashMap<>();
+ String key1 = "key1";
+ String key2 = "key2";
+ Util.addToMap(m, key1, "v1");
+ Util.addToMap(m, key2, "v2");
+ Util.addToMap(m, key2, "v3");
+ assertThat(m.get(key1)).containsExactly("v1");
+ assertThat(m.get(key2)).containsExactly("v2", "v3");
+ }
+
+ @Test
+ public void addAllToMapTest() {
+ Map<String, Set<String>> m = new HashMap<>();
+ Set<String> s = new HashSet<>();
+ String key = "key";
+ Util.addAllToMap(m, key, s);
+ assertThat(m.get(key)).isEmpty();
+ s.add("v1");
+ Util.addAllToMap(m, key, s);
+ assertThat(m.get(key)).containsExactly("v1");
+ Util.addAllToMap(m, key, s);
+ assertThat(m.get(key)).containsExactly("v1");
+ s.add("v2");
+ Util.addAllToMap(m, key, s);
+ assertThat(m.get(key)).containsExactly("v2", "v1");
+ Util.addAllToMap(m, key, s);
+ assertThat(m.get(key)).containsExactly("v2", "v1");
+ s.add("v3");
+ Util.addAllToMap(m, key, s);
+ assertThat(m.get(key)).containsExactly("v2", "v1", "v3");
+ }
+
+ @Test
public void makeSortedMapTest() {
// TODO: test Util.makeSortedMap
}