Create TestHelper method to make Settings mock return defaults
This commit is contained in:
parent
efc06ef2a6
commit
027d0fc775
@ -1,6 +1,8 @@
|
|||||||
package fr.xephi.authme;
|
package fr.xephi.authme;
|
||||||
|
|
||||||
|
import ch.jalu.configme.properties.Property;
|
||||||
import fr.xephi.authme.service.BukkitService;
|
import fr.xephi.authme.service.BukkitService;
|
||||||
|
import fr.xephi.authme.settings.Settings;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.mockito.ArgumentCaptor;
|
import org.mockito.ArgumentCaptor;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
@ -18,6 +20,7 @@ import java.nio.file.Path;
|
|||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.ArgumentMatchers.anyLong;
|
import static org.mockito.ArgumentMatchers.anyLong;
|
||||||
import static org.mockito.BDDMockito.given;
|
import static org.mockito.BDDMockito.given;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
@ -206,4 +209,14 @@ public final class TestHelper {
|
|||||||
given(player.getAddress()).willReturn(inetSocketAddress);
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,13 +1,10 @@
|
|||||||
package fr.xephi.authme.datasource;
|
package fr.xephi.authme.datasource;
|
||||||
|
|
||||||
import ch.jalu.configme.properties.Property;
|
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import fr.xephi.authme.TestHelper;
|
import fr.xephi.authme.TestHelper;
|
||||||
import fr.xephi.authme.settings.Settings;
|
import fr.xephi.authme.settings.Settings;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.runners.Parameterized;
|
import org.junit.runners.Parameterized;
|
||||||
import org.mockito.invocation.InvocationOnMock;
|
|
||||||
import org.mockito.stubbing.Answer;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
@ -16,8 +13,6 @@ import java.util.Collection;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
|
||||||
import static org.mockito.BDDMockito.given;
|
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -37,12 +32,7 @@ public abstract class AbstractSqlDataSourceResourceClosingTest extends AbstractR
|
|||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void initializeSettings() {
|
public static void initializeSettings() {
|
||||||
settings = mock(Settings.class);
|
settings = mock(Settings.class);
|
||||||
given(settings.getProperty(any(Property.class))).willAnswer(new Answer() {
|
TestHelper.returnDefaultsForAllProperties(settings);
|
||||||
@Override
|
|
||||||
public Object answer(InvocationOnMock invocation) {
|
|
||||||
return ((Property<?>) invocation.getArguments()[0]).getDefaultValue();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
TestHelper.setupLogger();
|
TestHelper.setupLogger();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -11,8 +11,6 @@ import fr.xephi.authme.settings.properties.DatabaseSettings;
|
|||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.mockito.invocation.InvocationOnMock;
|
|
||||||
import org.mockito.stubbing.Answer;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
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}.
|
* Set up the settings mock to return specific values for database settings and load {@link #sqlInitialize}.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void initializeSettings() throws IOException, ClassNotFoundException {
|
public static void initializeSettings() throws IOException, ClassNotFoundException {
|
||||||
// Check that we have an H2 driver
|
// Check that we have an H2 driver
|
||||||
Class.forName("org.h2.jdbcx.JdbcDataSource");
|
Class.forName("org.h2.jdbcx.JdbcDataSource");
|
||||||
|
|
||||||
settings = mock(Settings.class);
|
settings = mock(Settings.class);
|
||||||
when(settings.getProperty(any(Property.class))).thenAnswer(new Answer() {
|
TestHelper.returnDefaultsForAllProperties(settings);
|
||||||
@Override
|
|
||||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
|
||||||
return ((Property) invocation.getArguments()[0]).getDefaultValue();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
extensionsFactory = mock(MySqlExtensionsFactory.class);
|
extensionsFactory = mock(MySqlExtensionsFactory.class);
|
||||||
when(extensionsFactory.buildExtension(any(Columns.class))).thenReturn(mock(MySqlExtension.class));
|
when(extensionsFactory.buildExtension(any(Columns.class))).thenReturn(mock(MySqlExtension.class));
|
||||||
set(DatabaseSettings.MYSQL_DATABASE, "h2_test");
|
set(DatabaseSettings.MYSQL_DATABASE, "h2_test");
|
||||||
|
|||||||
@ -9,8 +9,6 @@ import org.junit.After;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.invocation.InvocationOnMock;
|
|
||||||
import org.mockito.stubbing.Answer;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
@ -22,7 +20,6 @@ import java.sql.Statement;
|
|||||||
|
|
||||||
import static org.hamcrest.Matchers.hasSize;
|
import static org.hamcrest.Matchers.hasSize;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
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}.
|
* Set up the settings mock to return specific values for database settings and load {@link #sqlInitialize}.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void initializeSettings() throws IOException, ClassNotFoundException {
|
public static void initializeSettings() throws IOException, ClassNotFoundException {
|
||||||
// Check that we have an implementation for SQLite
|
// Check that we have an implementation for SQLite
|
||||||
Class.forName("org.sqlite.JDBC");
|
Class.forName("org.sqlite.JDBC");
|
||||||
|
|
||||||
settings = mock(Settings.class);
|
settings = mock(Settings.class);
|
||||||
when(settings.getProperty(any(Property.class))).thenAnswer(new Answer() {
|
TestHelper.returnDefaultsForAllProperties(settings);
|
||||||
@Override
|
|
||||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
|
||||||
return ((Property) invocation.getArguments()[0]).getDefaultValue();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
set(DatabaseSettings.MYSQL_DATABASE, "sqlite-test");
|
set(DatabaseSettings.MYSQL_DATABASE, "sqlite-test");
|
||||||
set(DatabaseSettings.MYSQL_TABLE, "authme");
|
set(DatabaseSettings.MYSQL_TABLE, "authme");
|
||||||
TestHelper.setRealLogger();
|
TestHelper.setRealLogger();
|
||||||
|
|||||||
@ -1,13 +1,11 @@
|
|||||||
package fr.xephi.authme.datasource.mysqlextensions;
|
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.AbstractResourceClosingTest;
|
||||||
import fr.xephi.authme.datasource.Columns;
|
import fr.xephi.authme.datasource.Columns;
|
||||||
import fr.xephi.authme.settings.Settings;
|
import fr.xephi.authme.settings.Settings;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.runners.Parameterized;
|
import org.junit.runners.Parameterized;
|
||||||
import org.mockito.invocation.InvocationOnMock;
|
|
||||||
import org.mockito.stubbing.Answer;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
@ -15,8 +13,6 @@ import java.util.Arrays;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
|
||||||
import static org.mockito.BDDMockito.given;
|
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -34,12 +30,7 @@ public abstract class AbstractMySqlExtensionResourceClosingTest extends Abstract
|
|||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void initSettings() {
|
public static void initSettings() {
|
||||||
settings = mock(Settings.class);
|
settings = mock(Settings.class);
|
||||||
given(settings.getProperty(any(Property.class))).willAnswer(new Answer() {
|
TestHelper.returnDefaultsForAllProperties(settings);
|
||||||
@Override
|
|
||||||
public Object answer(InvocationOnMock invocation) {
|
|
||||||
return ((Property<?>) invocation.getArguments()[0]).getDefaultValue();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
columns = new Columns(settings);
|
columns = new Columns(settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
package fr.xephi.authme.datasource.mysqlextensions;
|
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.data.auth.PlayerAuth;
|
||||||
import fr.xephi.authme.datasource.Columns;
|
import fr.xephi.authme.datasource.Columns;
|
||||||
import fr.xephi.authme.security.crypts.HashedPassword;
|
import fr.xephi.authme.security.crypts.HashedPassword;
|
||||||
@ -13,8 +13,6 @@ import org.mockito.junit.MockitoJUnitRunner;
|
|||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
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.mock;
|
||||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||||
|
|
||||||
@ -27,11 +25,9 @@ public class NoOpExtensionTest {
|
|||||||
private NoOpExtension extension;
|
private NoOpExtension extension;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public void createExtension() {
|
public void createExtension() {
|
||||||
Settings settings = mock(Settings.class);
|
Settings settings = mock(Settings.class);
|
||||||
given(settings.getProperty(any(Property.class))).willAnswer(
|
TestHelper.returnDefaultsForAllProperties(settings);
|
||||||
invocation -> ((Property<?>) invocation.getArgument(0)).getDefaultValue());
|
|
||||||
Columns columns = new Columns(settings);
|
Columns columns = new Columns(settings);
|
||||||
extension = new NoOpExtension(settings, columns);
|
extension = new NoOpExtension(settings, columns);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
package tools.docs.hashmethods;
|
package tools.docs.hashmethods;
|
||||||
|
|
||||||
import ch.jalu.configme.properties.Property;
|
|
||||||
import ch.jalu.injector.Injector;
|
import ch.jalu.injector.Injector;
|
||||||
import ch.jalu.injector.InjectorBuilder;
|
import ch.jalu.injector.InjectorBuilder;
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
|
import fr.xephi.authme.TestHelper;
|
||||||
import fr.xephi.authme.security.HashAlgorithm;
|
import fr.xephi.authme.security.HashAlgorithm;
|
||||||
import fr.xephi.authme.security.crypts.EncryptionMethod;
|
import fr.xephi.authme.security.crypts.EncryptionMethod;
|
||||||
import fr.xephi.authme.security.crypts.HexSaltedMethod;
|
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.HasSalt;
|
||||||
import fr.xephi.authme.security.crypts.description.Recommendation;
|
import fr.xephi.authme.security.crypts.description.Recommendation;
|
||||||
import fr.xephi.authme.settings.Settings;
|
import fr.xephi.authme.settings.Settings;
|
||||||
import org.mockito.invocation.InvocationOnMock;
|
|
||||||
import org.mockito.stubbing.Answer;
|
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -20,9 +18,7 @@ import java.util.LinkedHashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gathers information on {@link EncryptionMethod} implementations based on
|
* Gathers information on {@link EncryptionMethod} implementations based on
|
||||||
@ -140,17 +136,9 @@ public class EncryptionMethodInfoGatherer {
|
|||||||
return key.cast(map.get(key));
|
return key.cast(map.get(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
private static Injector createInitializer() {
|
private static Injector createInitializer() {
|
||||||
Settings settings = mock(Settings.class);
|
Settings settings = mock(Settings.class);
|
||||||
// Return the default value for any property
|
TestHelper.returnDefaultsForAllProperties(settings);
|
||||||
when(settings.getProperty(any(Property.class))).thenAnswer(new Answer<Object>() {
|
|
||||||
@Override
|
|
||||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
|
||||||
Property<?> property = (Property<?>) invocation.getArguments()[0];
|
|
||||||
return property.getDefaultValue();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// By passing some bogus "package" to the constructor, the injector will throw if it needs to
|
// By passing some bogus "package" to the constructor, the injector will throw if it needs to
|
||||||
// instantiate any dependency other than what we provide.
|
// instantiate any dependency other than what we provide.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user