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.

ObservableListDecoratorTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2006-2015 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.Arrays;
  24. import java.util.LinkedList;
  25. import java.util.List;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. import static org.junit.Assert.assertEquals;
  29. import static org.junit.Assert.assertFalse;
  30. import static org.junit.Assert.assertTrue;
  31. import static org.mockito.Mockito.anyInt;
  32. import static org.mockito.Mockito.anyObject;
  33. import static org.mockito.Mockito.mock;
  34. import static org.mockito.Mockito.never;
  35. import static org.mockito.Mockito.verify;
  36. public class ObservableListDecoratorTest {
  37. private List<String> list;
  38. private ObservableList<String> obslist;
  39. private ListObserver observer;
  40. @Before
  41. public void setup() {
  42. list = new LinkedList<>();
  43. obslist = new ObservableListDecorator<>(list);
  44. observer = mock(ListObserver.class);
  45. obslist.addListListener(observer);
  46. }
  47. @Test
  48. public void testAddingSingleEntryFiresListener() {
  49. obslist.add("test");
  50. verify(observer).onItemsAdded(obslist, 0, 0);
  51. obslist.add("test");
  52. verify(observer).onItemsAdded(obslist, 1, 1);
  53. }
  54. @Test
  55. public void testAddingSingleEntryAtIndexFiresListener() {
  56. list.addAll(Arrays.asList("one", "two", "three", "four"));
  57. obslist.add(1, "one point five");
  58. verify(observer).onItemsAdded(obslist, 1, 1);
  59. }
  60. @Test
  61. public void testAddingRangeFiresListener() {
  62. obslist.addAll(Arrays.asList("one", "two", "three", "four"));
  63. verify(observer).onItemsAdded(obslist, 0, 3);
  64. obslist.addAll(Arrays.asList("one", "two", "three", "four"));
  65. verify(observer).onItemsAdded(obslist, 4, 7);
  66. }
  67. @Test
  68. public void testAddingRangeAtIndexFiresListener() {
  69. list.addAll(Arrays.asList("one", "two", "three", "four"));
  70. obslist.addAll(1, Arrays.asList("one", "two", "three", "four"));
  71. verify(observer).onItemsAdded(obslist, 1, 5);
  72. }
  73. @Test
  74. public void testClearingFiresListener() {
  75. list.addAll(Arrays.asList("one", "two", "three", "four"));
  76. obslist.clear();
  77. verify(observer).onItemsRemoved(obslist, 0, 3);
  78. }
  79. @Test
  80. public void testClearingEmptyListDoesntFireListener() {
  81. obslist.clear();
  82. verify(observer, never()).onItemsRemoved(anyObject(), anyInt(), anyInt());
  83. }
  84. @Test
  85. public void testRemovingByIndexFiresListener() {
  86. list.addAll(Arrays.asList("one", "two", "three", "four"));
  87. obslist.remove(2);
  88. verify(observer).onItemsRemoved(obslist, 2, 2);
  89. }
  90. @Test
  91. public void testRemovingByObjectFiresListener() {
  92. list.addAll(Arrays.asList("one", "two", "three", "four"));
  93. obslist.remove("three");
  94. verify(observer).onItemsRemoved(obslist, 2, 2);
  95. }
  96. @Test
  97. public void testRemovingListenerStopsFutureCalls() {
  98. obslist.removeListListener(observer);
  99. obslist.add("test");
  100. verify(observer, never()).onItemsAdded(obslist, 0, 0);
  101. }
  102. @Test
  103. public void testSize() {
  104. assertEquals(0, obslist.size());
  105. obslist.add("test");
  106. assertEquals(1, obslist.size());
  107. obslist.add("test");
  108. assertEquals(2, obslist.size());
  109. }
  110. @Test
  111. public void testIsEmpty() {
  112. assertTrue(obslist.isEmpty());
  113. obslist.add("test");
  114. assertFalse(obslist.isEmpty());
  115. obslist.remove("test");
  116. assertTrue(obslist.isEmpty());
  117. }
  118. @Test
  119. public void testContains() {
  120. assertFalse(obslist.contains("test"));
  121. obslist.add("test");
  122. assertTrue(obslist.contains("test"));
  123. obslist.remove("test");
  124. assertFalse(obslist.contains("test"));
  125. }
  126. }