diff --git a/pom.xml b/pom.xml
index 6f8a492..c6530a0 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
The currently selected server version for the given server type
* @param maxRamThe maximum amount of ram the server is allowed to use
*/ - public Server(String name, String path, boolean enabled, String typeName, String serverVersion, String maxRam) { + public Server(String name, String path, boolean enabled, String typeName, String serverVersion, String maxRam) throws ConfigurationException { this.name = name; this.path = path; this.enabled = enabled; @@ -133,7 +134,7 @@ public class Server implements java.io.Serializable { * @param saveStringThe string containing necessary data regarding the server
* @returnA server in the same state it was saved in
*/ - public static Server fromString(String saveString) { + public static Server fromString(String saveString) throws ConfigurationException { String[] data = saveString.split(";"); return new Server(data[0], data[1], Boolean.parseBoolean(data[2]), data[3], data[4], data[5]); } @@ -385,7 +386,7 @@ public class Server implements java.io.Serializable { } else { serverFile = this.type.getName() + serverVersion + ".jar"; } - if (Profile.getCurrent().getDownloadAllAvailableJARFiles() && !type.getName().equals("Custom")) { + if (!type.getName().equals("Custom")) { serverPath = jarDirectory + serverFile; } else { serverPath = this.path + File.separator + serverFile; @@ -444,9 +445,11 @@ public class Server implements java.io.Serializable { * @throws FileNotFoundExceptionIf the file was not found and could not be acquired
*/ private void downloadJar() throws IOException { - String path = this.path + File.separator; + String path; if (this.type.getName().equals("Custom")) { - path += this.serverVersion; + path = this.path + File.separator + this.serverVersion; + } else { + path = jarDirectory; } File file = new File(path); if (!(file.isFile() || type.downloadJar(path, this.serverVersion))) { diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerTypeHandler.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerTypeHandler.java index d1fcef9..ce5c4f2 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerTypeHandler.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerTypeHandler.java @@ -6,7 +6,6 @@ import net.knarcraft.minecraftserverlauncher.server.servertypes.Custom; import net.knarcraft.minecraftserverlauncher.server.servertypes.MCPCPlus; import net.knarcraft.minecraftserverlauncher.server.servertypes.Paper; import net.knarcraft.minecraftserverlauncher.server.servertypes.ServerType; -import net.knarcraft.minecraftserverlauncher.server.servertypes.Snapshot; import net.knarcraft.minecraftserverlauncher.server.servertypes.Spigot; import net.knarcraft.minecraftserverlauncher.server.servertypes.SpongeVanilla; import net.knarcraft.minecraftserverlauncher.server.servertypes.Travertine; @@ -33,6 +32,9 @@ public class ServerTypeHandler { * @returnA list of strings
*/ public static String[] getTypeNames() throws ConfigurationException { + if (serverTypes.isEmpty()) { + loadServerTypes(); + } ArrayListThen name of the server type
* @returnA AbstractServerType
*/ - public static ServerType getByName(String name) { + public static ServerType getByName(String name) throws ConfigurationException { + if (serverTypes.isEmpty()) { + loadServerTypes(); + } for (ServerType serverType : serverTypes) { if (serverType.getName().equals(name)) { return serverType; @@ -117,10 +122,6 @@ public class ServerTypeHandler { newType = new Vanilla("Vanilla", false, serverVersions, serverTypeInfo[2], serverTypeInfo[3]); break; - case "Snapshot": - newType = new Snapshot("Snapshot", false, serverVersions, serverTypeInfo[2], - serverTypeInfo[3]); - break; case "MCPCplus": newType = new MCPCPlus("MCPCplus", false, serverVersions, serverTypeInfo[2], serverTypeInfo[3]); diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/CraftBukkit.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/CraftBukkit.java index a1313e2..a56e389 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/CraftBukkit.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/CraftBukkit.java @@ -5,6 +5,9 @@ import java.nio.file.Paths; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile; +/** + * This class represents the CraftBukkit Minecraft server type + */ public class CraftBukkit extends AbstractServerType { private String downloadURLPart; diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Snapshot.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Snapshot.java deleted file mode 100644 index 6e172b2..0000000 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Snapshot.java +++ /dev/null @@ -1,19 +0,0 @@ -package net.knarcraft.minecraftserverlauncher.server.servertypes; - -public class Snapshot extends Vanilla { - - /** - * Instantiates a snapshot server type - * - * @param typeNameThe name of this server type
- * @param isProxyWhether this server type is a proxy server
- * @param versionsAvailable versions for this server type
- * @param versionURLThe URL used for downloading the version document
- * @param downloadURLThe URL used for downloading the new file
- */ - public Snapshot(String typeName, boolean isProxy, String[] versions, String versionURL, String downloadURL) { - super(typeName, isProxy, versions, versionURL, downloadURL); - this.releaseType = "snapshot"; - } - -} 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 365277f..fd2a5b7 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Vanilla.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Vanilla.java @@ -13,11 +13,14 @@ import java.nio.file.Paths; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.readFile; +/** + * This class represents the regular vanilla server type + */ public class Vanilla extends AbstractServerType { private final String versionURL; - String releaseType; - private String lastVersion; + private String lastVanillaVersion; + private String lastSnapshotVersion; /** * Instantiates a vanilla server type @@ -31,33 +34,51 @@ public class Vanilla extends AbstractServerType { public Vanilla(String typeName, boolean isProxy, String[] versions, String versionURL, String downloadURL) { super(typeName, isProxy, versions, downloadURL); this.versionURL = versionURL; - this.releaseType = "release"; } @Override public boolean downloadJar(String folder, String version) throws IOException { String file = this.getName() + version + ".jar"; File filePath = new File(folder + file); - if (version.equals("Latest")) { - String[] latestData = getLatestFile(); - String latest = latestData[0]; - String jarFile = latestData[1]; - String currentVersion = lastVersion; - lastVersion = latest; - return (filePath.isFile() && latest.equals(currentVersion)) || downloadFile(jarFile, Paths.get(filePath.toURI())); + 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); } else { String downloadURL = getVanillaDownloadURL(getServerFileVersionURL(version)); return filePath.isFile() || downloadFile(downloadURL, Paths.get(filePath.toURI())); } } + /** + * Downloads the latest .jar file found if necessary + * + * @param filePathThe path of the jar file to download
+ * @param releaseTypeThe release type used for downloading
+ * @param lastVersionThe last server version found
+ * @returnTrue if the jar exists and is the latest version or was downloaded
+ * @throws IOExceptionIf the .jar cannot be downloaded
+ */ + private boolean downloadLatestJar(File filePath, String releaseType, String lastVersion) throws IOException { + String[] latestData = getLatestFile(releaseType); + String latest = latestData[0]; + String jarFile = latestData[1]; + if (releaseType.equals("release")) { + lastVanillaVersion = latest; + } else { + lastSnapshotVersion = latest; + } + return (filePath.isFile() && latest.equals(lastVersion)) || downloadFile(jarFile, Paths.get(filePath.toURI())); + } + /** * Gets the URL to the .jar file for the newest version * + * @param releaseTypeThe type of release to read latest version from
* @returnAn array containing the latest version and a link to its file
* @throws IOExceptionIf the remote resource cannot be readFromServer
*/ - private String[] getLatestFile() throws IOException { + private String[] getLatestFile(String releaseType) throws IOException { String versionText = readFile(versionURL); JsonObject jsonObject = new JsonParser().parse(versionText).getAsJsonObject(); String latest = jsonObject.getAsJsonObject("latest").get(releaseType).getAsString(); diff --git a/src/main/resources/servertypes.csv b/src/main/resources/servertypes.csv index 535be3b..0e6ac59 100644 --- a/src/main/resources/servertypes.csv +++ b/src/main/resources/servertypes.csv @@ -6,6 +6,5 @@ Paper;1.16.1,1.15.2,1.14.4,1.13.2,1.12.2,1.11.2,1.10.2,1.9.4,1.8.8;https://stati Bungee;Latest;https://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/;Artifacts of BungeeCord #; ;http://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/BungeeCord.jar Waterfall;Latest;https://papermc.io/api/v1/waterfall/1.16;"latest":";";https://papermc.io/api/v1/waterfall/1.16/ Travertine;Latest;https://papermc.io/api/v1/travertine/1.16;"latest":";";https://papermc.io/api/v1/travertine/1.16/ -Snapshot;Latest;https://launchermeta.mojang.com/mc/game/version_manifest.json;"snapshot":";";https://s3.amazonaws.com/Minecraft.Download/versions/ -Vanilla;Latest,1.16.1,1.15.2,1.14.4,1.13.2,1.12.2,1.11.2,1.10.2,1.9.4,1.8.9,1.7.10,1.6.4,1.5.2,1.4.7,1.3.2,1.2.5;https://launchermeta.mojang.com/mc/game/version_manifest.json;"release":";";https://s3.amazonaws.com/Minecraft.Download/versions/ +Vanilla;Latest,Snapshot,1.16.1,1.15.2,1.14.4,1.13.2,1.12.2,1.11.2,1.10.2,1.9.4,1.8.9,1.7.10,1.6.4,1.5.2,1.4.7,1.3.2,1.2.5;https://launchermeta.mojang.com/mc/game/version_manifest.json;"release":";";https://s3.amazonaws.com/Minecraft.Download/versions/ Custom;; \ No newline at end of file diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/DownloadTests.java b/src/test/java/net/knarcraft/minecraftserverlauncher/DownloadTests.java deleted file mode 100644 index b2afd3d..0000000 --- a/src/test/java/net/knarcraft/minecraftserverlauncher/DownloadTests.java +++ /dev/null @@ -1,18 +0,0 @@ -package net.knarcraft.minecraftserverlauncher; - -import net.knarcraft.minecraftserverlauncher.userinterface.FakeGUI; -import net.knarcraft.minecraftserverlauncher.utility.JarDownloader; -import org.junit.Test; - -import java.io.File; -import java.io.IOException; - -public class DownloadTests { - - @Test - public void downloadJarsTest() throws IOException { - String targetDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "testjars" + File.separator; - JarDownloader downloader = new JarDownloader(new FakeGUI(), targetDirectory); - downloader.downloadJars(); - } -} diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerTypeHandlerTest.java b/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerTypeHandlerTest.java new file mode 100644 index 0000000..d466cfb --- /dev/null +++ b/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerTypeHandlerTest.java @@ -0,0 +1,38 @@ +package net.knarcraft.minecraftserverlauncher.server; + +import net.knarcraft.minecraftserverlauncher.server.servertypes.ServerType; +import org.junit.Test; + +import javax.naming.ConfigurationException; + +import java.util.Arrays; + +import static junit.framework.TestCase.assertEquals; + +public class ServerTypeHandlerTest { + + @Test + public void getTypeNamesTest() throws ConfigurationException { + String[] serverTypeNames = ServerTypeHandler.getTypeNames(); + + String[] typeNames = new String[serverTypeNames.length]; + int index = 0; + for (ServerType serverType : ServerTypeHandler.getServerTypes()) { + typeNames[index++] = serverType.getName(); + } + assertEquals(Arrays.asList(typeNames), Arrays.asList(serverTypeNames)); + } + + @Test + public void getTypeFromNameTest() throws ConfigurationException { + ServerType targetType = null; + for (ServerType serverType : ServerTypeHandler.getServerTypes()) { + if (serverType.getName().equals("Vanilla")) { + targetType = serverType; + } + } + ServerType typeFromName = ServerTypeHandler.getByName("Vanilla"); + assertEquals(targetType, typeFromName); + } + +} diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/Tests.java b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctionsTest.java similarity index 74% rename from src/test/java/net/knarcraft/minecraftserverlauncher/Tests.java rename to src/test/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctionsTest.java index 61f95d5..6a74f0f 100644 --- a/src/test/java/net/knarcraft/minecraftserverlauncher/Tests.java +++ b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctionsTest.java @@ -1,4 +1,4 @@ -package net.knarcraft.minecraftserverlauncher; +package net.knarcraft.minecraftserverlauncher.utility; import net.knarcraft.minecraftserverlauncher.profile.Profile; import org.junit.Test; @@ -8,16 +8,16 @@ import java.io.FileNotFoundException; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.stringBetween; import static org.junit.Assert.assertEquals; -public class Tests { +public class CommonFunctionsTest { @Test - public void saveProfile() throws FileNotFoundException { //Make sure we can write profiles to disk + public void saveProfileTest() throws FileNotFoundException { Profile.addProfile("Test"); Profile.getCurrent().save(); } @Test - public void stringBetweenTest() { //Make sure stringBetween is not creating exceptions + public void stringBetweenTest() { String substring = stringBetween("fish'nchips", "f", "'"); assertEquals("ish", substring); substring = stringBetween("something", "whale", "fish"); diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarDownloaderTest.java b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarDownloaderTest.java new file mode 100644 index 0000000..157eecb --- /dev/null +++ b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarDownloaderTest.java @@ -0,0 +1,111 @@ +package net.knarcraft.minecraftserverlauncher.utility; + +import net.knarcraft.minecraftserverlauncher.Main; +import net.knarcraft.minecraftserverlauncher.server.ServerTypeHandler; +import net.knarcraft.minecraftserverlauncher.server.servertypes.ServerType; +import net.knarcraft.minecraftserverlauncher.userinterface.FakeGUI; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import javax.naming.ConfigurationException; +import java.io.File; +import java.io.IOException; + +import static junit.framework.TestCase.assertNotNull; +import static junit.framework.TestCase.assertTrue; + +public class JarDownloaderTest { + private static String targetDirectory; + + @BeforeClass + public static void setUp() { + targetDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + + "testjars" + File.separator; + removeDownloadedFiles(); + } + + @AfterClass + public static void cleanUp() { + removeDownloadedFiles(); + } + + @Test + public void downloadJarsTest() throws IOException { + JarDownloader downloader = new JarDownloader(new FakeGUI(), targetDirectory); + downloader.downloadJars(); + } + + @Test + public void spongeVanillaDownloadTest() throws IOException, ConfigurationException { + singleDownloadTest(ServerTypeHandler.getByName("SpongeVanilla")); + } + + @Test + public void spigotDownloadTest() throws ConfigurationException, IOException { + singleDownloadTest(ServerTypeHandler.getByName("Spigot")); + } + + @Test + public void mcpcPlusDownloadTest() throws ConfigurationException, IOException { + singleDownloadTest(ServerTypeHandler.getByName("MCPCplus")); + } + + @Test + public void craftbukkitDownloadTest() throws ConfigurationException, IOException { + singleDownloadTest(ServerTypeHandler.getByName("Bukkit")); + } + + @Test + public void paperDownloadTest() throws ConfigurationException, IOException { + singleDownloadTest(ServerTypeHandler.getByName("Paper")); + } + + @Test + public void bungeeDownloadTest() throws ConfigurationException, IOException { + singleDownloadTest(ServerTypeHandler.getByName("Bungee")); + } + + @Test + public void waterfallDownloadTest() throws ConfigurationException, IOException { + singleDownloadTest(ServerTypeHandler.getByName("Waterfall")); + } + + @Test + public void travertineDownloadTest() throws ConfigurationException, IOException { + singleDownloadTest(ServerTypeHandler.getByName("Travertine")); + } + + @Test + public void vanillaDownloadTest() throws ConfigurationException, IOException { + singleDownloadTest(ServerTypeHandler.getByName("Vanilla")); + } + + /** + * Downloads all .jar files for a single server type and asserts it works + * + * @param serverTypeThe server type to test
+ * @throws IOExceptionIf unable to download any of the .jar files
+ */ + private void singleDownloadTest(ServerType serverType) throws IOException { + assertNotNull(serverType); + for (String serverVersion : serverType.getVersions()) { + System.out.println("Downloading " + serverType.getName() + serverVersion + ".jar"); + serverType.downloadJar(targetDirectory, serverVersion); + assertTrue(new File(targetDirectory + serverType.getName() + serverVersion + ".jar").exists()); + } + } + + /** + * Removes downloaded test jars + */ + private static void removeDownloadedFiles() { + File[] oldFiles = new File(targetDirectory).listFiles(); + if (oldFiles == null) { + throw new IllegalArgumentException("Unable to list files in jar test directory"); + } + for (File file : oldFiles) { + assertTrue(file.delete()); + } + } +}