You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MapListTest.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.util.collections;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.List;
  26. import org.junit.Test;
  27. import static org.junit.Assert.*;
  28. public class MapListTest {
  29. @Test
  30. public void testIsEmpty() {
  31. final MapList<String, String> test = new MapList<>();
  32. assertTrue(test.isEmpty());
  33. test.add("a", "b");
  34. assertFalse(test.isEmpty());
  35. test.removeFromAll("b");
  36. assertTrue(test.isEmpty());
  37. }
  38. @Test
  39. public void testAddCollection() {
  40. final MapList<String, String> test = new MapList<>();
  41. final Collection<String> testList = new ArrayList<>();
  42. testList.add("d");
  43. testList.add("e");
  44. test.add("key", testList);
  45. assertTrue(test.containsKey("key"));
  46. assertTrue(test.containsValue("key", "d"));
  47. assertTrue(test.containsValue("key", "e"));
  48. }
  49. @Test
  50. public void testClear() {
  51. final MapList<String, String> test = new MapList<>();
  52. test.add("a", "b");
  53. test.add("d", "e");
  54. test.clear();
  55. assertTrue(test.isEmpty());
  56. }
  57. @Test
  58. public void testClearKey() {
  59. final MapList<String, String> test = new MapList<>();
  60. test.add("a", "b");
  61. test.add("d", "e");
  62. test.clear("a");
  63. assertTrue(test.values("a").isEmpty());
  64. assertFalse(test.isEmpty());
  65. }
  66. @Test
  67. public void testRemove() {
  68. final MapList<String, String> test = new MapList<>();
  69. test.add("a", "b");
  70. test.add("d", "e");
  71. test.remove("z", "b");
  72. assertEquals(2, test.keySet().size());
  73. assertEquals(1, test.values("a").size());
  74. assertEquals(1, test.values("d").size());
  75. test.remove("a", "b");
  76. assertEquals(2, test.keySet().size());
  77. assertEquals(0, test.values("a").size());
  78. assertEquals(1, test.values("d").size());
  79. }
  80. @Test
  81. public void testRemoveKey() {
  82. final MapList<String, String> instance = new MapList<>();
  83. instance.add("a", "b");
  84. assertTrue(instance.containsKey("a"));
  85. instance.remove("a");
  86. assertFalse(instance.containsKey("a"));
  87. }
  88. @Test
  89. public void testKeySet() {
  90. final MapList<String, String> test = new MapList<>();
  91. test.add("a", "b");
  92. test.add("d", "e");
  93. assertEquals(2, test.keySet().size());
  94. assertTrue(test.keySet().contains("a"));
  95. assertTrue(test.keySet().contains("d"));
  96. }
  97. @Test
  98. public void testContainsKey() {
  99. final MapList<String, String> test = new MapList<>();
  100. test.add("a", "b");
  101. assertTrue(test.containsKey("a"));
  102. }
  103. @Test
  104. public void testContainsValue() {
  105. final MapList<String, String> test = new MapList<>();
  106. test.add("a", "b");
  107. assertTrue(test.containsValue("a", "b"));
  108. }
  109. @Test
  110. public void testGet() {
  111. final MapList<String, String> test = new MapList<>();
  112. test.add("a", "b");
  113. assertEquals(1, test.get("a").size());
  114. assertEquals("b", test.get("a").get(0));
  115. assertEquals("b", test.get("a", 0));
  116. }
  117. @Test
  118. public void testInherit() {
  119. final MapList<String, String> test1 = new MapList<>();
  120. test1.add("a", "b");
  121. final MapList<String, String> test2 = new MapList<>(test1);
  122. assertEquals(1, test2.get("a").size());
  123. assertEquals("b", test2.get("a").get(0));
  124. assertEquals("b", test2.get("a", 0));
  125. }
  126. @Test
  127. public void testGetMap() {
  128. final MapList<String, String> test1 = new MapList<>();
  129. test1.add("a", "b");
  130. assertNotSame(test1.getMap(), test1.getMap());
  131. final MapList<String, String> test2 = new MapList<>(test1);
  132. assertEquals(test1.getMap(), test2.getMap());
  133. }
  134. }