Search in sources :

Example 1 with DataSource

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

the class MySqlDefaultChangerTest method shouldLeaveMySqlFieldToNullOnInitialization.

@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());
}
Also used : CacheDataSource(fr.xephi.authme.datasource.CacheDataSource) PlayerCache(fr.xephi.authme.data.auth.PlayerCache) CacheDataSource(fr.xephi.authme.datasource.CacheDataSource) DataSource(fr.xephi.authme.datasource.DataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) Test(org.junit.Test)

Example 2 with DataSource

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

the class AsynchronousLoginTest method shouldNotForceLoginForCanceledEvent.

@Test
public void shouldNotForceLoginForCanceledEvent() {
    // given
    String name = "oscar";
    String ip = "1.1.1.245";
    Player player = mockPlayer(name);
    TestHelper.mockPlayerIp(player, ip);
    given(playerCache.isAuthenticated(name)).willReturn(false);
    PlayerAuth auth = PlayerAuth.builder().name(name).build();
    given(dataSource.getAuth(name)).willReturn(auth);
    given(commonService.getProperty(DatabaseSettings.MYSQL_COL_GROUP)).willReturn("");
    given(commonService.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(true);
    doReturn(false).when(asynchronousLogin).hasReachedMaxLoggedInPlayersForIp(any(Player.class), anyString());
    doAnswer((Answer<Void>) invocation -> {
        ((AuthMeAsyncPreLoginEvent) invocation.getArgument(0)).setCanLogin(false);
        return null;
    }).when(bukkitService).callEvent(any(AuthMeAsyncPreLoginEvent.class));
    // when
    asynchronousLogin.forceLogin(player);
    // then
    verify(playerCache, only()).isAuthenticated(name);
    verify(dataSource, only()).getAuth(name);
    verify(asynchronousLogin).hasReachedMaxLoggedInPlayersForIp(player, ip);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Player(org.bukkit.entity.Player) RestrictionSettings(fr.xephi.authme.settings.properties.RestrictionSettings) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) Assert.assertThat(org.junit.Assert.assertThat) Answer(org.mockito.stubbing.Answer) CommonService(fr.xephi.authme.service.CommonService) PlayerAuth(fr.xephi.authme.data.auth.PlayerAuth) BDDMockito.given(org.mockito.BDDMockito.given) VerificationModeFactory.only(org.mockito.internal.verification.VerificationModeFactory.only) DataSource(fr.xephi.authme.datasource.DataSource) Spy(org.mockito.Spy) Mockito.doAnswer(org.mockito.Mockito.doAnswer) TestHelper(fr.xephi.authme.TestHelper) PluginSettings(fr.xephi.authme.settings.properties.PluginSettings) Mockito.doReturn(org.mockito.Mockito.doReturn) PlayerStatePermission(fr.xephi.authme.permission.PlayerStatePermission) InjectMocks(org.mockito.InjectMocks) LimboService(fr.xephi.authme.data.limbo.LimboService) BukkitService(fr.xephi.authme.service.BukkitService) DatabaseSettings(fr.xephi.authme.settings.properties.DatabaseSettings) Test(org.junit.Test) MessageKey(fr.xephi.authme.message.MessageKey) Mockito.verify(org.mockito.Mockito.verify) List(java.util.List) AuthMeAsyncPreLoginEvent(fr.xephi.authme.events.AuthMeAsyncPreLoginEvent) Matchers.equalTo(org.hamcrest.Matchers.equalTo) HooksSettings(fr.xephi.authme.settings.properties.HooksSettings) PlayerCache(fr.xephi.authme.data.auth.PlayerCache) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Player(org.bukkit.entity.Player) AuthMeAsyncPreLoginEvent(fr.xephi.authme.events.AuthMeAsyncPreLoginEvent) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PlayerAuth(fr.xephi.authme.data.auth.PlayerAuth) Test(org.junit.Test)

Example 3 with DataSource

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

the class DataSourceProvider method createDataSource.

/**
 * Sets up the data source.
 *
 * @return the constructed data source
 * @throws SQLException when initialization of a SQL data source failed
 */
private DataSource createDataSource() throws SQLException {
    DataSourceType dataSourceType = settings.getProperty(DatabaseSettings.BACKEND);
    DataSource dataSource;
    switch(dataSourceType) {
        case MYSQL:
            dataSource = new MySQL(settings, mySqlExtensionsFactory);
            break;
        case POSTGRESQL:
            dataSource = new PostgreSqlDataSource(settings, mySqlExtensionsFactory);
            break;
        case SQLITE:
            dataSource = new SQLite(settings, dataFolder);
            break;
        default:
            throw new UnsupportedOperationException("Unknown data source type '" + dataSourceType + "'");
    }
    if (settings.getProperty(DatabaseSettings.USE_CACHING)) {
        dataSource = new CacheDataSource(dataSource, playerCache);
    }
    if (DataSourceType.SQLITE.equals(dataSourceType)) {
        checkDataSourceSize(dataSource);
    }
    return dataSource;
}
Also used : CacheDataSource(fr.xephi.authme.datasource.CacheDataSource) DataSourceType(fr.xephi.authme.datasource.DataSourceType) MySQL(fr.xephi.authme.datasource.MySQL) SQLite(fr.xephi.authme.datasource.SQLite) PostgreSqlDataSource(fr.xephi.authme.datasource.PostgreSqlDataSource) CacheDataSource(fr.xephi.authme.datasource.CacheDataSource) DataSource(fr.xephi.authme.datasource.DataSource) PostgreSqlDataSource(fr.xephi.authme.datasource.PostgreSqlDataSource)

Example 4 with DataSource

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

the class DebugSectionUtilsTest method shouldUnwrapCacheDataSource.

@Test
public void shouldUnwrapCacheDataSource() {
    // given
    DataSource source = mock(DataSource.class);
    PlayerCache playerCache = mock(PlayerCache.class);
    CacheDataSource cacheDataSource = new CacheDataSource(source, playerCache);
    // when
    DataSource result = DebugSectionUtils.unwrapSourceFromCacheDataSource(cacheDataSource);
    // then
    assertThat(result, equalTo(source));
}
Also used : CacheDataSource(fr.xephi.authme.datasource.CacheDataSource) PlayerCache(fr.xephi.authme.data.auth.PlayerCache) CacheDataSource(fr.xephi.authme.datasource.CacheDataSource) DataSource(fr.xephi.authme.datasource.DataSource) Test(org.junit.Test)

Example 5 with DataSource

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

the class ForceFlatToSqliteTest method shouldConvertToSqlite.

@Test
public void shouldConvertToSqlite() {
    // given
    DataSource dataSource = mock(DataSource.class);
    given(dataSource.getType()).willReturn(DataSourceType.MYSQL);
    ForceFlatToSqlite converter = new ForceFlatToSqlite(flatFile, dataSource);
    // when
    converter.execute(null);
    // then
    ArgumentCaptor<PlayerAuth> authCaptor = ArgumentCaptor.forClass(PlayerAuth.class);
    verify(dataSource, times(7)).saveAuth(authCaptor.capture());
    List<PlayerAuth> auths = authCaptor.getAllValues();
    assertThat(auths, hasItem(hasAuthBasicData("bobby", "Player", "your@email.com", "123.45.67.89")));
    assertThat(auths, hasItem(hasAuthLocation(1.05, 2.1, 4.2, "world", 0, 0)));
    assertThat(auths, hasItem(hasAuthBasicData("user", "Player", "user@example.org", "34.56.78.90")));
    assertThat(auths, hasItem(hasAuthLocation(124.1, 76.3, -127.8, "nether", 0, 0)));
    assertThat(auths, hasItem(hasAuthBasicData("eightfields", "Player", "your@email.com", "6.6.6.66")));
    assertThat(auths, hasItem(hasAuthLocation(8.8, 17.6, 26.4, "eightworld", 0, 0)));
}
Also used : 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