Implements MiscUtils#getMatchingPlayerNames

This commit is contained in:
Fuzzlemann 2017-09-01 13:13:44 +02:00
parent 2be6f7ff90
commit 1ed8600988
5 changed files with 111 additions and 29 deletions

View File

@ -5,6 +5,7 @@ import com.djrapitops.plugin.command.ISender;
import com.djrapitops.plugin.command.SubCommand; import com.djrapitops.plugin.command.SubCommand;
import com.djrapitops.plugin.task.AbsRunnable; import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.utilities.FormattingUtils; 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.Permissions;
import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.locale.Locale; import main.java.com.djrapitops.plan.locale.Locale;
@ -62,9 +63,12 @@ public class SearchCommand extends SubCommand {
public void run() { public void run() {
try { try {
List<String> names = MiscUtils.getMatchingPlayerNames(args[0]); List<String> 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 // Results
if (names.isEmpty()) { if (empty) {
sender.sendMessage(Locale.get(Msg.CMD_INFO_NO_RESULTS).parse(Arrays.toString(args))); sender.sendMessage(Locale.get(Msg.CMD_INFO_NO_RESULTS).parse(Arrays.toString(args)));
} else { } else {
sender.sendMessage(Locale.get(Msg.CMD_INFO_RESULTS).toString() + FormattingUtils.collectionToStringNoBrackets(names)); sender.sendMessage(Locale.get(Msg.CMD_INFO_RESULTS).toString() + FormattingUtils.collectionToStringNoBrackets(names));

View File

@ -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<UUID> getSavedUUIDs() throws SQLException { public Set<UUID> getSavedUUIDs() throws SQLException {
Set<UUID> uuids = new HashSet<>();
PreparedStatement statement = null; PreparedStatement statement = null;
ResultSet set = null; ResultSet set = null;
try { try {
Set<UUID> uuids = new HashSet<>();
statement = prepareStatement(Select.from(tableName, columnUUID).toString()); statement = prepareStatement(Select.from(tableName, columnUUID).toString());
statement.setFetchSize(2000); statement.setFetchSize(2000);
set = statement.executeQuery(); set = statement.executeQuery();
while (set.next()) { while (set.next()) {
UUID uuid = UUID.fromString(set.getString(columnUUID)); UUID uuid = UUID.fromString(set.getString(columnUUID));
@ -70,8 +73,8 @@ public class UsersTable extends UserIDTable {
} }
/** /**
* @param uuid * @param uuid the UUID of the user that should be removed.
* @return * @return if the removal was successful.
*/ */
@Override @Override
public boolean removeUser(UUID uuid) { 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() { public String getColumnID() {
return columnID; return columnID;
} }
/**
* @return the name of the column that inherits the UUID.
*/
public String getColumnUUID() { public String getColumnUUID() {
return columnUUID; 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<String> getMatchingNames(String name) throws SQLException {
String searchString = "%" + name + "%";
List<String> 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() { public String getColumnName() {
return columnName; return columnName;
} }

View File

@ -4,6 +4,7 @@ import com.djrapitops.plugin.api.TimeAmount;
import com.djrapitops.plugin.command.CommandUtils; import com.djrapitops.plugin.command.CommandUtils;
import com.djrapitops.plugin.command.ISender; import com.djrapitops.plugin.command.ISender;
import com.djrapitops.plugin.utilities.Compatibility; 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.Permissions;
import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.api.IPlan; 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.Closeable;
import java.io.IOException; 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; import java.util.stream.Collectors;
/** /**
@ -88,14 +93,14 @@ public class MiscUtils {
* @return Alphabetically sorted list of matching player names. * @return Alphabetically sorted list of matching player names.
*/ */
public static List<String> getMatchingPlayerNames(String search) { public static List<String> getMatchingPlayerNames(String search) {
final String searchFor = search.toLowerCase();
Database db = Plan.getInstance().getDB(); Database db = Plan.getInstance().getDB();
List<String> matches = new ArrayList<>(); List<String> matches;
// try { try {
// TODO GetMatchingPlayerNamesFromDB matches = db.getUsersTable().getMatchingNames(search);
// } catch (SQLException e) { } catch (SQLException e) {
// Log.toLog("MiscUtils.getMatchingPlayerNames", e); Log.toLog("MiscUtils.getMatchingPlayerNames", e);
// } return null;
}
Collections.sort(matches); Collections.sort(matches);
return matches; return matches;
} }

View File

@ -1,5 +1,6 @@
package main.java.com.djrapitops.plan.utilities; package main.java.com.djrapitops.plan.utilities;
import com.djrapitops.plugin.api.TimeAmount;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -32,10 +33,8 @@ public class FormatUtilsTest {
@Test @Test
public void testFormatTimeAmount() { public void testFormatTimeAmount() {
long ms = 1000L;
String expResult = "1s"; String expResult = "1s";
String result = FormatUtils.formatTimeAmount(ms); String result = FormatUtils.formatTimeAmount(TimeAmount.SECOND.ms());
assertEquals(expResult, result); assertEquals(expResult, result);
} }

View File

@ -7,19 +7,27 @@ package main.java.com.djrapitops.plan.utilities;
import com.djrapitops.plugin.command.ISender; import com.djrapitops.plugin.command.ISender;
import com.djrapitops.plugin.command.bukkit.BukkitCMDSender; 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.Bukkit;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
import test.java.utils.MockUtils; import test.java.utils.MockUtils;
import test.java.utils.RandomData;
import test.java.utils.TestInit; import test.java.utils.TestInit;
import java.util.List; import java.util.List;
import java.util.UUID;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.powermock.api.mockito.PowerMockito.when;
/** /**
* @author Rsl1122 * @author Rsl1122
@ -28,6 +36,9 @@ import static org.junit.Assert.assertEquals;
@PrepareForTest({JavaPlugin.class, Bukkit.class}) @PrepareForTest({JavaPlugin.class, Bukkit.class})
public class MiscUtilsTest { public class MiscUtilsTest {
private Plan plan;
private SQLDB db;
@Test @Test
public void testGetPlayerDisplayNameArgsPerm() { public void testGetPlayerDisplayNameArgsPerm() {
String[] args = new String[]{"Rsl1122", "Test"}; String[] args = new String[]{"Rsl1122", "Test"};
@ -86,7 +97,7 @@ public class MiscUtilsTest {
} }
@Test @Test
public void testGetPlayerDisplaynameConsole() { public void testGetPlayerDisplayNameConsole() {
String[] args = new String[]{"TestConsoleSender"}; String[] args = new String[]{"TestConsoleSender"};
ISender sender = new BukkitCMDSender(MockUtils.mockConsoleSender()); ISender sender = new BukkitCMDSender(MockUtils.mockConsoleSender());
@ -97,31 +108,57 @@ public class MiscUtilsTest {
} }
@Test @Test
@Ignore("DB mock") public void testGetMatchingNames() throws Exception {
public void testGetMatchingDisplayNames() throws Exception { setupDatabase();
TestInit.init();
String search = "testname";
String exp1 = "TestName"; String exp1 = "TestName";
String exp2 = "TestName2"; String exp2 = "TestName2";
UsersTable usersTable = db.getUsersTable();
usersTable.registerUser(UUID.randomUUID(), 0L, exp1);
usersTable.registerUser(UUID.randomUUID(), 0L, exp2);
String search = "testname";
List<String> result = MiscUtils.getMatchingPlayerNames(search); List<String> result = MiscUtils.getMatchingPlayerNames(search);
assertNotNull(result);
assertEquals(2, result.size()); assertEquals(2, result.size());
assertEquals(exp1, result.get(0)); assertEquals(exp1, result.get(0));
assertEquals(exp2, result.get(1)); assertEquals(exp2, result.get(1));
} }
@Test @Test
@Ignore("DB mock") public void testGetMatchingNickNames() throws Exception {
public void testGetMatchingDisplayNames2() throws Exception { setupDatabase();
TestInit.init();
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 search = "2";
String exp2 = "TestName2";
List<String> result = MiscUtils.getMatchingPlayerNames(search); List<String> result = MiscUtils.getMatchingPlayerNames(search);
assertNotNull(result);
assertEquals(1, result.size()); 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);
} }
} }