diff --git a/src/test/java/fr/xephi/authme/TestHelper.java b/src/test/java/fr/xephi/authme/TestHelper.java index ee7aaa5e0..5a60bc1e6 100644 --- a/src/test/java/fr/xephi/authme/TestHelper.java +++ b/src/test/java/fr/xephi/authme/TestHelper.java @@ -1,6 +1,8 @@ package fr.xephi.authme; +import ch.jalu.configme.properties.Property; import fr.xephi.authme.service.BukkitService; +import fr.xephi.authme.settings.Settings; import org.bukkit.entity.Player; import org.mockito.ArgumentCaptor; import org.mockito.Mockito; @@ -18,6 +20,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.logging.Logger; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -206,4 +209,14 @@ public final class TestHelper { given(player.getAddress()).willReturn(inetSocketAddress); } + /** + * Configures the Settings mock to return the property's default value for any given property. + * + * @param settings the settings mock + */ + @SuppressWarnings("unchecked") + public static void returnDefaultsForAllProperties(Settings settings) { + given(settings.getProperty(any(Property.class))) + .willAnswer(invocation -> ((Property) invocation.getArgument(0)).getDefaultValue()); + } } diff --git a/src/test/java/fr/xephi/authme/datasource/AbstractSqlDataSourceResourceClosingTest.java b/src/test/java/fr/xephi/authme/datasource/AbstractSqlDataSourceResourceClosingTest.java index ea92e4a0c..edb6d4f68 100644 --- a/src/test/java/fr/xephi/authme/datasource/AbstractSqlDataSourceResourceClosingTest.java +++ b/src/test/java/fr/xephi/authme/datasource/AbstractSqlDataSourceResourceClosingTest.java @@ -1,13 +1,10 @@ package fr.xephi.authme.datasource; -import ch.jalu.configme.properties.Property; import com.google.common.collect.ImmutableSet; import fr.xephi.authme.TestHelper; import fr.xephi.authme.settings.Settings; import org.junit.BeforeClass; import org.junit.runners.Parameterized; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import java.lang.reflect.Method; import java.sql.Connection; @@ -16,8 +13,6 @@ import java.util.Collection; import java.util.List; import java.util.Set; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; /** @@ -37,12 +32,7 @@ public abstract class AbstractSqlDataSourceResourceClosingTest extends AbstractR @BeforeClass public static void initializeSettings() { settings = mock(Settings.class); - given(settings.getProperty(any(Property.class))).willAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) { - return ((Property) invocation.getArguments()[0]).getDefaultValue(); - } - }); + TestHelper.returnDefaultsForAllProperties(settings); TestHelper.setupLogger(); } diff --git a/src/test/java/fr/xephi/authme/datasource/MySqlIntegrationTest.java b/src/test/java/fr/xephi/authme/datasource/MySqlIntegrationTest.java index 62e1ac3ec..9da39a3a6 100644 --- a/src/test/java/fr/xephi/authme/datasource/MySqlIntegrationTest.java +++ b/src/test/java/fr/xephi/authme/datasource/MySqlIntegrationTest.java @@ -11,8 +11,6 @@ import fr.xephi.authme.settings.properties.DatabaseSettings; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import java.io.IOException; import java.nio.file.Files; @@ -42,19 +40,13 @@ public class MySqlIntegrationTest extends AbstractDataSourceIntegrationTest { /** * Set up the settings mock to return specific values for database settings and load {@link #sqlInitialize}. */ - @SuppressWarnings({ "unchecked", "rawtypes" }) @BeforeClass public static void initializeSettings() throws IOException, ClassNotFoundException { // Check that we have an H2 driver Class.forName("org.h2.jdbcx.JdbcDataSource"); settings = mock(Settings.class); - when(settings.getProperty(any(Property.class))).thenAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - return ((Property) invocation.getArguments()[0]).getDefaultValue(); - } - }); + TestHelper.returnDefaultsForAllProperties(settings); extensionsFactory = mock(MySqlExtensionsFactory.class); when(extensionsFactory.buildExtension(any(Columns.class))).thenReturn(mock(MySqlExtension.class)); set(DatabaseSettings.MYSQL_DATABASE, "h2_test"); diff --git a/src/test/java/fr/xephi/authme/datasource/SQLiteIntegrationTest.java b/src/test/java/fr/xephi/authme/datasource/SQLiteIntegrationTest.java index 9ef0c2fa8..6153002a0 100644 --- a/src/test/java/fr/xephi/authme/datasource/SQLiteIntegrationTest.java +++ b/src/test/java/fr/xephi/authme/datasource/SQLiteIntegrationTest.java @@ -9,8 +9,6 @@ import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import java.io.IOException; import java.nio.file.Files; @@ -22,7 +20,6 @@ import java.sql.Statement; import static org.hamcrest.Matchers.hasSize; import static org.junit.Assert.assertThat; -import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -41,19 +38,13 @@ public class SQLiteIntegrationTest extends AbstractDataSourceIntegrationTest { /** * Set up the settings mock to return specific values for database settings and load {@link #sqlInitialize}. */ - @SuppressWarnings({ "unchecked", "rawtypes" }) @BeforeClass public static void initializeSettings() throws IOException, ClassNotFoundException { // Check that we have an implementation for SQLite Class.forName("org.sqlite.JDBC"); settings = mock(Settings.class); - when(settings.getProperty(any(Property.class))).thenAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - return ((Property) invocation.getArguments()[0]).getDefaultValue(); - } - }); + TestHelper.returnDefaultsForAllProperties(settings); set(DatabaseSettings.MYSQL_DATABASE, "sqlite-test"); set(DatabaseSettings.MYSQL_TABLE, "authme"); TestHelper.setRealLogger(); diff --git a/src/test/java/fr/xephi/authme/datasource/mysqlextensions/AbstractMySqlExtensionResourceClosingTest.java b/src/test/java/fr/xephi/authme/datasource/mysqlextensions/AbstractMySqlExtensionResourceClosingTest.java index 6425e33b6..3089e8b99 100644 --- a/src/test/java/fr/xephi/authme/datasource/mysqlextensions/AbstractMySqlExtensionResourceClosingTest.java +++ b/src/test/java/fr/xephi/authme/datasource/mysqlextensions/AbstractMySqlExtensionResourceClosingTest.java @@ -1,13 +1,11 @@ package fr.xephi.authme.datasource.mysqlextensions; -import ch.jalu.configme.properties.Property; +import fr.xephi.authme.TestHelper; import fr.xephi.authme.datasource.AbstractResourceClosingTest; import fr.xephi.authme.datasource.Columns; import fr.xephi.authme.settings.Settings; import org.junit.BeforeClass; import org.junit.runners.Parameterized; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import java.lang.reflect.Method; import java.lang.reflect.Modifier; @@ -15,8 +13,6 @@ import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; /** @@ -34,12 +30,7 @@ public abstract class AbstractMySqlExtensionResourceClosingTest extends Abstract @BeforeClass public static void initSettings() { settings = mock(Settings.class); - given(settings.getProperty(any(Property.class))).willAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) { - return ((Property) invocation.getArguments()[0]).getDefaultValue(); - } - }); + TestHelper.returnDefaultsForAllProperties(settings); columns = new Columns(settings); } diff --git a/src/test/java/fr/xephi/authme/datasource/mysqlextensions/NoOpExtensionTest.java b/src/test/java/fr/xephi/authme/datasource/mysqlextensions/NoOpExtensionTest.java index ce7f4a204..95a5dfa88 100644 --- a/src/test/java/fr/xephi/authme/datasource/mysqlextensions/NoOpExtensionTest.java +++ b/src/test/java/fr/xephi/authme/datasource/mysqlextensions/NoOpExtensionTest.java @@ -1,6 +1,6 @@ package fr.xephi.authme.datasource.mysqlextensions; -import ch.jalu.configme.properties.Property; +import fr.xephi.authme.TestHelper; import fr.xephi.authme.data.auth.PlayerAuth; import fr.xephi.authme.datasource.Columns; import fr.xephi.authme.security.crypts.HashedPassword; @@ -13,8 +13,6 @@ import org.mockito.junit.MockitoJUnitRunner; import java.sql.Connection; import java.sql.SQLException; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verifyZeroInteractions; @@ -27,11 +25,9 @@ public class NoOpExtensionTest { private NoOpExtension extension; @Before - @SuppressWarnings("unchecked") public void createExtension() { Settings settings = mock(Settings.class); - given(settings.getProperty(any(Property.class))).willAnswer( - invocation -> ((Property) invocation.getArgument(0)).getDefaultValue()); + TestHelper.returnDefaultsForAllProperties(settings); Columns columns = new Columns(settings); extension = new NoOpExtension(settings, columns); } diff --git a/src/test/java/tools/docs/hashmethods/EncryptionMethodInfoGatherer.java b/src/test/java/tools/docs/hashmethods/EncryptionMethodInfoGatherer.java index 646d8d371..50b4f50d0 100644 --- a/src/test/java/tools/docs/hashmethods/EncryptionMethodInfoGatherer.java +++ b/src/test/java/tools/docs/hashmethods/EncryptionMethodInfoGatherer.java @@ -1,9 +1,9 @@ package tools.docs.hashmethods; -import ch.jalu.configme.properties.Property; import ch.jalu.injector.Injector; import ch.jalu.injector.InjectorBuilder; import com.google.common.collect.ImmutableSet; +import fr.xephi.authme.TestHelper; import fr.xephi.authme.security.HashAlgorithm; import fr.xephi.authme.security.crypts.EncryptionMethod; import fr.xephi.authme.security.crypts.HexSaltedMethod; @@ -11,8 +11,6 @@ import fr.xephi.authme.security.crypts.description.AsciiRestricted; import fr.xephi.authme.security.crypts.description.HasSalt; import fr.xephi.authme.security.crypts.description.Recommendation; import fr.xephi.authme.settings.Settings; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import java.lang.annotation.Annotation; import java.util.HashMap; @@ -20,9 +18,7 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; -import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; /** * Gathers information on {@link EncryptionMethod} implementations based on @@ -140,17 +136,9 @@ public class EncryptionMethodInfoGatherer { return key.cast(map.get(key)); } - @SuppressWarnings("unchecked") private static Injector createInitializer() { Settings settings = mock(Settings.class); - // Return the default value for any property - when(settings.getProperty(any(Property.class))).thenAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - Property property = (Property) invocation.getArguments()[0]; - return property.getDefaultValue(); - } - }); + TestHelper.returnDefaultsForAllProperties(settings); // By passing some bogus "package" to the constructor, the injector will throw if it needs to // instantiate any dependency other than what we provide.