Skip to content

Commit b390f92

Browse files
committed
test: add JUnit 5 tests for utils and config classes
Add comprehensive unit tests: - CharsetDetectorTest: charset detection, locale parsing, codepage conversion - JsonFormatterTest: metrics formatting, JSON escaping - SystemInfoTest: byte formatting (KiB, MiB, GiB) - ConfigAccessTest: URL parsing, endpoint validation Also update CharsetDetector to make parseLocaleToCharset and codepageToCharset package-private for testing.
1 parent 9698114 commit b390f92

6 files changed

Lines changed: 578 additions & 2 deletions

File tree

build.gradle.kts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ dependencies {
5151

5252
// Mod Menu API (compile-only - for config screen integration)
5353
modCompileOnly("com.terraformersmc:modmenu:${providers.gradleProperty("modmenu_version").get()}")
54+
55+
// JUnit 5 for testing
56+
testImplementation("org.junit.jupiter:junit-jupiter:5.10.2")
57+
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
5458
}
5559

5660
tasks.processResources {
@@ -83,6 +87,10 @@ tasks.jar {
8387
}
8488
}
8589

90+
tasks.test {
91+
useJUnitPlatform()
92+
}
93+
8694
// configure the maven publication
8795
publishing {
8896
publications {

src/main/java/org/damon233/performtrackermod/utils/CharsetDetector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private static Charset detectLinux() {
7777
return parseLocaleToCharset(locale);
7878
}
7979

80-
private static Charset parseLocaleToCharset(String locale) {
80+
static Charset parseLocaleToCharset(String locale) {
8181
if (locale == null) return null;
8282

8383
locale = locale.trim().toUpperCase();
@@ -100,7 +100,7 @@ private static Charset parseLocaleToCharset(String locale) {
100100
return null;
101101
}
102102

103-
private static Charset codepageToCharset(int codepage) {
103+
static Charset codepageToCharset(int codepage) {
104104
return switch (codepage) {
105105
case 65001 -> StandardCharsets.UTF_8;
106106
case 936 -> Charset.forName("GBK");
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package org.damon233.performtrackermod.config;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.CsvSource;
6+
import org.junit.jupiter.params.provider.NullAndEmptySource;
7+
import org.junit.jupiter.params.provider.ValueSource;
8+
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
class ConfigAccessTest {
12+
13+
@ParameterizedTest
14+
@CsvSource({
15+
"http://localhost:31415, 31415",
16+
"http://localhost:8080, 8080",
17+
"http://example.com:3000, 3000",
18+
"https://localhost:443, 443",
19+
"http://127.0.0.1:9000, 9000"
20+
})
21+
void parsePort_withValidUrl_returnsCorrectPort(String url, int expectedPort) {
22+
int result = ConfigAccess.parsePort(url);
23+
assertEquals(expectedPort, result);
24+
}
25+
26+
@Test
27+
void parsePort_withoutPort_returnsDefaultPort() {
28+
String url = "http://localhost";
29+
int result = ConfigAccess.parsePort(url);
30+
assertEquals(31415, result);
31+
}
32+
33+
@Test
34+
void parsePort_withTrailingSlash_returnsDefaultPort() {
35+
String url = "http://localhost/";
36+
int result = ConfigAccess.parsePort(url);
37+
assertEquals(31415, result);
38+
}
39+
40+
@Test
41+
void parsePort_withPath_returnsCorrectPort() {
42+
String url = "http://localhost:8080/api/metrics";
43+
int result = ConfigAccess.parsePort(url);
44+
assertEquals(8080, result);
45+
}
46+
47+
@ParameterizedTest
48+
@CsvSource({
49+
"http://localhost:80/path, 80",
50+
"http://localhost:443/path, 443",
51+
"http://localhost:65535/path, 65535"
52+
})
53+
void parsePort_withVariousPorts_returnsCorrectPort(String url, int expectedPort) {
54+
int result = ConfigAccess.parsePort(url);
55+
assertEquals(expectedPort, result);
56+
}
57+
58+
@ParameterizedTest
59+
@ValueSource(strings = {
60+
"http://localhost:31415",
61+
"https://localhost:31415",
62+
"http://example.com:31415",
63+
"http://sub.example.com:31415",
64+
"http://localhost:8080",
65+
"http://127.0.0.1:31415"
66+
})
67+
void validateNetworkEndpoint_withValidUrl_returnsNormalizedUrl(String url) {
68+
String result = ConfigAccess.validateNetworkEndpoint(url);
69+
assertNotNull(result);
70+
assertTrue(result.endsWith("/"));
71+
}
72+
73+
@ParameterizedTest
74+
@ValueSource(strings = {
75+
"http://localhost:31415",
76+
"https://localhost:31415"
77+
})
78+
void validateNetworkEndpoint_withHttps_preservesProtocol(String url) {
79+
String result = ConfigAccess.validateNetworkEndpoint(url);
80+
assertNotNull(result);
81+
assertTrue(result.startsWith(url.substring(0, url.indexOf("://") + 3).substring(0, 4)));
82+
}
83+
84+
@Test
85+
void validateNetworkEndpoint_addsTrailingSlash() {
86+
String url = "http://localhost:31415";
87+
String result = ConfigAccess.validateNetworkEndpoint(url);
88+
assertTrue(result.endsWith("/"));
89+
}
90+
91+
@ParameterizedTest
92+
@NullAndEmptySource
93+
void validateNetworkEndpoint_withNullOrEmpty_returnsNull(String endpoint) {
94+
String result = ConfigAccess.validateNetworkEndpoint(endpoint);
95+
assertNull(result);
96+
}
97+
98+
@ParameterizedTest
99+
@ValueSource(strings = {
100+
"localhost:31415",
101+
"ftp://localhost:31415",
102+
"ws://localhost:31415",
103+
"httpx://localhost:31415",
104+
"http://localhost",
105+
"not-a-url",
106+
"http://",
107+
"://localhost:31415"
108+
})
109+
void validateNetworkEndpoint_withInvalidUrl_returnsNull(String endpoint) {
110+
String result = ConfigAccess.validateNetworkEndpoint(endpoint);
111+
assertNull(result);
112+
}
113+
114+
@Test
115+
void validateNetworkEndpoint_withOnlyWhitespace_returnsNull() {
116+
String result = ConfigAccess.validateNetworkEndpoint(" ");
117+
assertNull(result);
118+
}
119+
120+
@Test
121+
void validateNetworkEndpoint_withTrailingSlash_preservesIt() {
122+
String url = "http://localhost:31415/";
123+
String result = ConfigAccess.validateNetworkEndpoint(url);
124+
assertNotNull(result);
125+
assertEquals("http://localhost:31415/", result);
126+
}
127+
128+
@Test
129+
void validateNetworkEndpoint_withPath_preservesPath() {
130+
String url = "http://localhost:31415/api";
131+
String result = ConfigAccess.validateNetworkEndpoint(url);
132+
assertNotNull(result);
133+
assertTrue(result.contains("/api"));
134+
}
135+
136+
@ParameterizedTest
137+
@CsvSource({
138+
"http://localhost:31415, true",
139+
"https://localhost:31415, true",
140+
"http://example.com:8080, true",
141+
"localhost:31415, false",
142+
"ftp://localhost:31415, false",
143+
"http://, false"
144+
})
145+
void isValidNetworkEndpoint_withVariousUrls_returnsExpected(String url, boolean expected) {
146+
boolean result = ConfigAccess.isValidNetworkEndpoint(url);
147+
assertEquals(expected, result);
148+
}
149+
150+
@Test
151+
void validateNetworkEndpoint_complexSubdomain_accepted() {
152+
String url = "http://sub.domain.example.com:8080/api/v1";
153+
String result = ConfigAccess.validateNetworkEndpoint(url);
154+
assertNotNull(result);
155+
}
156+
157+
@ParameterizedTest
158+
@ValueSource(strings = {
159+
"http://localhost:80",
160+
"http://localhost:443",
161+
"http://localhost:8080",
162+
"http://localhost:3000",
163+
"http://localhost:9000"
164+
})
165+
void validateNetworkEndpoint_variousPorts_accepted(String url) {
166+
String result = ConfigAccess.validateNetworkEndpoint(url);
167+
assertNotNull(result);
168+
}
169+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package org.damon233.performtrackermod.data;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.CsvSource;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
class SystemInfoTest {
10+
11+
@ParameterizedTest
12+
@CsvSource({
13+
"0, 0 B",
14+
"512, 512 B",
15+
"1023, 1023 B"
16+
})
17+
void formatBytes_lessThan1KiB_returnsBytes(long bytes, String expected) {
18+
String result = SystemInfo.formatBytes(bytes);
19+
assertEquals(expected, result);
20+
}
21+
22+
@ParameterizedTest
23+
@CsvSource({
24+
"1024, 1 KiB",
25+
"1536, 2 KiB",
26+
"10240, 10 KiB",
27+
"1048575, 1023 KiB"
28+
})
29+
void formatBytes_between1KiBAnd1MiB_returnsKiB(long bytes, String expected) {
30+
String result = SystemInfo.formatBytes(bytes);
31+
assertEquals(expected, result);
32+
}
33+
34+
@ParameterizedTest
35+
@CsvSource({
36+
"1048576, 1 MiB",
37+
"1572864, 2 MiB",
38+
"10485760, 10 MiB",
39+
"104857600, 100 MiB",
40+
"1073741823, 1023 MiB"
41+
})
42+
void formatBytes_between1MiBAnd1GiB_returnsMiB(long bytes, String expected) {
43+
String result = SystemInfo.formatBytes(bytes);
44+
assertEquals(expected, result);
45+
}
46+
47+
@ParameterizedTest
48+
@CsvSource({
49+
"1073741824, 1.0 GiB",
50+
"2147483648, 2.0 GiB",
51+
"4294967296, 4.0 GiB",
52+
"8589934592, 8.0 GiB",
53+
"16384 MiB, 16.0 GiB"
54+
})
55+
void formatBytes_atOrAbove1GiB_returnsGiB(long bytes, String expected) {
56+
String result = SystemInfo.formatBytes(bytes);
57+
assertEquals(expected, result);
58+
}
59+
60+
@Test
61+
void formatBytes_withFractionalGiB_includesOneDecimalPlace() {
62+
String result = SystemInfo.formatBytes(1610612736L);
63+
assertTrue(result.contains("GiB"));
64+
assertTrue(result.contains("."));
65+
assertTrue(result.endsWith(" GiB"));
66+
}
67+
68+
@ParameterizedTest
69+
@CsvSource({
70+
"8589934592, 8.0 GiB",
71+
"9663676416, 9.0 GiB",
72+
"10737418240, 10.0 GiB",
73+
"12884901888, 12.0 GiB"
74+
})
75+
void formatBytes_withWholeNumberGiB_displaysCorrectly(long bytes, String expected) {
76+
String result = SystemInfo.formatBytes(bytes);
77+
assertEquals(expected, result);
78+
}
79+
80+
@Test
81+
void formatBytes_veryLargeValue_returnsGiB() {
82+
long bytes = 17179869184L;
83+
String result = SystemInfo.formatBytes(bytes);
84+
assertTrue(result.contains("GiB"));
85+
}
86+
87+
@Test
88+
void formatBytes_preciseValues() {
89+
assertEquals("1 KiB", SystemInfo.formatBytes(1024));
90+
assertEquals("1 MiB", SystemInfo.formatBytes(1048576));
91+
assertEquals("1 GiB", SystemInfo.formatBytes(1073741824));
92+
}
93+
}

0 commit comments

Comments
 (0)