diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/SearchCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/SearchCommand.java index e4eb3de73..c939087f4 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/SearchCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/SearchCommand.java @@ -5,6 +5,7 @@ import com.djrapitops.plugin.command.ISender; import com.djrapitops.plugin.command.SubCommand; import com.djrapitops.plugin.task.AbsRunnable; import com.djrapitops.plugin.utilities.FormattingUtils; +import com.djrapitops.plugin.utilities.Verify; import main.java.com.djrapitops.plan.Permissions; import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.locale.Locale; @@ -62,9 +63,12 @@ public class SearchCommand extends SubCommand { public void run() { try { List names = MiscUtils.getMatchingPlayerNames(args[0]); - sender.sendMessage(Locale.get(Msg.CMD_HEADER_SEARCH) + args[0] + " (" + names.size() + ")"); + + boolean empty = Verify.isEmpty(names); + + sender.sendMessage(Locale.get(Msg.CMD_HEADER_SEARCH) + args[0] + " (" + (empty ? 0 : names.size()) + ")"); // Results - if (names.isEmpty()) { + if (empty) { sender.sendMessage(Locale.get(Msg.CMD_INFO_NO_RESULTS).parse(Arrays.toString(args))); } else { sender.sendMessage(Locale.get(Msg.CMD_INFO_RESULTS).toString() + FormattingUtils.collectionToStringNoBrackets(names)); diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java index afad15e11..331aa4340 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java @@ -48,15 +48,18 @@ public class UsersTable extends UserIDTable { } /** - * @return @throws SQLException + * @return a {@link Set} of the saved UUIDs. + * @throws SQLException when an error at retrieving the UUIDs happens */ public Set getSavedUUIDs() throws SQLException { + Set uuids = new HashSet<>(); + PreparedStatement statement = null; ResultSet set = null; try { - Set uuids = new HashSet<>(); statement = prepareStatement(Select.from(tableName, columnUUID).toString()); statement.setFetchSize(2000); + set = statement.executeQuery(); while (set.next()) { UUID uuid = UUID.fromString(set.getString(columnUUID)); @@ -70,8 +73,8 @@ public class UsersTable extends UserIDTable { } /** - * @param uuid - * @return + * @param uuid the UUID of the user that should be removed. + * @return if the removal was successful. */ @Override public boolean removeUser(UUID uuid) { @@ -91,12 +94,15 @@ public class UsersTable extends UserIDTable { } /** - * @return + * @return the name of the column that inherits the ID. */ public String getColumnID() { return columnID; } + /** + * @return the name of the column that inherits the UUID. + */ public String getColumnUUID() { return columnUUID; } @@ -257,6 +263,37 @@ public class UsersTable extends UserIDTable { } } + /** + * Gets the names of the players which names or nicknames match {@code name}. + * + * @param name the name / nickname. + * @return a list of distinct names. + * @throws SQLException when an error at fetching the names happens. + */ + public List getMatchingNames(String name) throws SQLException { + String searchString = "%" + name + "%"; + List matchingNames = new ArrayList<>(); + + PreparedStatement statement = null; + ResultSet set = null; + try { + statement = prepareStatement( + "SELECT name FROM plan_users WHERE name LIKE LOWER(?) UNION SELECT name FROM plan_users WHERE id = (SELECT user_id FROM plan_nicknames WHERE nickname LIKE LOWER(?))" + ); + statement.setString(1, searchString); + statement.setString(2, searchString); + + set = statement.executeQuery(); + while (set.next()) { + matchingNames.add(set.getString("name")); + } + return matchingNames; + } finally { + endTransaction(statement); + close(set, statement); + } + } + public String getColumnName() { return columnName; } diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/MiscUtils.java b/Plan/src/main/java/com/djrapitops/plan/utilities/MiscUtils.java index 647ef67c4..53aa64968 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/MiscUtils.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/MiscUtils.java @@ -4,6 +4,7 @@ import com.djrapitops.plugin.api.TimeAmount; import com.djrapitops.plugin.command.CommandUtils; import com.djrapitops.plugin.command.ISender; import com.djrapitops.plugin.utilities.Compatibility; +import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Permissions; import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.api.IPlan; @@ -14,7 +15,11 @@ import main.java.com.djrapitops.plan.locale.Msg; import java.io.Closeable; import java.io.IOException; -import java.util.*; +import java.sql.SQLException; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.TimeZone; import java.util.stream.Collectors; /** @@ -88,14 +93,14 @@ public class MiscUtils { * @return Alphabetically sorted list of matching player names. */ public static List getMatchingPlayerNames(String search) { - final String searchFor = search.toLowerCase(); Database db = Plan.getInstance().getDB(); - List matches = new ArrayList<>(); -// try { - // TODO GetMatchingPlayerNamesFromDB -// } catch (SQLException e) { -// Log.toLog("MiscUtils.getMatchingPlayerNames", e); -// } + List matches; + try { + matches = db.getUsersTable().getMatchingNames(search); + } catch (SQLException e) { + Log.toLog("MiscUtils.getMatchingPlayerNames", e); + return null; + } Collections.sort(matches); return matches; } diff --git a/Plan/test/main/java/com/djrapitops/plan/utilities/FormatUtilsTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/FormatUtilsTest.java index 5f4eeaa0a..bbb60d89f 100644 --- a/Plan/test/main/java/com/djrapitops/plan/utilities/FormatUtilsTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/FormatUtilsTest.java @@ -1,5 +1,6 @@ package main.java.com.djrapitops.plan.utilities; +import com.djrapitops.plugin.api.TimeAmount; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.plugin.java.JavaPlugin; @@ -32,10 +33,8 @@ public class FormatUtilsTest { @Test public void testFormatTimeAmount() { - long ms = 1000L; - String expResult = "1s"; - String result = FormatUtils.formatTimeAmount(ms); + String result = FormatUtils.formatTimeAmount(TimeAmount.SECOND.ms()); assertEquals(expResult, result); } diff --git a/Plan/test/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java index e6ef0968a..3d6f3c51b 100644 --- a/Plan/test/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java @@ -7,19 +7,27 @@ package main.java.com.djrapitops.plan.utilities; import com.djrapitops.plugin.command.ISender; import com.djrapitops.plugin.command.bukkit.BukkitCMDSender; +import main.java.com.djrapitops.plan.Plan; +import main.java.com.djrapitops.plan.database.databases.SQLDB; +import main.java.com.djrapitops.plan.database.databases.SQLiteDB; +import main.java.com.djrapitops.plan.database.tables.UsersTable; +import main.java.com.djrapitops.plan.systems.info.server.ServerInfo; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import test.java.utils.MockUtils; +import test.java.utils.RandomData; import test.java.utils.TestInit; import java.util.List; +import java.util.UUID; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.powermock.api.mockito.PowerMockito.when; /** * @author Rsl1122 @@ -28,6 +36,9 @@ import static org.junit.Assert.assertEquals; @PrepareForTest({JavaPlugin.class, Bukkit.class}) public class MiscUtilsTest { + private Plan plan; + private SQLDB db; + @Test public void testGetPlayerDisplayNameArgsPerm() { String[] args = new String[]{"Rsl1122", "Test"}; @@ -86,7 +97,7 @@ public class MiscUtilsTest { } @Test - public void testGetPlayerDisplaynameConsole() { + public void testGetPlayerDisplayNameConsole() { String[] args = new String[]{"TestConsoleSender"}; ISender sender = new BukkitCMDSender(MockUtils.mockConsoleSender()); @@ -97,31 +108,57 @@ public class MiscUtilsTest { } @Test - @Ignore("DB mock") - public void testGetMatchingDisplayNames() throws Exception { - TestInit.init(); - String search = "testname"; + public void testGetMatchingNames() throws Exception { + setupDatabase(); String exp1 = "TestName"; String exp2 = "TestName2"; + + UsersTable usersTable = db.getUsersTable(); + usersTable.registerUser(UUID.randomUUID(), 0L, exp1); + usersTable.registerUser(UUID.randomUUID(), 0L, exp2); + + String search = "testname"; + List result = MiscUtils.getMatchingPlayerNames(search); + assertNotNull(result); assertEquals(2, result.size()); assertEquals(exp1, result.get(0)); assertEquals(exp2, result.get(1)); } @Test - @Ignore("DB mock") - public void testGetMatchingDisplayNames2() throws Exception { - TestInit.init(); + public void testGetMatchingNickNames() throws Exception { + setupDatabase(); + + UUID uuid = UUID.randomUUID(); + String userName = RandomData.randomString(10); + db.getUsersTable().registerUser(uuid, 0L, userName); + + String nickname = "2" + RandomData.randomString(10); + db.getNicknamesTable().saveUserName(uuid, nickname); String search = "2"; - String exp2 = "TestName2"; List result = MiscUtils.getMatchingPlayerNames(search); + assertNotNull(result); assertEquals(1, result.size()); - assertEquals(exp2, result.get(0)); + assertEquals(userName, result.get(0)); + } + + private void setupDatabase() throws Exception { + TestInit.init(); + + TestInit t = TestInit.init(); + plan = t.getPlanMock(); + + db = new SQLiteDB(plan, "debug" + MiscUtils.getTime()); + db.init(); + + db.getServerTable().saveCurrentServerInfo(new ServerInfo(-1, TestInit.getServerUUID(), "ServerName", "")); + + when(plan.getDB()).thenReturn(db); } }