Skip to content

Commit 1dca77e

Browse files
author
epicbigcat
committed
feat: add test suite for java version detector.
1 parent d898bab commit 1dca77e

File tree

3 files changed

+99
-8
lines changed

3 files changed

+99
-8
lines changed

src/util/java.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,16 @@
77

88
namespace allay_launcher::util::java {
99

10-
Version installed_version() {
10+
Version installed_version(std::string_view cmdoutput) {
1111
using namespace string;
1212

13-
// Do not use "java --version", which is not supported on java-1.8
14-
// The output of this command will be written into error stream
15-
// so we need to add the suffix "2>&1" to redirect the output
16-
auto output = os::execute("java -version 2>&1");
17-
18-
auto lines = split_nocopy(output, "\n");
13+
auto lines = split_nocopy(cmdoutput, "\n");
1914
auto first_line_tokens = split_nocopy(lines.at(0), " ");
2015

2116
// openjdk version "21.0.4" 2024-07-16
2217
// ^ (optional)
2318
if (first_line_tokens.size() < 3) {
24-
throw ParserException(output, "unable to parse java version.");
19+
throw ParserException(cmdoutput, "unable to parse java version.");
2520
}
2621

2722
auto version_string = first_line_tokens.at(2);
@@ -40,6 +35,13 @@ Version installed_version() {
4035
}
4136
}
4237

38+
Version installed_version() {
39+
// Do not use "java --version", which is not supported on java-1.8
40+
// The output of this command will be written into error stream
41+
// so we need to add the suffix "2>&1" to redirect the output
42+
return installed_version(os::execute("java -version 2>&1"));
43+
}
44+
4345
bool check_java() try {
4446
bool is_java_ok = false;
4547

src/util/java.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace allay_launcher::util::java {
66

7+
Version installed_version(std::string_view cmdoutput);
8+
79
/* @Throwable ParserException
810
* @Throwable CommandExecutionException
911
*/

tests/util/java.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include "test_suite.h" // IWYU pragma: keep
2+
3+
#include "util/java.h"
4+
5+
using namespace util::java;
6+
7+
TEST(TestJavaVersionDetection, Java_8) {
8+
auto version = installed_version(R"(java version "1.8.0_421"
9+
Java(TM) SE Runtime Environment (build 1.8.0_421-b08)
10+
Java HotSpot(TM) 64-Bit Server VM (build 25.421-b08, mixed mode)
11+
)");
12+
auto expected = Version{1, 8, 0};
13+
EXPECT_EQ(version, expected);
14+
}
15+
16+
TEST(TestJavaVersionDetection, OpenJDK_11) {
17+
auto version = installed_version(R"(openjdk version "11.0.23" 2024-04-16
18+
OpenJDK Runtime Environment (build 11.0.23+9-LTS)
19+
OpenJDK 64-Bit Server VM (build 11.0.23+9-LTS, mixed mode)
20+
)");
21+
auto expected = Version{11, 0, 23};
22+
EXPECT_EQ(version, expected);
23+
}
24+
25+
TEST(TestJavaVersionDetection, OpenJDK_17) {
26+
auto version = installed_version(R"(openjdk version "17.0.11" 2024-04-16
27+
OpenJDK Runtime Environment (build 17.0.11+9-LTS)
28+
OpenJDK 64-Bit Server VM (build 17.0.11+9-LTS, mixed mode, sharing)
29+
)");
30+
auto expected = Version{17, 0, 11};
31+
EXPECT_EQ(version, expected);
32+
}
33+
34+
TEST(TestJavaVersionDetection, OpenJDK_21) {
35+
auto version = installed_version(R"(openjdk version "21.0.3" 2024-04-16 LTS
36+
OpenJDK Runtime Environment (build 21.0.3+9-LTS)
37+
OpenJDK 64-Bit Server VM (build 21.0.3+9-LTS, mixed mode, sharing)
38+
)");
39+
auto expected = Version{21, 0, 3};
40+
EXPECT_EQ(version, expected);
41+
}
42+
43+
TEST(TestJavaVersionDetection, OpenJDK_23) {
44+
auto version = installed_version(R"(openjdk version "23"
45+
2024-09-17 OpenJDK Runtime Environment (build 23+37-1231)
46+
OpenJDK 64-Bit Server VM (build 23+37-1231, mixed mode, sharing)
47+
)");
48+
auto expected = Version{23, 0, 0};
49+
EXPECT_EQ(version, expected);
50+
}
51+
52+
TEST(TestJavaVersionDetection, OpenJDK_25_EA) {
53+
auto version = installed_version(R"(openjdk version "25-ea" 2026-03-17
54+
OpenJDK Runtime Environment (build 25-ea+3-19)
55+
OpenJDK 64-Bit Server VM (build 25-ea+3-19, mixed mode, sharing)
56+
)");
57+
auto expected = Version{25, 0, 0};
58+
EXPECT_EQ(version, expected);
59+
}
60+
61+
TEST(TestJavaVersionDetection, OpenJDK_GraalVM_11) {
62+
auto version = installed_version(R"(openjdk version "11.0.16" 2022-07-19
63+
OpenJDK Runtime Environment GraalVM EE 22.2.0 (build 11.0.16+8-jvmci-22.2-b08)
64+
OpenJDK 64-Bit Server VM GraalVM EE 22.2.0 (build 11.0.16+8-jvmci-22.2-b08, mixed mode, sharing)
65+
66+
)");
67+
auto expected = Version{11, 0, 16};
68+
EXPECT_EQ(version, expected);
69+
}
70+
71+
TEST(TestJavaVersionDetection, GraalVM_21) {
72+
auto version = installed_version(R"(java version "21.0.8" 2025-07-15 LTS
73+
Java(TM) SE Runtime Environment Oracle GraalVM 21.0.8+12.1 (build 21.0.8+12-LTS-jvmci-23.1-b72)
74+
Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 21.0.8+12.1 (build 21.0.8+12-LTS-jvmci-23.1-b72, mixed mode, sharing)
75+
)");
76+
auto expected = Version{21, 0, 8};
77+
EXPECT_EQ(version, expected);
78+
}
79+
80+
TEST(TestJavaVersionDetection, GraalVM_24) {
81+
auto version = installed_version(R"(java version "24.0.2" 2025-07-15
82+
Java(TM) SE Runtime Environment Oracle GraalVM 24.0.2+11.1 (build 24.0.2+11-jvmci-b01)
83+
Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 24.0.2+11.1 (build 24.0.2+11-jvmci-b01, mixed mode, sharing)
84+
)");
85+
auto expected = Version{24, 0, 2};
86+
EXPECT_EQ(version, expected);
87+
}

0 commit comments

Comments
 (0)