Deprecate getLastLogin, replace with java 8 getLastLoginTime

(Resolves #1421)
This commit is contained in:
Adam Harrison 2017-12-17 11:36:43 +01:00 committed by games647
parent 26a69297ce
commit 6f52449d49
No known key found for this signature in database
GPG Key ID: BFC68C8708713A88
2 changed files with 62 additions and 4 deletions

View File

@ -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;
}

View File

@ -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