Minor - make ReflectionTestUtils.getFieldValue do the casting directly

This commit is contained in:
ljacqu 2016-08-27 21:30:01 +02:00
parent 2417bf4c3f
commit f666ea9370
9 changed files with 16 additions and 25 deletions

View File

@ -167,8 +167,7 @@ public class AntiBotTest {
antiBot.handlePlayerJoin(player); antiBot.handlePlayerJoin(player);
// then // then
@SuppressWarnings("unchecked") List<String> playerList = ReflectionTestUtils
List<String> playerList = (List<String>) ReflectionTestUtils
.getFieldValue(AntiBot.class, antiBot, "antibotPlayers"); .getFieldValue(AntiBot.class, antiBot, "antibotPlayers");
assertThat(playerList, hasSize(1)); assertThat(playerList, hasSize(1));
verify(bukkitService).scheduleSyncDelayedTask(any(Runnable.class), eq((long) 15 * TICKS_PER_SECOND)); verify(bukkitService).scheduleSyncDelayedTask(any(Runnable.class), eq((long) 15 * TICKS_PER_SECOND));
@ -197,8 +196,7 @@ public class AntiBotTest {
antiBot.handlePlayerJoin(player); antiBot.handlePlayerJoin(player);
// then // then
@SuppressWarnings("rawtypes") List<?> playerList = ReflectionTestUtils.getFieldValue(AntiBot.class, antiBot, "antibotPlayers");
List<?> playerList = (List) ReflectionTestUtils.getFieldValue(AntiBot.class, antiBot, "antibotPlayers");
assertThat(playerList, empty()); assertThat(playerList, empty());
verify(bukkitService, never()).scheduleSyncDelayedTask(any(Runnable.class), anyLong()); verify(bukkitService, never()).scheduleSyncDelayedTask(any(Runnable.class), anyLong());
} }

View File

@ -64,11 +64,11 @@ public final class ReflectionTestUtils {
} }
} }
@SuppressWarnings("unchecked")
public static <T> Object getFieldValue(Class<T> clazz, T instance, String fieldName) { public static <T, V> V getFieldValue(Class<T> clazz, T instance, String fieldName) {
Field field = getField(clazz, instance, fieldName); Field field = getField(clazz, instance, fieldName);
try { try {
return field.get(instance); return (V) field.get(instance);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw new UnsupportedOperationException("Could not get value of field '" + fieldName + "'", e); throw new UnsupportedOperationException("Could not get value of field '" + fieldName + "'", e);
} }

View File

@ -154,7 +154,7 @@ public class CaptchaManagerTest {
private static void assertHasCount(CaptchaManager manager, String player, Integer count) { private static void assertHasCount(CaptchaManager manager, String player, Integer count) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Integer> playerCounts = (Map<String, Integer>) ReflectionTestUtils Map<String, Integer> playerCounts = ReflectionTestUtils
.getFieldValue(CaptchaManager.class, manager, "playerCounts"); .getFieldValue(CaptchaManager.class, manager, "playerCounts");
assertThat(playerCounts.get(player.toLowerCase()), equalTo(count)); assertThat(playerCounts.get(player.toLowerCase()), equalTo(count));
} }

View File

@ -161,10 +161,8 @@ public class SessionManagerTest {
assertThat(sessions, aMapWithSize(4)); // map not changed -> no cleanup performed assertThat(sessions, aMapWithSize(4)); // map not changed -> no cleanup performed
} }
@SuppressWarnings("unchecked")
private static Map<String, Long> getSessionsMap(SessionManager manager) { private static Map<String, Long> getSessionsMap(SessionManager manager) {
return (Map<String, Long>) ReflectionTestUtils return ReflectionTestUtils.getFieldValue(SessionManager.class, manager, "sessions");
.getFieldValue(SessionManager.class, manager, "sessions");
} }

View File

@ -196,18 +196,16 @@ public class TempbanManagerTest {
return settings; return settings;
} }
@SuppressWarnings("unchecked")
private static void assertHasNoEntries(TempbanManager manager, String address) { private static void assertHasNoEntries(TempbanManager manager, String address) {
Map<String, Map<?, ?>> playerCounts = (Map<String, Map<?, ?>>) ReflectionTestUtils Map<String, Map<?, ?>> playerCounts = ReflectionTestUtils
.getFieldValue(TempbanManager.class, manager, "ipLoginFailureCounts"); .getFieldValue(TempbanManager.class, manager, "ipLoginFailureCounts");
Map map = playerCounts.get(address); Map map = playerCounts.get(address);
assertThat(map == null || map.isEmpty(), equalTo(true)); assertThat(map == null || map.isEmpty(), equalTo(true));
} }
@SuppressWarnings("unchecked")
private static void assertHasCount(TempbanManager manager, String address, String name, int count) { private static void assertHasCount(TempbanManager manager, String address, String name, int count) {
Map<String, Map<String, TimedCounter>> playerCounts = (Map<String, Map<String, TimedCounter>>) Map<String, Map<String, TimedCounter>> playerCounts = ReflectionTestUtils
ReflectionTestUtils.getFieldValue(TempbanManager.class, manager, "ipLoginFailureCounts"); .getFieldValue(TempbanManager.class, manager, "ipLoginFailureCounts");
assertThat(playerCounts.get(address).get(name).getCount(10000L), equalTo(count)); assertThat(playerCounts.get(address).get(name).getCount(10000L), equalTo(count));
} }
} }

