From 650a97647afd592fe429c02376ecec41a3a56474 Mon Sep 17 00:00:00 2001 From: Gabriele C Date: Fri, 30 Nov 2018 15:29:30 +0100 Subject: [PATCH] Drop FlatFile storage converter It's time... --- .../authme/datasource/DataSourceType.java | 5 +- .../fr/xephi/authme/datasource/FlatFile.java | 437 ------------------ .../converter/ForceFlatToSqlite.java | 36 -- .../initialization/DataSourceProvider.java | 36 -- .../xephi/authme/service/BackupService.java | 2 - .../datasource/FlatFileIntegrationTest.java | 100 ---- .../converter/ForceFlatToSqliteTest.java | 74 --- 7 files changed, 1 insertion(+), 689 deletions(-) delete mode 100644 src/main/java/fr/xephi/authme/datasource/FlatFile.java delete mode 100644 src/main/java/fr/xephi/authme/datasource/converter/ForceFlatToSqlite.java delete mode 100644 src/test/java/fr/xephi/authme/datasource/FlatFileIntegrationTest.java delete mode 100644 src/test/java/fr/xephi/authme/datasource/converter/ForceFlatToSqliteTest.java diff --git a/src/main/java/fr/xephi/authme/datasource/DataSourceType.java b/src/main/java/fr/xephi/authme/datasource/DataSourceType.java index 14d3d23b5..d9969ac95 100644 --- a/src/main/java/fr/xephi/authme/datasource/DataSourceType.java +++ b/src/main/java/fr/xephi/authme/datasource/DataSourceType.java @@ -9,9 +9,6 @@ public enum DataSourceType { POSTGRESQL, - SQLITE, - - @Deprecated - FILE + SQLITE } diff --git a/src/main/java/fr/xephi/authme/datasource/FlatFile.java b/src/main/java/fr/xephi/authme/datasource/FlatFile.java deleted file mode 100644 index ab9bf85c1..000000000 --- a/src/main/java/fr/xephi/authme/datasource/FlatFile.java +++ /dev/null @@ -1,437 +0,0 @@ -package fr.xephi.authme.datasource; - -import ch.jalu.datasourcecolumns.data.DataSourceValue; -import fr.xephi.authme.ConsoleLogger; -import fr.xephi.authme.data.auth.PlayerAuth; -import fr.xephi.authme.security.crypts.HashedPassword; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Set; - -/** - * Deprecated flat file datasource. The only method guaranteed to work is {@link FlatFile#getAllAuths()} - * as to migrate the entries to {@link SQLite} when AuthMe starts. - */ -@Deprecated -public class FlatFile implements DataSource { - - /* - * file layout: - * - * PLAYERNAME:HASHSUM:IP:LOGININMILLIESECONDS:LASTPOSX:LASTPOSY:LASTPOSZ: - * LASTPOSWORLD:EMAIL - * - * Old but compatible: - * PLAYERNAME:HASHSUM:IP:LOGININMILLIESECONDS:LASTPOSX:LASTPOSY - * :LASTPOSZ:LASTPOSWORLD PLAYERNAME:HASHSUM:IP:LOGININMILLIESECONDS - * PLAYERNAME:HASHSUM:IP PLAYERNAME:HASHSUM - */ - private final File source; - - public FlatFile(File source) throws IOException { - this.source = source; - if (!source.exists() && !source.createNewFile()) { - throw new IOException("Could not create file '" + source.getPath() + "'"); - } - } - - @Override - public void reload() { - throw new UnsupportedOperationException("Flatfile no longer supported"); - } - - @Override - public synchronized boolean isAuthAvailable(String user) { - try (BufferedReader br = new BufferedReader(new FileReader(source))) { - String line; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - if (args.length > 1 && args[0].equalsIgnoreCase(user)) { - return true; - } - } - } catch (IOException ex) { - ConsoleLogger.warning(ex.getMessage()); - return false; - } - return false; - } - - @Override - public HashedPassword getPassword(String user) { - PlayerAuth auth = getAuth(user); - if (auth != null) { - return auth.getPassword(); - } - return null; - } - - @Override - public synchronized boolean saveAuth(PlayerAuth auth) { - if (isAuthAvailable(auth.getNickname())) { - return false; - } - try (BufferedWriter bw = new BufferedWriter(new FileWriter(source, true))) { - bw.write(auth.getNickname() + ":" + auth.getPassword().getHash() + ":" + auth.getLastIp() - + ":" + auth.getLastLogin() + ":" + auth.getQuitLocX() + ":" + auth.getQuitLocY() - + ":" + auth.getQuitLocZ() + ":" + auth.getWorld() + ":" + auth.getEmail() + "\n"); - } catch (IOException ex) { - ConsoleLogger.warning(ex.getMessage()); - return false; - } - return true; - } - - @Override - public synchronized boolean updatePassword(PlayerAuth auth) { - return updatePassword(auth.getNickname(), auth.getPassword()); - } - - @Override - // Note ljacqu 20151230: This does not persist the salt; it is not supported in flat file. - public boolean updatePassword(String user, HashedPassword password) { - user = user.toLowerCase(); - if (!isAuthAvailable(user)) { - return false; - } - PlayerAuth newAuth = null; - try (BufferedReader br = new BufferedReader(new FileReader(source))) { - String line; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - if (args[0].equals(user)) { - newAuth = buildAuthFromArray(args); - if (newAuth != null) { - newAuth.setPassword(password); - } - break; - } - } - } catch (IOException ex) { - ConsoleLogger.warning(ex.getMessage()); - return false; - } - if (newAuth != null) { - removeAuth(user); - saveAuth(newAuth); - } - return true; - } - - @Override - public boolean updateSession(PlayerAuth auth) { - if (!isAuthAvailable(auth.getNickname())) { - return false; - } - PlayerAuth newAuth = null; - try (BufferedReader br = new BufferedReader(new FileReader(source))) { - String line; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - if (args[0].equalsIgnoreCase(auth.getNickname())) { - newAuth = buildAuthFromArray(args); - if (newAuth != null) { - newAuth.setLastLogin(auth.getLastLogin()); - newAuth.setLastIp(auth.getLastIp()); - } - break; - } - } - } catch (IOException ex) { - ConsoleLogger.warning(ex.getMessage()); - return false; - } - if (newAuth != null) { - removeAuth(auth.getNickname()); - saveAuth(newAuth); - } - return true; - } - - @Override - public boolean updateQuitLoc(PlayerAuth auth) { - if (!isAuthAvailable(auth.getNickname())) { - return false; - } - PlayerAuth newAuth = null; - try (BufferedReader br = new BufferedReader(new FileReader(source))) { - String line; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - if (args[0].equalsIgnoreCase(auth.getNickname())) { - newAuth = buildAuthFromArray(args); - if (newAuth != null) { - newAuth.setQuitLocX(auth.getQuitLocX()); - newAuth.setQuitLocY(auth.getQuitLocY()); - newAuth.setQuitLocZ(auth.getQuitLocZ()); - newAuth.setWorld(auth.getWorld()); - newAuth.setEmail(auth.getEmail()); - } - break; - } - } - } catch (IOException ex) { - ConsoleLogger.warning(ex.getMessage()); - return false; - } - if (newAuth != null) { - removeAuth(auth.getNickname()); - saveAuth(newAuth); - } - return true; - } - - @Override - public Set getRecordsToPurge(long until) { - throw new UnsupportedOperationException("Flat file no longer supported"); - } - - @Override - public void purgeRecords(Collection toPurge) { - throw new UnsupportedOperationException("Flat file no longer supported"); - } - - @Override - public synchronized boolean removeAuth(String user) { - if (!isAuthAvailable(user)) { - return false; - } - ArrayList lines = new ArrayList<>(); - try (BufferedReader br = new BufferedReader(new FileReader(source))) { - - String line; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - if (args.length > 1 && !args[0].equals(user)) { - lines.add(line); - } - } - try (BufferedWriter bw = new BufferedWriter(new FileWriter(source))) { - for (String l : lines) { - bw.write(l + "\n"); - } - } - } catch (IOException ex) { - ConsoleLogger.warning(ex.getMessage()); - return false; - } - return true; - } - - @Override - public synchronized PlayerAuth getAuth(String user) { - try (BufferedReader br = new BufferedReader(new FileReader(source))) { - String line; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - if (args[0].equalsIgnoreCase(user)) { - return buildAuthFromArray(args); - } - } - } catch (IOException ex) { - ConsoleLogger.warning(ex.getMessage()); - return null; - } - return null; - } - - @Override - public void closeConnection() { - } - - @Override - public boolean updateEmail(PlayerAuth auth) { - if (!isAuthAvailable(auth.getNickname())) { - return false; - } - PlayerAuth newAuth = null; - try (BufferedReader br = new BufferedReader(new FileReader(source))) { - String line; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - if (args[0].equals(auth.getNickname())) { - newAuth = buildAuthFromArray(args); - if (newAuth != null) { - newAuth.setEmail(auth.getEmail()); - } - break; - } - } - } catch (IOException ex) { - ConsoleLogger.warning(ex.getMessage()); - return false; - } - if (newAuth != null) { - removeAuth(auth.getNickname()); - saveAuth(newAuth); - } - return true; - } - - @Override - public List getAllAuthsByIp(String ip) { - List countIp = new ArrayList<>(); - try (BufferedReader br = new BufferedReader(new FileReader(source))) { - String line; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - if (args.length > 3 && args[2].equals(ip)) { - countIp.add(args[0]); - } - } - return countIp; - } catch (IOException ex) { - ConsoleLogger.warning(ex.getMessage()); - return new ArrayList<>(); - } - } - - @Override - public int countAuthsByEmail(String email) { - int countEmail = 0; - try (BufferedReader br = new BufferedReader(new FileReader(source))) { - String line; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - if (args.length > 8 && args[8].equals(email)) { - ++countEmail; - } - } - return countEmail; - } catch (IOException ex) { - ConsoleLogger.warning(ex.getMessage()); - } - return 0; - } - - @Override - public DataSourceType getType() { - return DataSourceType.FILE; - } - - @Override - public boolean isLogged(String user) { - throw new UnsupportedOperationException("Flat file no longer supported"); - } - - @Override - public void setLogged(String user) { - } - - @Override - public void setUnlogged(String user) { - } - - @Override - public boolean hasSession(String user) { - throw new UnsupportedOperationException("Flat file no longer supported"); - } - - @Override - public void grantSession(String user) { - } - - @Override - public void revokeSession(String user) { - } - - @Override - public void purgeLogged() { - } - - @Override - public int getAccountsRegistered() { - int result = 0; - try (BufferedReader br = new BufferedReader(new FileReader(source))) { - while ((br.readLine()) != null) { - result++; - } - } catch (Exception ex) { - ConsoleLogger.warning(ex.getMessage()); - return result; - } - return result; - } - - @Override - public boolean updateRealName(String user, String realName) { - throw new UnsupportedOperationException("Flat file no longer supported"); - } - - @Override - public DataSourceValue getEmail(String user) { - throw new UnsupportedOperationException("Flat file no longer supported"); - } - - @Override - public List getAllAuths() { - List auths = new ArrayList<>(); - try (BufferedReader br = new BufferedReader(new FileReader(source))) { - String line; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - PlayerAuth auth = buildAuthFromArray(args); - if (auth != null) { - auths.add(auth); - } - } - } catch (IOException ex) { - ConsoleLogger.logException("Error while getting auths from flatfile:", ex); - } - return auths; - } - - @Override - public List getLoggedPlayersWithEmptyMail() { - throw new UnsupportedOperationException("Flat file no longer supported"); - } - - @Override - public List getRecentlyLoggedInPlayers() { - throw new UnsupportedOperationException("Flat file no longer supported"); - } - - @Override - public boolean setTotpKey(String user, String totpKey) { - throw new UnsupportedOperationException("Flat file no longer supported"); - } - - /** - * Creates a PlayerAuth object from the read data. - * - * @param args the data read from the line - * @return the player auth object with the data - */ - @SuppressWarnings("checkstyle:NeedBraces") - private static PlayerAuth buildAuthFromArray(String[] args) { - // Format allows 2, 3, 4, 7, 8, 9 fields. Anything else is unknown - if (args.length >= 2 && args.length <= 9 && args.length != 5 && args.length != 6) { - PlayerAuth.Builder builder = PlayerAuth.builder() - .name(args[0]).realName(args[0]).password(args[1], null); - - if (args.length >= 3) builder.lastIp(args[2]); - if (args.length >= 4) builder.lastLogin(parseNullableLong(args[3])); - if (args.length >= 7) { - builder.locX(Double.parseDouble(args[4])) - .locY(Double.parseDouble(args[5])) - .locZ(Double.parseDouble(args[6])); - } - if (args.length >= 8) builder.locWorld(args[7]); - if (args.length >= 9) builder.email(args[8]); - return builder.build(); - } - return null; - } - - private static Long parseNullableLong(String str) { - return "null".equals(str) ? null : Long.parseLong(str); - } -} diff --git a/src/main/java/fr/xephi/authme/datasource/converter/ForceFlatToSqlite.java b/src/main/java/fr/xephi/authme/datasource/converter/ForceFlatToSqlite.java deleted file mode 100644 index 1c67061f3..000000000 --- a/src/main/java/fr/xephi/authme/datasource/converter/ForceFlatToSqlite.java +++ /dev/null @@ -1,36 +0,0 @@ -package fr.xephi.authme.datasource.converter; - -import fr.xephi.authme.data.auth.PlayerAuth; -import fr.xephi.authme.datasource.DataSource; -import fr.xephi.authme.datasource.FlatFile; - -/** - * Mandatory migration from the deprecated flat file datasource to SQLite. - */ -public class ForceFlatToSqlite extends AbstractDataSourceConverter { - - private final FlatFile source; - - /** - * Constructor. - * - * @param source The datasource to convert (flatfile) - * @param destination The datasource to copy the data to (sqlite) - */ - public ForceFlatToSqlite(FlatFile source, DataSource destination) { - super(destination, destination.getType()); - this.source = source; - } - - @Override - public FlatFile getSource() { - return source; - } - - @Override - protected void adaptPlayerAuth(PlayerAuth auth) { - // Issue #1120: FlatFile returns PlayerAuth objects with realname = lower-case name all the time. - // We don't want to take this over into the new data source. - auth.setRealName("Player"); - } -} diff --git a/src/main/java/fr/xephi/authme/initialization/DataSourceProvider.java b/src/main/java/fr/xephi/authme/initialization/DataSourceProvider.java index f88bd5985..94e8898ed 100644 --- a/src/main/java/fr/xephi/authme/initialization/DataSourceProvider.java +++ b/src/main/java/fr/xephi/authme/initialization/DataSourceProvider.java @@ -5,11 +5,9 @@ import fr.xephi.authme.data.auth.PlayerCache; import fr.xephi.authme.datasource.CacheDataSource; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DataSourceType; -import fr.xephi.authme.datasource.FlatFile; import fr.xephi.authme.datasource.MySQL; import fr.xephi.authme.datasource.PostgreSqlDataSource; import fr.xephi.authme.datasource.SQLite; -import fr.xephi.authme.datasource.converter.ForceFlatToSqlite; import fr.xephi.authme.datasource.mysqlextensions.MySqlExtensionsFactory; import fr.xephi.authme.service.BukkitService; import fr.xephi.authme.settings.Settings; @@ -26,7 +24,6 @@ import java.sql.SQLException; */ public class DataSourceProvider implements Provider { - private static final String FLATFILE_FILENAME = "auths.db"; private static final int SQLITE_MAX_SIZE = 4000; @Inject @@ -65,10 +62,6 @@ public class DataSourceProvider implements Provider { DataSourceType dataSourceType = settings.getProperty(DatabaseSettings.BACKEND); DataSource dataSource; switch (dataSourceType) { - case FILE: - File source = new File(dataFolder, FLATFILE_FILENAME); - dataSource = new FlatFile(source); - break; case MYSQL: dataSource = new MySQL(settings, mySqlExtensionsFactory); break; @@ -82,8 +75,6 @@ public class DataSourceProvider implements Provider { throw new UnsupportedOperationException("Unknown data source type '" + dataSourceType + "'"); } - dataSource = convertFlatfileToSqlite(dataSource); - if (settings.getProperty(DatabaseSettings.USE_CACHING)) { dataSource = new CacheDataSource(dataSource, playerCache); } @@ -102,31 +93,4 @@ public class DataSourceProvider implements Provider { } }); } - - /** - * Converts the data source from the deprecated FLATFILE type to SQLITE. - * - * @param dataSource the data source to convert if necessary - * @return the data source to use: the converted datasource (SQLite), - * or the same data source if no conversion was performed - */ - private DataSource convertFlatfileToSqlite(DataSource dataSource) { - if (DataSourceType.FILE == settings.getProperty(DatabaseSettings.BACKEND)) { - ConsoleLogger.warning("FlatFile backend has been detected and is now deprecated; it will be changed " - + "to SQLite... Connection will be impossible until conversion is done!"); - FlatFile flatFile = (FlatFile) dataSource; - try { - SQLite sqlite = new SQLite(settings, dataFolder); - ForceFlatToSqlite converter = new ForceFlatToSqlite(flatFile, sqlite); - converter.execute(null); - settings.setProperty(DatabaseSettings.BACKEND, DataSourceType.SQLITE); - settings.save(); - return sqlite; - } catch (Exception e) { - ConsoleLogger.logException("Error during conversion from Flatfile to SQLite", e); - throw new IllegalStateException(e); - } - } - return dataSource; - } } diff --git a/src/main/java/fr/xephi/authme/service/BackupService.java b/src/main/java/fr/xephi/authme/service/BackupService.java index 6ae65a061..6d60226e7 100644 --- a/src/main/java/fr/xephi/authme/service/BackupService.java +++ b/src/main/java/fr/xephi/authme/service/BackupService.java @@ -83,8 +83,6 @@ public class BackupService { private boolean doBackup() { DataSourceType dataSourceType = settings.getProperty(DatabaseSettings.BACKEND); switch (dataSourceType) { - case FILE: - return performFileBackup("auths.db"); case MYSQL: return performMySqlBackup(); case SQLITE: diff --git a/src/test/java/fr/xephi/authme/datasource/FlatFileIntegrationTest.java b/src/test/java/fr/xephi/authme/datasource/FlatFileIntegrationTest.java deleted file mode 100644 index 9732ced79..000000000 --- a/src/test/java/fr/xephi/authme/datasource/FlatFileIntegrationTest.java +++ /dev/null @@ -1,100 +0,0 @@ -package fr.xephi.authme.datasource; - -import com.google.common.io.Files; -import fr.xephi.authme.TestHelper; -import fr.xephi.authme.data.auth.PlayerAuth; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; - -import java.io.File; -import java.io.IOException; -import java.util.Collection; -import java.util.List; - -import static fr.xephi.authme.AuthMeMatchers.equalToHash; -import static fr.xephi.authme.AuthMeMatchers.hasAuthBasicData; -import static fr.xephi.authme.AuthMeMatchers.hasAuthLocation; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.hasSize; -import static org.junit.Assert.assertThat; - -/** - * Integration test for the deprecated {@link FlatFile} datasource. The flatfile datasource is no longer used. - * Essentially, the only time we use it is in {@link fr.xephi.authme.datasource.converter.ForceFlatToSqlite}, - * which requires {@link FlatFile#getAllAuths()}. - */ -public class FlatFileIntegrationTest { - - @Rule - public TemporaryFolder temporaryFolder = new TemporaryFolder(); - - private DataSource dataSource; - - @Before - public void copyFileToTemporaryFolder() throws IOException { - File originalFile = TestHelper.getJarFile(TestHelper.PROJECT_ROOT + "datasource/flatfile-test.txt"); - File copy = temporaryFolder.newFile(); - Files.copy(originalFile, copy); - dataSource = new FlatFile(copy); - } - - @Test - public void shouldReturnIfAuthIsAvailableOrNot() { - // given / when - boolean isBobbyAvailable = dataSource.isAuthAvailable("bobby"); - boolean isChrisAvailable = dataSource.isAuthAvailable("chris"); - boolean isUserAvailable = dataSource.isAuthAvailable("USER"); - - // then - assertThat(isBobbyAvailable, equalTo(true)); - assertThat(isChrisAvailable, equalTo(false)); - assertThat(isUserAvailable, equalTo(true)); - } - - @Test - public void shouldReturnAllAuths() { - // given / when - List authList = dataSource.getAllAuths(); - - // then - assertThat(authList, hasSize(7)); - assertThat(getName("bobby", authList), hasAuthBasicData("bobby", "bobby", null, "123.45.67.89")); - assertThat(getName("bobby", authList), hasAuthLocation(1.05, 2.1, 4.2, "world", 0, 0)); - assertThat(getName("bobby", authList).getPassword(), equalToHash("$SHA$11aa0706173d7272$dbba966")); - assertThat(getName("twofields", authList), hasAuthBasicData("twofields", "twofields", null, null)); - assertThat(getName("twofields", authList).getPassword(), equalToHash("hash1234")); - assertThat(getName("threefields", authList), hasAuthBasicData("threefields", "threefields", null, "33.33.33.33")); - assertThat(getName("fourfields", authList), hasAuthBasicData("fourfields", "fourfields", null, "4.4.4.4")); - assertThat(getName("fourfields", authList).getLastLogin(), equalTo(404040404L)); - assertThat(getName("sevenfields", authList), hasAuthLocation(7.7, 14.14, 21.21, "world", 0, 0)); - assertThat(getName("eightfields", authList), hasAuthLocation(8.8, 17.6, 26.4, "eightworld", 0, 0)); - assertThat(getName("eightfields", authList).getLastLogin(), equalTo(1234567888L)); - assertThat(getName("eightfields", authList).getPassword(), equalToHash("hash8168")); - } - - @Test - public void shouldAddAuth() { - // given / when - boolean response = dataSource.saveAuth( - PlayerAuth.builder().name("Test").email("user@EXAMPLE.org").lastIp("123.45.67.77").build()); - List authList = dataSource.getAllAuths(); - - // then - assertThat(response, equalTo(true)); - assertThat(authList, hasSize(8)); - assertThat(authList, hasItem(hasAuthBasicData("test", "test", "user@EXAMPLE.org", "123.45.67.77"))); - } - - private static PlayerAuth getName(String name, Collection auths) { - for (PlayerAuth auth : auths) { - if (name.equals(auth.getNickname())) { - return auth; - } - } - throw new IllegalStateException("Did not find auth with name '" + name + "'"); - } - -} diff --git a/src/test/java/fr/xephi/authme/datasource/converter/ForceFlatToSqliteTest.java b/src/test/java/fr/xephi/authme/datasource/converter/ForceFlatToSqliteTest.java deleted file mode 100644 index 3cea7dd7a..000000000 --- a/src/test/java/fr/xephi/authme/datasource/converter/ForceFlatToSqliteTest.java +++ /dev/null @@ -1,74 +0,0 @@ -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 authCaptor = ArgumentCaptor.forClass(PlayerAuth.class); - verify(dataSource, times(7)).saveAuth(authCaptor.capture()); - List auths = authCaptor.getAllValues(); - assertThat(auths, hasItem(hasAuthBasicData("bobby", "Player", null, "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", null, "6.6.6.66"))); - assertThat(auths, hasItem(hasAuthLocation(8.8, 17.6, 26.4, "eightworld", 0, 0))); - } - -}