aboutsummaryrefslogtreecommitdiff
path: root/spark-common/src/test/java
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2024-07-29 18:33:08 +0100
committerLuck <git@lucko.me>2024-07-29 18:33:08 +0100
commit60d54cc4df05e3328f8b8d64ea3b44d5d22c9ed7 (patch)
tree2bf8fcf914ac57466549d35dcd89ef96d3a2d65f /spark-common/src/test/java
parent4c0149b6a15fa887328bbd88c8055c2138cc4d72 (diff)
downloadspark-60d54cc4df05e3328f8b8d64ea3b44d5d22c9ed7.tar.gz
spark-60d54cc4df05e3328f8b8d64ea3b44d5d22c9ed7.tar.bz2
spark-60d54cc4df05e3328f8b8d64ea3b44d5d22c9ed7.zip
Add some unit tests
Diffstat (limited to 'spark-common/src/test/java')
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/SparkPlatformTest.java46
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/activitylog/ActivityLogTest.java54
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/activitylog/ActivityTest.java78
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpSummaryTest.java47
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpTest.java41
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/monitor/cpu/CpuInfoTest.java38
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/monitor/cpu/CpuMonitorTest.java35
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/monitor/disk/DiskUsageTest.java35
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/monitor/memory/MemoryInfoTest.java36
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/monitor/net/NetworkInterfaceInfoTest.java57
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/monitor/os/OperatingSystemInfoTest.java42
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/platform/serverconfig/ExcludedConfigFilterTest.java106
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/platform/serverconfig/PropertiesConfigParserTest.java50
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/platform/world/CountMapTest.java49
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/sampler/ThreadDumperTest.java64
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/sampler/ThreadGrouperTest.java80
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/util/FormatUtilTest.java67
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/util/IndexedListBuilderTest.java42
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/util/JavaVersionTest.java41
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/util/MethodDisambiguatorTest.java66
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/util/RollingAverageTest.java81
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/util/ThreadFinderTest.java50
-rw-r--r--spark-common/src/test/java/me/lucko/spark/test/TestClass.java41
-rw-r--r--spark-common/src/test/java/me/lucko/spark/test/plugin/TestCommandSender.java53
-rw-r--r--spark-common/src/test/java/me/lucko/spark/test/plugin/TestSparkPlugin.java104
25 files changed, 1403 insertions, 0 deletions
diff --git a/spark-common/src/test/java/me/lucko/spark/common/SparkPlatformTest.java b/spark-common/src/test/java/me/lucko/spark/common/SparkPlatformTest.java
new file mode 100644
index 0000000..ec3638f
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/SparkPlatformTest.java
@@ -0,0 +1,46 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.common;
+
+import me.lucko.spark.test.plugin.TestCommandSender;
+import me.lucko.spark.test.plugin.TestSparkPlugin;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.nio.file.Path;
+
+public class SparkPlatformTest {
+
+ @Test
+ public void testEnableDisable(@TempDir Path directory) {
+ System.setProperty("spark.backgroundProfiler", "false");
+
+ SparkPlatform platform = new SparkPlatform(new TestSparkPlugin(directory));
+ platform.enable();
+
+ platform.executeCommand(TestCommandSender.INSTANCE, new String[]{"help"}).join();
+ platform.executeCommand(TestCommandSender.INSTANCE, new String[]{"profiler", "info"}).join();
+ platform.executeCommand(TestCommandSender.INSTANCE, new String[]{"health"}).join();
+
+ platform.disable();
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/activitylog/ActivityLogTest.java b/spark-common/src/test/java/me/lucko/spark/common/activitylog/ActivityLogTest.java
new file mode 100644
index 0000000..a94f954
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/activitylog/ActivityLogTest.java
@@ -0,0 +1,54 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.common.activitylog;
+
+import me.lucko.spark.common.command.sender.CommandSender;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.nio.file.Path;
+import java.util.UUID;
+import java.util.stream.Collectors;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class ActivityLogTest {
+
+ private static final CommandSender.Data USER = new CommandSender.Data("Test", UUID.fromString("5937921d-7051-45e1-bac7-3bbfdc12444f"));
+
+ @Test
+ public void testSaveLoad(@TempDir Path tempDir) {
+ ActivityLog log = new ActivityLog(tempDir.resolve("activity-log.json"));
+ log.addToLog(Activity.fileActivity(USER, 1721937782184L, "Profiler", "path/to/profile.sparkprofile"));
+ log.addToLog(Activity.urlActivity(USER, 1721937782184L, "Profiler", "https://spark.lucko.me/abcd"));
+ log.save();
+
+ ActivityLog log2 = new ActivityLog(tempDir.resolve("activity-log.json"));
+ log2.load();
+
+ // check the log contents
+ assertEquals(
+ log.getLog().stream().map(Activity::serialize).collect(Collectors.toList()),
+ log2.getLog().stream().map(Activity::serialize).collect(Collectors.toList())
+ );
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/activitylog/ActivityTest.java b/spark-common/src/test/java/me/lucko/spark/common/activitylog/ActivityTest.java
new file mode 100644
index 0000000..5bf88f8
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/activitylog/ActivityTest.java
@@ -0,0 +1,78 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.common.activitylog;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonElement;
+import me.lucko.spark.common.command.sender.CommandSender;
+import org.junit.jupiter.api.Test;
+
+import java.util.UUID;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class ActivityTest {
+ private static final Gson GSON = new Gson();
+
+ private static final CommandSender.Data USER = new CommandSender.Data("Test", UUID.fromString("5937921d-7051-45e1-bac7-3bbfdc12444f"));
+
+ private static final String FILE_ACTIVITY_JSON = "{\"user\":{\"type\":\"player\",\"name\":\"Test\",\"uniqueId\":\"5937921d-7051-45e1-bac7-3bbfdc12444f\"},\"time\":1721937782184,\"type\":\"Profiler\",\"data\":{\"type\":\"file\",\"value\":\"path/to/profile.sparkprofile\"}}";
+ private static final String URL_ACTIVITY_JSON = "{\"user\":{\"type\":\"player\",\"name\":\"Test\",\"uniqueId\":\"5937921d-7051-45e1-bac7-3bbfdc12444f\"},\"time\":1721937782184,\"type\":\"Profiler\",\"data\":{\"type\":\"url\",\"value\":\"https://spark.lucko.me/abcd\"}}";
+
+ @Test
+ public void testSerialize() {
+ Activity fileActivity = Activity.fileActivity(
+ USER,
+ 1721937782184L,
+ "Profiler",
+ "path/to/profile.sparkprofile"
+ );
+ assertEquals(FILE_ACTIVITY_JSON, GSON.toJson(fileActivity.serialize()));
+
+ Activity urlActivity = Activity.urlActivity(
+ USER,
+ 1721937782184L,
+ "Profiler",
+ "https://spark.lucko.me/abcd"
+ );
+ assertEquals(URL_ACTIVITY_JSON, GSON.toJson(urlActivity.serialize()));
+ }
+
+ @Test
+ public void testDeserialize() {
+ Activity fileActivity = Activity.deserialize(GSON.fromJson(FILE_ACTIVITY_JSON, JsonElement.class));
+ assertEquals(USER.getUniqueId(), fileActivity.getUser().getUniqueId());
+ assertEquals(USER.getName(), fileActivity.getUser().getName());
+ assertEquals(1721937782184L, fileActivity.getTime());
+ assertEquals("Profiler", fileActivity.getType());
+ assertEquals(Activity.DATA_TYPE_FILE, fileActivity.getDataType());
+ assertEquals("path/to/profile.sparkprofile", fileActivity.getDataValue());
+
+ Activity urlActivity = Activity.deserialize(GSON.fromJson(URL_ACTIVITY_JSON, JsonElement.class));
+ assertEquals(USER.getUniqueId(), urlActivity.getUser().getUniqueId());
+ assertEquals(USER.getName(), urlActivity.getUser().getName());
+ assertEquals(1721937782184L, urlActivity.getTime());
+ assertEquals("Profiler", urlActivity.getType());
+ assertEquals(Activity.DATA_TYPE_URL, urlActivity.getDataType());
+ assertEquals("https://spark.lucko.me/abcd", urlActivity.getDataValue());
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpSummaryTest.java b/spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpSummaryTest.java
new file mode 100644
index 0000000..42492d1
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpSummaryTest.java
@@ -0,0 +1,47 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.common.heapdump;
+
+import me.lucko.spark.test.TestClass;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+public class HeapDumpSummaryTest {
+
+ @Test
+ public void testHeapDumpSummary() throws Exception {
+ TestClass testClass1 = new TestClass();
+ TestClass testClass2 = new TestClass();
+
+ HeapDumpSummary dump = HeapDumpSummary.createNew();
+ List<HeapDumpSummary.Entry> entries = dump.getEntries();
+
+ HeapDumpSummary.Entry thisClassEntry = entries.stream().filter(entry -> entry.getType().equals(TestClass.class.getName())).findAny().orElse(null);
+ assertNotNull(thisClassEntry);
+ assertEquals(2, thisClassEntry.getInstances());
+ assertEquals(32, thisClassEntry.getBytes());
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpTest.java b/spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpTest.java
new file mode 100644
index 0000000..5df5c5d
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpTest.java
@@ -0,0 +1,41 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.common.heapdump;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class HeapDumpTest {
+
+ @Test
+ public void testHeapDump(@TempDir Path tempDir) throws Exception {
+ Path file = tempDir.resolve("heapdump.hprof");
+ HeapDump.dumpHeap(file, false);
+ assertTrue(Files.exists(file));
+ Files.delete(file);
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/monitor/cpu/CpuInfoTest.java b/spark-common/src/test/java/me/lucko/spark/common/monitor/cpu/CpuInfoTest.java
new file mode 100644
index 0000000..047e80d
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/monitor/cpu/CpuInfoTest.java
@@ -0,0 +1,38 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.common.monitor.cpu;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+public class CpuInfoTest {
+
+ @Test
+ public void testCpuInfo() {
+ String model = CpuInfo.queryCpuModel();
+ assertNotNull(model);
+ assertFalse(model.isEmpty());
+ System.out.println(model);
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/monitor/cpu/CpuMonitorTest.java b/spark-common/src/test/java/me/lucko/spark/common/monitor/cpu/CpuMonitorTest.java
new file mode 100644
index 0000000..d554976
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/monitor/cpu/CpuMonitorTest.java
@@ -0,0 +1,35 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.common.monitor.cpu;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class CpuMonitorTest {
+
+ @Test
+ public void testCpuLoad() {
+ assertTrue(CpuMonitor.processLoad() >= 0);
+ assertTrue(CpuMonitor.systemLoad() >= 0);
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/monitor/disk/DiskUsageTest.java b/spark-common/src/test/java/me/lucko/spark/common/monitor/disk/DiskUsageTest.java
new file mode 100644
index 0000000..d961b2f
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/monitor/disk/DiskUsageTest.java
@@ -0,0 +1,35 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.common.monitor.disk;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class DiskUsageTest {
+
+ @Test
+ public void testDiskUsage() {
+ assertTrue(DiskUsage.getUsed() > 0);
+ assertTrue(DiskUsage.getTotal() > 0);
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/monitor/memory/MemoryInfoTest.java b/spark-common/src/test/java/me/lucko/spark/common/monitor/memory/MemoryInfoTest.java
new file mode 100644
index 0000000..5ae8fdc
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/monitor/memory/MemoryInfoTest.java
@@ -0,0 +1,36 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.common.monitor.memory;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class MemoryInfoTest {
+
+ @Test
+ public void testMemoryInfo() {
+ assertTrue(MemoryInfo.getUsedPhysicalMemory() > 0);
+ assertTrue(MemoryInfo.getTotalPhysicalMemory() > 0);
+ assertTrue(MemoryInfo.getAvailablePhysicalMemory() > 0);
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/monitor/net/NetworkInterfaceInfoTest.java b/spark-common/src/test/java/me/lucko/spark/common/monitor/net/NetworkInterfaceInfoTest.java
new file mode 100644
index 0000000..6b50584
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/monitor/net/NetworkInterfaceInfoTest.java
@@ -0,0 +1,57 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.common.monitor.net;
+
+import com.google.common.collect.ImmutableSet;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+public class NetworkInterfaceInfoTest {
+
+ @Test
+ public void testLinuxProcParse() {
+ String input =
+ "Inter-| Receive | Transmit\n" +
+ " face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed\n" +
+ " lo: 2776770 11307 0 0 0 0 0 0 2776770 11307 0 0 0 0 0 0\n" +
+ " eth0: 1215645 2751 1 0 0 0 0 0 1782404 4324 2 0 0 427 0 0\n" +
+ " ppp0: 1622270 5552 1 0 0 0 0 0 354130 5669 0 0 0 0 0 0\n" +
+ " tap0: 7714 81 0 0 0 0 0 0 7714 81 0 0 0 0 0 0";
+
+ Map<String, NetworkInterfaceInfo> map = NetworkInterfaceInfo.read(Arrays.asList(input.split("\n")));
+ assertNotNull(map);
+ assertEquals(ImmutableSet.of("lo", "eth0", "ppp0", "tap0"), map.keySet());
+
+ NetworkInterfaceInfo eth0 = map.get("eth0");
+ assertEquals(1215645, eth0.getReceivedBytes());
+ assertEquals(2751, eth0.getReceivedPackets());
+ assertEquals(1, eth0.getReceiveErrors());
+ assertEquals(1782404, eth0.getTransmittedBytes());
+ assertEquals(4324, eth0.getTransmittedPackets());
+ assertEquals(2, eth0.getTransmitErrors());
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/monitor/os/OperatingSystemInfoTest.java b/spark-common/src/test/java/me/lucko/spark/common/monitor/os/OperatingSystemInfoTest.java
new file mode 100644
index 0000000..3e4fd13
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/monitor/os/OperatingSystemInfoTest.java
@@ -0,0 +1,42 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.common.monitor.os;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+public class OperatingSystemInfoTest {
+
+ @Test
+ public void testOperatingSystemInfo() {
+ OperatingSystemInfo info = OperatingSystemInfo.poll();
+ assertNotNull(info);
+ assertNotNull(info.name());
+ assertNotNull(info.version());
+ assertNotNull(info.arch());
+
+ System.out.println(info.name());
+ System.out.println(info.version());
+ System.out.println(info.arch());
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/platform/serverconfig/ExcludedConfigFilterTest.java b/spark-common/src/test/java/me/lucko/spark/common/platform/serverconfig/ExcludedConfigFilterTest.java
new file mode 100644
index 0000000..ba6f958
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/platform/serverconfig/ExcludedConfigFilterTest.java
@@ -0,0 +1,106 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.common.platform.serverconfig;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Maps;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonPrimitive;
+import org.junit.jupiter.api.Test;
+
+import java.util.Map;
+import java.util.Set;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class ExcludedConfigFilterTest {
+
+ @Test
+ public void testFilter() {
+ Set<String> excluded = ImmutableSet.<String>builder()
+ .add("database")
+ .add("settings.bungeecord-addresses")
+ .add("rcon<dot>password")
+ .add("world-settings.*.feature-seeds")
+ .add("world-settings.*.seed-*")
+ .build();
+
+ ExcludedConfigFilter filter = new ExcludedConfigFilter(excluded);
+
+ JsonPrimitive value = new JsonPrimitive("hello");
+ JsonObject before = obj(
+ element("hello", value),
+ element("database", obj(
+ element("username", value),
+ element("password", value)
+ )),
+ element("settings", obj(
+ element("hello", value),
+ element("bungeecord-addresses", value)
+ )),
+ element("rcon.password", value),
+ element("world-settings", obj(
+ element("world1", obj(
+ element("hello", value),
+ element("feature-seeds", value),
+ element("seed-test", value)
+ )),
+ element("world2", obj(
+ element("hello", value),
+ element("feature-seeds", value),
+ element("seed-test", value)
+ ))
+ ))
+ );
+ JsonObject after = obj(
+ element("hello", value),
+ element("settings", obj(
+ element("hello", value)
+ )),
+ element("world-settings", obj(
+ element("world1", obj(
+ element("hello", value)
+ )),
+ element("world2", obj(
+ element("hello", value)
+ ))
+ ))
+ );
+
+
+ assertEquals(after, filter.apply(before));
+ }
+
+ @SafeVarargs
+ private static JsonObject obj(Map.Entry<String, JsonElement>... elements) {
+ JsonObject object = new JsonObject();
+ for (Map.Entry<String, JsonElement> element : elements) {
+ object.add(element.getKey(), element.getValue());
+ }
+ return object;
+ }
+
+ private static Map.Entry<String, JsonElement> element(String key, JsonElement value) {
+ return Maps.immutableEntry(key, value);
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/platform/serverconfig/PropertiesConfigParserTest.java b/spark-common/src/test/java/me/lucko/spark/common/platform/serverconfig/PropertiesConfigParserTest.java
new file mode 100644
index 0000000..2b686ca
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/platform/serverconfig/PropertiesConfigParserTest.java
@@ -0,0 +1,50 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.common.platform.serverconfig;
+
+import com.google.common.collect.ImmutableMap;
+import org.junit.jupiter.api.Test;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class PropertiesConfigParserTest {
+
+ @Test
+ public void testParse() throws IOException {
+ String properties =
+ "hello=world\n" +
+ "a.b.c=1\n" +
+ "foo=true\n";
+
+ Map<String, Object> parse = PropertiesConfigParser.INSTANCE.parse(new BufferedReader(new StringReader(properties)));
+ assertEquals(ImmutableMap.of(
+ "hello", "world",
+ "a.b.c", 1L,
+ "foo", true
+ ), parse);
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/platform/world/CountMapTest.java b/spark-common/src/test/java/me/lucko/spark/common/platform/world/CountMapTest.java
new file mode 100644
index 0000000..8dcc8a9
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/platform/world/CountMapTest.java
@@ -0,0 +1,49 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.common.platform.world;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.HashMap;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class CountMapTest {
+
+ @Test
+ public void testSimple() {
+ CountMap.Simple<String> countMap = new CountMap.Simple<>(new HashMap<>());
+ assertTrue(countMap.asMap().isEmpty());
+
+ countMap.increment("test");
+ assertTrue(countMap.asMap().containsKey("test"));
+ assertEquals(1, countMap.asMap().get("test").get());
+
+ countMap.add("test", 5);
+ assertEquals(6, countMap.asMap().get("test").get());
+
+ countMap.increment("test2");
+
+ assertEquals(7, countMap.total().get());
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/sampler/ThreadDumperTest.java b/spark-common/src/test/java/me/lucko/spark/common/sampler/ThreadDumperTest.java
new file mode 100644
index 0000000..b96f149
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/sampler/ThreadDumperTest.java
@@ -0,0 +1,64 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.common.sampler;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Set;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class ThreadDumperTest {
+
+ @Test
+ public void testAll() {
+ assertTrue(ThreadDumper.ALL.isThreadIncluded(1, "test"));
+ assertTrue(ThreadDumper.ALL.isThreadIncluded(2, "test2"));
+ }
+
+ @Test
+ public void testSpecific() {
+ Thread thread = new Thread(() -> {
+ try {
+ Thread.sleep(100_000);
+ } catch (InterruptedException e) {
+ // ignore
+ }
+ }, "test-thread-1");
+ thread.start();
+
+ ThreadDumper.Specific specific = new ThreadDumper.Specific(thread);
+
+ assertTrue(specific.isThreadIncluded(thread.getId(), "test-thread-1"));
+
+ Set<Thread> threads = specific.getThreads();
+ assertEquals(1, threads.size());
+ assertTrue(threads.contains(thread));
+
+ Set<String> threadNames = specific.getThreadNames();
+ assertEquals(1, threadNames.size());
+ assertTrue(threadNames.contains("test-thread-1"));
+
+ thread.interrupt();
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/sampler/ThreadGrouperTest.java b/spark-common/src/test/java/me/lucko/spark/common/sampler/ThreadGrouperTest.java
new file mode 100644
index 0000000..5f4e5ae
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/sampler/ThreadGrouperTest.java
@@ -0,0 +1,80 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.common.sampler;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class ThreadGrouperTest {
+
+ @Test
+ public void testByName() {
+ ThreadGrouper threadGrouper = ThreadGrouper.BY_NAME.get();
+
+ String group = threadGrouper.getGroup(1, "main");
+ assertEquals("main", group);
+
+ String label = threadGrouper.getLabel("main");
+ assertEquals("main", label);
+ }
+
+ @Test
+ public void testAsOne() {
+ ThreadGrouper threadGrouper = ThreadGrouper.AS_ONE.get();
+
+ String group = threadGrouper.getGroup(1, "main");
+ assertEquals("root", group);
+
+ String label = threadGrouper.getLabel("root");
+ assertEquals("All (x1)", label);
+
+ group = threadGrouper.getGroup(2, "main2");
+ assertEquals("root", group);
+
+ label = threadGrouper.getLabel("root");
+ assertEquals("All (x2)", label);
+ }
+
+ @Test
+ public void testByPool() {
+ ThreadGrouper threadGrouper = ThreadGrouper.BY_POOL.get();
+
+ String group = threadGrouper.getGroup(1, "main");
+ assertEquals("main", group);
+
+ String label = threadGrouper.getLabel("main");
+ assertEquals("main", label);
+
+ group = threadGrouper.getGroup(2, "Test Pool - #1");
+ assertEquals("Test Pool", group);
+
+ label = threadGrouper.getLabel("Test Pool");
+ assertEquals("Test Pool (x1)", label);
+
+ group = threadGrouper.getGroup(3, "Test Pool - #2");
+ assertEquals("Test Pool", group);
+
+ label = threadGrouper.getLabel("Test Pool");
+ assertEquals("Test Pool (x2)", label);
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/util/FormatUtilTest.java b/spark-common/src/test/java/me/lucko/spark/common/util/FormatUtilTest.java
new file mode 100644
index 0000000..5b77fb5
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/util/FormatUtilTest.java
@@ -0,0 +1,67 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.common.util;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class FormatUtilTest {
+
+ @Test
+ public void testPercent() {
+ assertEquals("50%", FormatUtil.percent(0.5, 1));
+ assertEquals("100%", FormatUtil.percent(1, 1));
+ assertEquals("0%", FormatUtil.percent(0, 1));
+
+ assertEquals("50%", FormatUtil.percent(50, 100));
+ assertEquals("100%", FormatUtil.percent(100, 100));
+ assertEquals("0%", FormatUtil.percent(0, 100));
+ }
+
+ @Test
+ public void testBytes() {
+ assertEquals("0 bytes", FormatUtil.formatBytes(0));
+ assertEquals("1.0 bytes", FormatUtil.formatBytes(1));
+ assertEquals("1.0 KB", FormatUtil.formatBytes(1024));
+ assertEquals("1.0 MB", FormatUtil.formatBytes(1024 * 1024));
+ assertEquals("1.0 GB", FormatUtil.formatBytes(1024 * 1024 * 1024));
+ assertEquals("1.0 TB", FormatUtil.formatBytes(1024L * 1024 * 1024 * 1024));
+
+ assertEquals("2.5 KB", FormatUtil.formatBytes((long) (1024 * 2.5d)));
+ assertEquals("2.5 MB", FormatUtil.formatBytes((long) (1024 * 1024 * 2.5d)));
+ }
+
+ @Test
+ public void testSeconds() {
+ assertEquals("0s", FormatUtil.formatSeconds(0));
+ assertEquals("1s", FormatUtil.formatSeconds(1));
+ assertEquals("59s", FormatUtil.formatSeconds(59));
+ assertEquals("1m", FormatUtil.formatSeconds(60));
+ assertEquals("1m 1s", FormatUtil.formatSeconds(61));
+ assertEquals("1m 59s", FormatUtil.formatSeconds(119));
+ assertEquals("2m", FormatUtil.formatSeconds(120));
+ assertEquals("2m 1s", FormatUtil.formatSeconds(121));
+ assertEquals("2m 59s", FormatUtil.formatSeconds(179));
+ assertEquals("3m", FormatUtil.formatSeconds(180));
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/util/IndexedListBuilderTest.java b/spark-common/src/test/java/me/lucko/spark/common/util/IndexedListBuilderTest.java
new file mode 100644
index 0000000..bb3820e
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/util/IndexedListBuilderTest.java
@@ -0,0 +1,42 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.common.util;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class IndexedListBuilderTest {
+
+ @Test
+ public void testIndexes() {
+ IndexedListBuilder<String> builder = new IndexedListBuilder<>();
+
+ assertEquals(0, builder.add("a"));
+ assertEquals(1, builder.add("b"));
+ assertEquals(2, builder.add("c"));
+
+ assertEquals(Arrays.asList("a", "b", "c"), builder.build());
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/util/JavaVersionTest.java b/spark-common/src/test/java/me/lucko/spark/common/util/JavaVersionTest.java
new file mode 100644
index 0000000..470f4d0
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/util/JavaVersionTest.java
@@ -0,0 +1,41 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.common.util;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class JavaVersionTest {
+
+ @Test
+ public void testJavaVersion() {
+ assertEquals(7, JavaVersion.parseJavaVersion("1.7"));
+ assertEquals(8, JavaVersion.parseJavaVersion("1.8"));
+ assertEquals(9, JavaVersion.parseJavaVersion("9"));
+ assertEquals(11, JavaVersion.parseJavaVersion("11"));
+ assertEquals(17, JavaVersion.parseJavaVersion("17"));
+ assertEquals(9, JavaVersion.parseJavaVersion("9.0.1"));
+ assertEquals(11, JavaVersion.parseJavaVersion("11.0.1"));
+ assertEquals(17, JavaVersion.parseJavaVersion("17.0.1"));
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/util/MethodDisambiguatorTest.java b/spark-common/src/test/java/me/lucko/spark/common/util/MethodDisambiguatorTest.java
new file mode 100644
index 0000000..f0bea8f
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/util/MethodDisambiguatorTest.java
@@ -0,0 +1,66 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.common.util;
+
+import me.lucko.spark.test.TestClass;
+import me.lucko.spark.common.util.MethodDisambiguator.MethodDescription;
+import me.lucko.spark.common.util.classfinder.FallbackClassFinder;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+public class MethodDisambiguatorTest {
+
+ private static final MethodDisambiguator DISAMBIGUATOR = new MethodDisambiguator(FallbackClassFinder.INSTANCE);
+
+ @ParameterizedTest
+ @CsvSource({
+ "25, test(Ljava/lang/String;)V",
+ "26, test(Ljava/lang/String;)V",
+ "27, test(Ljava/lang/String;)V",
+ "28, test(Ljava/lang/String;)V",
+ "31, test(I)V",
+ "32, test(I)V",
+ "33, test(I)V",
+ "34, test(I)V",
+ "37, test(Z)V",
+ "38, test(Z)V",
+ "39, test(Z)V",
+ "40, test(Z)V",
+ })
+ public void testSuccessfulDisambiguate(int line, String expectedDesc) {
+ MethodDescription method = DISAMBIGUATOR.disambiguate(TestClass.class.getName(), "test", line).orElse(null);
+ assertNotNull(method);
+ assertEquals(expectedDesc, method.toString());
+ }
+
+ @ParameterizedTest
+ @ValueSource(ints = {24, 29, 100})
+ public void testUnsuccessfulDisambiguate(int line) {
+ MethodDescription method = DISAMBIGUATOR.disambiguate(TestClass.class.getName(), "test", line).orElse(null);
+ assertNull(method);
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/util/RollingAverageTest.java b/spark-common/src/test/java/me/lucko/spark/common/util/RollingAverageTest.java
new file mode 100644
index 0000000..a5b4a00
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/util/RollingAverageTest.java
@@ -0,0 +1,81 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.common.util;
+
+import org.junit.jupiter.api.Test;
+
+import java.math.BigDecimal;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class RollingAverageTest {
+
+ @Test
+ public void testMean() {
+ RollingAverage ra = new RollingAverage(3);
+ ra.add(BigDecimal.valueOf(1));
+ ra.add(BigDecimal.valueOf(2));
+ ra.add(BigDecimal.valueOf(3));
+
+ assertEquals(2, ra.mean());
+ ra.add(BigDecimal.valueOf(4));
+ assertEquals(3, ra.mean());
+ ra.add(BigDecimal.valueOf(5));
+ assertEquals(4, ra.mean());
+ ra.add(BigDecimal.valueOf(6));
+ assertEquals(5, ra.mean());
+ }
+
+ @Test
+ public void testMax() {
+ RollingAverage ra = new RollingAverage(3);
+ ra.add(BigDecimal.valueOf(1));
+ ra.add(BigDecimal.valueOf(2));
+ ra.add(BigDecimal.valueOf(3));
+
+ assertEquals(3, ra.max());
+ }
+
+ @Test
+ public void testMin() {
+ RollingAverage ra = new RollingAverage(3);
+ ra.add(BigDecimal.valueOf(1));
+ ra.add(BigDecimal.valueOf(2));
+ ra.add(BigDecimal.valueOf(3));
+
+ assertEquals(1, ra.min());
+ }
+
+ @Test
+ public void testPercentile() {
+ RollingAverage ra = new RollingAverage(3);
+ ra.add(BigDecimal.valueOf(1));
+ ra.add(BigDecimal.valueOf(2));
+ ra.add(BigDecimal.valueOf(3));
+
+ assertEquals(1, ra.percentile(0));
+ assertEquals(2, ra.percentile(0.25));
+ assertEquals(2, ra.percentile(0.5));
+ assertEquals(3, ra.percentile(0.75));
+ assertEquals(3, ra.percentile(1));
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/util/ThreadFinderTest.java b/spark-common/src/test/java/me/lucko/spark/common/util/ThreadFinderTest.java
new file mode 100644
index 0000000..bffbf27
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/util/ThreadFinderTest.java
@@ -0,0 +1,50 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.common.util;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class ThreadFinderTest {
+
+ @Test
+ public void testFindThread() {
+ Thread thread = new Thread(() -> {
+ try {
+ Thread.sleep(100_000);
+ } catch (InterruptedException e) {
+ // ignore
+ }
+ }, "test-thread-1");
+ thread.start();
+
+ ThreadFinder threadFinder = new ThreadFinder();
+ List<Thread> threads = threadFinder.getThreads().collect(Collectors.toList());
+ assertTrue(threads.contains(thread));
+
+ thread.interrupt();
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/test/TestClass.java b/spark-common/src/test/java/me/lucko/spark/test/TestClass.java
new file mode 100644
index 0000000..bd7ff4a
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/test/TestClass.java
@@ -0,0 +1,41 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.test;
+
+public class TestClass {
+ public void test(String string) {
+ System.out.println("Hello, world!");
+ System.out.println("Hello, world!");
+ System.out.println("Hello, world!");
+ }
+
+ public void test(int integer) {
+ System.out.println("Hello, world!");
+ System.out.println("Hello, world!");
+ System.out.println("Hello, world!");
+ }
+
+ public void test(boolean bool) {
+ System.out.println("Hello, world!");
+ System.out.println("Hello, world!");
+ System.out.println("Hello, world!");
+ }
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/test/plugin/TestCommandSender.java b/spark-common/src/test/java/me/lucko/spark/test/plugin/TestCommandSender.java
new file mode 100644
index 0000000..294b073
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/test/plugin/TestCommandSender.java
@@ -0,0 +1,53 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.test.plugin;
+
+import me.lucko.spark.common.command.sender.CommandSender;
+import net.kyori.adventure.text.Component;
+import net.kyori.adventure.text.serializer.ansi.ANSIComponentSerializer;
+
+import java.util.UUID;
+
+public enum TestCommandSender implements CommandSender {
+ INSTANCE;
+
+ private final UUID uniqueId = new UUID(0, 0);
+
+ @Override
+ public String getName() {
+ return "Test";
+ }
+
+ @Override
+ public UUID getUniqueId() {
+ return this.uniqueId;
+ }
+
+ @Override
+ public void sendMessage(Component message) {
+ System.out.println(ANSIComponentSerializer.ansi().serialize(message));
+ }
+
+ @Override
+ public boolean hasPermission(String permission) {
+ return true;
+ }
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/test/plugin/TestSparkPlugin.java b/spark-common/src/test/java/me/lucko/spark/test/plugin/TestSparkPlugin.java
new file mode 100644
index 0000000..423e3e3
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/test/plugin/TestSparkPlugin.java
@@ -0,0 +1,104 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * 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/>.
+ */
+
+package me.lucko.spark.test.plugin;
+
+import me.lucko.spark.common.SparkPlugin;
+import me.lucko.spark.common.command.sender.CommandSender;
+import me.lucko.spark.common.platform.PlatformInfo;
+
+import java.nio.file.Path;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.stream.Stream;
+
+public class TestSparkPlugin implements SparkPlugin {
+
+ private static final Logger LOGGER = Logger.getLogger("spark-test");
+ private static final ScheduledExecutorService EXECUTOR_SERVICE = Executors.newScheduledThreadPool(16);
+
+ private final Path directory;
+
+ public TestSparkPlugin(Path directory) {
+ this.directory = directory;
+ }
+
+ @Override
+ public String getVersion() {
+ return "1.0-test";
+ }
+
+ @Override
+ public Path getPluginDirectory() {
+ return this.directory;
+ }
+
+ @Override
+ public String getCommandName() {
+ return "spark";
+ }
+
+ @Override
+ public Stream<? extends CommandSender> getCommandSenders() {
+ return Stream.of(TestCommandSender.INSTANCE);
+ }
+
+ @Override
+ public void executeAsync(Runnable task) {
+ EXECUTOR_SERVICE.execute(task);
+ }
+
+ @Override
+ public void log(Level level, String msg) {
+ LOGGER.log(level, msg);
+ }
+
+ @Override
+ public PlatformInfo getPlatformInfo() {
+ return new PlatformInfo() {
+ @Override
+ public Type getType() {
+ return Type.SERVER;
+ }
+
+ @Override
+ public String getName() {
+ return "Test";
+ }
+
+ @Override
+ public String getBrand() {
+ return "Test";
+ }
+
+ @Override
+ public String getVersion() {
+ return "v1.0-test";
+ }
+
+ @Override
+ public String getMinecraftVersion() {
+ return null;
+ }
+ };
+ }
+}