From 60d54cc4df05e3328f8b8d64ea3b44d5d22c9ed7 Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 29 Jul 2024 18:33:08 +0100 Subject: Add some unit tests --- spark-common/build.gradle | 27 +++- .../java/me/lucko/spark/common/SparkPlatform.java | 18 ++- .../common/command/modules/SamplerModule.java | 3 +- .../spark/common/heapdump/HeapDumpSummary.java | 16 ++ .../me/lucko/spark/common/monitor/MacosSysctl.java | 67 +++++++++ .../me/lucko/spark/common/monitor/cpu/CpuInfo.java | 7 + .../common/monitor/net/NetworkInterfaceInfo.java | 4 +- .../common/sampler/BackgroundSamplerManager.java | 5 +- .../lucko/spark/common/sampler/SamplerBuilder.java | 7 +- .../lucko/spark/common/sampler/ThreadGrouper.java | 32 +++- .../me/lucko/spark/common/util/Configuration.java | 153 ------------------- .../me/lucko/spark/common/util/JavaVersion.java | 5 +- .../common/util/config/CombinedConfiguration.java | 132 +++++++++++++++++ .../spark/common/util/config/Configuration.java | 54 +++++++ .../common/util/config/FileConfiguration.java | 165 +++++++++++++++++++++ .../common/util/config/RuntimeConfiguration.java | 106 +++++++++++++ .../me/lucko/spark/common/ws/TrustedKeyStore.java | 2 +- .../me/lucko/spark/common/SparkPlatformTest.java | 46 ++++++ .../spark/common/activitylog/ActivityLogTest.java | 54 +++++++ .../spark/common/activitylog/ActivityTest.java | 78 ++++++++++ .../spark/common/heapdump/HeapDumpSummaryTest.java | 47 ++++++ .../lucko/spark/common/heapdump/HeapDumpTest.java | 41 +++++ .../spark/common/monitor/cpu/CpuInfoTest.java | 38 +++++ .../spark/common/monitor/cpu/CpuMonitorTest.java | 35 +++++ .../spark/common/monitor/disk/DiskUsageTest.java | 35 +++++ .../common/monitor/memory/MemoryInfoTest.java | 36 +++++ .../monitor/net/NetworkInterfaceInfoTest.java | 57 +++++++ .../common/monitor/os/OperatingSystemInfoTest.java | 42 ++++++ .../serverconfig/ExcludedConfigFilterTest.java | 106 +++++++++++++ .../serverconfig/PropertiesConfigParserTest.java | 50 +++++++ .../spark/common/platform/world/CountMapTest.java | 49 ++++++ .../spark/common/sampler/ThreadDumperTest.java | 64 ++++++++ .../spark/common/sampler/ThreadGrouperTest.java | 80 ++++++++++ .../me/lucko/spark/common/util/FormatUtilTest.java | 67 +++++++++ .../spark/common/util/IndexedListBuilderTest.java | 42 ++++++ .../lucko/spark/common/util/JavaVersionTest.java | 41 +++++ .../spark/common/util/MethodDisambiguatorTest.java | 66 +++++++++ .../spark/common/util/RollingAverageTest.java | 81 ++++++++++ .../lucko/spark/common/util/ThreadFinderTest.java | 50 +++++++ .../test/java/me/lucko/spark/test/TestClass.java | 41 +++++ .../lucko/spark/test/plugin/TestCommandSender.java | 53 +++++++ .../lucko/spark/test/plugin/TestSparkPlugin.java | 104 +++++++++++++ 42 files changed, 2030 insertions(+), 176 deletions(-) create mode 100644 spark-common/src/main/java/me/lucko/spark/common/monitor/MacosSysctl.java delete mode 100644 spark-common/src/main/java/me/lucko/spark/common/util/Configuration.java create mode 100644 spark-common/src/main/java/me/lucko/spark/common/util/config/CombinedConfiguration.java create mode 100644 spark-common/src/main/java/me/lucko/spark/common/util/config/Configuration.java create mode 100644 spark-common/src/main/java/me/lucko/spark/common/util/config/FileConfiguration.java create mode 100644 spark-common/src/main/java/me/lucko/spark/common/util/config/RuntimeConfiguration.java create mode 100644 spark-common/src/test/java/me/lucko/spark/common/SparkPlatformTest.java create mode 100644 spark-common/src/test/java/me/lucko/spark/common/activitylog/ActivityLogTest.java create mode 100644 spark-common/src/test/java/me/lucko/spark/common/activitylog/ActivityTest.java create mode 100644 spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpSummaryTest.java create mode 100644 spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpTest.java create mode 100644 spark-common/src/test/java/me/lucko/spark/common/monitor/cpu/CpuInfoTest.java create mode 100644 spark-common/src/test/java/me/lucko/spark/common/monitor/cpu/CpuMonitorTest.java create mode 100644 spark-common/src/test/java/me/lucko/spark/common/monitor/disk/DiskUsageTest.java create mode 100644 spark-common/src/test/java/me/lucko/spark/common/monitor/memory/MemoryInfoTest.java create mode 100644 spark-common/src/test/java/me/lucko/spark/common/monitor/net/NetworkInterfaceInfoTest.java create mode 100644 spark-common/src/test/java/me/lucko/spark/common/monitor/os/OperatingSystemInfoTest.java create mode 100644 spark-common/src/test/java/me/lucko/spark/common/platform/serverconfig/ExcludedConfigFilterTest.java create mode 100644 spark-common/src/test/java/me/lucko/spark/common/platform/serverconfig/PropertiesConfigParserTest.java create mode 100644 spark-common/src/test/java/me/lucko/spark/common/platform/world/CountMapTest.java create mode 100644 spark-common/src/test/java/me/lucko/spark/common/sampler/ThreadDumperTest.java create mode 100644 spark-common/src/test/java/me/lucko/spark/common/sampler/ThreadGrouperTest.java create mode 100644 spark-common/src/test/java/me/lucko/spark/common/util/FormatUtilTest.java create mode 100644 spark-common/src/test/java/me/lucko/spark/common/util/IndexedListBuilderTest.java create mode 100644 spark-common/src/test/java/me/lucko/spark/common/util/JavaVersionTest.java create mode 100644 spark-common/src/test/java/me/lucko/spark/common/util/MethodDisambiguatorTest.java create mode 100644 spark-common/src/test/java/me/lucko/spark/common/util/RollingAverageTest.java create mode 100644 spark-common/src/test/java/me/lucko/spark/common/util/ThreadFinderTest.java create mode 100644 spark-common/src/test/java/me/lucko/spark/test/TestClass.java create mode 100644 spark-common/src/test/java/me/lucko/spark/test/plugin/TestCommandSender.java create mode 100644 spark-common/src/test/java/me/lucko/spark/test/plugin/TestSparkPlugin.java (limited to 'spark-common') diff --git a/spark-common/build.gradle b/spark-common/build.gradle index 0eaf711..5bc4bda 100644 --- a/spark-common/build.gradle +++ b/spark-common/build.gradle @@ -20,17 +20,17 @@ dependencies { exclude(module: 'slf4j-api') } - api('net.kyori:adventure-api:4.13.1') { + api('net.kyori:adventure-api:4.17.0') { exclude(module: 'adventure-bom') exclude(module: 'checker-qual') exclude(module: 'annotations') } - api('net.kyori:adventure-text-serializer-gson:4.13.1') { + api('net.kyori:adventure-text-serializer-gson:4.17.0') { exclude(module: 'adventure-bom') exclude(module: 'adventure-api') exclude(module: 'gson') } - api('net.kyori:adventure-text-serializer-legacy:4.13.1') { + api('net.kyori:adventure-text-serializer-legacy:4.17.0') { exclude(module: 'adventure-bom') exclude(module: 'adventure-api') } @@ -40,6 +40,22 @@ dependencies { compileOnly 'com.google.code.gson:gson:2.7' compileOnly 'com.google.guava:guava:19.0' compileOnly 'org.checkerframework:checker-qual:3.44.0' + + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.11.0-M2' + testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.11.0-M2' + testImplementation 'org.junit.jupiter:junit-jupiter-params:5.11.0-M2' + // testImplementation "org.testcontainers:junit-jupiter:1.19.8" + // testImplementation 'org.mockito:mockito-core:5.12.0' + // testImplementation 'org.mockito:mockito-junit-jupiter:5.12.0' + + testImplementation 'com.google.code.gson:gson:2.7' + testImplementation 'com.google.guava:guava:19.0' + testImplementation 'org.checkerframework:checker-qual:3.44.0' + + testImplementation('net.kyori:adventure-text-serializer-ansi:4.17.0') { + exclude(module: 'adventure-bom') + exclude(module: 'adventure-api') + } } protobuf { @@ -56,3 +72,8 @@ protobuf { } } } + +test { + useJUnitPlatform {} + systemProperty('net.kyori.ansi.colorLevel', 'indexed16') +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java b/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java index 733510d..5e25d91 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java +++ b/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java @@ -52,10 +52,12 @@ import me.lucko.spark.common.sampler.source.ClassSourceLookup; import me.lucko.spark.common.tick.TickHook; import me.lucko.spark.common.tick.TickReporter; import me.lucko.spark.common.util.BytebinClient; -import me.lucko.spark.common.util.Configuration; import me.lucko.spark.common.util.SparkStaticLogger; import me.lucko.spark.common.util.TemporaryFiles; import me.lucko.spark.common.util.classfinder.ClassFinder; +import me.lucko.spark.common.util.config.Configuration; +import me.lucko.spark.common.util.config.FileConfiguration; +import me.lucko.spark.common.util.config.RuntimeConfiguration; import me.lucko.spark.common.ws.TrustedKeyStore; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.event.ClickEvent; @@ -71,6 +73,7 @@ import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.locks.ReentrantLock; @@ -121,7 +124,11 @@ public class SparkPlatform { SparkStaticLogger.setLogger(plugin::log); this.temporaryFiles = new TemporaryFiles(this.plugin.getPluginDirectory().resolve("tmp")); - this.configuration = new Configuration(this.plugin.getPluginDirectory().resolve("config.json")); + this.configuration = Configuration.combining( + RuntimeConfiguration.SYSTEM_PROPERTIES, + RuntimeConfiguration.ENVIRONMENT_VARIABLES, + new FileConfiguration(this.plugin.getPluginDirectory().resolve("config.json")) + ); this.viewerUrl = this.configuration.getString("viewerUrl", "https://spark.lucko.me/"); String bytebinUrl = this.configuration.getString("bytebinUrl", "https://spark-usercontent.lucko.me/"); @@ -330,7 +337,8 @@ public class SparkPlatform { return !getAvailableCommands(sender).isEmpty(); } - public void executeCommand(CommandSender sender, String[] args) { + public CompletableFuture executeCommand(CommandSender sender, String[] args) { + CompletableFuture future = new CompletableFuture<>(); AtomicReference executorThread = new AtomicReference<>(); AtomicReference timeoutThread = new AtomicReference<>(); AtomicBoolean completed = new AtomicBoolean(false); @@ -341,9 +349,11 @@ public class SparkPlatform { this.commandExecuteLock.lock(); try { executeCommand0(sender, args); + future.complete(null); } catch (Exception e) { this.plugin.log(Level.SEVERE, "Exception occurred whilst executing a spark command"); e.printStackTrace(); + future.completeExceptionally(e); } finally { this.commandExecuteLock.unlock(); executorThread.set(null); @@ -393,6 +403,8 @@ public class SparkPlatform { timeoutThread.set(null); } }); + + return future; } private void executeCommand0(CommandSender sender, String[] args) { diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java b/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java index 9e2647a..334e416 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java +++ b/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java @@ -59,6 +59,7 @@ import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; +import java.util.function.Supplier; import static net.kyori.adventure.text.Component.empty; import static net.kyori.adventure.text.Component.space; @@ -208,7 +209,7 @@ public class SamplerModule implements CommandModule { } } - ThreadGrouper threadGrouper; + Supplier threadGrouper; if (arguments.boolFlag("combine-all")) { threadGrouper = ThreadGrouper.AS_ONE; } else if (arguments.boolFlag("not-combined")) { diff --git a/spark-common/src/main/java/me/lucko/spark/common/heapdump/HeapDumpSummary.java b/spark-common/src/main/java/me/lucko/spark/common/heapdump/HeapDumpSummary.java index 364edd6..4d34d4a 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/heapdump/HeapDumpSummary.java +++ b/spark-common/src/main/java/me/lucko/spark/common/heapdump/HeapDumpSummary.java @@ -20,6 +20,7 @@ package me.lucko.spark.common.heapdump; +import com.google.common.annotations.VisibleForTesting; import me.lucko.spark.common.SparkPlatform; import me.lucko.spark.common.command.sender.CommandSender; import me.lucko.spark.proto.SparkHeapProtos.HeapData; @@ -123,6 +124,11 @@ public final class HeapDumpSummary { this.entries = entries; } + @VisibleForTesting + List getEntries() { + return this.entries; + } + public HeapData toProto(SparkPlatform platform, CommandSender.Data creator) { HeapMetadata.Builder metadata = HeapMetadata.newBuilder() .setPlatformMetadata(platform.getPlugin().getPlatformInfo().toData().toProto()) @@ -186,6 +192,16 @@ public final class HeapDumpSummary { .setType(this.type) .build(); } + + @Override + public String toString() { + return "Entry{" + + "order=" + this.order + + ", instances=" + this.instances + + ", bytes=" + this.bytes + + ", type='" + this.type + '\'' + + '}'; + } } public interface DiagnosticCommandMXBean { diff --git a/spark-common/src/main/java/me/lucko/spark/common/monitor/MacosSysctl.java b/spark-common/src/main/java/me/lucko/spark/common/monitor/MacosSysctl.java new file mode 100644 index 0000000..c279f31 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/monitor/MacosSysctl.java @@ -0,0 +1,67 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * 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 . + */ + +package me.lucko.spark.common.monitor; + +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Locale; + +/** + * Utility for reading from sysctl on macOS systems. + */ +public enum MacosSysctl { + + SYSCTL("sysctl", "-a"),; + + private static final boolean SUPPORTED = System.getProperty("os.name").toLowerCase(Locale.ROOT).replace(" ", "").equals("macosx"); + + private final String[] cmdArgs; + + MacosSysctl(String... cmdArgs) { + this.cmdArgs = cmdArgs; + } + + public @NonNull List read() { + if (SUPPORTED) { + ProcessBuilder process = new ProcessBuilder(this.cmdArgs).redirectErrorStream(true); + try (BufferedReader buf = new BufferedReader(new InputStreamReader(process.start().getInputStream()))) { + List lines = new ArrayList<>(); + + String line; + while ((line = buf.readLine()) != null) { + lines.add(line); + } + + return lines; + } catch (Exception e) { + // ignore + } + } + + return Collections.emptyList(); + } +} + diff --git a/spark-common/src/main/java/me/lucko/spark/common/monitor/cpu/CpuInfo.java b/spark-common/src/main/java/me/lucko/spark/common/monitor/cpu/CpuInfo.java index 9954bd5..07875cc 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/monitor/cpu/CpuInfo.java +++ b/spark-common/src/main/java/me/lucko/spark/common/monitor/cpu/CpuInfo.java @@ -21,6 +21,7 @@ package me.lucko.spark.common.monitor.cpu; import me.lucko.spark.common.monitor.LinuxProc; +import me.lucko.spark.common.monitor.MacosSysctl; import me.lucko.spark.common.monitor.WindowsWmic; import java.util.regex.Pattern; @@ -52,6 +53,12 @@ public enum CpuInfo { } } + for (String line : MacosSysctl.SYSCTL.read()) { + if (line.startsWith("machdep.cpu.brand_string:")) { + return line.substring("machdep.cpu.brand_string:".length()).trim(); + } + } + return ""; } diff --git a/spark-common/src/main/java/me/lucko/spark/common/monitor/net/NetworkInterfaceInfo.java b/spark-common/src/main/java/me/lucko/spark/common/monitor/net/NetworkInterfaceInfo.java index eed695e..c2ba1da 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/monitor/net/NetworkInterfaceInfo.java +++ b/spark-common/src/main/java/me/lucko/spark/common/monitor/net/NetworkInterfaceInfo.java @@ -20,6 +20,7 @@ package me.lucko.spark.common.monitor.net; +import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableMap; import me.lucko.spark.common.monitor.LinuxProc; import org.checkerframework.checker.nullness.qual.NonNull; @@ -200,7 +201,8 @@ public final class NetworkInterfaceInfo { private static final Pattern PROC_NET_DEV_PATTERN = Pattern.compile("^\\s*(\\w+):([\\d\\s]+)$"); - private static @NonNull Map read(List output) { + @VisibleForTesting + static @NonNull Map read(List output) { // Inter-| Receive | Transmit // face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed // lo: 2776770 11307 0 0 0 0 0 0 2776770 11307 0 0 0 0 0 0 diff --git a/spark-common/src/main/java/me/lucko/spark/common/sampler/BackgroundSamplerManager.java b/spark-common/src/main/java/me/lucko/spark/common/sampler/BackgroundSamplerManager.java index 4e9ca9e..1889304 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/sampler/BackgroundSamplerManager.java +++ b/spark-common/src/main/java/me/lucko/spark/common/sampler/BackgroundSamplerManager.java @@ -22,8 +22,9 @@ package me.lucko.spark.common.sampler; import me.lucko.spark.common.SparkPlatform; import me.lucko.spark.common.platform.PlatformInfo; -import me.lucko.spark.common.util.Configuration; +import me.lucko.spark.common.util.config.Configuration; +import java.util.function.Supplier; import java.util.logging.Level; public class BackgroundSamplerManager { @@ -103,7 +104,7 @@ public class BackgroundSamplerManager { private void startSampler() { boolean forceJavaEngine = this.configuration.getString(OPTION_ENGINE, "async").equals("java"); - ThreadGrouper threadGrouper = ThreadGrouper.parseConfigSetting(this.configuration.getString(OPTION_THREAD_GROUPER, "by-pool")); + Supplier threadGrouper = ThreadGrouper.parseConfigSetting(this.configuration.getString(OPTION_THREAD_GROUPER, "by-pool")); ThreadDumper threadDumper = ThreadDumper.parseConfigSetting(this.configuration.getString(OPTION_THREAD_DUMPER, "default")); if (threadDumper == null) { threadDumper = this.platform.getPlugin().getDefaultThreadDumper(); diff --git a/spark-common/src/main/java/me/lucko/spark/common/sampler/SamplerBuilder.java b/spark-common/src/main/java/me/lucko/spark/common/sampler/SamplerBuilder.java index b6895ce..3046d92 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/sampler/SamplerBuilder.java +++ b/spark-common/src/main/java/me/lucko/spark/common/sampler/SamplerBuilder.java @@ -28,6 +28,7 @@ import me.lucko.spark.common.sampler.java.JavaSampler; import me.lucko.spark.common.tick.TickHook; import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; /** * Builds {@link Sampler} instances. @@ -44,7 +45,7 @@ public class SamplerBuilder { private long autoEndTime = -1; private boolean background = false; private ThreadDumper threadDumper = ThreadDumper.ALL; - private ThreadGrouper threadGrouper = ThreadGrouper.BY_NAME; + private Supplier threadGrouper = ThreadGrouper.BY_NAME; private int ticksOver = -1; private TickHook tickHook = null; @@ -80,7 +81,7 @@ public class SamplerBuilder { return this; } - public SamplerBuilder threadGrouper(ThreadGrouper threadGrouper) { + public SamplerBuilder threadGrouper(Supplier threadGrouper) { this.threadGrouper = threadGrouper; return this; } @@ -131,7 +132,7 @@ public class SamplerBuilder { this.samplingInterval ); - SamplerSettings settings = new SamplerSettings(interval, this.threadDumper, this.threadGrouper, this.autoEndTime, this.background); + SamplerSettings settings = new SamplerSettings(interval, this.threadDumper, this.threadGrouper.get(), this.autoEndTime, this.background); Sampler sampler; if (this.mode == SamplerMode.ALLOCATION) { diff --git a/spark-common/src/main/java/me/lucko/spark/common/sampler/ThreadGrouper.java b/spark-common/src/main/java/me/lucko/spark/common/sampler/ThreadGrouper.java index b6cfbea..c8d5b3c 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/sampler/ThreadGrouper.java +++ b/spark-common/src/main/java/me/lucko/spark/common/sampler/ThreadGrouper.java @@ -26,6 +26,7 @@ import java.util.Collections; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Supplier; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -64,7 +65,7 @@ public interface ThreadGrouper { * @param setting the config setting * @return the thread grouper */ - static ThreadGrouper parseConfigSetting(String setting) { + static Supplier parseConfigSetting(String setting) { switch (setting) { case "as-one": return AS_ONE; @@ -75,10 +76,15 @@ public interface ThreadGrouper { } } + /** + * Supplier for {@link ByName} thread groupers. + */ + Supplier BY_NAME = ByName::new; + /** * Implementation of {@link ThreadGrouper} that just groups by thread name. */ - ThreadGrouper BY_NAME = new ThreadGrouper() { + class ByName implements ThreadGrouper { @Override public String getGroup(long threadId, String threadName) { return threadName; @@ -93,7 +99,12 @@ public interface ThreadGrouper { public SamplerMetadata.DataAggregator.ThreadGrouper asProto() { return SamplerMetadata.DataAggregator.ThreadGrouper.BY_NAME; } - }; + } + + /** + * Supplier for {@link ByPool} thread groupers. + */ + Supplier BY_POOL = ByPool::new; /** * Implementation of {@link ThreadGrouper} that attempts to group by the name of the pool @@ -102,8 +113,8 @@ public interface ThreadGrouper { *

The regex pattern used to match pools expects a digit at the end of the thread name, * separated from the pool name with any of one or more of ' ', '-', or '#'.

*/ - ThreadGrouper BY_POOL = new ThreadGrouper() { - private /* static */ final Pattern pattern = Pattern.compile("^(.*?)[-# ]+\\d+$"); + class ByPool implements ThreadGrouper { + private static final Pattern PATTERN = Pattern.compile("^(.*?)[-# ]+\\d+$"); // thread id -> group private final Map cache = new ConcurrentHashMap<>(); @@ -117,7 +128,7 @@ public interface ThreadGrouper { return cached; } - Matcher matcher = this.pattern.matcher(threadName); + Matcher matcher = PATTERN.matcher(threadName); if (!matcher.matches()) { return threadName; } @@ -141,13 +152,18 @@ public interface ThreadGrouper { public SamplerMetadata.DataAggregator.ThreadGrouper asProto() { return SamplerMetadata.DataAggregator.ThreadGrouper.BY_POOL; } - }; + } + + /** + * Supplier for {@link AsOne} thread groupers. + */ + Supplier AS_ONE = AsOne::new; /** * Implementation of {@link ThreadGrouper} which groups all threads as one, under * the name "All". */ - ThreadGrouper AS_ONE = new ThreadGrouper() { + class AsOne implements ThreadGrouper { private final Set seen = ConcurrentHashMap.newKeySet(); @Override diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/Configuration.java b/spark-common/src/main/java/me/lucko/spark/common/util/Configuration.java deleted file mode 100644 index 586a845..0000000 --- a/spark-common/src/main/java/me/lucko/spark/common/util/Configuration.java +++ /dev/null @@ -1,153 +0,0 @@ -/* - * This file is part of spark. - * - * Copyright (c) lucko (Luck) - * 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 . - */ - -package me.lucko.spark.common.util; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public final class Configuration { - private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); - - private final Path file; - private JsonObject root; - - public Configuration(Path file) { - this.file = file; - load(); - } - - public void load() { - JsonObject root = null; - if (Files.exists(this.file)) { - try (BufferedReader reader = Files.newBufferedReader(this.file, StandardCharsets.UTF_8)) { - root = GSON.fromJson(reader, JsonObject.class); - } catch (IOException e) { - e.printStackTrace(); - } - } - if (root == null) { - root = new JsonObject(); - root.addProperty("_header", "spark configuration file - https://spark.lucko.me/docs/Configuration"); - } - this.root = root; - } - - public void save() { - try { - Files.createDirectories(this.file.getParent()); - } catch (IOException e) { - // ignore - } - - try (BufferedWriter writer = Files.newBufferedWriter(this.file, StandardCharsets.UTF_8)) { - GSON.toJson(this.root, writer); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public String getString(String path, String def) { - JsonElement el = this.root.get(path); - if (el == null || !el.isJsonPrimitive()) { - return def; - } - - return el.getAsJsonPrimitive().getAsString(); - } - - public boolean getBoolean(String path, boolean def) { - JsonElement el = this.root.get(path); - if (el == null || !el.isJsonPrimitive()) { - return def; - } - - JsonPrimitive val = el.getAsJsonPrimitive(); - return val.isBoolean() ? val.getAsBoolean() : def; - } - - public int getInteger(String path, int def) { - JsonElement el = this.root.get(path); - if (el == null || !el.isJsonPrimitive()) { - return def; - } - - JsonPrimitive val = el.getAsJsonPrimitive(); - return val.isNumber() ? val.getAsInt() : def; - } - - public List getStringList(String path) { - JsonElement el = this.root.get(path); - if (el == null || !el.isJsonArray()) { - return Collections.emptyList(); - } - - List list = new ArrayList<>(); - for (JsonElement child : el.getAsJsonArray()) { - if (child.isJsonPrimitive()) { - list.add(child.getAsJsonPrimitive().getAsString()); - } - } - return list; - } - - public void setString(String path, String value) { - this.root.add(path, new JsonPrimitive(value)); - } - - public void setBoolean(String path, boolean value) { - this.root.add(path, new JsonPrimitive(value)); - } - - public void setInteger(String path, int value) { - this.root.add(path, new JsonPrimitive(value)); - } - - public void setStringList(String path, List value) { - JsonArray array = new JsonArray(); - for (String str : value) { - array.add(str); - } - this.root.add(path, array); - } - - public boolean contains(String path) { - return this.root.has(path); - } - - public void remove(String path) { - this.root.remove(path); - } - -} diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/JavaVersion.java b/spark-common/src/main/java/me/lucko/spark/common/util/JavaVersion.java index efeabfc..1dad75b 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/util/JavaVersion.java +++ b/spark-common/src/main/java/me/lucko/spark/common/util/JavaVersion.java @@ -20,6 +20,8 @@ package me.lucko.spark.common.util; +import com.google.common.annotations.VisibleForTesting; + public class JavaVersion { ; @@ -28,7 +30,8 @@ public class JavaVersion { JAVA_VERSION = parseJavaVersion(System.getProperty("java.version")); } - private static int parseJavaVersion(String version) { + @VisibleForTesting + static int parseJavaVersion(String version) { if (version.startsWith("1.")) { // Java 8 and below return Integer.parseInt(version.substring(2, 3)); diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/config/CombinedConfiguration.java b/spark-common/src/main/java/me/lucko/spark/common/util/config/CombinedConfiguration.java new file mode 100644 index 0000000..ff7388a --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/util/config/CombinedConfiguration.java @@ -0,0 +1,132 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * 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 . + */ + +package me.lucko.spark.common.util.config; + +import com.google.common.collect.ImmutableList; + +import java.util.Collections; +import java.util.List; + +class CombinedConfiguration implements Configuration { + + private final List configurations; + + CombinedConfiguration(Configuration... configurations) { + this.configurations = ImmutableList.copyOf(configurations).reverse(); + } + + @Override + public void load() { + for (Configuration configuration : this.configurations) { + configuration.load(); + } + } + + @Override + public void save() { + for (Configuration configuration : this.configurations) { + configuration.save(); + } + } + + @Override + public String getString(String path, String def) { + String result = def; + for (Configuration configuration : this.configurations) { + result = configuration.getString(path, result); + } + return result; + } + + @Override + public boolean getBoolean(String path, boolean def) { + boolean result = def; + for (Configuration configuration : this.configurations) { + result = configuration.getBoolean(path, result); + } + return result; + } + + @Override + public int getInteger(String path, int def) { + int result = def; + for (Configuration configuration : this.configurations) { + result = configuration.getInteger(path, result); + } + return result; + } + + @Override + public List getStringList(String path) { + for (Configuration configuration : this.configurations) { + List result = configuration.getStringList(path); + if (!result.isEmpty()) { + return result; + } + } + return Collections.emptyList(); + } + + @Override + public void setString(String path, String value) { + for (Configuration configuration : this.configurations) { + configuration.setString(path, value); + } + } + + @Override + public void setBoolean(String path, boolean value) { + for (Configuration configuration : this.configurations) { + configuration.setBoolean(path, value); + } + } + + @Override + public void setInteger(String path, int value) { + for (Configuration configuration : this.configurations) { + configuration.setInteger(path, value); + } + } + + @Override + public void setStringList(String path, List value) { + for (Configuration configuration : this.configurations) { + configuration.setStringList(path, value); + } + } + + @Override + public boolean contains(String path) { + for (Configuration configuration : this.configurations) { + if (configuration.contains(path)) { + return true; + } + } + return false; + } + + @Override + public void remove(String path) { + for (Configuration configuration : this.configurations) { + configuration.remove(path); + } + } +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/config/Configuration.java b/spark-common/src/main/java/me/lucko/spark/common/util/config/Configuration.java new file mode 100644 index 0000000..c2c2d88 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/util/config/Configuration.java @@ -0,0 +1,54 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * 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 . + */ + +package me.lucko.spark.common.util.config; + +import java.util.List; + +public interface Configuration { + + static Configuration combining(Configuration... configurations) { + return new CombinedConfiguration(configurations); + } + + void load(); + + void save(); + + String getString(String path, String def); + + boolean getBoolean(String path, boolean def); + + int getInteger(String path, int def); + + List getStringList(String path); + + void setString(String path, String value); + + void setBoolean(String path, boolean value); + + void setInteger(String path, int value); + + void setStringList(String path, List value); + + boolean contains(String path); + + void remove(String path); +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/config/FileConfiguration.java b/spark-common/src/main/java/me/lucko/spark/common/util/config/FileConfiguration.java new file mode 100644 index 0000000..72a4681 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/util/config/FileConfiguration.java @@ -0,0 +1,165 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * 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 . + */ + +package me.lucko.spark.common.util.config; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class FileConfiguration implements Configuration { + private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); + + private final Path file; + private JsonObject root; + + public FileConfiguration(Path file) { + this.file = file; + load(); + } + + @Override + public void load() { + JsonObject root = null; + if (Files.exists(this.file)) { + try (BufferedReader reader = Files.newBufferedReader(this.file, StandardCharsets.UTF_8)) { + root = GSON.fromJson(reader, JsonObject.class); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (root == null) { + root = new JsonObject(); + root.addProperty("_header", "spark configuration file - https://spark.lucko.me/docs/Configuration"); + } + this.root = root; + } + + @Override + public void save() { + try { + Files.createDirectories(this.file.getParent()); + } catch (IOException e) { + // ignore + } + + try (BufferedWriter writer = Files.newBufferedWriter(this.file, StandardCharsets.UTF_8)) { + GSON.toJson(this.root, writer); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public String getString(String path, String def) { + JsonElement el = this.root.get(path); + if (el == null || !el.isJsonPrimitive()) { + return def; + } + + return el.getAsJsonPrimitive().getAsString(); + } + + @Override + public boolean getBoolean(String path, boolean def) { + JsonElement el = this.root.get(path); + if (el == null || !el.isJsonPrimitive()) { + return def; + } + + JsonPrimitive val = el.getAsJsonPrimitive(); + return val.isBoolean() ? val.getAsBoolean() : def; + } + + @Override + public int getInteger(String path, int def) { + JsonElement el = this.root.get(path); + if (el == null || !el.isJsonPrimitive()) { + return def; + } + + JsonPrimitive val = el.getAsJsonPrimitive(); + return val.isNumber() ? val.getAsInt() : def; + } + + @Override + public List getStringList(String path) { + JsonElement el = this.root.get(path); + if (el == null || !el.isJsonArray()) { + return Collections.emptyList(); + } + + List list = new ArrayList<>(); + for (JsonElement child : el.getAsJsonArray()) { + if (child.isJsonPrimitive()) { + list.add(child.getAsJsonPrimitive().getAsString()); + } + } + return list; + } + + @Override + public void setString(String path, String value) { + this.root.add(path, new JsonPrimitive(value)); + } + + @Override + public void setBoolean(String path, boolean value) { + this.root.add(path, new JsonPrimitive(value)); + } + + @Override + public void setInteger(String path, int value) { + this.root.add(path, new JsonPrimitive(value)); + } + + @Override + public void setStringList(String path, List value) { + JsonArray array = new JsonArray(); + for (String str : value) { + array.add(str); + } + this.root.add(path, array); + } + + @Override + public boolean contains(String path) { + return this.root.has(path); + } + + @Override + public void remove(String path) { + this.root.remove(path); + } + +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/config/RuntimeConfiguration.java b/spark-common/src/main/java/me/lucko/spark/common/util/config/RuntimeConfiguration.java new file mode 100644 index 0000000..d076554 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/util/config/RuntimeConfiguration.java @@ -0,0 +1,106 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * 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 . + */ + +package me.lucko.spark.common.util.config; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public enum RuntimeConfiguration implements Configuration { + SYSTEM_PROPERTIES { + @Override + public String getString(String path, String def) { + return System.getProperty("spark." + path, def); + } + }, + + ENVIRONMENT_VARIABLES { + @Override + public String getString(String path, String def) { + String name = "SPARK_" + path.replace(".", "_").replace("-", "_").toUpperCase(); + String value = System.getenv(name); + return value != null ? value : def; + } + }; + + @Override + public boolean getBoolean(String path, boolean def) { + return Boolean.parseBoolean(getString(path, Boolean.toString(def))); + } + + @Override + public int getInteger(String path, int def) { + try { + return Integer.parseInt(getString(path, Integer.toString(def))); + } catch (NumberFormatException e) { + return def; + } + } + + @Override + public List getStringList(String path) { + String value = getString(path, ""); + if (value.isEmpty()) { + return Collections.emptyList(); + } + return Arrays.asList(value.split(",")); + } + + @Override + public boolean contains(String path) { + return getString(path, null) != null; + } + + @Override + public void load() { + // no-op + } + + @Override + public void save() { + // no-op + } + + @Override + public void setString(String path, String value) { + // no-op + } + + @Override + public void setBoolean(String path, boolean value) { + // no-op + } + + @Override + public void setInteger(String path, int value) { + // no-op + } + + @Override + public void setStringList(String path, List value) { + // no-op + } + + @Override + public void remove(String path) { + // no-op + } +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/ws/TrustedKeyStore.java b/spark-common/src/main/java/me/lucko/spark/common/ws/TrustedKeyStore.java index 1605a38..0d82514 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/ws/TrustedKeyStore.java +++ b/spark-common/src/main/java/me/lucko/spark/common/ws/TrustedKeyStore.java @@ -20,7 +20,7 @@ package me.lucko.spark.common.ws; -import me.lucko.spark.common.util.Configuration; +import me.lucko.spark.common.util.config.Configuration; import java.security.KeyPair; import java.security.PrivateKey; 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) + * 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 . + */ + +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) + * 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 . + */ + +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) + * 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 . + */ + +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) + * 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 . + */ + +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 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) + * 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 . + */ + +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) + * 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 . + */ + +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) + * 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 . + */ + +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) + * 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 . + */ + +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) + * 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 . + */ + +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) + * 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 . + */ + +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 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) + * 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 . + */ + +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) + * 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 . + */ + +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 excluded = ImmutableSet.builder() + .add("database") + .add("settings.bungeecord-addresses") + .add("rconpassword") + .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... elements) { + JsonObject object = new JsonObject(); + for (Map.Entry element : elements) { + object.add(element.getKey(), element.getValue()); + } + return object; + } + + private static Map.Entry 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) + * 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 . + */ + +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 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) + * 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 . + */ + +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 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) + * 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 . + */ + +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 threads = specific.getThreads(); + assertEquals(1, threads.size()); + assertTrue(threads.contains(thread)); + + Set 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) + * 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 . + */ + +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) + * 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 . + */ + +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) + * 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 . + */ + +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 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) + * 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 . + */ + +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) + * 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 . + */ + +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) + * 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 . + */ + +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) + * 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 . + */ + +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 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) + * 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 . + */ + +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) + * 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 . + */ + +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) + * 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 . + */ + +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 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; + } + }; + } +} -- cgit