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

View File

@ -64,11 +64,11 @@ public final class ReflectionTestUtils {
}
}
public static <T> Object getFieldValue(Class<T> clazz, T instance, String fieldName) {
@SuppressWarnings("unchecked")
public static <T, V> V getFieldValue(Class<T> clazz, T instance, String fieldName) {
Field field = getField(clazz, instance, fieldName);
try {
return field.get(instance);
return (V) field.get(instance);
} catch (IllegalAccessException 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) {
@SuppressWarnings("unchecked")
Map<String, Integer> playerCounts = (Map<String, Integer>) ReflectionTestUtils
Map<String, Integer> playerCounts = ReflectionTestUtils
.getFieldValue(CaptchaManager.class, manager, "playerCounts");
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
}
@SuppressWarnings("unchecked")
private static Map<String, Long> getSessionsMap(SessionManager manager) {
return (Map<String, Long>) ReflectionTestUtils
.getFieldValue(SessionManager.class, manager, "sessions");
return ReflectionTestUtils.getFieldValue(SessionManager.class, manager, "sessions");
}

View File

@ -196,18 +196,16 @@ public class TempbanManagerTest {
return settings;
}
@SuppressWarnings("unchecked")
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");
Map map = playerCounts.get(address);
assertThat(map == null || map.isEmpty(), equalTo(true));
}
@SuppressWarnings("unchecked")
private static void assertHasCount(TempbanManager manager, String address, String name, int count) {
Map<String, Map<String, TimedCounter>> playerCounts = (Map<String, Map<String, TimedCounter>>)
ReflectionTestUtils.getFieldValue(TempbanManager.class, manager, "ipLoginFailureCounts");
Map<String, Map<String, TimedCounter>> playerCounts = ReflectionTestUtils
.getFieldValue(TempbanManager.class, manager, "ipLoginFailureCounts");
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));
}
@SuppressWarnings("unchecked")
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) {
if (Property.class.isAssignableFrom(field.getType())) {
Property<?> property =
(Property<?>) ReflectionTestUtils.getFieldValue(clazz, null, field.getName());
ReflectionTestUtils.getFieldValue(clazz, null, field.getName());
if (!paths.add(property.getPath())) {
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.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyCollectionOf;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anySet;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
@ -116,7 +116,6 @@ public class PurgeServiceTest {
}
@Test
@SuppressWarnings("unchecked")
public void shouldRecognizeNoPlayersToPurge() {
// given
long delay = 123012301L;
@ -128,7 +127,7 @@ public class PurgeServiceTest {
// then
verify(dataSource).getRecordsToPurge(delay);
verify(dataSource, never()).purgeRecords(anySet());
verify(dataSource, never()).purgeRecords(anyCollectionOf(String.class));
verify(sender).sendMessage("No players to purge");
verifyZeroInteractions(bukkitService, permissionsManager);
}
@ -208,14 +207,13 @@ public class PurgeServiceTest {
Math.abs(timestamp - expectedTimestamp), not(greaterThan(toleranceMillis)));
}
@SuppressWarnings("unchecked")
private void verifyScheduledPurgeTask(UUID senderUuid, Set<String> names) {
ArgumentCaptor<PurgeTask> captor = ArgumentCaptor.forClass(PurgeTask.class);
verify(bukkitService).runTaskTimer(captor.capture(), eq(0L), eq(1L));
PurgeTask task = captor.getValue();
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(namesInTask, containsInAnyOrder(names.toArray()));
}

View File

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