Fix some more tests

Add some assertions so IntelliJ isn't crying
This commit is contained in:
Fuzzlemann 2017-08-24 09:24:21 +02:00
parent 238a547181
commit 954ff23667
29 changed files with 80 additions and 63 deletions

View File

@ -17,7 +17,7 @@
</repository> </repository>
</repositories> </repositories>
<dependencies> <dependencies>
<!-- PaperSpigot 1.12 built with Buildtools for Database classes.--> <!-- PaperSpigot 1.12 built with BuildTools for Database classes.-->
<dependency> <dependency>
<groupId>com.destroystokyo.paper</groupId> <groupId>com.destroystokyo.paper</groupId>
<artifactId>paper</artifactId> <artifactId>paper</artifactId>

View File

@ -119,7 +119,7 @@ public class Log {
* Logs trace of caught Exception to Errors.txt and notifies on console. * Logs trace of caught Exception to Errors.txt and notifies on console.
* *
* @param source Class name the exception was caught in. * @param source Class name the exception was caught in.
* @param e Throwable, eg NullPointerException * @param e {@code Throwable}, eg NullPointerException
*/ */
public static void toLog(String source, Throwable e) { public static void toLog(String source, Throwable e) {
Plan.getInstance().getPluginLogger().toLog(source, e); Plan.getInstance().getPluginLogger().toLog(source, e);
@ -129,7 +129,7 @@ public class Log {
* Logs multiple caught Errors to Errors.txt. * Logs multiple caught Errors to Errors.txt.
* *
* @param source Class name the exception was caught in. * @param source Class name the exception was caught in.
* @param e Collection of Throwables, eg NullPointerException * @param e Collection of {@code Throwable}, eg NullPointerException
*/ */
public static void toLog(String source, Collection<Throwable> e) { public static void toLog(String source, Collection<Throwable> e) {
Plan.getInstance().getPluginLogger().toLog(source, e); Plan.getInstance().getPluginLogger().toLog(source, e);

View File

@ -69,6 +69,8 @@ public class ManageHotswapCommand extends SubCommand {
return true; return true;
} }
assert database != null;
try { try {
database.getVersion(); //Test db connection database.getVersion(); //Test db connection
} catch (Exception e) { } catch (Exception e) {

View File

@ -31,7 +31,7 @@ public class AnalysisCacheHandler {
private AnalysisData cache; private AnalysisData cache;
private boolean analysisEnabled; private boolean analysisEnabled;
private Set<UUID> notifyWhenCached; private final Set<UUID> notifyWhenCached;
/** /**
* Class Constructor. * Class Constructor.

View File

@ -15,7 +15,7 @@ import org.bukkit.event.player.AsyncPlayerChatEvent;
*/ */
public class PlanChatListener implements Listener { public class PlanChatListener implements Listener {
private Plan plugin; private final Plan plugin;
/** /**
* Class Constructor. * Class Constructor.

View File

@ -27,10 +27,10 @@ import java.util.UUID;
*/ */
public class ServerInfoManager { public class ServerInfoManager {
private Plan plugin; private final Plan plugin;
private ServerInfo serverInfo; private ServerInfo serverInfo;
private ServerInfoFile serverInfoFile; private ServerInfoFile serverInfoFile;
private ServerTable serverTable; private final ServerTable serverTable;
public ServerInfoManager(Plan plugin) { public ServerInfoManager(Plan plugin) {
this.plugin = plugin; this.plugin = plugin;

View File

@ -168,6 +168,8 @@ public abstract class Database {
return getName().toLowerCase().replace(" ", ""); return getName().toLowerCase().replace(" ", "");
} }
public abstract boolean isNewDatabase() throws SQLException;
/** /**
* Used to get the database schema version. * Used to get the database schema version.
* *

View File

@ -138,15 +138,6 @@ public abstract class SQLDB extends Database {
return true; return true;
} }
private boolean isNewDatabase() {
try {
getVersion();
return false;
} catch (Exception ignored) {
return true;
}
}
/** /**
* @return * @return
*/ */
@ -197,6 +188,11 @@ public abstract class SQLDB extends Database {
return versionTable.getVersion(); return versionTable.getVersion();
} }
@Override
public boolean isNewDatabase() throws SQLException {
return versionTable.isNewDatabase();
}
/** /**
* @param version * @param version
* @throws SQLException * @throws SQLException

View File

@ -8,7 +8,7 @@ package main.java.com.djrapitops.plan.database.sql;
*/ */
public class SqlParser { public class SqlParser {
private StringBuilder s; private final StringBuilder s;
public SqlParser() { public SqlParser() {
s = new StringBuilder(); s = new StringBuilder();

View File

@ -39,7 +39,7 @@ public class ActionsTable extends UserIDTable {
private final String columnActionID = "action_id"; private final String columnActionID = "action_id";
private final String columnAdditionalInfo = "additional_info"; private final String columnAdditionalInfo = "additional_info";
private ServerTable serverTable; private final ServerTable serverTable;
public ActionsTable(SQLDB db, boolean usingMySQL) { public ActionsTable(SQLDB db, boolean usingMySQL) {
super("plan_actions", db, usingMySQL); super("plan_actions", db, usingMySQL);

View File

@ -25,7 +25,7 @@ public class CommandUseTable extends Table {
private final String columnCommand = "command"; private final String columnCommand = "command";
private final String columnTimesUsed = "times_used"; private final String columnTimesUsed = "times_used";
private final String columnServerID = "server_id"; private final String columnServerID = "server_id";
private ServerTable serverTable; private final ServerTable serverTable;
/** /**
* @param db * @param db

View File

@ -20,7 +20,7 @@ public class NicknamesTable extends UserIDTable {
private final String columnNick = "nickname"; private final String columnNick = "nickname";
private final String columnServerID = "server_id"; private final String columnServerID = "server_id";
private ServerTable serverTable; private final ServerTable serverTable;
/** /**
* @param db The database * @param db The database

View File

@ -115,7 +115,6 @@ public abstract class Table {
* @throws SQLException * @throws SQLException
*/ */
protected PreparedStatement prepareStatement(String sql) throws SQLException { protected PreparedStatement prepareStatement(String sql) throws SQLException {
System.out.println(sql);
return getConnection().prepareStatement(sql); return getConnection().prepareStatement(sql);
} }

View File

@ -89,6 +89,7 @@ public class UserInfoTable extends UserIDTable {
set = statement.executeQuery(); set = statement.executeQuery();
return set.next(); return set.next();
} finally { } finally {
endTransaction(statement);
close(set, statement); close(set, statement);
} }
} }
@ -142,6 +143,7 @@ public class UserInfoTable extends UserIDTable {
} }
return null; return null;
} finally { } finally {
endTransaction(statement);
close(set, statement); close(set, statement);
} }
} }

View File

@ -251,6 +251,7 @@ public class UsersTable extends UserIDTable {
} }
return null; return null;
} finally { } finally {
endTransaction(statement);
close(set, statement); close(set, statement);
} }
} }

View File

@ -1,6 +1,5 @@
package main.java.com.djrapitops.plan.database.tables; package main.java.com.djrapitops.plan.database.tables;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.database.databases.SQLDB; import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Sql; import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser; import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
@ -33,6 +32,26 @@ public class VersionTable extends Table {
); );
} }
public boolean isNewDatabase() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
if (usingMySQL) {
statement = prepareStatement("SHOW TABLES LIKE " + tableName);
} else {
statement = prepareStatement("SELECT tbl_name FROM sqlite_master WHERE tbl_name=?");
statement.setString(1, tableName);
}
set = statement.executeQuery();
return set.next();
} finally {
endTransaction(statement);
close(set, statement);
}
}
/** /**
* @return @throws SQLException * @return @throws SQLException
*/ */
@ -47,12 +66,15 @@ public class VersionTable extends Table {
if (set.next()) { if (set.next()) {
version = set.getInt("version"); version = set.getInt("version");
} }
Log.debug("Database", "DB Schema version: " + version);
return version; return version;
} catch (Exception exc) {
exc.printStackTrace();
} finally { } finally {
endTransaction(statement); endTransaction(statement);
close(set, statement); close(set, statement);
} }
return 1;
} }
/** /**

View File

@ -10,7 +10,7 @@ package main.java.com.djrapitops.plan.queue.processing;
* @author Rsl1122 * @author Rsl1122
*/ */
public abstract class Processor<T> { public abstract class Processor<T> {
protected T object; protected final T object;
public Processor(T object) { public Processor(T object) {
this.object = object; this.object = object;

View File

@ -60,7 +60,7 @@ public enum Html {
@Deprecated ERROR_TABLE_2(TABLELINE_2.parse("No data", "No data")), @Deprecated ERROR_TABLE_2(TABLELINE_2.parse("No data", "No data")),
TABLE_END("</tbody></table>"); // KILLDATA_NONE("No Kills"), TABLE_END("</tbody></table>"); // KILLDATA_NONE("No Kills"),
private String html; private final String html;
Html(String html) { Html(String html) {
this.html = html; this.html = html;

View File

@ -47,7 +47,7 @@ public enum Theme {
"fff", "000", "267F00", "e5cc12", "b74343", "1E90FF", "fff", "000", "267F00", "e5cc12", "b74343", "1E90FF",
"e0d264", "7dcc24", "b58310", "ac69ef", "ddd", "565556"); "e0d264", "7dcc24", "b58310", "ac69ef", "ddd", "565556");
private String[] colors; private final String[] colors;
Theme(String... colors) { Theme(String... colors) {
int length = colors.length; int length = colors.length;
@ -63,19 +63,17 @@ public enum Theme {
} }
public String replaceThemeColors(String resourceString) { public String replaceThemeColors(String resourceString) {
Theme def = Theme.DEFAULT;
String replaced = resourceString; String replaced = resourceString;
for (Colors c : Colors.values()) { for (Colors c : Colors.values()) {
replaced = replaced.replace("#" + def.getColor(c.getId()), "#" + this.getColor(c.getId())); replaced = replaced.replace("#" + Theme.DEFAULT.getColor(c.getId()), "#" + this.getColor(c.getId()));
} }
return replaced; return replaced;
} }
public static String replaceColors(String resourceString) { public static String replaceColors(String resourceString) {
Theme def = Theme.DEFAULT;
String replaced = resourceString; String replaced = resourceString;
for (Colors c : Colors.values()) { for (Colors c : Colors.values()) {
replaced = replaced.replace("#" + def.getColor(c.getId()), c.getColor()); replaced = replaced.replace("#" + Theme.DEFAULT.getColor(c.getId()), c.getColor());
} }
return replaced; return replaced;
} }

View File

@ -10,7 +10,6 @@ import main.java.com.djrapitops.plan.ui.webserver.response.Response;
import main.java.com.djrapitops.plan.ui.webserver.response.api.BadRequestResponse; import main.java.com.djrapitops.plan.ui.webserver.response.api.BadRequestResponse;
import main.java.com.djrapitops.plan.ui.webserver.response.api.SuccessResponse; import main.java.com.djrapitops.plan.ui.webserver.response.api.SuccessResponse;
import main.java.com.djrapitops.plan.utilities.webserver.api.WebAPI; import main.java.com.djrapitops.plan.utilities.webserver.api.WebAPI;
import org.apache.commons.lang3.text.translate.CharSequenceTranslator;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import java.util.Map; import java.util.Map;

View File

@ -30,6 +30,8 @@ public class ReduceGapTriangles {
continue; continue;
} }
assert lastPoint != null;
long date = (long) point.getX(); long date = (long) point.getX();
long lastDate = (long) lastPoint.getX(); long lastDate = (long) lastPoint.getX();
double y = point.getY(); double y = point.getY();

View File

@ -19,7 +19,7 @@ public class WebAPIManager {
throw new IllegalStateException("Utility class"); throw new IllegalStateException("Utility class");
} }
private static Map<String, WebAPI> registry = new HashMap<>(); private static final Map<String, WebAPI> registry = new HashMap<>();
public static void registerNewAPI(String method, WebAPI api) { public static void registerNewAPI(String method, WebAPI api) {
registry.put(method.toLowerCase(), api); registry.put(method.toLowerCase(), api);

View File

@ -28,8 +28,5 @@ public class SessionTest {
public void setUp() { public void setUp() {
} }
/**
*
*/
// TODO Rewrite // TODO Rewrite
} }

View File

@ -21,7 +21,7 @@ import static junit.framework.TestCase.*;
@PrepareForTest(JavaPlugin.class) @PrepareForTest(JavaPlugin.class)
public class GeolocationCacheTest { public class GeolocationCacheTest {
private Map<String, String> ipsToCountries = new HashMap<>(); private final Map<String, String> ipsToCountries = new HashMap<>();
@Before @Before
public void setUp() { public void setUp() {

View File

@ -35,7 +35,7 @@ public class WorldTimesTest {
public void testWorldChange() { public void testWorldChange() {
long changeTime = time + 1000L; long changeTime = time + 1000L;
test.updateState(worldTwo, gms[0], changeTime); test.updateState(worldTwo, gms[0], changeTime);
assertEquals(1000L, (long) test.getWorldPlaytime(worldOne)); assertEquals(1000L, test.getWorldPlaytime(worldOne));
assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0])); assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0]));
} }
@ -43,7 +43,7 @@ public class WorldTimesTest {
public void testGMChange() { public void testGMChange() {
long changeTime = time + 1000L; long changeTime = time + 1000L;
test.updateState(worldOne, gms[0], changeTime); test.updateState(worldOne, gms[0], changeTime);
assertEquals(1000L, (long) test.getWorldPlaytime(worldOne)); assertEquals(1000L, test.getWorldPlaytime(worldOne));
assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0])); assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0]));
} }
@ -52,10 +52,10 @@ public class WorldTimesTest {
long changeTime = time + 1000L; long changeTime = time + 1000L;
long changeTime2 = changeTime + 1000L; long changeTime2 = changeTime + 1000L;
test.updateState(worldTwo, gms[2], changeTime); test.updateState(worldTwo, gms[2], changeTime);
assertEquals(1000L, (long) test.getWorldPlaytime(worldOne)); assertEquals(1000L, test.getWorldPlaytime(worldOne));
assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0])); assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0]));
test.updateState(worldOne, gms[1], changeTime2); test.updateState(worldOne, gms[1], changeTime2);
assertEquals(1000L, (long) test.getWorldPlaytime(worldOne)); assertEquals(1000L, test.getWorldPlaytime(worldOne));
assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0])); assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0]));
assertEquals(1000L, test.getGMTimes(worldTwo).getTime(gms[2])); assertEquals(1000L, test.getGMTimes(worldTwo).getTime(gms[2]));
} }

