Search in sources :

Example 6 with DataSource

use of fr.xephi.authme.datasource.DataSource in project AuthMeReloaded by AuthMe.

the class DebugSectionUtils method unwrapSourceFromCacheDataSource.

/**
 * Unwraps the "cache data source" and returns the underlying source. Returns the
 * same as the input argument otherwise.
 *
 * @param dataSource the data source to unwrap if applicable
 * @return the non-cache data source
 */
static DataSource unwrapSourceFromCacheDataSource(DataSource dataSource) {
    if (dataSource instanceof CacheDataSource) {
        try {
            Field source = CacheDataSource.class.getDeclaredField("source");
            source.setAccessible(true);
            return (DataSource) source.get(dataSource);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            logger.logException("Could not get source of CacheDataSource:", e);
            return null;
        }
    }
    return dataSource;
}
Also used : Field(java.lang.reflect.Field) CacheDataSource(fr.xephi.authme.datasource.CacheDataSource) CacheDataSource(fr.xephi.authme.datasource.CacheDataSource) DataSource(fr.xephi.authme.datasource.DataSource)

Example 7 with DataSource

use of fr.xephi.authme.datasource.DataSource in project AuthMeReloaded by AuthMe.

the class DebugSectionUtilsTest method shouldReturnSameDataSourceInstance.

@Test
public void shouldReturnSameDataSourceInstance() {
    // given
    DataSource dataSource = mock(DataSource.class);
    // when
    DataSource result = DebugSectionUtils.unwrapSourceFromCacheDataSource(dataSource);
    // then
    assertThat(result, equalTo(dataSource));
}
Also used : CacheDataSource(fr.xephi.authme.datasource.CacheDataSource) DataSource(fr.xephi.authme.datasource.DataSource) Test(org.junit.Test)

Example 8 with DataSource

use of fr.xephi.authme.datasource.DataSource in project AuthMeReloaded by AuthMe.

the class AbstractDataSourceConverterTest method shouldThrowForDestinationTypeMismatch.

@Test
public void shouldThrowForDestinationTypeMismatch() {
    // given
    DataSource destination = mock(DataSource.class);
    given(destination.getType()).willReturn(DataSourceType.MYSQL);
    DataSourceType destinationType = DataSourceType.SQLITE;
    DataSource source = mock(DataSource.class);
    Converter converter = new DataSourceConverterTestImpl<>(source, destination, destinationType);
    CommandSender sender = mock(CommandSender.class);
    // when
    converter.execute(sender);
    // then
    verify(sender).sendMessage(argThat(containsString("Please configure your connection to SQLITE")));
    verify(destination, only()).getType();
    verifyNoInteractions(source);
}
Also used : DataSourceType(fr.xephi.authme.datasource.DataSourceType) CommandSender(org.bukkit.command.CommandSender) DataSource(fr.xephi.authme.datasource.DataSource) Test(org.junit.Test)

Example 9 with DataSource

use of fr.xephi.authme.datasource.DataSource in project AuthMeReloaded by AuthMe.

the class AbstractDataSourceConverterTest method shouldHandleSourceThrowingException.

@Test
public void shouldHandleSourceThrowingException() {
    // given
    DataSource source = mock(DataSource.class);
    DataSource destination = mock(DataSource.class);
    DataSourceType destinationType = DataSourceType.SQLITE;
    given(destination.getType()).willReturn(destinationType);
    DataSourceConverterTestImpl<DataSource> converter = Mockito.spy(new DataSourceConverterTestImpl<>(source, destination, destinationType));
    doThrow(IllegalStateException.class).when(converter).getSource();
    CommandSender sender = mock(CommandSender.class);
    // when
    converter.execute(sender);
    // then
    verify(sender).sendMessage("The data source to convert from could not be initialized");
    verify(destination, only()).getType();
    verifyNoInteractions(source);
}
Also used : DataSourceType(fr.xephi.authme.datasource.DataSourceType) CommandSender(org.bukkit.command.CommandSender) DataSource(fr.xephi.authme.datasource.DataSource) Test(org.junit.Test)

Example 10 with DataSource

use of fr.xephi.authme.datasource.DataSource in project AuthMeReloaded by AuthMe.

the class AbstractDataSourceConverterTest method shouldConvertAndSkipExistingPlayers.

@Test
public void shouldConvertAndSkipExistingPlayers() {
    // given
    DataSource source = mock(DataSource.class);
    DataSource destination = mock(DataSource.class);
    DataSourceType destinationType = DataSourceType.MYSQL;
    given(destination.getType()).willReturn(destinationType);
    List<PlayerAuth> auths = Arrays.asList(mockAuthWithName("Steven"), mockAuthWithName("bobby"), mockAuthWithName("Jack"));
    given(source.getAllAuths()).willReturn(auths);
    given(destination.isAuthAvailable(auths.get(0).getNickname())).willReturn(true);
    Converter converter = new DataSourceConverterTestImpl<>(source, destination, destinationType);
    CommandSender sender = mock(CommandSender.class);
    // when
    converter.execute(sender);
    // then
    verify(destination).getType();
    verify(destination, times(3)).isAuthAvailable(anyString());
    verify(destination, times(2)).saveAuth(any(PlayerAuth.class));
    verify(destination, times(2)).updateSession(any(PlayerAuth.class));
    verify(destination, times(2)).updateQuitLoc(any(PlayerAuth.class));
    verifyNoMoreInteractions(destination);
    verify(sender).sendMessage(argThat(containsString(auths.get(0).getNickname())));
    verify(sender).sendMessage(argThat(containsString("successfully converted")));
}
Also used : DataSourceType(fr.xephi.authme.datasource.DataSourceType) CommandSender(org.bukkit.command.CommandSender) PlayerAuth(fr.xephi.authme.data.auth.PlayerAuth) DataSource(fr.xephi.authme.datasource.DataSource) Test(org.junit.Test)

Aggregations

DataSource (fr.xephi.authme.datasource.DataSource)10 Test (org.junit.Test)8 CacheDataSource (fr.xephi.authme.datasource.CacheDataSource)5 DataSourceType (fr.xephi.authme.datasource.DataSourceType)4 PlayerAuth (fr.xephi.authme.data.auth.PlayerAuth)3 PlayerCache (fr.xephi.authme.data.auth.PlayerCache)3 CommandSender (org.bukkit.command.CommandSender)3 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 TestHelper (fr.xephi.authme.TestHelper)1 LimboService (fr.xephi.authme.data.limbo.LimboService)1 MySQL (fr.xephi.authme.datasource.MySQL)1 PostgreSqlDataSource (fr.xephi.authme.datasource.PostgreSqlDataSource)1 SQLite (fr.xephi.authme.datasource.SQLite)1 AuthMeAsyncPreLoginEvent (fr.xephi.authme.events.AuthMeAsyncPreLoginEvent)1 MessageKey (fr.xephi.authme.message.MessageKey)1 PlayerStatePermission (fr.xephi.authme.permission.PlayerStatePermission)1 BukkitService (fr.xephi.authme.service.BukkitService)1 CommonService (fr.xephi.authme.service.CommonService)1 DatabaseSettings (fr.xephi.authme.settings.properties.DatabaseSettings)1 HooksSettings (fr.xephi.authme.settings.properties.HooksSettings)1