Improve testing and change playtime permissions

This commit is contained in:
okx-code
2021-06-25 18:18:29 +01:00
parent e125f962db
commit 59e97d3f61
21 changed files with 127 additions and 61 deletions
+13 -4
View File
@@ -1,7 +1,7 @@
plugins { plugins {
id 'java' id 'java'
id 'com.github.johnrengelman.shadow' version '6.1.0' id 'com.github.johnrengelman.shadow' version '6.1.0'
id "io.freefair.lombok" version "5.1.0" id "io.freefair.lombok" version "6.0.0-m2"
} }
group 'sh.okx' group 'sh.okx'
@@ -29,13 +29,14 @@ repositories {
} }
dependencies { dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.12' testImplementation("org.junit.platform:junit-platform-launcher:1.7.2")
testImplementation 'com.github.seeseemelk:MockBukkit-v1.16:1.0.0' testImplementation("org.junit.jupiter:junit-jupiter-engine:5.7.2")
testImplementation 'com.github.MockBukkit:MockBukkit:04889261630cd6f5aaebd86a576bbcd12c442ea7'
testImplementation group: 'org.slf4j', name: 'slf4j-nop', version: '1.7.30' testImplementation group: 'org.slf4j', name: 'slf4j-nop', version: '1.7.30'
testImplementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.30' testImplementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.30'
compileOnly 'org.jetbrains:annotations:16.0.2' compileOnly 'org.jetbrains:annotations:16.0.2'
compileOnly 'org.spigotmc:spigot-api:1.17-R0.1-SNAPSHOT' compileOnly 'org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT'
compileOnly('com.github.Realizedd:TokenManager:3.2.4') { compileOnly('com.github.Realizedd:TokenManager:3.2.4') {
transitive = false transitive = false
} }
@@ -72,6 +73,7 @@ shadowJar {
processResources { processResources {
// do this again whenever version changes // do this again whenever version changes
inputs.property 'version', project.version inputs.property 'version', project.version
duplicatesStrategy = 'include'
// copy plugin.yml and replace version // copy plugin.yml and replace version
from(sourceSets.main.resources.srcDirs) { from(sourceSets.main.resources.srcDirs) {
@@ -91,3 +93,10 @@ task spigot(type: com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
archiveFileName = "Rankup.jar" archiveFileName = "Rankup.jar"
} }
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}
Binary file not shown.
+1 -1
View File
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
Vendored
+2
View File
@@ -82,6 +82,7 @@ esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM. # Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
@@ -129,6 +130,7 @@ fi
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"` APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"` JAVACMD=`cygpath --unix "$JAVACMD"`
# We build the pattern for arguments to be converted via cygpath # We build the pattern for arguments to be converted via cygpath
Vendored
+4 -18
View File
@@ -40,7 +40,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1 %JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init if "%ERRORLEVEL%" == "0" goto execute
echo. echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
@@ -54,7 +54,7 @@ goto fail
set JAVA_HOME=%JAVA_HOME:"=% set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto init if exist "%JAVA_EXE%" goto execute
echo. echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
@@ -64,28 +64,14 @@ echo location of your Java installation.
goto fail goto fail
:init
@rem Get command-line arguments, handling Windows variants
if not "%OS%" == "Windows_NT" goto win9xME_args
:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2
:win9xME_args_slurp
if "x%~1" == "x" goto execute
set CMD_LINE_ARGS=%*
:execute :execute
@rem Setup the command line @rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle @rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
:end :end
@rem End local scope for the variables with windows NT shell @rem End local scope for the variables with windows NT shell
@@ -201,7 +201,7 @@ public class InfoCommand implements TabExecutor {
element = next; element = next;
} }
return true; return true;
} else if (args[0].equalsIgnoreCase("playtime") && (sender.hasPermission("rankup.playtime.get") || sender.hasPermission("rankup.playtime.set"))) { } else if (args[0].equalsIgnoreCase("playtime") && (sender.hasPermission("rankup.playtime.get") || sender.hasPermission("rankup.playtime"))) {
Statistic playOneTick; Statistic playOneTick;
try { try {
playOneTick = Statistic.valueOf("PLAY_ONE_MINUTE"); playOneTick = Statistic.valueOf("PLAY_ONE_MINUTE");
@@ -241,7 +241,7 @@ public class InfoCommand implements TabExecutor {
} }
player.sendMessage(ChatColor.LIGHT_PURPLE + who + " played for " + minutes + " minutes."); player.sendMessage(ChatColor.LIGHT_PURPLE + who + " played for " + minutes + " minutes.");
return true; return true;
} else if (args[1].equalsIgnoreCase("set") && sender.hasPermission("rankup.playtime.set")) { } else if (args[1].equalsIgnoreCase("set") && sender.hasPermission("rankup.playtime")) {
if (args.length < 4) { if (args.length < 4) {
sender.sendMessage(ChatColor.GREEN + "/" + label + " " + args[0] + " set <player> <minutes>" + ChatColor.YELLOW + " Update the playtime statistic for a player"); sender.sendMessage(ChatColor.GREEN + "/" + label + " " + args[0] + " set <player> <minutes>" + ChatColor.YELLOW + " Update the playtime statistic for a player");
return true; return true;
@@ -264,7 +264,7 @@ public class InfoCommand implements TabExecutor {
player.setStatistic(playOneTick, minutes * 20 * 60); player.setStatistic(playOneTick, minutes * 20 * 60);
player.sendMessage(ChatColor.LIGHT_PURPLE + "Updated playtime for " + player.getName() + " to " + minutes + " minutes"); player.sendMessage(ChatColor.LIGHT_PURPLE + "Updated playtime for " + player.getName() + " to " + minutes + " minutes");
return true; return true;
} else if (args[1].equalsIgnoreCase("add") && sender.hasPermission("rankup.playtime.set")) { } else if (args[1].equalsIgnoreCase("add") && sender.hasPermission("rankup.playtime")) {
if (args.length < 4) { if (args.length < 4) {
sender.sendMessage(ChatColor.GREEN + "/" + label + " " + args[0] + " add <player> <minutes>" + ChatColor.YELLOW + " Increase the playtime statistic for a player"); sender.sendMessage(ChatColor.GREEN + "/" + label + " " + args[0] + " add <player> <minutes>" + ChatColor.YELLOW + " Increase the playtime statistic for a player");
return true; return true;
@@ -304,7 +304,7 @@ public class InfoCommand implements TabExecutor {
ChatColor.GREEN + "/" + label + " " + args[0] + " get [player] " + ChatColor.YELLOW ChatColor.GREEN + "/" + label + " " + args[0] + " get [player] " + ChatColor.YELLOW
+ " Get amount of minutes played"); + " Get amount of minutes played");
} }
if (sender.hasPermission("rankup.playtime.set")) { if (sender.hasPermission("rankup.playtime")) {
sender.sendMessage( sender.sendMessage(
ChatColor.GREEN + "/" + label + " " + args[0] + " set <player> <minutes>" ChatColor.GREEN + "/" + label + " " + args[0] + " set <player> <minutes>"
+ ChatColor.YELLOW + " Update the playtime statistic for a player"); + ChatColor.YELLOW + " Update the playtime statistic for a player");
@@ -356,7 +356,7 @@ public class InfoCommand implements TabExecutor {
list.add("forceprestige"); list.add("forceprestige");
list.add("rankdown"); list.add("rankdown");
} }
if (sender.hasPermission("rankup.playtime.get") || sender.hasPermission("rankup.playtime.set")) { if (sender.hasPermission("rankup.playtime.get") || sender.hasPermission("rankup.playtime")) {
list.add("playtime"); list.add("playtime");
} }
return StringUtil.copyPartialMatches(args[0], list, new ArrayList<>()); return StringUtil.copyPartialMatches(args[0], list, new ArrayList<>());
@@ -372,7 +372,7 @@ public class InfoCommand implements TabExecutor {
if (sender.hasPermission("rankup.playtime.get")) { if (sender.hasPermission("rankup.playtime.get")) {
options.add("get"); options.add("get");
} }
if (sender.hasPermission("rankup.playtime.set")) { if (sender.hasPermission("rankup.playtime")) {
options.add("set"); options.add("set");
options.add("add"); options.add("add");
} }
@@ -33,14 +33,13 @@ public class PebbleMessageBuilder implements MessageBuilder {
} }
private void replaceInitial() { private void replaceInitial() {
Function<Player, Object> lastMinute = player -> { lastMinuteContext.put("ranks", player -> {
List<RankContext> ranks = new ArrayList<>(); List<RankContext> ranks = new ArrayList<>();
for (Rank rank : plugin.getRankups().getTree()) { for (Rank rank : plugin.getRankups().getTree()) {
ranks.add(new RankContext(plugin, player, rank)); ranks.add(new RankContext(plugin, player, rank));
} }
return ranks; return ranks;
}; });
lastMinuteContext.put("ranks", lastMinute);
lastMinuteContext.put("player", HumanEntity::getName); lastMinuteContext.put("player", HumanEntity::getName);
} }
+4 -1
View File
@@ -61,7 +61,10 @@ permissions:
rankup.maxrankup: rankup.maxrankup:
default: true default: true
rankup.playtime: rankup.playtime:
description: Use all /rankup3 playtime subcommands for anyone.
children: children:
rankup.playtime.get: true rankup.playtime.get: true
rankup.playtime.set: true default: op
rankup.playtime.get:
description: Use /rankup3 playtime get for anyone.
default: op default: op
@@ -1,10 +1,11 @@
package sh.okx.rankup; package sh.okx.rankup;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import be.seeseemelk.mockbukkit.entity.PlayerMock; import be.seeseemelk.mockbukkit.entity.PlayerMock;
import org.junit.Test; import org.junit.jupiter.api.Test;
import sh.okx.rankup.messages.Message; import sh.okx.rankup.messages.Message;
import sh.okx.rankup.ranks.Rank; import sh.okx.rankup.ranks.Rank;
import sh.okx.rankup.ranks.RankElement; import sh.okx.rankup.ranks.RankElement;
@@ -1,10 +1,11 @@
package sh.okx.rankup; package sh.okx.rankup;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import be.seeseemelk.mockbukkit.entity.PlayerMock; import be.seeseemelk.mockbukkit.entity.PlayerMock;
import org.junit.Test; import org.junit.jupiter.api.Test;
public class RankupCommandTest extends RankupTest { public class RankupCommandTest extends RankupTest {
@Test @Test
@@ -1,9 +1,10 @@
package sh.okx.rankup; package sh.okx.rankup;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import be.seeseemelk.mockbukkit.entity.PlayerMock; import be.seeseemelk.mockbukkit.entity.PlayerMock;
import org.junit.Test; import org.junit.jupiter.api.Test;
import sh.okx.rankup.placeholders.RankupExpansion; import sh.okx.rankup.placeholders.RankupExpansion;
public class RankupPlaceholderTest extends RankupTest { public class RankupPlaceholderTest extends RankupTest {
+6 -7
View File
@@ -10,12 +10,11 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor; import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import org.junit.After; import org.junit.jupiter.api.BeforeEach;
import org.junit.Before; import sh.okx.rankup.providers.TestEconomyProvider;
import sh.okx.rankup.economy.TestEconomyProvider;
import sh.okx.rankup.hook.GroupProvider; import sh.okx.rankup.hook.GroupProvider;
import sh.okx.rankup.hook.TestGroupProvider; import sh.okx.rankup.providers.TestGroupProvider;
import sh.okx.rankup.hook.TestPermissionManager; import sh.okx.rankup.providers.TestPermissionManager;
public abstract class RankupTest { public abstract class RankupTest {
private final File testResourceFolder; private final File testResourceFolder;
@@ -37,7 +36,7 @@ public abstract class RankupTest {
protected ServerMock server; protected ServerMock server;
protected RankupPlugin plugin; protected RankupPlugin plugin;
@Before @BeforeEach
public void setup() { public void setup() {
System.setProperty("RANKUP_TEST", "true"); System.setProperty("RANKUP_TEST", "true");
@@ -77,7 +76,7 @@ public abstract class RankupTest {
} }
} }
@After @BeforeEach
public void tearDown() { public void tearDown() {
MockBukkit.unmock(); MockBukkit.unmock();
System.clearProperty("RANKUP_TEST"); System.clearProperty("RANKUP_TEST");
@@ -0,0 +1,56 @@
package sh.okx.rankup.commands;
import static org.junit.jupiter.api.Assertions.*;
import be.seeseemelk.mockbukkit.entity.PlayerMock;
import org.bukkit.ChatColor;
import org.bukkit.Statistic;
import org.junit.jupiter.api.Test;
import sh.okx.rankup.RankupTest;
public class ComandPlaytimeTest extends RankupTest {
@Test
public void testAdd() {
PlayerMock player = server.addPlayer();
player.setStatistic(Statistic.PLAY_ONE_MINUTE, ticks(10));
player.addAttachment(plugin, "rankup.playtime", true);
player.performCommand("pru playtime add " + player.getName() + " 20");
assertEquals(ticks(30), player.getStatistic(Statistic.PLAY_ONE_MINUTE));
}
@Test
public void testSet() {
PlayerMock player = server.addPlayer();
player.setStatistic(Statistic.PLAY_ONE_MINUTE, ticks(20));
player.addAttachment(plugin, "rankup.playtime", true);
player.performCommand("pru playtime set " + player.getName() + " 25");
assertEquals(ticks(25), player.getStatistic(Statistic.PLAY_ONE_MINUTE));
}
@Test
public void testGetSelf() {
PlayerMock player = server.addPlayer();
player.setStatistic(Statistic.PLAY_ONE_MINUTE, ticks(5));
player.addAttachment(plugin, "rankup.playtime.get", true);
player.performCommand("pru playtime get " + player.getName());
player.assertSaid(ChatColor.LIGHT_PURPLE + "You have played for 5 minutes.");
player.assertNoMoreSaid();
}
private int ticks(int minutes) {
return minutes * 20 * 60;
}
private int minutes(int ticks) {
return ticks / 20 / 60;
}
}
@@ -2,7 +2,7 @@ package sh.okx.rankup.legacy;
import be.seeseemelk.mockbukkit.entity.PlayerMock; import be.seeseemelk.mockbukkit.entity.PlayerMock;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import org.junit.Test; import org.junit.jupiter.api.Test;
import sh.okx.rankup.RankupTest; import sh.okx.rankup.RankupTest;
public class LegacyPlaceholderTest extends RankupTest { public class LegacyPlaceholderTest extends RankupTest {
@@ -1,13 +1,13 @@
package sh.okx.rankup.messages; package sh.okx.rankup.messages;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.instanceOf; import static org.junit.jupiter.api.Assertions.*;
import static org.junit.Assert.assertThat;
import org.junit.jupiter.api.Test;
public class MessageBuilderTest { public class MessageBuilderTest {
@Test @Test
public void testFailIfEmpty() { public void testFailIfEmpty() {
assertThat(new StringMessageBuilder("").failIfEmpty(), instanceOf(NullMessageBuilder.class)); assertTrue(new StringMessageBuilder("").failIfEmpty() instanceof NullMessageBuilder);
} }
} }
@@ -1,10 +1,10 @@
package sh.okx.rankup.messages; package sh.okx.rankup.messages;
import static org.junit.Assert.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import be.seeseemelk.mockbukkit.entity.PlayerMock; import be.seeseemelk.mockbukkit.entity.PlayerMock;
import org.junit.Test; import org.junit.jupiter.api.Test;
import sh.okx.rankup.RankupTest; import sh.okx.rankup.RankupTest;
public class RankupPlaceholderTest extends RankupTest { public class RankupPlaceholderTest extends RankupTest {
@@ -1,12 +1,13 @@
package sh.okx.rankup.pebble; package sh.okx.rankup.pebble;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.junit.Test; import org.junit.jupiter.api.Test;
import sh.okx.rankup.text.pebble.PebbleTextProcessor; import sh.okx.rankup.text.pebble.PebbleTextProcessor;
public class PebbleTest { public class PebbleTest {
@@ -1,10 +1,11 @@
package sh.okx.rankup.economy; package sh.okx.rankup.providers;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import sh.okx.rankup.economy.Economy;
public class TestEconomy implements Economy { public class TestEconomy implements Economy {
private final Map<UUID, Double> balances = new HashMap<>(); private final Map<UUID, Double> balances = new HashMap<>();
@@ -1,4 +1,7 @@
package sh.okx.rankup.economy; package sh.okx.rankup.providers;
import sh.okx.rankup.economy.Economy;
import sh.okx.rankup.economy.EconomyProvider;
public class TestEconomyProvider implements EconomyProvider { public class TestEconomyProvider implements EconomyProvider {
@Override @Override
@@ -1,9 +1,10 @@
package sh.okx.rankup.hook; package sh.okx.rankup.providers;
import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import java.util.UUID; import java.util.UUID;
import sh.okx.rankup.hook.GroupProvider;
public class TestGroupProvider implements GroupProvider { public class TestGroupProvider implements GroupProvider {
private Multimap<UUID, String> groups = ArrayListMultimap.create(); private Multimap<UUID, String> groups = ArrayListMultimap.create();
@@ -1,4 +1,7 @@
package sh.okx.rankup.hook; package sh.okx.rankup.providers;
import sh.okx.rankup.hook.GroupProvider;
import sh.okx.rankup.hook.PermissionManager;
public class TestPermissionManager implements PermissionManager { public class TestPermissionManager implements PermissionManager {
private final GroupProvider groupProvider; private final GroupProvider groupProvider;