From acd4a772e806f28b9a39624585570f5e63e3bb3a Mon Sep 17 00:00:00 2001 From: ljacqu Date: Wed, 15 Jun 2016 21:24:57 +0200 Subject: [PATCH] Minor householding in tests - Add missing test for injector - Remove redundant Mock fields --- .../authme/command/CommandHandlerTest.java | 2 -- .../authme/AccountsCommandTest.java | 9 +++++++-- .../AuthMeServiceInitializerTest.java | 16 +++++++++++++++ .../authme/process/ProcessServiceTest.java | 20 +++++++++++++++++++ .../fr/xephi/authme/util/StringUtilsTest.java | 6 ++++++ .../authme/util/TeleportationServiceTest.java | 4 ---- 6 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/test/java/fr/xephi/authme/command/CommandHandlerTest.java b/src/test/java/fr/xephi/authme/command/CommandHandlerTest.java index d831c85a..137d9f1f 100644 --- a/src/test/java/fr/xephi/authme/command/CommandHandlerTest.java +++ b/src/test/java/fr/xephi/authme/command/CommandHandlerTest.java @@ -56,8 +56,6 @@ public class CommandHandlerTest { @Mock private AuthMeServiceInitializer initializer; @Mock - private CommandInitializer commandInitializer; - @Mock private CommandMapper commandMapper; @Mock private PermissionsManager permissionsManager; diff --git a/src/test/java/fr/xephi/authme/command/executable/authme/AccountsCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/authme/AccountsCommandTest.java index df464811..09615751 100644 --- a/src/test/java/fr/xephi/authme/command/executable/authme/AccountsCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/authme/AccountsCommandTest.java @@ -36,8 +36,6 @@ public class AccountsCommandTest { @InjectMocks private AccountsCommand command; @Mock - private CommandSender sender; - @Mock private CommandService service; @Mock private DataSource dataSource; @@ -47,6 +45,7 @@ public class AccountsCommandTest { @Test public void shouldGetAccountsOfCurrentUser() { // given + CommandSender sender = mock(CommandSender.class); given(sender.getName()).willReturn("Tester"); List arguments = Collections.emptyList(); given(dataSource.getAuth("tester")).willReturn(authWithIp("123.45.67.89")); @@ -65,6 +64,7 @@ public class AccountsCommandTest { @Test public void shouldReturnUnknownUserForNullAuth() { // given + CommandSender sender = mock(CommandSender.class); List arguments = Collections.singletonList("SomeUser"); given(dataSource.getAuth("someuser")).willReturn(null); @@ -80,6 +80,7 @@ public class AccountsCommandTest { @Test public void shouldReturnUnregisteredMessageForEmptyAuthList() { // given + CommandSender sender = mock(CommandSender.class); List arguments = Collections.singletonList("SomeUser"); given(dataSource.getAuth("someuser")).willReturn(mock(PlayerAuth.class)); given(dataSource.getAllAuthsByIp(anyString())).willReturn(Collections.emptyList()); @@ -96,6 +97,7 @@ public class AccountsCommandTest { @Test public void shouldReturnSingleAccountMessage() { // given + CommandSender sender = mock(CommandSender.class); List arguments = Collections.singletonList("SomeUser"); given(dataSource.getAuth("someuser")).willReturn(authWithIp("56.78.90.123")); given(dataSource.getAllAuthsByIp("56.78.90.123")).willReturn(Collections.singletonList("SomeUser")); @@ -115,6 +117,7 @@ public class AccountsCommandTest { @Test public void shouldReturnIpUnknown() { // given + CommandSender sender = mock(CommandSender.class); List arguments = Collections.singletonList("123.45.67.89"); given(dataSource.getAllAuthsByIp("123.45.67.89")).willReturn(Collections.emptyList()); @@ -130,6 +133,7 @@ public class AccountsCommandTest { @Test public void shouldReturnSingleAccountForIpQuery() { // given + CommandSender sender = mock(CommandSender.class); List arguments = Collections.singletonList("24.24.48.48"); given(dataSource.getAllAuthsByIp("24.24.48.48")).willReturn(Collections.singletonList("SomeUser")); @@ -145,6 +149,7 @@ public class AccountsCommandTest { @Test public void shouldReturnAccountListForIpQuery() { // given + CommandSender sender = mock(CommandSender.class); List arguments = Collections.singletonList("98.76.41.122"); given(dataSource.getAllAuthsByIp("98.76.41.122")).willReturn(Arrays.asList("Tester", "Lester", "Taster")); diff --git a/src/test/java/fr/xephi/authme/initialization/AuthMeServiceInitializerTest.java b/src/test/java/fr/xephi/authme/initialization/AuthMeServiceInitializerTest.java index d85377f5..e03a1435 100644 --- a/src/test/java/fr/xephi/authme/initialization/AuthMeServiceInitializerTest.java +++ b/src/test/java/fr/xephi/authme/initialization/AuthMeServiceInitializerTest.java @@ -316,6 +316,22 @@ public class AuthMeServiceInitializerTest { initializer.performReloadOnServices(); } + @Test + public void shouldRetrieveExistingInstancesOnly() { + // given + initializer.get(GammaService.class); + + // when + AlphaService alphaService = initializer.getIfAvailable(AlphaService.class); + BetaManager betaManager = initializer.getIfAvailable(BetaManager.class); + + // then + // was initialized because is dependency of GammaService + assertThat(alphaService, not(nullValue())); + // nothing caused this to be initialized + assertThat(betaManager, nullValue()); + } + private void expectRuntimeExceptionWith(String message) { expectedException.expect(RuntimeException.class); expectedException.expectMessage(containsString(message)); diff --git a/src/test/java/fr/xephi/authme/process/ProcessServiceTest.java b/src/test/java/fr/xephi/authme/process/ProcessServiceTest.java index c0505113..617e0d80 100644 --- a/src/test/java/fr/xephi/authme/process/ProcessServiceTest.java +++ b/src/test/java/fr/xephi/authme/process/ProcessServiceTest.java @@ -2,6 +2,8 @@ package fr.xephi.authme.process; import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.Messages; +import fr.xephi.authme.permission.AuthGroupHandler; +import fr.xephi.authme.permission.AuthGroupType; import fr.xephi.authme.permission.PermissionNode; import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.permission.PlayerPermission; @@ -43,6 +45,9 @@ public class ProcessServiceTest { @Mock private PermissionsManager permissionsManager; + @Mock + private AuthGroupHandler authGroupHandler; + @Test public void shouldGetProperty() { // given @@ -166,4 +171,19 @@ public class ProcessServiceTest { verify(permissionsManager).hasPermission(player, permission); assertThat(result, equalTo(true)); } + + @Test + public void shouldSetPermissionGroup() { + // given + Player player = mock(Player.class); + AuthGroupType type = AuthGroupType.LOGGED_IN; + given(authGroupHandler.setGroup(player, type)).willReturn(true); + + // when + boolean result = processService.setGroup(player, type); + + // then + verify(authGroupHandler).setGroup(player, type); + assertThat(result, equalTo(true)); + } } diff --git a/src/test/java/fr/xephi/authme/util/StringUtilsTest.java b/src/test/java/fr/xephi/authme/util/StringUtilsTest.java index 6cc7799a..a09ac87c 100644 --- a/src/test/java/fr/xephi/authme/util/StringUtilsTest.java +++ b/src/test/java/fr/xephi/authme/util/StringUtilsTest.java @@ -1,5 +1,6 @@ package fr.xephi.authme.util; +import fr.xephi.authme.TestHelper; import org.junit.Test; import java.io.File; @@ -145,4 +146,9 @@ public class StringUtilsTest { // then assertThat(result, equalTo("path" + File.separator + "to" + File.separator + "test-file.txt")); } + + @Test + public void shouldHaveHiddenConstructor() { + TestHelper.validateHasOnlyPrivateEmptyConstructor(StringUtils.class); + } } diff --git a/src/test/java/fr/xephi/authme/util/TeleportationServiceTest.java b/src/test/java/fr/xephi/authme/util/TeleportationServiceTest.java index b39fc9cf..ad9d9620 100644 --- a/src/test/java/fr/xephi/authme/util/TeleportationServiceTest.java +++ b/src/test/java/fr/xephi/authme/util/TeleportationServiceTest.java @@ -5,7 +5,6 @@ import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.limbo.LimboPlayer; import fr.xephi.authme.events.FirstSpawnTeleportEvent; import fr.xephi.authme.events.SpawnTeleportEvent; -import fr.xephi.authme.output.Messages; import fr.xephi.authme.settings.NewSetting; import fr.xephi.authme.settings.SpawnLoader; import fr.xephi.authme.settings.properties.RestrictionSettings; @@ -48,9 +47,6 @@ public class TeleportationServiceTest { @Mock private NewSetting settings; - @Mock - private Messages messages; - @Mock private BukkitService bukkitService;