Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import co.aikar.commands.ACFUtil;
import io.vavr.control.Option;
import io.vavr.control.Try;
import org.mvplugins.multiverse.core.exceptions.MultiverseException;

Check warning on line 9 in src/main/java/org/mvplugins/multiverse/core/configuration/functions/DefaultStringParserProvider.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 'org.mvplugins.multiverse.core.exceptions.MultiverseException' should be separated from previous imports. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/configuration/functions/DefaultStringParserProvider.java:9:1: warning: 'org.mvplugins.multiverse.core.exceptions.MultiverseException' should be separated from previous imports. (com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck)

/**
* Provides default string parsers for common types.
Expand Down Expand Up @@ -50,19 +51,19 @@

private static final NodeStringParser<Integer> INTEGER_STRING_PARSER = (input, type) -> Try.of(
() -> ACFUtil.parseInt(input))
.andThenTry(number -> Objects.requireNonNull(number, "Unable to convert '" + input + "' to number. (integer)"));
.flatMap(number -> Option.of(number).toTry(() -> new MultiverseException("Unable to convert '" + input + "' to number. (integer)")));

Check warning on line 54 in src/main/java/org/mvplugins/multiverse/core/configuration/functions/DefaultStringParserProvider.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Line is longer than 120 characters (found 145). Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/configuration/functions/DefaultStringParserProvider.java:54:0: warning: Line is longer than 120 characters (found 145). (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)

Check warning on line 54 in src/main/java/org/mvplugins/multiverse/core/configuration/functions/DefaultStringParserProvider.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 The String "Unable to convert '" appears 4 times in the file. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/configuration/functions/DefaultStringParserProvider.java:54:86: warning: The String "Unable to convert '" appears 4 times in the file. (com.puppycrawl.tools.checkstyle.checks.coding.MultipleStringLiteralsCheck)

private static final NodeStringParser<Double> DOUBLE_STRING_PARSER = (input, type) -> Try.of(
() -> ACFUtil.parseDouble(input))
.andThenTry(number -> Objects.requireNonNull(number, "Unable to convert '" + input + "' to number. (double)"));
.flatMap(number -> Option.of(number).toTry(() -> new MultiverseException("Unable to convert '" + input + "' to number. (double)")));

Check warning on line 58 in src/main/java/org/mvplugins/multiverse/core/configuration/functions/DefaultStringParserProvider.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Line is longer than 120 characters (found 144). Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/configuration/functions/DefaultStringParserProvider.java:58:0: warning: Line is longer than 120 characters (found 144). (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)

private static final NodeStringParser<Float> FLOAT_STRING_PARSER = (input, type) -> Try.of(
() -> ACFUtil.parseFloat(input))
.andThenTry(number -> Objects.requireNonNull(number, "Unable to convert '" + input + "' to number. (float)"));
.flatMap(number -> Option.of(number).toTry(() -> new MultiverseException("Unable to convert '" + input + "' to number. (float)")));

Check warning on line 62 in src/main/java/org/mvplugins/multiverse/core/configuration/functions/DefaultStringParserProvider.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Line is longer than 120 characters (found 143). Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/configuration/functions/DefaultStringParserProvider.java:62:0: warning: Line is longer than 120 characters (found 143). (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)

private static final NodeStringParser<Long> LONG_STRING_PARSER = (input, type) -> Try.of(
() -> ACFUtil.parseLong(input))
.andThenTry(number -> Objects.requireNonNull(number, "Unable to convert '" + input + "' to number. (long)"));
.flatMap(number -> Option.of(number).toTry(() -> new MultiverseException("Unable to convert '" + input + "' to number. (long)")));

Check warning on line 66 in src/main/java/org/mvplugins/multiverse/core/configuration/functions/DefaultStringParserProvider.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Line is longer than 120 characters (found 142). Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/configuration/functions/DefaultStringParserProvider.java:66:0: warning: Line is longer than 120 characters (found 142). (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)

static {
addDefaultStringParser(String.class, STRING_STRING_PARSER);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.mvplugins.multiverse.core.commands

import org.bukkit.ChatColor
import org.bukkit.command.ConsoleCommandSender
import org.bukkit.permissions.PermissionAttachment
import org.mockbukkit.mockbukkit.entity.PlayerMock
import org.mvplugins.multiverse.core.TestWithMockBukkit
import org.mvplugins.multiverse.core.commandtools.PluginLocales
import org.mvplugins.multiverse.core.utils.message.Message
import kotlin.test.BeforeTest
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

abstract class BaseCommandTest : TestWithMockBukkit() {

protected lateinit var player: PlayerMock
protected lateinit var console: ConsoleCommandSender

private lateinit var locales : PluginLocales
private lateinit var attachment : PermissionAttachment

@BeforeTest
fun setUpCommand() {
locales = serviceLocator.getActiveService(PluginLocales::class.java).takeIf { it != null } ?: run {
throw IllegalStateException("PluginLocales is not available as a service") }

console = server.consoleSender
player = server.addPlayer("benwoo1110");
assertEquals(player, server.getPlayer("benwoo1110"))
attachment = player.addAttachment(multiverseCore)
assertNotNull(attachment)
}

fun addPermission(permission: String) {
attachment.setPermission(permission, true)
}

fun assertCommandOutput(message : Message) {
assertEquals(
ChatColor.stripColor(ChatColor.translateAlternateColorCodes('&',message.formatted(locales))),
ChatColor.stripColor(player.nextMessage())
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.mvplugins.multiverse.core.commands

import co.aikar.commands.MessageKeys
import org.bukkit.Bukkit
import org.junit.jupiter.api.Test
import org.mvplugins.multiverse.core.config.MVCoreConfig
import org.mvplugins.multiverse.core.utils.MVCorei18n
import org.mvplugins.multiverse.core.utils.message.Message
import org.mvplugins.multiverse.core.utils.message.MessageReplacement.replace
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class ConfigCommandTest : BaseCommandTest() {

@Test
fun `Modify config global-debug`() {
assertTrue(Bukkit.dispatchCommand(console, "mv config global-debug 2"))
val coreConfig = serviceLocator.getActiveService(MVCoreConfig::class.java).takeIf { it != null } ?: run {
throw IllegalStateException("MVCoreConfig is not available as a service") }
assertEquals(2, coreConfig.globalDebug)
}

@Test
fun `Modify config global-debug player no permission`() {
assertTrue(Bukkit.dispatchCommand(player, "mv config global-debug 2"))
assertCommandOutput(Message.of(MessageKeys.PERMISSION_DENIED, ""))

val coreConfig = serviceLocator.getActiveService(MVCoreConfig::class.java).takeIf { it != null } ?: run {
throw IllegalStateException("MVCoreConfig is not available as a service") }
assertEquals(0, coreConfig.globalDebug)
}

@Test
fun `Modify non-existing config property`() {
addPermission("multiverse.core.config")
assertTrue(Bukkit.dispatchCommand(player, "mv config invalid-property test"))
player.nextMessage() // ignore the first line
assertCommandOutput(Message.of(
MVCorei18n.CONFIG_NODE_NOTFOUND,
"",
replace("{node}").with("invalid-property")))
}

@Test
fun `Modify config global-debug invalid type`() {
addPermission("multiverse.core.config")
assertTrue(Bukkit.dispatchCommand(player, "mv config global-debug what"))
player.nextMessage() // ignore the first line
assertCommandOutput(Message.of("Unable to convert 'what' to number. (integer)")) // todo: localize
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
package org.mvplugins.multiverse.core.commands

import co.aikar.commands.MessageKeys
import org.bukkit.Bukkit
import org.bukkit.ChatColor
import org.mockbukkit.mockbukkit.entity.PlayerMock
import org.mvplugins.multiverse.core.TestWithMockBukkit
import kotlin.test.BeforeTest
import org.mvplugins.multiverse.core.utils.MVCorei18n
import org.mvplugins.multiverse.core.utils.message.Message
import org.mvplugins.multiverse.core.utils.message.MessageReplacement.replace
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class VersionCommandTest : TestWithMockBukkit() {

private lateinit var player: PlayerMock

@BeforeTest
fun setUp() {
player = server.addPlayer("benwoo1110");
assertEquals(player, server.getPlayer("benwoo1110"))
}
class VersionCommandTest : BaseCommandTest() {

@Test
fun `Run version command as console`() {
Expand All @@ -29,8 +22,17 @@ class VersionCommandTest : TestWithMockBukkit() {

@Test
fun `Run version command as player`() {
addPermission("multiverse.core.version")
assertTrue(player.performCommand("mv version"))
assertCommandOutput(Message.of(
MVCorei18n.VERSION_MV,
"",
replace("{version}").with(multiverseCore.getDescription().getVersion())))
}

@Test
fun `Run version command as player - no permission`() {
assertTrue(player.performCommand("mv version"))
val output = ChatColor.stripColor(player.nextMessage())
assertEquals("I'm sorry, but you do not have permission to perform this command.", output)
assertCommandOutput(Message.of(MessageKeys.PERMISSION_DENIED, ""))
}
}
Loading