From: David ‘Bombe’ Roden Date: Sat, 9 Nov 2013 15:33:01 +0000 (+0100) Subject: Add unit test for CollectionAccessor. X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=2dca84615faf7f1d657a6a11fdcb80abed7e06f9 Add unit test for CollectionAccessor. --- diff --git a/src/test/java/net/pterodactylus/sone/template/CollectionAccessorTest.java b/src/test/java/net/pterodactylus/sone/template/CollectionAccessorTest.java new file mode 100644 index 0000000..a8262ac --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/template/CollectionAccessorTest.java @@ -0,0 +1,74 @@ +/* + * Sone - CollectionAccessorTest.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.template; + +import static java.util.Arrays.asList; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; + +import java.util.Collection; + +import net.pterodactylus.sone.data.Mocks; +import net.pterodactylus.sone.data.Sone; + +import org.junit.Test; + +/** + * Unit test for {@link CollectionAccessor}. + * + * @author David ‘Bombe’ Roden + */ +public class CollectionAccessorTest { + + private final Mocks mocks = new Mocks(); + private final CollectionAccessor collectionAccessor = new CollectionAccessor(); + + @Test + public void soneNamesAreReturnedSorted() { + Sone sone1 = mocks.mockSone("Sone1").withName("Sone1").create(); + Sone sone2 = mocks.mockSone("Sone2").withName("Sone2").create(); + Sone sone3 = mocks.mockSone("Sone3").withName("Sone3").create(); + Collection sones = asList(sone3, sone1, sone2); + String soneNames = (String) collectionAccessor.get(null, sones, "soneNames"); + assertThat(soneNames, is("Sone1, Sone2, Sone3")); + } + + @Test + public void collectionsWithOtherObjectsUseOnlyTheSones() { + Sone sone1 = mocks.mockSone("Sone1").withName("Sone1").create(); + Sone sone2 = mocks.mockSone("Sone2").withName("Sone2").create(); + Sone sone3 = mocks.mockSone("Sone3").withName("Sone3").create(); + Collection sones = asList(sone3, new Object(), sone1, sone2); + String soneNames = (String) collectionAccessor.get(null, sones, "soneNames"); + assertThat(soneNames, is("Sone1, Sone2, Sone3")); + } + + @Test + public void reflectionAccessorIsUsed() { + Collection objects = asList(new Object(), new Object()); + int size = (Integer) collectionAccessor.get(null, objects, "size"); + assertThat(size, is(objects.size())); + } + + @Test + public void nullCollectionReturnsNull() { + assertThat(collectionAccessor.get(null, null, null), nullValue()); + } + +}