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.

ListenerListTest.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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
  20. * THE SOFTWARE.
  21. */
  22. package com.dmdirc.util.collections;
  23. import org.junit.Test;
  24. import static org.junit.Assert.*;
  25. import static org.mockito.Mockito.*;
  26. public class ListenerListTest {
  27. @Test
  28. public void testGenericAddListener() {
  29. final Object listener = new Object();
  30. final Object listener2 = new Object();
  31. final ListenerList instance = new ListenerList();
  32. instance.add(Object.class, listener);
  33. instance.add(Object.class, listener2);
  34. assertTrue(instance.get(Object.class).contains(listener));
  35. assertTrue(instance.get(Object.class).contains(listener));
  36. }
  37. @Test(expected=IllegalArgumentException.class)
  38. public void testGenericAddNull() {
  39. final ListenerList instance = new ListenerList();
  40. instance.add(Object.class, null);
  41. assertFalse(instance.get(Object.class).contains(null));
  42. }
  43. @Test
  44. public void testStringAddListener() {
  45. final Object listener = new Object();
  46. final Object listener2 = new Object();
  47. final ListenerList instance = new ListenerList();
  48. instance.add("Object", listener);
  49. instance.add("Object", listener2);
  50. assertTrue(instance.get("Object").contains(listener));
  51. assertTrue(instance.get("Object").contains(listener2));
  52. }
  53. @Test(expected=IllegalArgumentException.class)
  54. public void testStringAddNull() {
  55. final ListenerList instance = new ListenerList();
  56. instance.add("Object", null);
  57. assertFalse(instance.get(Object.class).contains(null));
  58. }
  59. @Test
  60. public void testGenericRemoveListener() {
  61. final Object listener = new Object();
  62. final ListenerList instance = new ListenerList();
  63. instance.add(Object.class, listener);
  64. assertTrue(instance.get(Object.class).contains(listener));
  65. instance.remove(Object.class, listener);
  66. assertFalse(instance.get(Object.class).contains(listener));
  67. }
  68. @Test
  69. public void testStringRemoveListener() {
  70. final Object listener = new Object();
  71. final ListenerList instance = new ListenerList();
  72. instance.add("Object", listener);
  73. assertTrue(instance.get("Object").contains(listener));
  74. instance.remove("Object", listener);
  75. assertFalse(instance.get("Object").contains(listener));
  76. }
  77. @Test
  78. public void testStringRemoveUnknownListener() {
  79. final Object listener = new Object();
  80. final ListenerList instance = new ListenerList();
  81. instance.add("Object", listener);
  82. assertTrue(instance.get("Object").contains(listener));
  83. instance.remove("String", listener);
  84. assertTrue(instance.get("Object").contains(listener));
  85. }
  86. @Test
  87. public void testGenericGetListeners() {
  88. final Object listener = new Object();
  89. final ListenerList instance = new ListenerList();
  90. assertTrue(instance.get(Object.class).isEmpty());
  91. instance.add(Object.class, listener);
  92. assertTrue(instance.get(Object.class).contains(listener));
  93. assertTrue(instance.get(String.class).isEmpty());
  94. final String listener2 = "";
  95. instance.add(String.class, listener2);
  96. assertTrue(instance.get(String.class).contains(listener2));
  97. }
  98. @Test
  99. public void testStringGetListeners() {
  100. final Object listener = new Object();
  101. final ListenerList instance = new ListenerList();
  102. assertTrue(instance.get("Object").isEmpty());
  103. instance.add("Object", listener);
  104. assertTrue(instance.get("Object").contains(listener));
  105. assertTrue(instance.get("String").isEmpty());
  106. final String listener2 = "";
  107. instance.add("String", listener2);
  108. assertTrue(instance.get("String").contains(listener2));
  109. }
  110. @Test
  111. public void testGetCallableNoArgs() {
  112. final TestCallable one = mock(TestCallable.class);
  113. final TestCallable two = mock(TestCallable.class);
  114. final ListenerList instance = new ListenerList();
  115. instance.add(TestCallable.class, one);
  116. instance.add(TestCallable.class, two);
  117. final TestCallable test = instance.getCallable(TestCallable.class);
  118. test.testMethod();
  119. verify(one).testMethod();
  120. verify(two).testMethod();
  121. }
  122. @Test
  123. public void testGetCallableArg() {
  124. final TestCallable one = mock(TestCallable.class);
  125. final ListenerList instance = new ListenerList();
  126. instance.add(TestCallable.class, one);
  127. final TestCallable test = instance.getCallable(TestCallable.class);
  128. test.testMethod("test");
  129. verify(one).testMethod("test");
  130. }
  131. @Test
  132. public void testGetCallableArgs() {
  133. final TestCallable one = mock(TestCallable.class);
  134. final ListenerList instance = new ListenerList();
  135. instance.add(TestCallable.class, one);
  136. final TestCallable test = instance.getCallable(TestCallable.class);
  137. test.testMethod("test", "test1");
  138. verify(one).testMethod("test", "test1");
  139. }
  140. @Test(expected=IndexOutOfBoundsException.class)
  141. public void testGetCallableThrowsException() {
  142. final TestCallable one = mock(TestCallable.class);
  143. when(one.testMethod()).thenThrow(new IndexOutOfBoundsException());
  144. final ListenerList instance = new ListenerList();
  145. instance.add(TestCallable.class, one);
  146. final TestCallable test = instance.getCallable(TestCallable.class);
  147. test.testMethod();
  148. verify(one).testMethod();
  149. }
  150. private interface TestCallable {
  151. String testMethod();
  152. String testMethod(final String test);
  153. String testMethod(final String test, final String test2);
  154. }
  155. }