From: David ‘Bombe’ Roden Date: Wed, 19 Feb 2014 20:22:26 +0000 (+0100) Subject: Add test for list notifications. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=e9f73604d887aacb99e95b903938777d00a0f7db;p=Sone.git Add test for list notifications. --- 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..286b82b --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/notify/ListNotificationTest.java @@ -0,0 +1,105 @@ +/* + * Sone - ListNotificationTest.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.notify; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.collection.IsIterableContainingInOrder.contains; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.Collection; + +import net.pterodactylus.util.template.Template; +import net.pterodactylus.util.template.TemplateContext; + +import org.junit.Before; +import org.junit.Test; + +/** + * Unit test for {@link ListNotification}. + * + * @author David ‘Bombe’ Roden + */ +public class ListNotificationTest { + + private final TemplateContext initialContext = new TemplateContext(); + private final Template template = mock(Template.class); + private ListNotification listNotification; + + @Before + public void setup() { + when(template.getInitialContext()).thenReturn(initialContext); + listNotification = new ListNotification("id", "key", template); + } + + @Test + public void newListNotificationDoesNotContainElements() { + assertThat(listNotification.getElements(), empty()); + assertThat(listNotification.isEmpty(), is(true)); + } + + @Test + public void addingAnElement() { + Object newObject = new Object(); + listNotification.add(newObject); + assertThat(listNotification.isEmpty(), is(false)); + assertThat(listNotification.getElements(), contains(newObject)); + } + + @Test + public void removingAnEelement() { + Object newObject = new Object(); + listNotification.add(newObject); + listNotification.remove(newObject); + assertThat(listNotification.getElements(), empty()); + assertThat(listNotification.isEmpty(), is(true)); + } + + @Test + public void settingElements() { + Object object1 = new Object(); + Object object2 = new Object(); + Collection elements = Arrays.asList(object1, object2); + listNotification.setElements(elements); + assertThat(listNotification.getElements(), contains(object1, object2)); + } + + @Test + public void addingElementsChangesHashCode() { + int emptyListNoficiationHashCode=listNotification.hashCode(); + listNotification.add(new Object()); + assertThat(listNotification.hashCode(), not(is(emptyListNoficiationHashCode))); + } + + @Test + public void listNotificationsWithDifferentIdsAreNotEqual() { + ListNotification secondListNotification = new ListNotification("otherId", "key", template); + assertThat(listNotification, not(is(secondListNotification))); + } + + @Test + public void listNotificationsWithDifferentKeysAreNotEqual() { + ListNotification secondListNotification = new ListNotification("id", "otherKey", template); + assertThat(listNotification, not(is(secondListNotification))); + } + +}