diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/Main.java b/src/main/java/net/knarcraft/minecraftserverlauncher/Main.java index 21fe201..8b77908 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/Main.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/Main.java @@ -5,6 +5,7 @@ import net.knarcraft.minecraftserverlauncher.profile.Profile; import net.knarcraft.minecraftserverlauncher.server.Server; import net.knarcraft.minecraftserverlauncher.userinterface.GUI; import net.knarcraft.minecraftserverlauncher.userinterface.ServerConsoles; +import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions; import net.knarcraft.minecraftserverlauncher.utility.Updater; import java.awt.*; @@ -80,7 +81,7 @@ public class Main { Server server = collection.getServer(); if (server.isEnabled() && server.getProcess() != null) { try { - String readText = server.readFromServer(); + String readText = CommonFunctions.readBufferedReader(server.getReader()); if (!readText.equals("")) { collection.getServerConsole().output(readText); updatePlayerList(readText, server); diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Profile.java b/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Profile.java index ce12b67..e56b8e6 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Profile.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Profile.java @@ -29,9 +29,10 @@ import java.util.concurrent.Executors; */ public class Profile implements java.io.Serializable { private static final ArrayList profiles = new ArrayList<>(); - private static final String profilesDir = Main.getApplicationWorkDirectory() + File.separator + "files"; - private static final String profilesFile = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "Profiles.txt"; - private static final String jarDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "Jars" + File.separator; + private static final String workingDirectory = Main.getApplicationWorkDirectory() + File.separator + "files"; + private static final String profilesDir = workingDirectory; + private static final String profilesFile = workingDirectory + File.separator + "Profiles.txt"; + private static final String jarDirectory = workingDirectory + File.separator + "Jars" + File.separator; private static Profile current; private static transient ServerLauncherGUI serverLauncherGui; private final ArrayList collections; diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java index ef0358e..2a237d5 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java @@ -84,10 +84,24 @@ public class Server implements java.io.Serializable { this.playerList = new ArrayList<>(); } + /** + * Gets the list of available RAM choices allowed + * + * @return

All available RAM choices

+ */ public static String[] getRamList() { return ramList; } + /** + * Gets the buffered reader used to read from this server + * + * @return

The buffered reader used to read from this server

+ */ + public BufferedReader getReader() { + return this.reader; + } + /** * Tries to stop all enabled servers. * @@ -357,21 +371,6 @@ public class Server implements java.io.Serializable { } } - /** - * Reads all available output from the server process - * - * @return

The server output

- * @throws IOException

If reading from the reader fails

- */ - public String readFromServer() throws IOException { - String line; - StringBuilder text = new StringBuilder(); - while (reader.ready() && (line = reader.readLine()) != null) { - text.append(line).append("\n"); - } - return text.toString().trim(); - } - /** * Starts the process running this server * diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainer.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainer.java index 07afa5f..7d31d0e 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainer.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainer.java @@ -1,9 +1,19 @@ package net.knarcraft.minecraftserverlauncher.server; +import net.knarcraft.minecraftserverlauncher.Main; +import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions; + +import java.io.*; +import java.util.HashMap; import java.util.Map; +/** + * This class acts as a container for all "latest" server versions + */ public class ServerVersionContainer implements java.io.Serializable { + private static ServerVersionContainer serverVersionContainer; + private String versionFile = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "versions.csv"; private String vanillaVersion; private String snapshotVersion; private String bungeeVersion; @@ -11,10 +21,157 @@ public class ServerVersionContainer implements java.io.Serializable { private String travertineVersion; private Map spongeVanillaVersions; - public ServerVersionContainer() { - + /** + * Initializes a new server version container + */ + private ServerVersionContainer() { + spongeVanillaVersions = new HashMap<>(); + loadState(); } + /** + * Gives a server version container instance + * + * @return

A server version container instance

+ */ + public static ServerVersionContainer getInstance() { + if (serverVersionContainer == null) { + serverVersionContainer = new ServerVersionContainer(); + } + return serverVersionContainer; + } + + /** + * Resets the state of the server version container + */ + public void reset() { + this.vanillaVersion = null; + this.snapshotVersion = null; + this.bungeeVersion = null; + this.waterfallVersion = null; + this.travertineVersion = null; + this.spongeVanillaVersions = new HashMap<>(); + } + + @Override + public String toString() { + return "vanillaVersion;" + vanillaVersion + "\n" + + "snapshotVersion;" + snapshotVersion + "\n" + + "bungeeVersion;" + bungeeVersion + "\n" + + "waterfallVersion;" + waterfallVersion + "\n" + + "travertineVersion;" + travertineVersion + "\n" + + "spongeVanillaVersions;" + + mapToString(spongeVanillaVersions); + } + + /** + * Formats a map to a string given a map + * + * @param targetMap

The map to format

+ * @return

A string representing the map

+ */ + private String mapToString(Map targetMap) { + StringBuilder stringBuilder = new StringBuilder(); + int mapSize = targetMap.keySet().size(); + int mapIndex = 0; + for (Object key : targetMap.keySet()) { + stringBuilder.append(key).append("!").append(targetMap.get(key)); + if (mapIndex < mapSize) { + stringBuilder.append(","); + } + mapIndex++; + } + return stringBuilder.toString(); + } + + /** + * Tries to save the state of this server version container + */ + public void saveState() { + PrintWriter file; + try { + file = new PrintWriter(versionFile); + file.println(this.toString()); + file.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + /** + * Loads the object state from the save file + */ + private void loadState() { + BufferedReader reader; + if (!new File(versionFile).exists()) { + return; + } + try { + reader = new BufferedReader(new InputStreamReader(new FileInputStream(versionFile))); + String currentData = CommonFunctions.readBufferedReader(reader); + for (String line : currentData.split("\n")) { + parseSaveLine(line); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Parses one line in the version save file and updates the appropriate variable + * + * @param line

The line from the save file to parse

+ */ + private void parseSaveLine(String line) { + String[] lineData = line.split(";"); + if (lineData.length != 2) { + return; + } + String variableKey = lineData[0]; + String variableValue = lineData[1]; + switch (variableKey) { + case "vanillaVersion": + vanillaVersion = variableValue; + break; + case "snapshotVersion": + snapshotVersion = variableValue; + break; + case "bungeeVersion": + bungeeVersion = variableValue; + break; + case "waterfallVersion": + waterfallVersion = variableValue; + break; + case "travertineVersion": + travertineVersion = variableValue; + break; + case "spongeVanillaVersions": + parseSpongeVanillaVersions(variableValue); + break; + default: + throw new IllegalArgumentException("Invalid key encountered in the server version file."); + } + } + + /** + * Reads spongeVanilla versions from a text string and updates the version map + * + * @param data

The data string to parse

+ */ + private void parseSpongeVanillaVersions(String data) { + String[] versions = data.split(","); + for (String version : versions) { + String[] versionData = version.split("!"); + spongeVanillaVersions.put(versionData[0], versionData[1]); + } + } + + + /** + * Gets the current vanilla version + * + * @return

The current vanilla version

+ */ public String getVanillaVersion() { return this.vanillaVersion; } @@ -23,65 +180,126 @@ public class ServerVersionContainer implements java.io.Serializable { * Sets the vanilla server version * * @param newVersion

The new vanilla server version

- * @return

This object

*/ - public ServerVersionContainer setVanillaVersion(String newVersion) { + public void setVanillaVersion(String newVersion) { this.vanillaVersion = newVersion; - return this; + saveState(); } + /** + * Gets the current snapshot version + * + * @return

The current snapshot version

+ */ public String getSnapshotVersion() { return this.snapshotVersion; } - public ServerVersionContainer setSnapshotVersion(String newVersion) { + /** + * Sets the snapshot server version + * + * @param newVersion

The new snapshot server version

+ */ + public void setSnapshotVersion(String newVersion) { this.snapshotVersion = newVersion; - return this; + saveState(); } + /** + * Gets the current bungee version + * + * @return

The current bungee version

+ */ public String getBungeeVersion() { return this.bungeeVersion; } - public ServerVersionContainer setBungeeVersion(String newVersion) { + /** + * Sets the bungee server version + * + * @param newVersion

The new bungee server version

+ */ + public void setBungeeVersion(String newVersion) { this.bungeeVersion = newVersion; - return this; + saveState(); } + /** + * Gets the current waterfall version + * + * @return

The current waterfall version

+ */ public String getWaterfallVersion() { return this.waterfallVersion; } - public ServerVersionContainer setWaterfallVersion(String newVersion) { + /** + * Sets the waterfall server version + * + * @param newVersion

The new waterfall server version

+ */ + public void setWaterfallVersion(String newVersion) { this.waterfallVersion = newVersion; - return this; + saveState(); } + /** + * Gets the current travertine server version + * + * @return

The current travertine version

+ */ public String getTravertineVersion() { return this.travertineVersion; } - public ServerVersionContainer setTravertineVersion(String newVersion) { + /** + * Sets the travertine server version + * + * @param newVersion

The new travertine server version

+ */ + public void setTravertineVersion(String newVersion) { this.travertineVersion = newVersion; - return this; + saveState(); } + /** + * Gets the entire sponge vanilla version map + * + * @return

The entire sponge vanilla version map

+ */ public Map getSpongeVanillaVersions() { return this.spongeVanillaVersions; } + /** + * Gets a specific sponge vanilla version + * + * @param versionKey

The version to check current version of

+ * @return

The current sponge vanilla version

+ */ public String getSpongeVanillaVersion(String versionKey) { return spongeVanillaVersions.get(versionKey); } - public ServerVersionContainer setSpongeVanillaVersion(Map newVersions) { + /** + * Replaces the entire sponge vanilla version map + * + * @param newVersions

The new version map to use

+ */ + public void setSpongeVanillaVersion(Map newVersions) { this.spongeVanillaVersions = newVersions; - return this; + saveState(); } - public ServerVersionContainer setSpongeVanillaVersion(String mapKey, String newValue) { + /** + * Sets the current version for a given sponge vanilla version + * + * @param mapKey

The version key to set version for

+ * @param newValue

The new current version

+ */ + public void setSpongeVanillaVersion(String mapKey, String newValue) { spongeVanillaVersions.put(mapKey, newValue); - return this; + saveState(); } } diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/BungeeCord.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/BungeeCord.java index 3fc7b0a..0f2e93e 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/BungeeCord.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/BungeeCord.java @@ -1,5 +1,7 @@ package net.knarcraft.minecraftserverlauncher.server.servertypes; +import net.knarcraft.minecraftserverlauncher.server.ServerVersionContainer; + import java.io.File; import java.io.IOException; import java.nio.file.Paths; @@ -11,7 +13,6 @@ import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.stri public class BungeeCord extends AbstractServerType { private String versionURL; - private String lastVersion; private String srcStart; private String srcEnd; @@ -35,11 +36,20 @@ public class BungeeCord extends AbstractServerType { @Override public boolean downloadJar(String folder, String version) throws IOException { + ServerVersionContainer versionContainer = ServerVersionContainer.getInstance(); String file = this.getName() + version + ".jar"; File filePath = new File(folder + file); String newestVersion = stringBetween(readFile(versionURL), srcStart, srcEnd); - String oldVersion = lastVersion; - lastVersion = newestVersion; - return (filePath.isFile() && newestVersion.equals(oldVersion)) || downloadFile(downloadURL, Paths.get(filePath.toURI())); + String oldVersion = versionContainer.getBungeeVersion(); + //The file is already the newest version + if (filePath.isFile() && newestVersion.equals(oldVersion)) { + return true; + } + //The new jar file could not be downloaded + if (!downloadFile(downloadURL, Paths.get(filePath.toURI()))) { + return false; + } + versionContainer.setBungeeVersion(newestVersion); + return true; } } diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/SpongeVanilla.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/SpongeVanilla.java index 102853c..3fa326f 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/SpongeVanilla.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/SpongeVanilla.java @@ -1,5 +1,6 @@ package net.knarcraft.minecraftserverlauncher.server.servertypes; +import net.knarcraft.minecraftserverlauncher.server.ServerVersionContainer; import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions; import java.io.File; @@ -11,7 +12,6 @@ import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.down public class SpongeVanilla extends AbstractServerType { private String versionURL; - private String lastVersion; private String srcStart; private String srcEnd; private String downloadURLPart; @@ -39,6 +39,7 @@ public class SpongeVanilla extends AbstractServerType { @Override public boolean downloadJar(String folder, String version) throws IOException { + ServerVersionContainer versionContainer = ServerVersionContainer.getInstance(); String file = this.getName() + version + ".jar"; File filePath = new File(folder + file); @@ -46,9 +47,17 @@ public class SpongeVanilla extends AbstractServerType { String newestVersion = CommonFunctions.stringBetween(versionText, srcStart, srcEnd); String jarURL = downloadURL + newestVersion + downloadURLPart + newestVersion + ".jar"; - String oldVersion = lastVersion; - lastVersion = newestVersion; - return (filePath.isFile() && newestVersion.equals(oldVersion)) || downloadFile(jarURL, Paths.get(filePath.toURI())); + String oldVersion = versionContainer.getSpongeVanillaVersion(version); + //The file is already the newest version + if (filePath.isFile() && newestVersion.equals(oldVersion)) { + return true; + } + //The new jar file could not be downloaded + if (!downloadFile(jarURL, Paths.get(filePath.toURI()))) { + return false; + } + versionContainer.setSpongeVanillaVersion(version, newestVersion); + return true; } } diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Travertine.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Travertine.java index 550e06a..8286d6f 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Travertine.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Travertine.java @@ -1,5 +1,7 @@ package net.knarcraft.minecraftserverlauncher.server.servertypes; +import net.knarcraft.minecraftserverlauncher.server.ServerVersionContainer; + public class Travertine extends Waterfall { /** @@ -13,8 +15,11 @@ public class Travertine extends Waterfall { * @param srcEnd

The string marking the end of the version id

* @param downloadURL

The URL used for downloading the latest version

*/ - public Travertine(String typeName, boolean isProxy, String[] versions, String versionURL, String srcStart, String srcEnd, String downloadURL) { + public Travertine(String typeName, boolean isProxy, String[] versions, String versionURL, String srcStart, + String srcEnd, String downloadURL) { super(typeName, isProxy, versions, versionURL, srcStart, srcEnd, downloadURL); + this.oldVersion = ServerVersionContainer.getInstance().getTravertineVersion(); + this.versionUpdateFunction = (newVersion) -> serverVersionContainer.setTravertineVersion(newVersion); } } diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Vanilla.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Vanilla.java index fd2a5b7..76dabb1 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Vanilla.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Vanilla.java @@ -4,6 +4,7 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import net.knarcraft.minecraftserverlauncher.server.ServerVersionContainer; import java.io.File; import java.io.FileNotFoundException; @@ -19,8 +20,6 @@ import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.read public class Vanilla extends AbstractServerType { private final String versionURL; - private String lastVanillaVersion; - private String lastSnapshotVersion; /** * Instantiates a vanilla server type @@ -38,12 +37,14 @@ public class Vanilla extends AbstractServerType { @Override public boolean downloadJar(String folder, String version) throws IOException { + ServerVersionContainer versionContainer = ServerVersionContainer.getInstance(); String file = this.getName() + version + ".jar"; File filePath = new File(folder + file); if (version.equals("Latest") || version.equals("Snapshot")) { String releaseType = version.equals("Latest") ? "release" : "snapshot"; - String lastVersion = version.equals("Latest") ? lastVanillaVersion : lastSnapshotVersion; - return downloadLatestJar(filePath, releaseType, lastVersion); + String lastVersion = version.equals("Latest") ? versionContainer.getVanillaVersion() : + versionContainer.getSnapshotVersion(); + return downloadLatestJar(filePath, releaseType, lastVersion, versionContainer); } else { String downloadURL = getVanillaDownloadURL(getServerFileVersionURL(version)); return filePath.isFile() || downloadFile(downloadURL, Paths.get(filePath.toURI())); @@ -56,19 +57,29 @@ public class Vanilla extends AbstractServerType { * @param filePath

The path of the jar file to download

* @param releaseType

The release type used for downloading

* @param lastVersion

The last server version found

+ * @param versionContainer

The version container to use

* @return

True if the jar exists and is the latest version or was downloaded

* @throws IOException

If the .jar cannot be downloaded

*/ - private boolean downloadLatestJar(File filePath, String releaseType, String lastVersion) throws IOException { + private boolean downloadLatestJar(File filePath, String releaseType, String lastVersion, + ServerVersionContainer versionContainer) throws IOException { String[] latestData = getLatestFile(releaseType); String latest = latestData[0]; String jarFile = latestData[1]; - if (releaseType.equals("release")) { - lastVanillaVersion = latest; - } else { - lastSnapshotVersion = latest; + //The file is already the newest version + if (filePath.isFile() && latest.equals(lastVersion)) { + return true; } - return (filePath.isFile() && latest.equals(lastVersion)) || downloadFile(jarFile, Paths.get(filePath.toURI())); + //The new jar file could not be downloaded + if (!downloadFile(jarFile, Paths.get(filePath.toURI()))) { + return false; + } + if (releaseType.equals("release")) { + versionContainer.setVanillaVersion(latest); + } else { + versionContainer.setSnapshotVersion(latest); + } + return true; } /** diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Waterfall.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Waterfall.java index 6f4f002..cea6ce0 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Waterfall.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Waterfall.java @@ -1,8 +1,12 @@ package net.knarcraft.minecraftserverlauncher.server.servertypes; +import net.knarcraft.minecraftserverlauncher.server.ServerVersionContainer; + import java.io.File; import java.io.IOException; import java.nio.file.Paths; +import java.util.function.Consumer; +import java.util.function.Function; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.readFile; @@ -13,7 +17,9 @@ public class Waterfall extends AbstractServerType { private String srcStart; private String srcEnd; private String versionURL; - private String lastVersion; + String oldVersion; + Consumer versionUpdateFunction; + ServerVersionContainer serverVersionContainer; /** * Instantiates a new Waterfall server type @@ -29,9 +35,12 @@ public class Waterfall extends AbstractServerType { public Waterfall(String typeName, boolean isProxy, String[] versions, String versionURL, String srcStart, String srcEnd, String downloadURL) { super(typeName, isProxy, versions, downloadURL); + this.serverVersionContainer = ServerVersionContainer.getInstance(); this.srcStart = srcStart; this.srcEnd = srcEnd; this.versionURL = versionURL; + this.oldVersion = serverVersionContainer.getWaterfallVersion(); + this.versionUpdateFunction = (newVersion) -> serverVersionContainer.setWaterfallVersion(newVersion); } @Override @@ -39,9 +48,16 @@ public class Waterfall extends AbstractServerType { String file = this.getName() + version + ".jar"; File filePath = new File(folder + file); String newestVersion = stringBetween(readFile(versionURL), srcStart, srcEnd); - String oldVersion = lastVersion; - lastVersion = newestVersion; String fullURL = downloadURL + newestVersion + "/download"; - return (filePath.isFile() && newestVersion.equals(oldVersion)) || downloadFile(fullURL, Paths.get(filePath.toURI())); + //The file is already the newest version + if (filePath.isFile() && newestVersion.equals(this.oldVersion)) { + return true; + } + //The new jar file could not be downloaded + if (!downloadFile(fullURL, Paths.get(filePath.toURI()))) { + return false; + } + versionUpdateFunction.accept(newestVersion); + return true; } } diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java index e26a7ec..adab827 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java @@ -2,13 +2,7 @@ package net.knarcraft.minecraftserverlauncher.utility; import net.knarcraft.minecraftserverlauncher.userinterface.ServerLauncherGUI; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.*; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; @@ -161,4 +155,20 @@ public final class CommonFunctions { e1.printStackTrace(); } } + + /** + * Reads all lines from a buffered reader + * + * @param reader

The buffered reader to read from

+ * @return

All lines currently readable from the reader, split by the \n character

+ * @throws IOException

If unable to read from the buffered reader

+ */ + public static String readBufferedReader(BufferedReader reader) throws IOException { + String line; + StringBuilder text = new StringBuilder(); + while (reader.ready() && (line = reader.readLine()) != null) { + text.append(line).append("\n"); + } + return text.toString().trim(); + } } diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainerTest.java b/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainerTest.java new file mode 100644 index 0000000..7d39795 --- /dev/null +++ b/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainerTest.java @@ -0,0 +1,38 @@ +package net.knarcraft.minecraftserverlauncher.server; + +import net.knarcraft.minecraftserverlauncher.Main; +import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions; +import org.junit.Before; +import org.junit.Test; + +import java.io.*; + +import static junit.framework.TestCase.assertEquals; + +public class ServerVersionContainerTest { + + private String versionFile = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "versions.csv"; + private ServerVersionContainer serverVersionContainer; + + @Before + public void setUp() { + serverVersionContainer = ServerVersionContainer.getInstance(); + } + + @Test + public void toStringTest() { + serverVersionContainer.reset(); + assertEquals("vanillaVersion;null\nsnapshotVersion;null\nbungeeVersion;null\nwaterfallVersion;null\n" + + "travertineVersion;null\nspongeVanillaVersions;", + serverVersionContainer.toString()); + } + + @Test + public void saveStateTest() throws IOException { + serverVersionContainer.saveState(); + BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(versionFile))); + String savedData = CommonFunctions.readBufferedReader(reader); + assertEquals(serverVersionContainer.toString(), savedData); + } + +} diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarDownloaderTest.java b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarDownloaderTest.java index 157eecb..4477c92 100644 --- a/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarDownloaderTest.java +++ b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarDownloaderTest.java @@ -100,7 +100,11 @@ public class JarDownloaderTest { * Removes downloaded test jars */ private static void removeDownloadedFiles() { - File[] oldFiles = new File(targetDirectory).listFiles(); + File target = new File(targetDirectory); + if (!target.exists() && !target.mkdirs()) { + throw new IllegalArgumentException("Unable to create the test files directory"); + } + File[] oldFiles = target.listFiles(); if (oldFiles == null) { throw new IllegalArgumentException("Unable to list files in jar test directory"); }