From 6f52449d4964fb232df7d61213eacbaf8d07cfcf Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Sun, 17 Dec 2017 11:36:43 +0100 Subject: [PATCH] Deprecate getLastLogin, replace with java 8 getLastLoginTime (Resolves #1421) --- .../fr/xephi/authme/api/v3/AuthMeApi.java | 29 +++++++++++++-- .../fr/xephi/authme/api/v3/AuthMeApiTest.java | 37 ++++++++++++++++++- 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/src/main/java/fr/xephi/authme/api/v3/AuthMeApi.java b/src/main/java/fr/xephi/authme/api/v3/AuthMeApi.java index d4fae3ff..25bdb209 100644 --- a/src/main/java/fr/xephi/authme/api/v3/AuthMeApi.java +++ b/src/main/java/fr/xephi/authme/api/v3/AuthMeApi.java @@ -12,11 +12,14 @@ import fr.xephi.authme.security.crypts.HashedPassword; import fr.xephi.authme.service.GeoIpService; import fr.xephi.authme.service.ValidationService; import fr.xephi.authme.util.PlayerUtils; + import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; import javax.inject.Inject; + +import java.time.Instant; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -159,18 +162,38 @@ public class AuthMeApi { } /** - * Get the last login date of a player. + * Get the last (AuthMe) login date of a player. * * @param playerName The name of the player to process + * * @return The date of the last login, or null if the player doesn't exist or has never logged in + * @Deprecated Use Java 8's Instant method {@link #getLastLoginTime(String)} */ + @Deprecated public Date getLastLogin(String playerName) { + Long lastLogin = getLastLoginMillis(playerName); + return lastLogin == null ? null : new Date(lastLogin); + } + + /** + * Get the last (AuthMe) login timestamp of a player. + * + * @param playerName The name of the player to process + * + * @return The timestamp of the last login, or null if the player doesn't exist or has never logged in + */ + public Instant getLastLoginTime(String playerName) { + Long lastLogin = getLastLoginMillis(playerName); + return lastLogin == null ? null : Instant.ofEpochMilli(lastLogin); + } + + private Long getLastLoginMillis(String playerName) { PlayerAuth auth = playerCache.getAuth(playerName); if (auth == null) { auth = dataSource.getAuth(playerName); } - if (auth != null && auth.getLastLogin() != null) { - return new Date(auth.getLastLogin()); + if (auth != null) { + return auth.getLastLogin(); } return null; } diff --git a/src/test/java/fr/xephi/authme/api/v3/AuthMeApiTest.java b/src/test/java/fr/xephi/authme/api/v3/AuthMeApiTest.java index 03297aba..433675f9 100644 --- a/src/test/java/fr/xephi/authme/api/v3/AuthMeApiTest.java +++ b/src/test/java/fr/xephi/authme/api/v3/AuthMeApiTest.java @@ -28,6 +28,7 @@ import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.stream.Collectors; +import java.time.Instant; import static fr.xephi.authme.IsEqualByReflectionMatcher.isEqualTo; import static org.hamcrest.Matchers.contains; @@ -202,7 +203,7 @@ public class AuthMeApiTest { // then assertThat(result, not(nullValue())); - assertThat(result, equalTo(new Date(1501597979))); + assertThat(result, equalTo(new Date(1501597979L))); } @Test @@ -222,6 +223,40 @@ public class AuthMeApiTest { verify(dataSource).getAuth(name); } + @Test + public void shouldGetLastLoginTime() { + // given + String name = "David"; + PlayerAuth auth = PlayerAuth.builder().name(name) + .lastLogin(1501597979L) + .build(); + given(playerCache.getAuth(name)).willReturn(auth); + + // when + Instant result = api.getLastLoginTime(name); + + // then + assertThat(result, not(nullValue())); + assertThat(result, equalTo(Instant.ofEpochMilli(1501597979L))); + } + + @Test + public void shouldHandleNullLastLoginTime() { + // given + String name = "John"; + PlayerAuth auth = PlayerAuth.builder().name(name) + .lastLogin(null) + .build(); + given(dataSource.getAuth(name)).willReturn(auth); + + // when + Instant result = api.getLastLoginTime(name); + + // then + assertThat(result, nullValue()); + verify(dataSource).getAuth(name); + } + @Test public void shouldReturnNullForUnavailablePlayer() { // given