1 package net.pterodactylus.sone.notify;
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.emptyIterable;
5 import static org.hamcrest.Matchers.is;
6 import static org.hamcrest.Matchers.not;
7 import static org.mockito.ArgumentMatchers.eq;
8 import static org.mockito.Mockito.mock;
9 import static org.mockito.Mockito.verify;
10 import static org.mockito.Mockito.when;
11 import static org.mockito.hamcrest.MockitoHamcrest.argThat;
13 import java.util.Arrays;
15 import net.pterodactylus.util.notify.NotificationListener;
16 import net.pterodactylus.util.template.Template;
17 import net.pterodactylus.util.template.TemplateContext;
19 import org.hamcrest.Matchers;
20 import org.junit.Test;
23 * Unit test for {@link ListNotification}.
25 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
27 public class ListNotificationTest {
29 private static final String ID = "notification-id";
30 private static final String KEY = "element-key";
31 private static final String OTHER_KEY = "other-key";
33 private final Template template = mock(Template.class);
34 private final TemplateContext templateInitialContext = mock(TemplateContext.class);
35 private ListNotification<Object> listNotification;
37 public ListNotificationTest() {
38 when(template.getInitialContext()).thenReturn(templateInitialContext);
39 listNotification = new ListNotification<Object>(ID, KEY, template);
43 public void creatingAListNotificationSetsEmptyIterableOnElementKeyInTemplateContext() {
44 verify(templateInitialContext).set(eq(KEY), argThat(emptyIterable()));
48 public void newListNotificationHasNoElement() {
49 assertThat(listNotification.getElements(), emptyIterable());
53 public void newListNotificationIsEmpty() {
54 assertThat(listNotification.isEmpty(), is(true));
58 public void listNotificationRetainsSetElements() {
59 listNotification.setElements(Arrays.asList("a", "b", "c"));
60 assertThat(listNotification.getElements(), Matchers.<Object>contains("a", "b", "c"));
64 public void listNotificationRetainsAddedElements() {
65 listNotification.add("a");
66 listNotification.add("b");
67 listNotification.add("c");
68 assertThat(listNotification.getElements(), Matchers.<Object>contains("a", "b", "c"));
72 public void listNotificationRemovesCorrectElement() {
73 listNotification.setElements(Arrays.asList("a", "b", "c"));
74 listNotification.remove("b");
75 assertThat(listNotification.getElements(), Matchers.<Object>contains("a", "c"));
79 public void removingTheLastElementDismissesTheNotification() {
80 NotificationListener notificationListener = mock(NotificationListener.class);
81 listNotification.addNotificationListener(notificationListener);
82 listNotification.add("a");
83 listNotification.remove("a");
84 verify(notificationListener).notificationDismissed(listNotification);
88 public void dismissingTheListNotificationRemovesAllElements() {
89 listNotification.setElements(Arrays.asList("a", "b", "c"));
90 listNotification.dismiss();
91 assertThat(listNotification.getElements(), emptyIterable());
95 public void listNotificationWithDifferentElementsIsNotEqual() {
96 ListNotification secondNotification = new ListNotification(ID, KEY, template);
97 listNotification.add("a");
98 secondNotification.add("b");
99 assertThat(listNotification, not(is(secondNotification)));
103 public void listNotificationWithDifferentKeyIsNotEqual() {
104 ListNotification secondNotification = new ListNotification(ID, OTHER_KEY, template);
105 assertThat(listNotification, not(is(secondNotification)));
109 public void copiedNotificationsHaveTheSameHashCode() {
110 ListNotification secondNotification = new ListNotification(listNotification);
111 listNotification.add("a");
112 secondNotification.add("a");
113 assertThat(listNotification.hashCode(), is(secondNotification.hashCode()));
117 public void listNotificationIsNotEqualToOtherObjects() {
118 assertThat(listNotification, not(is(new Object())));