diff options
| author | Luck <git@lucko.me> | 2024-07-29 18:33:08 +0100 |
|---|---|---|
| committer | Luck <git@lucko.me> | 2024-07-29 18:33:08 +0100 |
| commit | 60d54cc4df05e3328f8b8d64ea3b44d5d22c9ed7 (patch) | |
| tree | 2bf8fcf914ac57466549d35dcd89ef96d3a2d65f /spark-common/src/test/java/me/lucko/spark/common | |
| parent | 4c0149b6a15fa887328bbd88c8055c2138cc4d72 (diff) | |
| download | spark-60d54cc4df05e3328f8b8d64ea3b44d5d22c9ed7.tar.gz spark-60d54cc4df05e3328f8b8d64ea3b44d5d22c9ed7.tar.bz2 spark-60d54cc4df05e3328f8b8d64ea3b44d5d22c9ed7.zip | |
Add some unit tests
Diffstat (limited to 'spark-common/src/test/java/me/lucko/spark/common')
22 files changed, 1205 insertions, 0 deletions
diff --git a/spark-common/src/test/java/me/lucko/spark/common/SparkPlatformTest.java b/spark-common/src/test/java/me/lucko/spark/common/SparkPlatformTest.java new file mode 100644 index 0000000..ec3638f --- /dev/null +++ b/spark-common/src/test/java/me/lucko/spark/common/SparkPlatformTest.java @@ -0,0 +1,46 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.common; + +import me.lucko.spark.test.plugin.TestCommandSender; +import me.lucko.spark.test.plugin.TestSparkPlugin; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.nio.file.Path; + +public class SparkPlatformTest { + + @Test + public void testEnableDisable(@TempDir Path directory) { + System.setProperty("spark.backgroundProfiler", "false"); + + SparkPlatform platform = new SparkPlatform(new TestSparkPlugin(directory)); + platform.enable(); + + platform.executeCommand(TestCommandSender.INSTANCE, new String[]{"help"}).join(); + platform.executeCommand(TestCommandSender.INSTANCE, new String[]{"profiler", "info"}).join(); + platform.executeCommand(TestCommandSender.INSTANCE, new String[]{"health"}).join(); + + platform.disable(); + } + +} diff --git a/spark-common/src/test/java/me/lucko/spark/common/activitylog/ActivityLogTest.java b/spark-common/src/test/java/me/lucko/spark/common/activitylog/ActivityLogTest.java new file mode 100644 index 0000000..a94f954 --- /dev/null +++ b/spark-common/src/test/java/me/lucko/spark/common/activitylog/ActivityLogTest.java @@ -0,0 +1,54 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.common.activitylog; + +import me.lucko.spark.common.command.sender.CommandSender; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.nio.file.Path; +import java.util.UUID; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ActivityLogTest { + + private static final CommandSender.Data USER = new CommandSender.Data("Test", UUID.fromString("5937921d-7051-45e1-bac7-3bbfdc12444f")); + + @Test + public void testSaveLoad(@TempDir Path tempDir) { + ActivityLog log = new ActivityLog(tempDir.resolve("activity-log.json")); + log.addToLog(Activity.fileActivity(USER, 1721937782184L, "Profiler", "path/to/profile.sparkprofile")); + log.addToLog(Activity.urlActivity(USER, 1721937782184L, "Profiler", "https://spark.lucko.me/abcd")); + log.save(); + + ActivityLog log2 = new ActivityLog(tempDir.resolve("activity-log.json")); + log2.load(); + + // check the log contents + assertEquals( + log.getLog().stream().map(Activity::serialize).collect(Collectors.toList()), + log2.getLog().stream().map(Activity::serialize).collect(Collectors.toList()) + ); + } + +} diff --git a/spark-common/src/test/java/me/lucko/spark/common/activitylog/ActivityTest.java b/spark-common/src/test/java/me/lucko/spark/common/activitylog/ActivityTest.java new file mode 100644 index 0000000..5bf88f8 --- /dev/null +++ b/spark-common/src/test/java/me/lucko/spark/common/activitylog/ActivityTest.java @@ -0,0 +1,78 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.common.activitylog; + +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import me.lucko.spark.common.command.sender.CommandSender; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ActivityTest { + private static final Gson GSON = new Gson(); + + private static final CommandSender.Data USER = new CommandSender.Data("Test", UUID.fromString("5937921d-7051-45e1-bac7-3bbfdc12444f")); + + private static final String FILE_ACTIVITY_JSON = "{\"user\":{\"type\":\"player\",\"name\":\"Test\",\"uniqueId\":\"5937921d-7051-45e1-bac7-3bbfdc12444f\"},\"time\":1721937782184,\"type\":\"Profiler\",\"data\":{\"type\":\"file\",\"value\":\"path/to/profile.sparkprofile\"}}"; + private static final String URL_ACTIVITY_JSON = "{\"user\":{\"type\":\"player\",\"name\":\"Test\",\"uniqueId\":\"5937921d-7051-45e1-bac7-3bbfdc12444f\"},\"time\":1721937782184,\"type\":\"Profiler\",\"data\":{\"type\":\"url\",\"value\":\"https://spark.lucko.me/abcd\"}}"; + + @Test + public void testSerialize() { + Activity fileActivity = Activity.fileActivity( + USER, + 1721937782184L, + "Profiler", + "path/to/profile.sparkprofile" + ); + assertEquals(FILE_ACTIVITY_JSON, GSON.toJson(fileActivity.serialize())); + + Activity urlActivity = Activity.urlActivity( + USER, + 1721937782184L, + "Profiler", + "https://spark.lucko.me/abcd" + ); + assertEquals(URL_ACTIVITY_JSON, GSON.toJson(urlActivity.serialize())); + } + + @Test + public void testDeserialize() { + Activity fileActivity = Activity.deserialize(GSON.fromJson(FILE_ACTIVITY_JSON, JsonElement.class)); + assertEquals(USER.getUniqueId(), fileActivity.getUser().getUniqueId()); + assertEquals(USER.getName(), fileActivity.getUser().getName()); + assertEquals(1721937782184L, fileActivity.getTime()); + assertEquals("Profiler", fileActivity.getType()); + assertEquals(Activity.DATA_TYPE_FILE, fileActivity.getDataType()); + assertEquals("path/to/profile.sparkprofile", fileActivity.getDataValue()); + + Activity urlActivity = Activity.deserialize(GSON.fromJson(URL_ACTIVITY_JSON, JsonElement.class)); + assertEquals(USER.getUniqueId(), urlActivity.getUser().getUniqueId()); + assertEquals(USER.getName(), urlActivity.getUser().getName()); + assertEquals(1721937782184L, urlActivity.getTime()); + assertEquals("Profiler", urlActivity.getType()); + assertEquals(Activity.DATA_TYPE_URL, urlActivity.getDataType()); + assertEquals("https://spark.lucko.me/abcd", urlActivity.getDataValue()); + } + +} diff --git a/spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpSummaryTest.java b/spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpSummaryTest.java new file mode 100644 index 0000000..42492d1 --- /dev/null +++ b/spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpSummaryTest.java @@ -0,0 +1,47 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.common.heapdump; + +import me.lucko.spark.test.TestClass; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class HeapDumpSummaryTest { + + @Test + public void testHeapDumpSummary() throws Exception { + TestClass testClass1 = new TestClass(); + TestClass testClass2 = new TestClass(); + + HeapDumpSummary dump = HeapDumpSummary.createNew(); + List<HeapDumpSummary.Entry> entries = dump.getEntries(); + + HeapDumpSummary.Entry thisClassEntry = entries.stream().filter(entry -> entry.getType().equals(TestClass.class.getName())).findAny().orElse(null); + assertNotNull(thisClassEntry); + assertEquals(2, thisClassEntry.getInstances()); + assertEquals(32, thisClassEntry.getBytes()); + } + +} diff --git a/spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpTest.java b/spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpTest.java new file mode 100644 index 0000000..5df5c5d --- /dev/null +++ b/spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpTest.java @@ -0,0 +1,41 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.common.heapdump; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class HeapDumpTest { + + @Test + public void testHeapDump(@TempDir Path tempDir) throws Exception { + Path file = tempDir.resolve("heapdump.hprof"); + HeapDump.dumpHeap(file, false); + assertTrue(Files.exists(file)); + Files.delete(file); + } + +} diff --git a/spark-common/src/test/java/me/lucko/spark/common/monitor/cpu/CpuInfoTest.java b/spark-common/src/test/java/me/lucko/spark/common/monitor/cpu/CpuInfoTest.java new file mode 100644 index 0000000..047e80d --- /dev/null +++ b/spark-common/src/test/java/me/lucko/spark/common/monitor/cpu/CpuInfoTest.java @@ -0,0 +1,38 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.common.monitor.cpu; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class CpuInfoTest { + + @Test + public void testCpuInfo() { + String model = CpuInfo.queryCpuModel(); + assertNotNull(model); + assertFalse(model.isEmpty()); + System.out.println(model); + } + +} diff --git a/spark-common/src/test/java/me/lucko/spark/common/monitor/cpu/CpuMonitorTest.java b/spark-common/src/test/java/me/lucko/spark/common/monitor/cpu/CpuMonitorTest.java new file mode 100644 index 0000000..d554976 --- /dev/null +++ b/spark-common/src/test/java/me/lucko/spark/common/monitor/cpu/CpuMonitorTest.java @@ -0,0 +1,35 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.common.monitor.cpu; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class CpuMonitorTest { + + @Test + public void testCpuLoad() { + assertTrue(CpuMonitor.processLoad() >= 0); + assertTrue(CpuMonitor.systemLoad() >= 0); + } + +} diff --git a/spark-common/src/test/java/me/lucko/spark/common/monitor/disk/DiskUsageTest.java b/spark-common/src/test/java/me/lucko/spark/common/monitor/disk/DiskUsageTest.java new file mode 100644 index 0000000..d961b2f --- /dev/null +++ b/spark-common/src/test/java/me/lucko/spark/common/monitor/disk/DiskUsageTest.java @@ -0,0 +1,35 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.common.monitor.disk; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class DiskUsageTest { + + @Test + public void testDiskUsage() { + assertTrue(DiskUsage.getUsed() > 0); + assertTrue(DiskUsage.getTotal() > 0); + } + +} diff --git a/spark-common/src/test/java/me/lucko/spark/common/monitor/memory/MemoryInfoTest.java b/spark-common/src/test/java/me/lucko/spark/common/monitor/memory/MemoryInfoTest.java new file mode 100644 index 0000000..5ae8fdc --- /dev/null +++ b/spark-common/src/test/java/me/lucko/spark/common/monitor/memory/MemoryInfoTest.java @@ -0,0 +1,36 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.common.monitor.memory; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class MemoryInfoTest { + + @Test + public void testMemoryInfo() { + assertTrue(MemoryInfo.getUsedPhysicalMemory() > 0); + assertTrue(MemoryInfo.getTotalPhysicalMemory() > 0); + assertTrue(MemoryInfo.getAvailablePhysicalMemory() > 0); + } + +} diff --git a/spark-common/src/test/java/me/lucko/spark/common/monitor/net/NetworkInterfaceInfoTest.java b/spark-common/src/test/java/me/lucko/spark/common/monitor/net/NetworkInterfaceInfoTest.java new file mode 100644 index 0000000..6b50584 --- /dev/null +++ b/spark-common/src/test/java/me/lucko/spark/common/monitor/net/NetworkInterfaceInfoTest.java @@ -0,0 +1,57 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.common.monitor.net; + +import com.google.common.collect.ImmutableSet; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class NetworkInterfaceInfoTest { + + @Test + public void testLinuxProcParse() { + String input = + "Inter-| Receive | Transmit\n" + + " face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed\n" + + " lo: 2776770 11307 0 0 0 0 0 0 2776770 11307 0 0 0 0 0 0\n" + + " eth0: 1215645 2751 1 0 0 0 0 0 1782404 4324 2 0 0 427 0 0\n" + + " ppp0: 1622270 5552 1 0 0 0 0 0 354130 5669 0 0 0 0 0 0\n" + + " tap0: 7714 81 0 0 0 0 0 0 7714 81 0 0 0 0 0 0"; + + Map<String, NetworkInterfaceInfo> map = NetworkInterfaceInfo.read(Arrays.asList(input.split("\n"))); + assertNotNull(map); + assertEquals(ImmutableSet.of("lo", "eth0", "ppp0", "tap0"), map.keySet()); + + NetworkInterfaceInfo eth0 = map.get("eth0"); + assertEquals(1215645, eth0.getReceivedBytes()); + assertEquals(2751, eth0.getReceivedPackets()); + assertEquals(1, eth0.getReceiveErrors()); + assertEquals(1782404, eth0.getTransmittedBytes()); + assertEquals(4324, eth0.getTransmittedPackets()); + assertEquals(2, eth0.getTransmitErrors()); + } + +} diff --git a/spark-common/src/test/java/me/lucko/spark/common/monitor/os/OperatingSystemInfoTest.java b/spark-common/src/test/java/me/lucko/spark/common/monitor/os/OperatingSystemInfoTest.java new file mode 100644 index 0000000..3e4fd13 --- /dev/null +++ b/spark-common/src/test/java/me/lucko/spark/common/monitor/os/OperatingSystemInfoTest.java @@ -0,0 +1,42 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.common.monitor.os; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class OperatingSystemInfoTest { + + @Test + public void testOperatingSystemInfo() { + OperatingSystemInfo info = OperatingSystemInfo.poll(); + assertNotNull(info); + assertNotNull(info.name()); + assertNotNull(info.version()); + assertNotNull(info.arch()); + + System.out.println(info.name()); + System.out.println(info.version()); + System.out.println(info.arch()); + } + +} diff --git a/spark-common/src/test/java/me/lucko/spark/common/platform/serverconfig/ExcludedConfigFilterTest.java b/spark-common/src/test/java/me/lucko/spark/common/platform/serverconfig/ExcludedConfigFilterTest.java new file mode 100644 index 0000000..ba6f958 --- /dev/null +++ b/spark-common/src/test/java/me/lucko/spark/common/platform/serverconfig/ExcludedConfigFilterTest.java @@ -0,0 +1,106 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.common.platform.serverconfig; + +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Maps; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import org.junit.jupiter.api.Test; + +import java.util.Map; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ExcludedConfigFilterTest { + + @Test + public void testFilter() { + Set<String> excluded = ImmutableSet.<String>builder() + .add("database") + .add("settings.bungeecord-addresses") + .add("rcon<dot>password") + .add("world-settings.*.feature-seeds") + .add("world-settings.*.seed-*") + .build(); + + ExcludedConfigFilter filter = new ExcludedConfigFilter(excluded); + + JsonPrimitive value = new JsonPrimitive("hello"); + JsonObject before = obj( + element("hello", value), + element("database", obj( + element("username", value), + element("password", value) + )), + element("settings", obj( + element("hello", value), + element("bungeecord-addresses", value) + )), + element("rcon.password", value), + element("world-settings", obj( + element("world1", obj( + element("hello", value), + element("feature-seeds", value), + element("seed-test", value) + )), + element("world2", obj( + element("hello", value), + element("feature-seeds", value), + element("seed-test", value) + )) + )) + ); + JsonObject after = obj( + element("hello", value), + element("settings", obj( + element("hello", value) + )), + element("world-settings", obj( + element("world1", obj( + element("hello", value) + )), + element("world2", obj( + element("hello", value) + )) + )) + ); + + + assertEquals(after, filter.apply(before)); + } + + @SafeVarargs + private static JsonObject obj(Map.Entry<String, JsonElement>... elements) { + JsonObject object = new JsonObject(); + for (Map.Entry<String, JsonElement> element : elements) { + object.add(element.getKey(), element.getValue()); + } + return object; + } + + private static Map.Entry<String, JsonElement> element(String key, JsonElement value) { + return Maps.immutableEntry(key, value); + } + +} diff --git a/spark-common/src/test/java/me/lucko/spark/common/platform/serverconfig/PropertiesConfigParserTest.java b/spark-common/src/test/java/me/lucko/spark/common/platform/serverconfig/PropertiesConfigParserTest.java new file mode 100644 index 0000000..2b686ca --- /dev/null +++ b/spark-common/src/test/java/me/lucko/spark/common/platform/serverconfig/PropertiesConfigParserTest.java @@ -0,0 +1,50 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.common.platform.serverconfig; + +import com.google.common.collect.ImmutableMap; +import org.junit.jupiter.api.Test; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.StringReader; +import java.util.Map; + +import static org.junit.jupiter.api.Ass |
