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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.util.collections;
  18. import java.util.Arrays;
  19. import java.util.LinkedList;
  20. import java.util.List;
  21. import org.junit.Before;
  22. import org.junit.Test;
  23. import static org.junit.Assert.assertEquals;
  24. import static org.junit.Assert.assertFalse;
  25. import static org.junit.Assert.assertTrue;
  26. import static org.mockito.ArgumentMatchers.any;
  27. import static org.mockito.Mockito.anyInt;
  28. import static org.mockito.Mockito.mock;
  29. import static org.mockito.Mockito.never;
  30. import static org.mockito.Mockito.verify;
  31. public class ObservableListDecoratorTest {
  32. private List<String> list;
  33. private ObservableList<String> obslist;
  34. private ListObserver observer;
  35. @Before
  36. public void setup() {
  37. list = new LinkedList<>();
  38. obslist = new ObservableListDecorator<>(list);
  39. observer = mock(ListObserver.class);
  40. obslist.addListListener(observer);
  41. }
  42. @Test
  43. public void testAddingSingleEntryFiresListener() {
  44. obslist.add("test");
  45. verify(observer).onItemsAdded(obslist, 0, 0);
  46. obslist.add("test");
  47. verify(observer).onItemsAdded(obslist, 1, 1);
  48. }
  49. @Test
  50. public void testAddingSingleEntryAtIndexFiresListener() {
  51. list.addAll(Arrays.asList("one", "two", "three", "four"));
  52. obslist.add(1, "one point five");
  53. verify(observer).onItemsAdded(obslist, 1, 1);
  54. }
  55. @Test
  56. public void testAddingRangeFiresListener() {
  57. obslist.addAll(Arrays.asList("one", "two", "three", "four"));
  58. verify(observer).onItemsAdded(obslist, 0, 3);
  59. obslist.addAll(Arrays.asList("one", "two", "three", "four"));
  60. verify(observer).onItemsAdded(obslist, 4, 7);
  61. }
  62. @Test
  63. public void testAddingRangeAtIndexFiresListener() {
  64. list.addAll(Arrays.asList("one", "two", "three", "four"));
  65. obslist.addAll(1, Arrays.asList("one", "two", "three", "four"));
  66. verify(observer).onItemsAdded(obslist, 1, 5);
  67. }
  68. @Test
  69. public void testClearingFiresListener() {
  70. list.addAll(Arrays.asList("one", "two", "three", "four"));
  71. obslist.clear();
  72. verify(observer).onItemsRemoved(obslist, 0, 3);
  73. }
  74. @Test
  75. public void testClearingEmptyListDoesntFireListener() {
  76. obslist.clear();
  77. verify(observer, never()).onItemsRemoved(any(), anyInt(), anyInt());
  78. }
  79. @Test
  80. public void testRemovingByIndexFiresListener() {
  81. list.addAll(Arrays.asList("one", "two", "three", "four"));
  82. obslist.remove(2);
  83. verify(observer).onItemsRemoved(obslist, 2, 2);
  84. }
  85. @Test
  86. public void testRemovingByObjectFiresListener() {
  87. list.addAll(Arrays.asList("one", "two", "three", "four"));
  88. obslist.remove("three");
  89. verify(observer).onItemsRemoved(obslist, 2, 2);
  90. }
  91. @Test
  92. public void testRemovingListenerStopsFutureCalls() {
  93. obslist.removeListListener(observer);
  94. obslist.add("test");
  95. verify(observer, never()).onItemsAdded(obslist, 0, 0);
  96. }
  97. @Test
  98. public void testSize() {
  99. assertEquals(0, obslist.size());
  100. obslist.add("test");
  101. assertEquals(1, obslist.size());
  102. obslist.add("test");
  103. assertEquals(2, obslist.size());
  104. }
  105. @Test
  106. public void testIsEmpty() {
  107. assertTrue(obslist.isEmpty());
  108. obslist.add("test");
  109. assertFalse(obslist.isEmpty());
  110. obslist.remove("test");
  111. assertTrue(obslist.isEmpty());
  112. }
  113. @Test
  114. public void testContains() {
  115. assertFalse(obslist.contains("test"));
  116. obslist.add("test");
  117. assertTrue(obslist.contains("test"));
  118. obslist.remove("test");
  119. assertFalse(obslist.contains("test"));
  120. }
  121. }