From 0e7267cdf05217939687c3d97bb357801a27c7e6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 3 Mar 2016 12:20:25 +0100 Subject: [PATCH] Add unit test for list notification --- .../sone/notify/ListNotificationTest.java | 121 +++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 src/test/java/net/pterodactylus/sone/notify/ListNotificationTest.java diff --git a/src/test/java/net/pterodactylus/sone/notify/ListNotificationTest.java b/src/test/java/net/pterodactylus/sone/notify/ListNotificationTest.java new file mode 100644 index 0000000..c354afa --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/notify/ListNotificationTest.java @@ -0,0 +1,121 @@ +package net.pterodactylus.sone.notify; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.emptyIterable; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.mockito.Matchers.argThat; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; + +import net.pterodactylus.util.notify.NotificationListener; +import net.pterodactylus.util.template.Template; +import net.pterodactylus.util.template.TemplateContext; + +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Unit test for {@link ListNotification}. + * + * @author David ‘Bombe’ Roden + */ +public class ListNotificationTest { + + private static final String ID = "notification-id"; + private static final String KEY = "element-key"; + private static final String OTHER_KEY = "other-key"; + + private final Template template = mock(Template.class); + private final TemplateContext templateInitialContext = mock(TemplateContext.class); + private ListNotification listNotification; + + public ListNotificationTest() { + when(template.getInitialContext()).thenReturn(templateInitialContext); + listNotification = new ListNotification(ID, KEY, template); + } + + @Test + public void creatingAListNotificationSetsEmptyIterableOnElementKeyInTemplateContext() { + verify(templateInitialContext).set(eq(KEY), argThat(emptyIterable())); + } + + @Test + public void newListNotificationHasNoElement() { + assertThat(listNotification.getElements(), emptyIterable()); + } + + @Test + public void newListNotificationIsEmpty() { + assertThat(listNotification.isEmpty(), is(true)); + } + + @Test + public void listNotificationRetainsSetElements() { + listNotification.setElements(Arrays.asList("a", "b", "c")); + assertThat(listNotification.getElements(), Matchers.contains("a", "b", "c")); + } + + @Test + public void listNotificationRetainsAddedElements() { + listNotification.add("a"); + listNotification.add("b"); + listNotification.add("c"); + assertThat(listNotification.getElements(), Matchers.contains("a", "b", "c")); + } + + @Test + public void listNotificationRemovesCorrectElement() { + listNotification.setElements(Arrays.asList("a", "b", "c")); + listNotification.remove("b"); + assertThat(listNotification.getElements(), Matchers.contains("a", "c")); + } + + @Test + public void removingTheLastElementDismissesTheNotification() { + NotificationListener notificationListener = mock(NotificationListener.class); + listNotification.addNotificationListener(notificationListener); + listNotification.add("a"); + listNotification.remove("a"); + verify(notificationListener).notificationDismissed(listNotification); + } + + @Test + public void dismissingTheListNotificationRemovesAllElements() { + listNotification.setElements(Arrays.asList("a", "b", "c")); + listNotification.dismiss(); + assertThat(listNotification.getElements(), emptyIterable()); + } + + @Test + public void listNotificationWithDifferentElementsIsNotEqual() { + ListNotification secondNotification = new ListNotification(ID, KEY, template); + listNotification.add("a"); + secondNotification.add("b"); + assertThat(listNotification, not(is(secondNotification))); + } + + @Test + public void listNotificationWithDifferentKeyIsNotEqual() { + ListNotification secondNotification = new ListNotification(ID, OTHER_KEY, template); + assertThat(listNotification, not(is(secondNotification))); + } + + @Test + public void copiedNotificationsHaveTheSameHashCode() { + ListNotification secondNotification = new ListNotification(listNotification); + listNotification.add("a"); + secondNotification.add("a"); + assertThat(listNotification.hashCode(), is(secondNotification.hashCode())); + } + + @Test + public void listNotificationIsNotEqualToOtherObjects() { + assertThat(listNotification, not(is(new Object()))); + } + +} -- 2.7.4