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 public class ListNotificationTest {
27 private static final String ID = "notification-id";
28 private static final String KEY = "element-key";
29 private static final String OTHER_KEY = "other-key";
31 private final Template template = mock(Template.class);
32 private final TemplateContext templateInitialContext = mock(TemplateContext.class);
33 private ListNotification<Object> listNotification;
35 public ListNotificationTest() {
36 when(template.getInitialContext()).thenReturn(templateInitialContext);
37 listNotification = new ListNotification<>(ID, KEY, template);
41 public void creatingAListNotificationSetsEmptyIterableOnElementKeyInTemplateContext() {
42 verify(templateInitialContext).set(eq(KEY), argThat(emptyIterable()));
46 public void newListNotificationHasNoElement() {
47 assertThat(listNotification.getElements(), emptyIterable());
51 public void newListNotificationIsEmpty() {
52 assertThat(listNotification.isEmpty(), is(true));
56 public void listNotificationRetainsSetElements() {
57 listNotification.setElements(Arrays.asList("a", "b", "c"));
58 assertThat(listNotification.getElements(), Matchers.<Object>contains("a", "b", "c"));
62 public void listNotificationRetainsAddedElements() {
63 listNotification.add("a");
64 listNotification.add("b");
65 listNotification.add("c");
66 assertThat(listNotification.getElements(), Matchers.<Object>contains("a", "b", "c"));
70 public void listNotificationRemovesCorrectElement() {
71 listNotification.setElements(Arrays.asList("a", "b", "c"));
72 listNotification.remove("b");
73 assertThat(listNotification.getElements(), Matchers.<Object>contains("a", "c"));
77 public void removingTheLastElementDismissesTheNotification() {
78 NotificationListener notificationListener = mock(NotificationListener.class);
79 listNotification.addNotificationListener(notificationListener);
80 listNotification.add("a");
81 listNotification.remove("a");
82 verify(notificationListener).notificationDismissed(listNotification);
86 public void dismissingTheListNotificationRemovesAllElements() {
87 listNotification.setElements(Arrays.asList("a", "b", "c"));
88 listNotification.dismiss();
89 assertThat(listNotification.getElements(), emptyIterable());
93 public void listNotificationWithDifferentElementsIsNotEqual() {
94 ListNotification secondNotification = new ListNotification(ID, KEY, template);
95 listNotification.add("a");
96 secondNotification.add("b");
97 assertThat(listNotification, not(is(secondNotification)));
101 public void listNotificationWithDifferentKeyIsNotEqual() {
102 ListNotification secondNotification = new ListNotification(ID, OTHER_KEY, template);
103 assertThat(listNotification, not(is(secondNotification)));
107 public void copiedNotificationsHaveTheSameHashCode() {
108 ListNotification secondNotification = new ListNotification(listNotification);
109 listNotification.add("a");
110 secondNotification.add("a");
111 listNotification.setLastUpdateTime(secondNotification.getLastUpdatedTime());
112 assertThat(listNotification.hashCode(), is(secondNotification.hashCode()));
116 public void listNotificationIsNotEqualToOtherObjects() {
117 assertThat(listNotification, not(is(new Object())));