+++ /dev/null
-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.ArgumentMatchers.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-import static org.mockito.hamcrest.MockitoHamcrest.argThat;
-
-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}.
- */
-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<Object> 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.<Object>contains("a", "b", "c"));
- }
-
- @Test
- public void listNotificationRetainsAddedElements() {
- listNotification.add("a");
- listNotification.add("b");
- listNotification.add("c");
- assertThat(listNotification.getElements(), Matchers.<Object>contains("a", "b", "c"));
- }
-
- @Test
- public void listNotificationRemovesCorrectElement() {
- listNotification.setElements(Arrays.asList("a", "b", "c"));
- listNotification.remove("b");
- assertThat(listNotification.getElements(), Matchers.<Object>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");
- listNotification.setLastUpdateTime(secondNotification.getLastUpdatedTime());
- assertThat(listNotification.hashCode(), is(secondNotification.hashCode()));
- }
-
- @Test
- public void listNotificationIsNotEqualToOtherObjects() {
- assertThat(listNotification, not(is(new Object())));
- }
-
-}
--- /dev/null
+package net.pterodactylus.sone.notify
+
+import net.pterodactylus.util.notify.*
+import net.pterodactylus.util.template.*
+import org.hamcrest.MatcherAssert.*
+import org.hamcrest.Matchers.*
+import org.junit.*
+import java.util.concurrent.atomic.*
+
+/**
+ * Unit test for [ListNotification].
+ */
+class ListNotificationTest {
+
+ private val template = Template()
+ private val listNotification = ListNotification<String>(ID, KEY, template)
+
+ @Test
+ fun `creating a list notification sets empty iterable on element key in template context`() {
+ assertThat(template.initialContext.get(KEY) as Iterable<*>, emptyIterable())
+ }
+
+ @Test
+ fun `new list notification has no element`() {
+ assertThat(listNotification.elements, emptyIterable())
+ }
+
+ @Test
+ fun `new list notification is empty`() {
+ assertThat(listNotification.isEmpty, equalTo(true))
+ }
+
+ @Test
+ fun `list notification retains set elements`() {
+ listNotification.setElements(listOf("a", "b", "c"))
+ assertThat(listNotification.elements, contains("a", "b", "c"))
+ }
+
+ @Test
+ fun `list notification retains added elements`() {
+ listNotification.add("a")
+ listNotification.add("b")
+ listNotification.add("c")
+ assertThat(listNotification.elements, contains("a", "b", "c"))
+ }
+
+ @Test
+ fun `list notification removes correct element`() {
+ listNotification.setElements(listOf("a", "b", "c"))
+ listNotification.remove("b")
+ assertThat(listNotification.elements, contains("a", "c"))
+ }
+
+ @Test
+ fun `removing the last element dismisses the notification`() {
+ val notificationDismissed = AtomicBoolean()
+ val notificationListener = NotificationListener { notificationDismissed.set(it == listNotification) }
+ listNotification.addNotificationListener(notificationListener)
+ listNotification.add("a")
+ listNotification.remove("a")
+ assertThat(notificationDismissed.get(), equalTo(true))
+ }
+
+ @Test
+ fun `dismissing the list notification removes all elements`() {
+ listNotification.setElements(listOf("a", "b", "c"))
+ listNotification.dismiss()
+ assertThat(listNotification.elements, emptyIterable())
+ }
+
+ @Test
+ fun `list notification with different elements is not equal`() {
+ val secondNotification = ListNotification<String>(ID, KEY, template)
+ listNotification.add("a")
+ secondNotification.add("b")
+ assertThat(listNotification, not(equalTo(secondNotification)))
+ }
+
+ @Test
+ fun `list notification with different key is not equal`() {
+ val secondNotification = ListNotification<String>(ID, OTHER_KEY, template)
+ assertThat(listNotification, not(equalTo(secondNotification)))
+ }
+
+ @Test
+ fun `copied notifications have the same hash code`() {
+ val secondNotification = ListNotification(listNotification)
+ listNotification.add("a")
+ secondNotification.add("a")
+ listNotification.setLastUpdateTime(secondNotification.lastUpdatedTime)
+ assertThat(listNotification.hashCode(), equalTo(secondNotification.hashCode()))
+ }
+
+ @Test
+ fun `list notification is not equal to other objects`() {
+ assertThat(listNotification, not(equalTo(Any())))
+ }
+
+}
+
+private const val ID = "notification-id"
+private const val KEY = "element-key"
+private const val OTHER_KEY = "other-key"