#792 Add missing tests / fix CodeClimate issues

This commit is contained in:
ljacqu
2017-10-20 17:49:30 +02:00
parent 90073ef95d
commit a425eacf2d
7 changed files with 165 additions and 33 deletions
@@ -21,9 +21,9 @@ public class MySqlDefaultChangerColumnsTest {
// when / then
for (MySqlDefaultChanger.Columns col : MySqlDefaultChanger.Columns.values()) {
if (!properties.add(col.columnName.getPath())) {
if (!properties.add(col.getColumnNameProperty().getPath())) {
fail("Column '" + col + "' has a column name property path that was already encountered: "
+ col.columnName.getPath());
+ col.getColumnNameProperty().getPath());
}
}
}
@@ -44,8 +44,8 @@ public class MySqlDefaultChangerColumnsTest {
private void verifyHasCorrespondingColumnDefinitions(MySqlDefaultChanger.Columns column) {
// given / when
String nullable = column.nullableDefinition;
String notNull = column.notNullDefinition;
String nullable = column.getNullableDefinition();
String notNull = column.getNotNullDefinition();
// then
String expectedNotNull = nullable + " NOT NULL DEFAULT ";
@@ -56,8 +56,8 @@ public class MySqlDefaultChangerColumnsTest {
private void verifyHasSameDefaultValueInNotNullDefinition(MySqlDefaultChanger.Columns column) {
// given / when
String notNull = column.notNullDefinition;
Object defaultValue = column.defaultValue;
String notNull = column.getNotNullDefinition();
Object defaultValue = column.getDefaultValue();
// then
String defaultValueAsString = String.valueOf(defaultValue);
@@ -1,18 +1,29 @@
package fr.xephi.authme.command.executable.authme.debug;
import com.zaxxer.hikari.HikariDataSource;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.CacheDataSource;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.MySQL;
import fr.xephi.authme.datasource.MySqlTestUtil;
import fr.xephi.authme.settings.Settings;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.sql.Connection;
import java.sql.SQLException;
import static org.hamcrest.Matchers.equalTo;
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 MySqlDefaultChanger}.
@@ -49,7 +60,54 @@ public class MySqlDefaultChangerTest {
assertThat(result, equalTo(source));
}
// TODO #792: Add more tests
@Test
public void shouldReturnMySqlConnection() throws SQLException {
// given
Settings settings = mock(Settings.class);
TestHelper.returnDefaultsForAllProperties(settings);
HikariDataSource dataSource = mock(HikariDataSource.class);
Connection connection = mock(Connection.class);
given(dataSource.getConnection()).willReturn(connection);
MySQL mySQL = MySqlTestUtil.createMySql(settings, dataSource);
MySqlDefaultChanger defaultChanger = createDefaultChanger(mySQL);
// when
Connection result = defaultChanger.getConnection(mySQL);
// then
assertThat(result, equalTo(connection));
verify(dataSource).getConnection();
}
@Test
public void shouldSetMySqlFieldOnInitialization() {
// given
MySQL mySql = mock(MySQL.class);
MySqlDefaultChanger defaultChanger = createDefaultChanger(mySql);
// when
defaultChanger.setMySqlField();
// then
assertThat(ReflectionTestUtils.getFieldValue(MySqlDefaultChanger.class, defaultChanger, "mySql"),
sameInstance(mySql));
}
@Test
public void shouldLeaveMySqlFieldToNullOnInitialization() {
// given
DataSource dataSource = mock(DataSource.class);
PlayerCache playerCache = mock(PlayerCache.class);
CacheDataSource cacheDataSource = new CacheDataSource(dataSource, playerCache);
MySqlDefaultChanger defaultChanger = createDefaultChanger(cacheDataSource);
// when
defaultChanger.setMySqlField();
// then
assertThat(ReflectionTestUtils.getFieldValue(MySqlDefaultChanger.class, defaultChanger, "mySql"),
nullValue());
}
private MySqlDefaultChanger createDefaultChanger(DataSource dataSource) {
MySqlDefaultChanger defaultChanger = new MySqlDefaultChanger();
@@ -4,8 +4,6 @@ import ch.jalu.configme.properties.Property;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.datasource.mysqlextensions.MySqlExtension;
import fr.xephi.authme.datasource.mysqlextensions.MySqlExtensionsFactory;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.DatabaseSettings;
import org.junit.After;
@@ -19,7 +17,6 @@ import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -30,8 +27,6 @@ public class MySqlIntegrationTest extends AbstractDataSourceIntegrationTest {
/** Mock of a settings instance. */
private static Settings settings;
/** Mock of extensions factory. */
private static MySqlExtensionsFactory extensionsFactory;
/** SQL statement to execute before running a test. */
private static String sqlInitialize;
/** Connection to the H2 test database. */
@@ -47,8 +42,6 @@ public class MySqlIntegrationTest extends AbstractDataSourceIntegrationTest {
settings = mock(Settings.class);
TestHelper.returnDefaultsForAllProperties(settings);
extensionsFactory = mock(MySqlExtensionsFactory.class);
when(extensionsFactory.buildExtension(any(Columns.class))).thenReturn(mock(MySqlExtension.class));
set(DatabaseSettings.MYSQL_DATABASE, "h2_test");
set(DatabaseSettings.MYSQL_TABLE, "authme");
TestHelper.setRealLogger();
@@ -83,7 +76,7 @@ public class MySqlIntegrationTest extends AbstractDataSourceIntegrationTest {
@Override
protected DataSource getDataSource(String saltColumn) {
when(settings.getProperty(DatabaseSettings.MYSQL_COL_SALT)).thenReturn(saltColumn);
return new MySQL(settings, hikariSource, extensionsFactory);
return MySqlTestUtil.createMySql(settings, hikariSource);
}
private static <T> void set(Property<T> property, T value) {
@@ -0,0 +1,30 @@
package fr.xephi.authme.datasource;
import com.zaxxer.hikari.HikariDataSource;
import fr.xephi.authme.datasource.mysqlextensions.MySqlExtension;
import fr.xephi.authme.datasource.mysqlextensions.MySqlExtensionsFactory;
import fr.xephi.authme.settings.Settings;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Test util for the MySQL data source.
*/
public final class MySqlTestUtil {
private MySqlTestUtil() {
}
public static MySQL createMySql(Settings settings, HikariDataSource hikariDataSource) {
MySqlExtensionsFactory extensionsFactory = mock(MySqlExtensionsFactory.class);
given(extensionsFactory.buildExtension(any())).willReturn(mock(MySqlExtension.class));
return createMySql(settings, hikariDataSource, extensionsFactory);
}
public static MySQL createMySql(Settings settings, HikariDataSource hikariDataSource,
MySqlExtensionsFactory extensionsFactory) {
return new MySQL(settings, hikariDataSource, extensionsFactory);
}
}
@@ -113,7 +113,7 @@ public class SessionServiceTest {
}
@Test
public void shouldRefuseSessionForAuthWithZeroLastLoginTimestamp() {
public void shouldRefuseSessionForAuthWithNullLastLoginTimestamp() {
// given
String name = "Bobby";
String ip = "127.3.12.15";
@@ -122,7 +122,7 @@ public class SessionServiceTest {
given(dataSource.hasSession(name)).willReturn(true);
PlayerAuth auth = PlayerAuth.builder()
.name(name)
.lastLogin(0L)
.lastLogin(null)
.lastIp(ip).build();
given(dataSource.getAuth(name)).willReturn(auth);