Package cleanup
- authme.cache to authme.data - Rename PlayerData to LimboPlayer to match with LimboCache - Move authme.converter to authme.datasource.converter - Split output package into output and message
This commit is contained in:
+126
@@ -0,0 +1,126 @@
|
||||
package fr.xephi.authme.datasource.converter;
|
||||
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.datasource.DataSourceType;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Matchers.argThat;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.only;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
|
||||
/**
|
||||
* Test for {@link AbstractDataSourceConverter}.
|
||||
*/
|
||||
public class AbstractDataSourceConverterTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void initLogger() {
|
||||
TestHelper.setupLogger();
|
||||
}
|
||||
|
||||
@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();
|
||||
verifyZeroInteractions(source);
|
||||
}
|
||||
|
||||
@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();
|
||||
verifyZeroInteractions(source);
|
||||
}
|
||||
|
||||
@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)).updateQuitLoc(any(PlayerAuth.class));
|
||||
verifyNoMoreInteractions(destination);
|
||||
verify(sender).sendMessage(argThat(containsString(auths.get(0).getNickname())));
|
||||
verify(sender).sendMessage(argThat(containsString("successfully converted")));
|
||||
}
|
||||
|
||||
private static PlayerAuth mockAuthWithName(String name) {
|
||||
PlayerAuth auth = mock(PlayerAuth.class);
|
||||
given(auth.getNickname()).willReturn(name);
|
||||
return auth;
|
||||
}
|
||||
|
||||
private static class DataSourceConverterTestImpl<S extends DataSource> extends AbstractDataSourceConverter<S> {
|
||||
private final S source;
|
||||
|
||||
DataSourceConverterTestImpl(S source, DataSource destination, DataSourceType destinationType) {
|
||||
super(destination, destinationType);
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected S getSource() {
|
||||
return source;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package fr.xephi.authme.datasource.converter;
|
||||
|
||||
import ch.jalu.injector.testing.DelayedInjectionRunner;
|
||||
import ch.jalu.injector.testing.InjectDelayed;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.initialization.DataFolder;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.ConverterSettings;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import static fr.xephi.authme.AuthMeMatchers.equalToHash;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Matchers.argThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
|
||||
/**
|
||||
* Test for {@link CrazyLoginConverter}.
|
||||
*/
|
||||
@RunWith(DelayedInjectionRunner.class)
|
||||
public class CrazyLoginConverterTest {
|
||||
|
||||
@InjectDelayed
|
||||
private CrazyLoginConverter crazyLoginConverter;
|
||||
|
||||
@Mock
|
||||
private DataSource dataSource;
|
||||
|
||||
@Mock
|
||||
private Settings settings;
|
||||
|
||||
@DataFolder
|
||||
private File dataFolder = TestHelper.getJarFile(TestHelper.PROJECT_ROOT + "/datasource/converter/");
|
||||
|
||||
@BeforeClass
|
||||
public static void initializeLogger() {
|
||||
TestHelper.setupLogger();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldImportUsers() {
|
||||
// given
|
||||
given(settings.getProperty(ConverterSettings.CRAZYLOGIN_FILE_NAME)).willReturn("crazylogin.db");
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
|
||||
// when
|
||||
crazyLoginConverter.execute(sender);
|
||||
|
||||
// then
|
||||
ArgumentCaptor<PlayerAuth> authCaptor = ArgumentCaptor.forClass(PlayerAuth.class);
|
||||
verify(dataSource, times(2)).saveAuth(authCaptor.capture());
|
||||
List<PlayerAuth> savedAuths = authCaptor.getAllValues();
|
||||
assertNameAndRealName(savedAuths.get(0), "qotato", "qotaTo");
|
||||
assertThat(savedAuths.get(0).getPassword(), equalToHash("8267663ab198a96437b9f455429a2c1b6c943111613c217bf2703c14d08a309d34e510ddb5549507b1500759dbcf9d4a99bc765ff37b32bd31adbb1e92e74ac5"));
|
||||
assertNameAndRealName(savedAuths.get(1), "bobby", "Bobby");
|
||||
assertThat(savedAuths.get(1).getPassword(), equalToHash("ad50dbc841e6321210530801f5219a5ffb4c7c41f11878d465374a4b8db2965c50f69b6098918a58e4adea312e3633c7724b15e24a217009e6fa2b3c299d55f2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldStopForNonExistentFile() {
|
||||
// given
|
||||
given(settings.getProperty(ConverterSettings.CRAZYLOGIN_FILE_NAME)).willReturn("invalid-file");
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
|
||||
// when
|
||||
crazyLoginConverter.execute(sender);
|
||||
|
||||
// then
|
||||
verifyZeroInteractions(dataSource);
|
||||
verify(sender).sendMessage(argThat(containsString("file not found")));
|
||||
}
|
||||
|
||||
private static void assertNameAndRealName(PlayerAuth auth, String name, String realName) {
|
||||
assertThat(auth.getNickname(), equalTo(name));
|
||||
assertThat(auth.getRealName(), equalTo(realName));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package fr.xephi.authme.datasource.converter;
|
||||
|
||||
import com.google.common.io.Files;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.datasource.DataSourceType;
|
||||
import fr.xephi.authme.datasource.FlatFile;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static fr.xephi.authme.AuthMeMatchers.hasAuthBasicData;
|
||||
import static fr.xephi.authme.AuthMeMatchers.hasAuthLocation;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Test for {@link ForceFlatToSqlite}.
|
||||
*/
|
||||
public class ForceFlatToSqliteTest {
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
private FlatFile flatFile;
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() {
|
||||
TestHelper.setupLogger();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void copyFile() throws IOException {
|
||||
File source = TestHelper.getJarFile(TestHelper.PROJECT_ROOT + "datasource/flatfile-test.txt");
|
||||
File destination = temporaryFolder.newFile();
|
||||
Files.copy(source, destination);
|
||||
flatFile = new FlatFile(destination);
|
||||
}
|
||||
|
||||
@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", "Bobby", "your@email.com", "123.45.67.89")));
|
||||
assertThat(auths, hasItem(hasAuthLocation(1.05, 2.1, 4.2, "world")));
|
||||
assertThat(auths, hasItem(hasAuthBasicData("user", "user", "user@example.org", "34.56.78.90")));
|
||||
assertThat(auths, hasItem(hasAuthLocation(124.1, 76.3, -127.8, "nether")));
|
||||
assertThat(auths, hasItem(hasAuthBasicData("eightfields", "eightFields", "your@email.com", "6.6.6.66")));
|
||||
assertThat(auths, hasItem(hasAuthLocation(8.8, 17.6, 26.4, "eightworld")));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user