Fix minor checkstyle issues

- Add JavaDoc where checkstyle expects it
- Fix line too long issues
- ...
This commit is contained in:
ljacqu
2017-05-07 11:59:01 +02:00
parent 1a48348824
commit 1f8307c8f6
43 changed files with 605 additions and 134 deletions
@@ -6,7 +6,6 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import fr.xephi.authme.datasource.Columns;
import fr.xephi.authme.initialization.HasCleanup;
import fr.xephi.authme.listener.PlayerListener;
import fr.xephi.authme.process.register.executors.RegistrationMethod;
import fr.xephi.authme.security.crypts.Whirlpool;
import fr.xephi.authme.util.expiring.ExpiringMap;
@@ -0,0 +1,250 @@
package fr.xephi.authme.api;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.api.v3.AuthMeApi;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.service.PluginHookService;
import fr.xephi.authme.service.ValidationService;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Test for {@link fr.xephi.authme.api.NewAPI}.
*/
@RunWith(MockitoJUnitRunner.class)
public class NewAPITest {
@InjectMocks
private NewAPI api;
@Mock
private PluginHookService pluginHookService;
@Mock
private ValidationService validationService;
@Mock
private DataSource dataSource;
@Mock
private Management management;
@Mock
private PasswordSecurity passwordSecurity;
@Mock
private PlayerCache playerCache;
@Test
public void shouldReturnInstanceOrNull() {
NewAPI result = NewAPI.getInstance();
assertThat(result, sameInstance(api));
ReflectionTestUtils.setField(AuthMeApi.class, null, "singleton", null);
assertThat(AuthMeApi.getInstance(), nullValue());
}
@Test
public void shouldReturnIfPlayerIsAuthenticated() {
// given
String name = "Bobby";
Player player = mockPlayerWithName(name);
given(playerCache.isAuthenticated(name)).willReturn(true);
// when
boolean result = api.isAuthenticated(player);
// then
verify(playerCache).isAuthenticated(name);
assertThat(result, equalTo(true));
}
@Test
public void shouldReturnIfPlayerIsNpc() {
// given
Player player = mock(Player.class);
given(pluginHookService.isNpc(player)).willReturn(true);
// when
boolean result = api.isNPC(player);
// then
assertThat(result, equalTo(true));
}
@Test
public void shouldReturnIfPlayerIsUnrestricted() {
// given
String name = "Tester";
Player player = mockPlayerWithName(name);
given(validationService.isUnrestricted(name)).willReturn(true);
// when
boolean result = api.isUnrestricted(player);
// then
verify(validationService).isUnrestricted(name);
assertThat(result, equalTo(true));
}
@Test
public void shouldGetLastLocation() {
// given
String name = "Gary";
Player player = mockPlayerWithName(name);
PlayerAuth auth = PlayerAuth.builder().name(name)
.locWorld("world")
.locX(12.4)
.locY(24.6)
.locZ(-438.2)
.locYaw(3.41f)
.locPitch(0.29f)
.build();
given(playerCache.getAuth(name)).willReturn(auth);
Server server = mock(Server.class);
ReflectionTestUtils.setField(Bukkit.class, null, "server", server);
World world = mock(World.class);
given(server.getWorld(auth.getWorld())).willReturn(world);
// when
Location result = api.getLastLocation(player);
// then
assertThat(result, not(nullValue()));
assertThat(result.getX(), equalTo(auth.getQuitLocX()));
assertThat(result.getY(), equalTo(auth.getQuitLocY()));
assertThat(result.getZ(), equalTo(auth.getQuitLocZ()));
assertThat(result.getWorld(), equalTo(world));
}
@Test
public void shouldReturnNullForUnavailablePlayer() {
// given
String name = "Numan";
Player player = mockPlayerWithName(name);
given(playerCache.getAuth(name)).willReturn(null);
// when
Location result = api.getLastLocation(player);
// then
assertThat(result, nullValue());
}
@Test
public void shouldCheckForRegisteredName() {
// given
String name = "toaster";
given(dataSource.isAuthAvailable(name)).willReturn(true);
// when
boolean result = api.isRegistered(name);
// then
assertThat(result, equalTo(true));
}
@Test
public void shouldCheckPassword() {
// given
String playerName = "Robert";
String password = "someSecretPhrase2983";
given(passwordSecurity.comparePassword(password, playerName)).willReturn(true);
// when
boolean result = api.checkPassword(playerName, password);
// then
verify(passwordSecurity).comparePassword(password, playerName);
assertThat(result, equalTo(true));
}
@Test
public void shouldReturnAuthNames() {
// given
String[] names = {"bobby", "peter", "elisabeth", "craig"};
List<PlayerAuth> auths = Arrays.stream(names)
.map(name -> PlayerAuth.builder().name(name).build())
.collect(Collectors.toList());
given(dataSource.getAllAuths()).willReturn(auths);
// when
List<String> result = api.getRegisteredNames();
// then
assertThat(result, contains(names));
}
@Test
public void shouldReturnAuthRealNames() {
// given
String[] names = {"Bobby", "peter", "Elisabeth", "CRAIG"};
List<PlayerAuth> auths = Arrays.stream(names)
.map(name -> PlayerAuth.builder().name(name).realName(name).build())
.collect(Collectors.toList());
given(dataSource.getAllAuths()).willReturn(auths);
// when
List<String> result = api.getRegisteredRealNames();
// then
assertThat(result, contains(names));
}
@Test
public void shouldUnregisterPlayer() {
// given
Player player = mock(Player.class);
String name = "Donald";
given(player.getName()).willReturn(name);
// when
api.forceUnregister(player);
// then
verify(management).performUnregisterByAdmin(null, name, player);
}
@Test
public void shouldUnregisterPlayerByName() {
// given
Server server = mock(Server.class);
ReflectionTestUtils.setField(Bukkit.class, null, "server", server);
String name = "tristan";
Player player = mock(Player.class);
given(server.getPlayer(name)).willReturn(player);
// when
api.forceUnregister(name);
// then
verify(management).performUnregisterByAdmin(null, name, player);
}
private static Player mockPlayerWithName(String name) {
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
return player;
}
}
@@ -212,6 +212,7 @@ public class CommandInitializerTest {
testCollectionForCommand(command, CommandUtils.getMinNumberOfArguments(command), mandatoryArguments);
testCollectionForCommand(command, CommandUtils.getMaxNumberOfArguments(command), totalArguments);
}
private void testCollectionForCommand(CommandDescription command, int argCount,
Map<Class<? extends ExecutableCommand>, Integer> collection) {
final Class<? extends ExecutableCommand> clazz = command.getExecutableCommand();
@@ -82,7 +82,7 @@ public final class TestCommandsUtil {
throw new IllegalStateException("Could not find command with label '" + label + "'");
}
/** Shortcut command to initialize a new test command. */
/* Shortcut command to initialize a new test command. */
private static CommandDescription createCommand(PermissionNode permission, CommandDescription parent,
List<String> labels, Class<? extends ExecutableCommand> commandClass,
CommandArgumentDescription... arguments) {
@@ -103,7 +103,7 @@ public final class TestCommandsUtil {
return command.register();
}
/** Shortcut command to initialize a new argument description. */
/* Shortcut command to initialize a new argument description. */
private static CommandArgumentDescription newArgument(String label, boolean isOptional) {
return new CommandArgumentDescription(label, "'" + label + "' argument description", isOptional);
}
@@ -118,7 +118,7 @@ public class AccountsCommandTest {
// given
CommandSender sender = mock(CommandSender.class);
List<String> arguments = Collections.singletonList("123.45.67.89");
given(dataSource.getAllAuthsByIp("123.45.67.89")).willReturn(Collections.<String>emptyList());
given(dataSource.getAllAuthsByIp("123.45.67.89")).willReturn(Collections.emptyList());
// when
command.executeCommand(sender, arguments);
@@ -25,7 +25,7 @@ public class AuthMeCommandTest {
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.<String>emptyList());
command.executeCommand(sender, Collections.emptyList());
// then
ArgumentCaptor<String> messagesCaptor = ArgumentCaptor.forClass(String.class);
@@ -40,7 +40,7 @@ public class FirstSpawnCommandTest {
Player player = mock(Player.class);
// when
command.executeCommand(player, Collections.<String>emptyList());
command.executeCommand(player, Collections.emptyList());
// then
verify(player).teleport(firstSpawn);
@@ -54,7 +54,7 @@ public class FirstSpawnCommandTest {
Player player = mock(Player.class);
// when
command.executeCommand(player, Collections.<String>emptyList());
command.executeCommand(player, Collections.emptyList());
// then
verify(player).sendMessage(argThat(containsString("spawn has failed")));
@@ -90,15 +90,15 @@ public class LastLoginCommandTest {
CommandSender sender = mock(CommandSender.class);
given(sender.getName()).willReturn(name);
long lastLogin = System.currentTimeMillis() -
(412 * DAY_IN_MSEC + 10 * HOUR_IN_MSEC - 9000);
long lastLogin = System.currentTimeMillis()
- (412 * DAY_IN_MSEC + 10 * HOUR_IN_MSEC - 9000);
PlayerAuth auth = mock(PlayerAuth.class);
given(auth.getLastLogin()).willReturn(lastLogin);
given(auth.getIp()).willReturn("123.45.66.77");
given(dataSource.getAuth(name)).willReturn(auth);
// when
command.executeCommand(sender, Collections.<String>emptyList());
command.executeCommand(sender, Collections.emptyList());
// then
verify(dataSource).getAuth(name);
@@ -46,7 +46,7 @@ public class PurgeBannedPlayersCommandTest {
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.<String>emptyList());
command.executeCommand(sender, Collections.emptyList());
// then
verify(bukkitService).getBannedPlayers();
@@ -63,7 +63,7 @@ public class PurgeLastPositionCommandTest {
given(dataSource.getAuth(player)).willReturn(auth);
// when
command.executeCommand(sender, Collections.<String>emptyList());
command.executeCommand(sender, Collections.emptyList());
// then
verify(dataSource).getAuth(player);
@@ -39,7 +39,7 @@ public class SetFirstSpawnCommandTest {
given(spawnLoader.setFirstSpawn(location)).willReturn(true);
// when
command.executeCommand(player, Collections.<String>emptyList());
command.executeCommand(player, Collections.emptyList());
// then
verify(spawnLoader).setFirstSpawn(location);
@@ -55,7 +55,7 @@ public class SetFirstSpawnCommandTest {
given(spawnLoader.setFirstSpawn(location)).willReturn(false);
// when
command.executeCommand(player, Collections.<String>emptyList());
command.executeCommand(player, Collections.emptyList());
// then
verify(spawnLoader).setFirstSpawn(location);
@@ -41,7 +41,7 @@ public class SpawnCommandTest {
Player player = mock(Player.class);
// when
command.executeCommand(player, Collections.<String>emptyList());
command.executeCommand(player, Collections.emptyList());
// then
verify(player).teleport(spawn);
@@ -55,7 +55,7 @@ public class SpawnCommandTest {
Player player = mock(Player.class);
// when
command.executeCommand(player, Collections.<String>emptyList());
command.executeCommand(player, Collections.emptyList());
// then
verify(player).sendMessage(argThat(containsString("Spawn has failed")));
@@ -47,7 +47,7 @@ public class SwitchAntiBotCommandTest {
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.<String>emptyList());
command.executeCommand(sender, Collections.emptyList());
// then
verify(sender).sendMessage(argThat(containsString("status: ACTIVE")));
@@ -212,7 +212,7 @@ public class PlayerListenerTest {
public void shouldNotCancelEventForAuthenticatedPlayer() {
// given
given(settings.getProperty(HooksSettings.USE_ESSENTIALS_MOTD)).willReturn(false);
given(settings.getProperty(RestrictionSettings.ALLOW_COMMANDS)).willReturn(Collections.<String>emptyList());
given(settings.getProperty(RestrictionSettings.ALLOW_COMMANDS)).willReturn(Collections.emptyList());
Player player = playerWithMockedServer();
// PlayerCommandPreprocessEvent#getPlayer is final, so create a spy instead of a mock
PlayerCommandPreprocessEvent event = spy(new PlayerCommandPreprocessEvent(player, "/hub"));
@@ -44,7 +44,7 @@ public class PlayerUtilsTest {
given(player.getUniqueId()).willReturn(uuid);
// when
String result = PlayerUtils.getUUIDorName(player);
String result = PlayerUtils.getUuidOrName(player);
// then
assertThat(result, equalTo(uuid.toString()));
@@ -59,7 +59,7 @@ public class PlayerUtilsTest {
given(player.getName()).willReturn(name);
// when
String result = PlayerUtils.getUUIDorName(player);
String result = PlayerUtils.getUuidOrName(player);
// then
assertThat(result, equalTo(name));