aboutsummaryrefslogtreecommitdiff
path: root/spark-common/src/test/java
diff options
context:
space:
mode:
authorlucko <git@lucko.me>2024-08-06 21:29:48 +0100
committerGitHub <noreply@github.com>2024-08-06 21:29:48 +0100
commit8017dbd288fca21dac3813409dec959ae6e6c822 (patch)
treebce438e9a7156dcf04f99194295c753702600f1c /spark-common/src/test/java
parent613f702ddd8c1a7515298917a74464e6799df0c4 (diff)
downloadspark-8017dbd288fca21dac3813409dec959ae6e6c822.tar.gz
spark-8017dbd288fca21dac3813409dec959ae6e6c822.tar.bz2
spark-8017dbd288fca21dac3813409dec959ae6e6c822.zip
Add uploadable health report (#436)
Diffstat (limited to 'spark-common/src/test/java')
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/command/ArgumentsTest.java137
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/platform/PlatformStatisticsProviderTest.java3
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/platform/SparkMetadataTest.java43
3 files changed, 182 insertions, 1 deletions
diff --git a/spark-common/src/test/java/me/lucko/spark/common/command/ArgumentsTest.java b/spark-common/src/test/java/me/lucko/spark/common/command/ArgumentsTest.java
new file mode 100644
index 0000000..dda6c5d
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/command/ArgumentsTest.java
@@ -0,0 +1,137 @@
+/*
+ * 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.command;
+
+import com.google.common.collect.ImmutableList;
+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.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class ArgumentsTest {
+
+ @Test
+ public void testInitialParse() {
+ Arguments arguments = new Arguments(ImmutableList.of("hello"), true);
+ assertEquals("hello", arguments.subCommand());
+
+ Arguments.ParseException exception = assertThrowsExactly(
+ Arguments.ParseException.class,
+ () -> new Arguments(ImmutableList.of("hello"), false)
+ );
+ assertEquals("Expected flag at position 0 but got 'hello' instead!", exception.getMessage());
+
+ exception = assertThrowsExactly(
+ Arguments.ParseException.class,
+ () -> new Arguments(ImmutableList.of("hello", "world"), true)
+ );
+ assertEquals("Expected flag at position 1 but got 'world' instead!", exception.getMessage());
+ }
+
+ @Test
+ public void testStringFlag() {
+ Arguments arguments = new Arguments(ImmutableList.of("--test-flag", "hello"), false);
+
+ Set<String> values = arguments.stringFlag("test-flag");
+ assertEquals(1, values.size());
+ assertEquals("hello", values.iterator().next());
+ }
+
+ @Test
+ public void testStringFlagWithSpace() {
+ Arguments arguments = new Arguments(ImmutableList.of("--test-flag", "hello", "world"), false);
+
+ Set<String> values = arguments.stringFlag("test-flag");
+ assertEquals(1, values.size());
+ assertEquals("hello world", values.iterator().next());
+ }
+
+ @Test
+ public void testStringFlagWithMultipleValues() {
+ Arguments arguments = new Arguments(ImmutableList.of("--test-flag", "hello", "--test-flag", "world"), false);
+
+ Set<String> values = arguments.stringFlag("test-flag");
+ assertEquals(2, values.size());
+ assertEquals(ImmutableList.of("hello", "world"), ImmutableList.copyOf(values));
+ }
+
+ @Test
+ public void testMissingStringFlag() {
+ Arguments arguments = new Arguments(ImmutableList.of("--test-flag", "hello"), false);
+
+ Set<String> values = arguments.stringFlag("missing-flag");
+ assertEquals(0, values.size());
+ }
+
+ @Test
+ public void testIntFlag() {
+ Arguments arguments = new Arguments(ImmutableList.of("--test-flag", "123", "--negative-test", "-100"), false);
+
+ int value = arguments.intFlag("test-flag");
+ assertEquals(123, value);
+
+ value = arguments.intFlag("negative-test");
+ assertEquals(100, value);
+ }
+
+ @Test
+ public void testMissingIntFlag() {
+ Arguments arguments = new Arguments(ImmutableList.of("--test-flag", "hello"), false);
+
+ int value = arguments.intFlag("missing-flag");
+ assertEquals(-1, value);
+ }
+
+ @Test
+ public void testDoubleFlag() {
+ Arguments arguments = new Arguments(ImmutableList.of("--test-flag", "123.45", "--negative-test", "-100.5"), false);
+
+ double value = arguments.doubleFlag("test-flag");
+ assertEquals(123.45, value, 0.0001);
+
+ value = arguments.doubleFlag("negative-test");
+ assertEquals(100.5, value, 0.0001);
+ }
+
+ @Test
+ public void testMissingDoubleFlag() {
+ Arguments arguments = new Arguments(ImmutableList.of("--test-flag", "hello"), false);
+
+ double value = arguments.doubleFlag("missing-flag");
+ assertEquals(-1, value);
+ }
+
+ @Test
+ public void testBooleanFlag() {
+ Arguments arguments = new Arguments(ImmutableList.of("--test-flag"), false);
+
+ boolean value = arguments.boolFlag("test-flag");
+ assertTrue(value);
+
+ value = arguments.boolFlag("negative-test");
+ assertFalse(value);
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/platform/PlatformStatisticsProviderTest.java b/spark-common/src/test/java/me/lucko/spark/common/platform/PlatformStatisticsProviderTest.java
index 63e62d9..4b26322 100644
--- a/spark-common/src/test/java/me/lucko/spark/common/platform/PlatformStatisticsProviderTest.java
+++ b/spark-common/src/test/java/me/lucko/spark/common/platform/PlatformStatisticsProviderTest.java
@@ -26,6 +26,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Path;
+import java.util.Collections;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -42,7 +43,7 @@ public class PlatformStatisticsProviderTest {
@Test
public void testPlatformStatistics(@TempDir Path directory) {
try (TestSparkPlugin plugin = new TestSparkPlugin(directory)) {
- SparkProtos.PlatformStatistics platformStatistics = new PlatformStatisticsProvider(plugin.platform()).getPlatformStatistics(null, true);
+ SparkProtos.PlatformStatistics platformStatistics = new PlatformStatisticsProvider(plugin.platform()).getPlatformStatistics(Collections.emptyMap(), true);
assertNotNull(platformStatistics);
}
}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/platform/SparkMetadataTest.java b/spark-common/src/test/java/me/lucko/spark/common/platform/SparkMetadataTest.java
new file mode 100644
index 0000000..e2b8374
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/platform/SparkMetadataTest.java
@@ -0,0 +1,43 @@
+/*
+ * 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;
+
+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;
+import java.util.Collections;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+public class SparkMetadataTest {
+
+ @Test
+ public void testGather(@TempDir Path directory) {
+ try (TestSparkPlugin plugin = new TestSparkPlugin(directory)) {
+ SparkMetadata metadata = SparkMetadata.gather(plugin.platform(), TestCommandSender.INSTANCE.toData(), Collections.emptyMap());
+ assertNotNull(metadata);
+ }
+ }
+
+}