#1460 Fix null handling in recent players command

- Last login might be null
This commit is contained in:
ljacqu 2018-01-06 20:26:07 +01:00
parent ea87075cd2
commit a29738e2a8
2 changed files with 32 additions and 3 deletions

View File

@ -42,8 +42,13 @@ public class RecentPlayersCommand implements ExecutableCommand {
}
private String formatPlayerMessage(PlayerAuth auth) {
LocalDateTime lastLogin = LocalDateTime.ofInstant(ofEpochMilli(auth.getLastLogin()), getZoneId());
String lastLoginText = DATE_FORMAT.format(lastLogin);
String lastLoginText;
if (auth.getLastLogin() == null) {
lastLoginText = "never";
} else {
LocalDateTime lastLogin = LocalDateTime.ofInstant(ofEpochMilli(auth.getLastLogin()), getZoneId());
lastLoginText = DATE_FORMAT.format(lastLogin);
}
return "- " + auth.getRealName() + " (" + lastLoginText + " with IP " + auth.getLastIp() + ")";
}

View File

@ -15,7 +15,6 @@ import java.util.Arrays;
import java.util.Collections;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.containsStringIgnoringCase;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doReturn;
@ -60,4 +59,29 @@ public class RecentPlayersCommandTest {
verify(sender).sendMessage(argThat(equalToIgnoringCase("- Hannah (08:09 AM, 11 Nov with IP 11.11.11.11)")));
verify(sender).sendMessage(argThat(equalToIgnoringCase("- MATT (11:15 PM, 09 Nov with IP 22.11.22.33)")));
}
@Test
public void shouldHandlePlayerWithNullLastLogin() {
// given
PlayerAuth auth1 = PlayerAuth.builder()
.name("xephren").realName("Xephren").lastIp("11.11.11.11")
.lastLogin(null)
.build();
PlayerAuth auth2 = PlayerAuth.builder()
.name("silvah777").realName("silvah777").lastIp("22.11.22.33")
.lastLogin(1510269301000L) // 11/09/2017 @ 11:15pm
.build();
doReturn(ZoneId.of("UTC")).when(command).getZoneId();
given(dataSource.getRecentlyLoggedInPlayers()).willReturn(Arrays.asList(auth1, auth2));
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.emptyList());
// then
verify(sender).sendMessage(argThat(containsString("Recently logged in players")));
verify(sender).sendMessage(argThat(equalToIgnoringCase("- Xephren (never with IP 11.11.11.11)")));
verify(sender).sendMessage(argThat(equalToIgnoringCase("- silvah777 (11:15 PM, 09 Nov with IP 22.11.22.33)")));
}
}