From 2dca84615faf7f1d657a6a11fdcb80abed7e06f9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 9 Nov 2013 16:33:01 +0100 Subject: [PATCH] Add unit test for CollectionAccessor. --- .../sone/template/CollectionAccessorTest.java | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/test/java/net/pterodactylus/sone/template/CollectionAccessorTest.java 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()); + } + +} -- 2.7.4