View File

@ -47,9 +47,9 @@ public class DatabaseTest {
private Database db; private Database db;
private Database backup; private Database backup;
private int rows; private int rows;
private UUID uuid = MockUtils.getPlayerUUID(); private final UUID uuid = MockUtils.getPlayerUUID();
private List<String> worlds = Arrays.asList("TestWorld", "TestWorld2"); private final List<String> worlds = Arrays.asList("TestWorld", "TestWorld2");
private UUID uuid2 = MockUtils.getPlayer2UUID(); private final UUID uuid2 = MockUtils.getPlayer2UUID();
public DatabaseTest() { public DatabaseTest() {
} }
@ -63,8 +63,6 @@ public class DatabaseTest {
db.getServerTable().saveCurrentServerInfo(new ServerInfo(-1, TestInit.getServerUUID(), "ServerName", "")); db.getServerTable().saveCurrentServerInfo(new ServerInfo(-1, TestInit.getServerUUID(), "ServerName", ""));
File f = new File(plan.getDataFolder(), "Errors.txt"); File f = new File(plan.getDataFolder(), "Errors.txt");
rows = FileUtil.lines(f).size(); rows = FileUtil.lines(f).size();
db.init();
} }
@After @After
@ -497,13 +495,8 @@ public class DatabaseTest {
assertTrue(usersTable.isRegistered(uuid)); assertTrue(usersTable.isRegistered(uuid));
System.out.println("0 " + uuid);
System.out.println("1 " + db.getUsersTable().getSavedUUIDs());
db.removeAccount(uuid); db.removeAccount(uuid);
System.out.println("2 " + db.getUsersTable().getSavedUUIDs());
assertFalse(usersTable.isRegistered(uuid)); assertFalse(usersTable.isRegistered(uuid));
assertFalse(userInfoTable.isRegistered(uuid)); assertFalse(userInfoTable.isRegistered(uuid));
assertTrue(nicknamesTable.getNicknames(uuid).isEmpty()); assertTrue(nicknamesTable.getNicknames(uuid).isEmpty());

View File

@ -21,10 +21,10 @@ import static junit.framework.TestCase.assertEquals;
*/ */
public class GraphTest { public class GraphTest {
private List<TPS> tpsList = new ArrayList<>(); private final List<TPS> tpsList = new ArrayList<>();
private List<Session> sessionList = new ArrayList<>(); private final List<Session> sessionList = new ArrayList<>();
private Map<String, Integer> geoList = new HashMap<>(); private final Map<String, Integer> geoList = new HashMap<>();
private Map<String, Long> worldTimes = new HashMap<>(); private final Map<String, Long> worldTimes = new HashMap<>();
private List<Point> points = new ArrayList<>(); private List<Point> points = new ArrayList<>();

View File

@ -28,7 +28,7 @@ import static junit.framework.TestCase.assertNotNull;
@PrepareForTest(JavaPlugin.class) @PrepareForTest(JavaPlugin.class)
public class HastebinTest { public class HastebinTest {
private AtomicReference<String> testLink = new AtomicReference<>(null); private final AtomicReference<String> testLink = new AtomicReference<>(null);
@Before @Before
public void checkAvailability() throws Exception { public void checkAvailability() throws Exception {

View File

@ -22,27 +22,31 @@ import java.io.IOException;
public class DBTestSuite { public class DBTestSuite {
@BeforeClass @BeforeClass
public static void setUp() throws IOException { public static void setUp() throws IOException {
clean(true); clean();
} }
@AfterClass @AfterClass
public static void tearDown() throws IOException { public static void tearDown() throws IOException {
clean(false); clean();
} }
private static void clean(boolean dbOnly) throws IOException { private static void clean() {
File testFolder = TestInit.getTestFolder(); File testFolder = TestInit.getTestFolder();
if (!testFolder.exists() || !testFolder.isDirectory()) { if (!testFolder.exists() || !testFolder.isDirectory()) {
return; return;
} }
for (File f : testFolder.listFiles()) { File[] files = testFolder.listFiles();
if (dbOnly && !f.getName().contains(".db")) {
continue;
}
f.delete(); if (files == null) {
return;
}
for (File f : files) {
if (!f.delete()) {
f.deleteOnExit();
}
} }
} }
} }