fix: invalid Java versions being found causing crashes

This commit is contained in:
Ryan Dowling 2019-06-25 12:28:00 +10:00
parent c90f57bf64
commit 5a7743e3f1
No known key found for this signature in database
GPG key ID: 5539FCDB88950EFD
8 changed files with 177 additions and 13 deletions

2
.gitignore vendored
View file

@ -6,7 +6,7 @@ bin/
build/
dist/
target/
test/
testLauncher/
# Files
.classpath

2
.vscode/launch.json vendored
View file

@ -7,7 +7,7 @@
"request": "launch",
"mainClass": "com.atlauncher.App",
"projectName": "ATLauncher",
"cwd": "${workspaceFolder}/test",
"cwd": "${workspaceFolder}/testLauncher",
"args": "--debug --debug-level 3"
}
]

View file

@ -1,9 +1,5 @@
# Changelog
## 3.2.9.1
## 3.2.9.2
- Increase width of dropdown when selecting a curse mod file
- Add some basic filtering to not show files for a different loader
- Add a warning and button when Fabric API not installed for Fabric loader
- Show dependant mods when installing a mod to an instance
- Fix crash on 64 bit java check
- Fix invalid Java versions being found causing crashes

View file

@ -14,7 +14,7 @@ sourceCompatibility = '1.8'
targetCompatibility = '1.8'
group = 'com.atlauncher'
version = '3.2.9.1'
version = '3.2.9.2'
repositories {
mavenCentral()

View file

@ -18,7 +18,7 @@
package com.atlauncher.data;
public class Constants {
public static final LauncherVersion VERSION = new LauncherVersion(3, 2, 9, 1);
public static final LauncherVersion VERSION = new LauncherVersion(3, 2, 9, 2);
public static final String LAUNCHER_NAME = "ATLauncher";
public static final String DISCORD_CLIENT_ID = "589393213723246592";
public static final String API_BASE_URL = "https://api.atlauncher.com/v1/launcher/";

View file

@ -43,9 +43,11 @@ import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import com.atlauncher.App;
import com.atlauncher.FileSystem;
import com.atlauncher.Gsons;
import com.atlauncher.LogManager;
import com.atlauncher.Update;
import com.atlauncher.data.Constants;
@ -215,8 +217,21 @@ public enum OS {
return null;
}
installedJavas.stream().forEach(version -> {
LogManager.debug(Gsons.DEFAULT.toJson(version));
});
List<JavaInfo> validVersions = installedJavas.stream()
.filter(javaInfo -> javaInfo.majorVersion != null && javaInfo.minorVersion != null)
.collect(Collectors.toList());
if (validVersions.size() == 0) {
return null;
}
// get newest Java 8 64 bit if installed
Optional<JavaInfo> java864bit = installedJavas.stream()
Optional<JavaInfo> java864bit = validVersions.stream()
.filter(javaInfo -> javaInfo.majorVersion != null && javaInfo.minorVersion != null)
.sorted(Comparator.comparingInt((JavaInfo javaInfo) -> javaInfo.minorVersion).reversed())
.filter(javaInfo -> javaInfo.majorVersion == 8 && javaInfo.is64bits).findFirst();
if (java864bit.isPresent()) {
@ -224,13 +239,13 @@ public enum OS {
}
// get newest 64 bit if installed
Optional<JavaInfo> java64bit = installedJavas.stream().filter(javaInfo -> javaInfo.is64bits).findFirst();
Optional<JavaInfo> java64bit = validVersions.stream().filter(javaInfo -> javaInfo.is64bits).findFirst();
if (java64bit.isPresent()) {
return java64bit.get().rootPath;
}
// default to the first java installed
return installedJavas.get(0).rootPath;
return validVersions.get(0).rootPath;
}
/**

View file

@ -0,0 +1,47 @@
/*
* ATLauncher - https://github.com/ATLauncher/ATLauncher
* Copyright (C) 2013-2019 ATLauncher
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.atlauncher.utils.Java;
import org.junit.jupiter.api.Test;
public class TestJava {
@Test
public void testThatJavaVersionNumberIsReturnedCorrectly() {
assertEquals(7, Java.parseJavaVersionNumber("1.7.0_64"));
assertEquals(8, Java.parseJavaVersionNumber("1.8.0_212"));
assertEquals(9, Java.parseJavaVersionNumber("9.0.4"));
assertEquals(10, Java.parseJavaVersionNumber("10.0.2"));
assertEquals(11, Java.parseJavaVersionNumber("11.0.3"));
assertEquals(12, Java.parseJavaVersionNumber("12.0.1"));
assertEquals(13, Java.parseJavaVersionNumber("13-ea"));
}
@Test
public void testThatJavaBuildNumberIsReturnedCorrectly() {
assertEquals(64, Java.parseJavaBuildVersion("1.7.0_64"));
assertEquals(212, Java.parseJavaBuildVersion("1.8.0_212"));
assertEquals(4, Java.parseJavaBuildVersion("9.0.4"));
assertEquals(2, Java.parseJavaBuildVersion("10.0.2"));
assertEquals(3, Java.parseJavaBuildVersion("11.0.3"));
assertEquals(1, Java.parseJavaBuildVersion("12.0.1"));
assertEquals(0, Java.parseJavaBuildVersion("13-ea"));
}
}

106
src/test/java/TestOS.java Normal file
View file

@ -0,0 +1,106 @@
/*
* ATLauncher - https://github.com/ATLauncher/ATLauncher
* Copyright (C) 2013-2019 ATLauncher
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.List;
import com.atlauncher.utils.OS;
import com.atlauncher.utils.javafinder.JavaInfo;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class TestOS {
private List<JavaInfo> installedJavas;
@BeforeEach
public void initialize() {
installedJavas = new ArrayList<>();
}
@Test
public void testThatJava864BitIsPrefferedOverJava832Bit() {
// Test preferencing Java 8 64 bit over Java 8 32 bit
installedJavas.add(new JavaInfo("C:/Java/8.111/64bit/bin/java.exe", "C:/Java/8/64bit", "8.111", 8, 111, true));
installedJavas.add(new JavaInfo("C:/Java/8.111/32bit/bin/java.exe", "C:/Java/8/32bit", "8.111", 8, 111, false));
assertEquals("C:/Java/8/64bit", OS.getPrefferedJavaPath(installedJavas));
}
@Test
public void testThatJavaVersionsWithSameMajorPreferHigherMinor() {
// Test preferencing the highest minor version when all the same major versions
installedJavas
.add(new JavaInfo("C:/Java/8.108/64bit/bin/java.exe", "C:/Java/8.108/64bit", "8.108", 8, 108, true));
installedJavas
.add(new JavaInfo("C:/Java/8.105/64bit/bin/java.exe", "C:/Java/8.105/64bit", "8.105", 8, 105, true));
installedJavas
.add(new JavaInfo("C:/Java/8.111/64bit/bin/java.exe", "C:/Java/8.111/64bit", "8.111", 8, 111, true));
installedJavas
.add(new JavaInfo("C:/Java/8.102/64bit/bin/java.exe", "C:/Java/8.102/64bit", "8.102", 8, 102, true));
installedJavas
.add(new JavaInfo("C:/Java/8.100/64bit/bin/java.exe", "C:/Java/8.100/64bit", "8.100", 8, 100, true));
assertEquals("C:/Java/8.111/64bit", OS.getPrefferedJavaPath(installedJavas));
}
@Test
public void testThatJava764BitIsPrefferedOverJava832Bit() {
// Test preferencing Java 7 64 bit over Java 8 32 bit
installedJavas.add(new JavaInfo("C:/Java/7.111/64bit/bin/java.exe", "C:/Java/7/64bit", "7.111", 7, 111, true));
installedJavas.add(new JavaInfo("C:/Java/8.111/32bit/bin/java.exe", "C:/Java/8/32bit", "8.111", 8, 111, false));
assertEquals("C:/Java/7/64bit", OS.getPrefferedJavaPath(installedJavas));
}
@Test
public void testThatJava964BitIsPrefferedOverJava732Bit() {
// Test preferencing Java 9 64 bit over Java 7 64 bit
installedJavas.add(new JavaInfo("C:/Java/9.111/64bit/bin/java.exe", "C:/Java/9/64bit", "9.111", 9, 111, true));
installedJavas.add(new JavaInfo("C:/Java/7.111/64bit/bin/java.exe", "C:/Java/7/64bit", "7.111", 7, 111, true));
assertEquals("C:/Java/9/64bit", OS.getPrefferedJavaPath(installedJavas));
}
@Test
public void testThatJava864BitIsPrefferedOverJava964Bit() {
// Test preferencing Java 8 64 bit over Java 9 64 bit
installedJavas.add(new JavaInfo("C:/Java/8.111/64bit/bin/java.exe", "C:/Java/8/64bit", "8.111", 8, 111, true));
installedJavas.add(new JavaInfo("C:/Java/9.111/64bit/bin/java.exe", "C:/Java/9/64bit", "9.111", 9, 111, true));
assertEquals("C:/Java/8/64bit", OS.getPrefferedJavaPath(installedJavas));
}
@Test
public void testThatWhenOnlyASingleJavaIsInstalledItIsReturned() {
// Test preferencing Java 7 32 bit when it's the only option
installedJavas.add(new JavaInfo("C:/Java/7.111/64bit/bin/java.exe", "C:/Java/7/64bit", "7.111", 7, 111, true));
assertEquals("C:/Java/7/64bit", OS.getPrefferedJavaPath(installedJavas));
}
@Test
public void testThatWhenNoJavaIsDetectedOnTheSystemItReturnsNull() {
// Test null being returned when no installed java paths detected
assertEquals(null, OS.getPrefferedJavaPath(installedJavas));
}
@Test
public void testThatWhenAnInvalidJavaVersionIsFoundItReturnsNull() {
// Test an invalid version found returning null
installedJavas.add(new JavaInfo("C:/Java/8.111/64bit/bin/java.exe", "C:/Java/8/64bit", null, null, null, true));
assertEquals(null, OS.getPrefferedJavaPath(installedJavas));
}
}