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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import org.junit.Test;
  26. import static org.junit.Assert.*;
  27. public class MapListTest {
  28. @Test
  29. public void testIsEmpty() {
  30. final MapList<String, String> test = new MapList<String, String>();
  31. assertTrue(test.isEmpty());
  32. test.add("a", "b");
  33. assertFalse(test.isEmpty());
  34. test.removeFromAll("b");
  35. assertTrue(test.isEmpty());
  36. }
  37. @Test
  38. public void testAddCollection() {
  39. final MapList<String, String> test = new MapList<String, String>();
  40. final List<String> testList = new ArrayList<String>();
  41. testList.add("d");
  42. testList.add("e");
  43. test.add("key", testList);
  44. assertTrue(test.containsKey("key"));
  45. assertTrue(test.containsValue("key", "d"));
  46. assertTrue(test.containsValue("key", "e"));
  47. }
  48. @Test
  49. public void testClear() {
  50. final MapList<String, String> test = new MapList<String, String>();
  51. test.add("a", "b");
  52. test.add("d", "e");
  53. test.clear();
  54. assertTrue(test.isEmpty());
  55. }
  56. @Test
  57. public void testClearKey() {
  58. final MapList<String, String> test = new MapList<String, String>();
  59. test.add("a", "b");
  60. test.add("d", "e");
  61. test.clear("a");
  62. assertTrue(test.values("a").isEmpty());
  63. assertFalse(test.isEmpty());
  64. }
  65. @Test
  66. public void testRemove() {
  67. final MapList<String, String> test = new MapList<String, String>();
  68. test.add("a", "b");
  69. test.add("d", "e");
  70. test.remove("z", "b");
  71. assertEquals(2, test.keySet().size());
  72. assertEquals(1, test.values("a").size());
  73. assertEquals(1, test.values("d").size());
  74. test.remove("a", "b");
  75. assertEquals(2, test.keySet().size());
  76. assertEquals(0, test.values("a").size());
  77. assertEquals(1, test.values("d").size());
  78. }
  79. @Test
  80. public void testKeySet() {
  81. final MapList<String, String> test = new MapList<String, String>();
  82. test.add("a", "b");
  83. test.add("d", "e");
  84. assertEquals(2, test.keySet().size());
  85. assertTrue(test.keySet().contains("a"));
  86. assertTrue(test.keySet().contains("d"));
  87. }
  88. @Test
  89. public void testContainsKey() {
  90. final MapList<String, String> test = new MapList<String, String>();
  91. test.add("a", "b");
  92. assertTrue(test.containsKey("a"));
  93. }
  94. @Test
  95. public void testContainsValue() {
  96. final MapList<String, String> test = new MapList<String, String>();
  97. test.add("a", "b");
  98. assertTrue(test.containsValue("a", "b"));
  99. }
  100. @Test
  101. public void testGet() {
  102. final MapList<String, String> test = new MapList<String, String>();
  103. test.add("a", "b");
  104. assertEquals(1, test.get("a").size());
  105. assertEquals("b", test.get("a").get(0));
  106. assertEquals("b", test.get("a", 0));
  107. }
  108. @Test
  109. public void testInherit() {
  110. final MapList<String, String> test1 = new MapList<String, String>();
  111. test1.add("a", "b");
  112. final MapList<String, String> test2 = new MapList<String, String>(test1);
  113. assertEquals(1, test2.get("a").size());
  114. assertEquals("b", test2.get("a").get(0));
  115. assertEquals("b", test2.get("a", 0));
  116. }
  117. @Test
  118. public void testGetMap() {
  119. final MapList<String, String> test1 = new MapList<String, String>();
  120. test1.add("a", "b");
  121. assertNotSame(test1.getMap(), test1.getMap());
  122. final MapList<String, String> test2 = new MapList<String, String>(test1);
  123. assertEquals(test1.getMap(), test2.getMap());
  124. }
  125. }