Guest User

Untitled

a guest
Oct 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. import org.junit.Before;
  2. import org.junit.Test;
  3.  
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. import static org.hamcrest.MatcherAssert.assertThat;
  8. import static org.hamcrest.Matchers.is;
  9. import static org.hamcrest.Matchers.nullValue;
  10.  
  11. public class HashMapTest {
  12.  
  13. private Map<String, String> map1;
  14.  
  15. @Before
  16. public void setUp() throws Exception {
  17. map1 = new HashMap<>();
  18. }
  19.  
  20. @Test
  21. public void empty_after_init() {
  22. assertThat(map1.isEmpty(), is(true));
  23. }
  24.  
  25. @Test
  26. public void compute() {
  27. map1.put("foo1", "bar1");
  28. map1.put("foo2", "bar2");
  29.  
  30. map1.compute("foo1", (k, v) -> (v == null) ? "null value" : "not null value");
  31. assertThat(map1.get("foo1"), is("not null value"));
  32. }
  33.  
  34. @Test
  35. public void computeIfAbsent() {
  36. map1.put("foo1", "bar1");
  37.  
  38. map1.computeIfAbsent("foo2", k -> "null value");
  39. assertThat(map1.get("foo2"), is("null value"));
  40. assertThat(map1.size(), is(2));
  41. }
  42.  
  43. @Test
  44. public void computeIfPresent() {
  45. map1.put("foo1", "bar1");
  46.  
  47. map1.computeIfPresent("foo1", (k, v) -> "was " + v);
  48. assertThat(map1.get("foo1"), is("was bar1"));
  49. assertThat(map1.size(), is(1));
  50.  
  51. map1.computeIfPresent("foo2", (k, v) -> "was " + v);
  52. assertThat(map1.size(), is(1));
  53. }
  54.  
  55. @Test
  56. public void put_overwrite() {
  57. map1.put("foo1", "bar1");
  58. map1.put("foo1", "bar2");
  59.  
  60. assertThat(map1.size(), is(1));
  61. assertThat(map1.get("foo1"), is("bar2"));
  62. }
  63.  
  64. @Test
  65. public void putIfAbsent() {
  66. map1.put("foo1", "bar1");
  67.  
  68. map1.putIfAbsent("foo1", "insert because of absence");
  69. map1.putIfAbsent("foo2", "insert becase of absence");
  70.  
  71. assertThat(map1.size(), is(2));
  72. assertThat(map1.get("foo1"), is("bar1"));
  73. assertThat(map1.get("foo2"), is("insert becase of absence"));
  74. }
  75.  
  76. @Test
  77. public void merge() {
  78. map1.put("foo1", "bar1");
  79.  
  80. map1.merge("foo1", ": message", String::concat);
  81. map1.merge("foo2", ": message", String::concat);
  82.  
  83. assertThat(map1.size(), is(2));
  84. assertThat(map1.get("foo1"), is("bar1: message"));
  85. assertThat(map1.get("foo2"), is(": message"));
  86. }
  87.  
  88. @Test
  89. public void remove() {
  90. map1.put("foo1", "bar1");
  91.  
  92. map1.remove("foo1", "bar2");
  93. assertThat(map1.size(), is(1));
  94. assertThat(map1.get("foo1"), is("bar1"));
  95.  
  96. map1.remove("foo1");
  97. assertThat(map1.size(), is(0));
  98. assertThat(map1.get("foo1"), is(nullValue()));
  99. }
  100.  
  101. @Test
  102. public void replace() {
  103. map1.replace("foo1", "bar1");
  104. assertThat(map1.size(), is(0));
  105.  
  106. map1.put("key1", "bar1");
  107. map1.replace("key1", "value1");
  108. assertThat(map1.get("key1"), is("value1"));
  109.  
  110. map1.replace("key1", "bar1", "newvalue");
  111. assertThat(map1.get("key1"), is("value1"));
  112.  
  113. map1.replace("key1", "value1", "newvalue");
  114. assertThat(map1.get("key1"), is("newvalue"));
  115. }
  116. }
Add Comment
Please, Sign In to add comment