View File

@ -224,8 +224,7 @@ public class LimboCacheTest {
assertThat(limboCache.hasPlayerData("someone_else"), equalTo(false)); assertThat(limboCache.hasPlayerData("someone_else"), equalTo(false));
} }
@SuppressWarnings("unchecked")
private Map<String, PlayerData> getCache() { private Map<String, PlayerData> getCache() {
return (Map<String, PlayerData>) ReflectionTestUtils.getFieldValue(LimboCache.class, limboCache, "cache"); return ReflectionTestUtils.getFieldValue(LimboCache.class, limboCache, "cache");
} }
} }

View File

@ -73,7 +73,7 @@ public class SettingsClassConsistencyTest {
for (Field field : fields) { for (Field field : fields) {
if (Property.class.isAssignableFrom(field.getType())) { if (Property.class.isAssignableFrom(field.getType())) {
Property<?> property = Property<?> property =
(Property<?>) ReflectionTestUtils.getFieldValue(clazz, null, field.getName()); ReflectionTestUtils.getFieldValue(clazz, null, field.getName());
if (!paths.add(property.getPath())) { if (!paths.add(property.getPath())) {
fail("Path '" + property.getPath() + "' should be used by only one constant"); fail("Path '" + property.getPath() + "' should be used by only one constant");
} }

View File

@ -34,8 +34,8 @@ import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyCollectionOf;
import static org.mockito.Matchers.anyLong; import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anySet;
import static org.mockito.Matchers.argThat; import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -116,7 +116,6 @@ public class PurgeServiceTest {
} }
@Test @Test
@SuppressWarnings("unchecked")
public void shouldRecognizeNoPlayersToPurge() { public void shouldRecognizeNoPlayersToPurge() {
// given // given
long delay = 123012301L; long delay = 123012301L;
@ -128,7 +127,7 @@ public class PurgeServiceTest {
// then // then
verify(dataSource).getRecordsToPurge(delay); verify(dataSource).getRecordsToPurge(delay);
verify(dataSource, never()).purgeRecords(anySet()); verify(dataSource, never()).purgeRecords(anyCollectionOf(String.class));
verify(sender).sendMessage("No players to purge"); verify(sender).sendMessage("No players to purge");
verifyZeroInteractions(bukkitService, permissionsManager); verifyZeroInteractions(bukkitService, permissionsManager);
} }
@ -208,14 +207,13 @@ public class PurgeServiceTest {
Math.abs(timestamp - expectedTimestamp), not(greaterThan(toleranceMillis))); Math.abs(timestamp - expectedTimestamp), not(greaterThan(toleranceMillis)));
} }
@SuppressWarnings("unchecked")
private void verifyScheduledPurgeTask(UUID senderUuid, Set<String> names) { private void verifyScheduledPurgeTask(UUID senderUuid, Set<String> names) {
ArgumentCaptor<PurgeTask> captor = ArgumentCaptor.forClass(PurgeTask.class); ArgumentCaptor<PurgeTask> captor = ArgumentCaptor.forClass(PurgeTask.class);
verify(bukkitService).runTaskTimer(captor.capture(), eq(0L), eq(1L)); verify(bukkitService).runTaskTimer(captor.capture(), eq(0L), eq(1L));
PurgeTask task = captor.getValue(); PurgeTask task = captor.getValue();
Object senderInTask = ReflectionTestUtils.getFieldValue(PurgeTask.class, task, "sender"); Object senderInTask = ReflectionTestUtils.getFieldValue(PurgeTask.class, task, "sender");
Set<String> namesInTask = (Set<String>) ReflectionTestUtils.getFieldValue(PurgeTask.class, task, "toPurge"); Set<String> namesInTask = ReflectionTestUtils.getFieldValue(PurgeTask.class, task, "toPurge");
assertThat(senderInTask, Matchers.<Object>equalTo(senderUuid)); assertThat(senderInTask, Matchers.<Object>equalTo(senderUuid));
assertThat(namesInTask, containsInAnyOrder(names.toArray())); assertThat(namesInTask, containsInAnyOrder(names.toArray()));
} }

View File

@ -34,7 +34,7 @@ public class BukkitServiceTest {
BukkitService bukkitService = new BukkitService(authMe); BukkitService bukkitService = new BukkitService(authMe);
// when // when
boolean doesMethodReturnCollection = (Boolean) ReflectionTestUtils boolean doesMethodReturnCollection = ReflectionTestUtils
.getFieldValue(BukkitService.class, bukkitService, "getOnlinePlayersIsCollection"); .getFieldValue(BukkitService.class, bukkitService, "getOnlinePlayersIsCollection");
// then // then