mirror of
https://github.com/BentoBoxWorld/Challenges.git
synced 2024-11-22 02:25:52 +01:00
Test coverage (#199)
* Test coverage for Challenges Command * Added CompleteChallengeCommand test class * Added Utils test class * Added ChallengesGUI test class * Fix code smells from sonarcloud analysis * Added .gitignore * Added Travis CI config file * WIP ChallengesManager Test class * Added ChallengesManager test class * Removed debug * Removed code smells. * Added ChallengesAddon test class. * Added onDisbale test * Added new TryToComplete test class - WIP Covers inventory challenges. * Added Island Challenge entity tests to TryToComplete test class
This commit is contained in:
parent
d1c9e8fd44
commit
9d45f08fb1
1
.gitignore
vendored
1
.gitignore
vendored
@ -131,3 +131,4 @@ local.properties
|
||||
/target/
|
||||
checkstyle.xml
|
||||
classes/
|
||||
/.DS_Store
|
||||
|
21
.travis.yml
Normal file
21
.travis.yml
Normal file
@ -0,0 +1,21 @@
|
||||
language: java
|
||||
sudo: false
|
||||
addons:
|
||||
sonarcloud:
|
||||
organization: "bentobox-world"
|
||||
token:
|
||||
secure: $SONAR_TOKEN
|
||||
branches:
|
||||
- develop
|
||||
- master
|
||||
jdk:
|
||||
- openjdk8
|
||||
|
||||
script:
|
||||
#- sonar-scanner
|
||||
- mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent package -P sonar sonar:sonar -B
|
||||
#- echo "${TRAVIS_PULL_REQUEST_BRANCH:-$TRAVIS_BRANCH}"
|
||||
cache:
|
||||
directories:
|
||||
- '$HOME/.m2/repository'
|
||||
- '$HOME/.sonar/cache'
|
9
pom.xml
9
pom.xml
@ -33,7 +33,7 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<powermock.version>1.7.4</powermock.version>
|
||||
<powermock.version>2.0.2</powermock.version>
|
||||
<!-- More visible way how to change dependency versions -->
|
||||
<spigot.version>1.14.4-R0.1-SNAPSHOT</spigot.version>
|
||||
<bentobox.version>1.7.0</bentobox.version>
|
||||
@ -129,10 +129,11 @@
|
||||
<version>${spigot.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- Mockito (Unit testing) -->
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-all</artifactId>
|
||||
<version>1.10.19</version>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@ -143,7 +144,7 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-mockito</artifactId>
|
||||
<artifactId>powermock-api-mockito2</artifactId>
|
||||
<version>${powermock.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
@ -5,6 +5,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
|
||||
import world.bentobox.bentobox.api.addons.Addon;
|
||||
@ -88,7 +89,7 @@ public class ChallengesAddon extends Addon {
|
||||
* This flag allows to complete challenges in any part of the world. It will not limit
|
||||
* player to their island. Useful for skygrid without protection flags.
|
||||
*/
|
||||
public static Flag CHALLENGES_WORLD_PROTECTION =
|
||||
public static final Flag CHALLENGES_WORLD_PROTECTION =
|
||||
new Flag.Builder("CHALLENGES_WORLD_PROTECTION", Material.GRASS_BLOCK).type(Flag.Type.WORLD_SETTING).defaultSetting(true).build();
|
||||
|
||||
/**
|
||||
@ -96,7 +97,7 @@ public class ChallengesAddon extends Addon {
|
||||
* that only Island owner can complete challenge.
|
||||
* By default it is set to Visitor.
|
||||
*/
|
||||
public static Flag CHALLENGES_ISLAND_PROTECTION =
|
||||
public static final Flag CHALLENGES_ISLAND_PROTECTION =
|
||||
new Flag.Builder("CHALLENGES_ISLAND_PROTECTION", Material.COMMAND_BLOCK).defaultRank(RanksManager.VISITOR_RANK).build();
|
||||
|
||||
|
||||
@ -155,7 +156,11 @@ public class ChallengesAddon extends Addon {
|
||||
List<GameModeAddon> hookedGameModes = new ArrayList<>();
|
||||
|
||||
this.getPlugin().getAddonsManager().getGameModeAddons().forEach(gameModeAddon -> {
|
||||
if (!this.settings.getDisabledGameModes().contains(gameModeAddon.getDescription().getName()))
|
||||
if (!this.settings
|
||||
.getDisabledGameModes()
|
||||
.contains(gameModeAddon
|
||||
.getDescription()
|
||||
.getName()))
|
||||
{
|
||||
if (gameModeAddon.getPlayerCommand().isPresent())
|
||||
{
|
||||
@ -238,7 +243,7 @@ public class ChallengesAddon extends Addon {
|
||||
|
||||
if (this.settings.getAutoSaveTimer() > 0)
|
||||
{
|
||||
this.getPlugin().getServer().getScheduler().runTaskTimerAsynchronously(
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(
|
||||
this.getPlugin(),
|
||||
bukkitTask -> ChallengesAddon.this.challengesManager.save(),
|
||||
this.settings.getAutoSaveTimer() * 60 * 20,
|
||||
@ -264,7 +269,7 @@ public class ChallengesAddon extends Addon {
|
||||
{
|
||||
this.loadSettings();
|
||||
this.challengesManager.reload();
|
||||
this.getLogger().info("Challenges addon reloaded.");
|
||||
this.log("Challenges addon reloaded.");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -92,7 +92,7 @@ public class ChallengesImportManager
|
||||
// each game mode.
|
||||
String uniqueIDPrefix = Utils.getGameMode(world) + "_";
|
||||
DefaultDataHolder defaultChallenges = new DefaultJSONHandler(this.addon).loadObject();
|
||||
|
||||
if (defaultChallenges != null) {
|
||||
// All new challenges should get correct ID. So we need to map it to loaded challenges.
|
||||
defaultChallenges.getChallengeList().forEach(challenge -> {
|
||||
// Set correct challenge ID
|
||||
@ -119,6 +119,7 @@ public class ChallengesImportManager
|
||||
manager.loadLevel(challengeLevel, false, user, user == null);
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
@ -130,7 +131,7 @@ public class ChallengesImportManager
|
||||
if (removeAtEnd)
|
||||
{
|
||||
// Remove default.yml file from resources to avoid interacting with it.
|
||||
new File(this.addon.getDataFolder(), "default.json").delete();
|
||||
return new File(this.addon.getDataFolder(), "default.json").delete();
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -198,7 +199,7 @@ public class ChallengesImportManager
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
addon.getPlugin().logStacktrace(e);
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -299,11 +300,11 @@ public class ChallengesImportManager
|
||||
defaultChallenges.setLevelList(levelList);
|
||||
defaultChallenges.setVersion(this.addon.getDescription().getVersion());
|
||||
|
||||
BufferedWriter writer = new BufferedWriter(
|
||||
new OutputStreamWriter(new FileOutputStream(defaultFile), StandardCharsets.UTF_8));
|
||||
try (BufferedWriter writer = new BufferedWriter(
|
||||
new OutputStreamWriter(new FileOutputStream(defaultFile), StandardCharsets.UTF_8))) {
|
||||
writer.write(Objects.requireNonNull(
|
||||
new DefaultJSONHandler(this.addon).toJsonString(defaultChallenges)));
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
|
@ -19,6 +19,7 @@ import java.util.stream.Collectors;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import org.eclipse.jdt.annotation.NonNull;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
|
||||
import world.bentobox.bentobox.api.logs.LogEntry;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
@ -43,7 +44,7 @@ import world.bentobox.challenges.utils.Utils;
|
||||
|
||||
|
||||
/**
|
||||
* This class manges challenges. It allows access to all data that is stored to database.
|
||||
* This class manages challenges. It allows access to all data that is stored to database.
|
||||
* It also provides information about challenge level status for each user.
|
||||
*/
|
||||
public class ChallengesManager
|
||||
@ -107,6 +108,11 @@ public class ChallengesManager
|
||||
* String for free Challenge Level.
|
||||
*/
|
||||
public static final String FREE = "";
|
||||
public static final String VALUE = "[value]";
|
||||
public static final String USER_ID = "user-id";
|
||||
public static final String CHALLENGE_ID = "challenge-id";
|
||||
public static final String ADMIN_ID = "admin-id";
|
||||
public static final String RESET = "RESET";
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
@ -189,6 +195,7 @@ public class ChallengesManager
|
||||
*/
|
||||
public void load()
|
||||
{
|
||||
this.addon.log("Loading challenges...");
|
||||
this.challengeCacheData.clear();
|
||||
this.levelCacheData.clear();
|
||||
|
||||
@ -199,17 +206,15 @@ public class ChallengesManager
|
||||
}
|
||||
|
||||
this.playerCacheData.clear();
|
||||
loadAndValidate();
|
||||
}
|
||||
|
||||
this.addon.getLogger().info("Loading challenges...");
|
||||
|
||||
private void loadAndValidate() {
|
||||
this.challengeDatabase.loadObjects().forEach(this::loadChallenge);
|
||||
this.levelDatabase.loadObjects().forEach(this::loadLevel);
|
||||
|
||||
// this validate challenge levels
|
||||
this.validateChallenges();
|
||||
|
||||
// It is not necessary to load all players in memory.
|
||||
// this.playersDatabase.loadObjects().forEach(this::loadPlayerData);
|
||||
}
|
||||
|
||||
|
||||
@ -218,24 +223,17 @@ public class ChallengesManager
|
||||
*/
|
||||
public void reload()
|
||||
{
|
||||
this.addon.log("Reloading challenges...");
|
||||
if (!this.playerCacheData.isEmpty())
|
||||
{
|
||||
// store player data before cleaning.
|
||||
this.savePlayersData();
|
||||
}
|
||||
//this.challengeDatabase = new Database<>(addon, Challenge.class);
|
||||
//this.levelDatabase = new Database<>(addon, ChallengeLevel.class);
|
||||
//this.playersDatabase = new Database<>(addon, ChallengesPlayerData.class);
|
||||
|
||||
this.addon.getLogger().info("Reloading challenges...");
|
||||
|
||||
this.challengeDatabase = new Database<>(addon, Challenge.class);
|
||||
this.levelDatabase = new Database<>(addon, ChallengeLevel.class);
|
||||
this.playersDatabase = new Database<>(addon, ChallengesPlayerData.class);
|
||||
|
||||
this.challengeDatabase.loadObjects().forEach(this::loadChallenge);
|
||||
this.levelDatabase.loadObjects().forEach(this::loadLevel);
|
||||
|
||||
this.validateChallenges();
|
||||
// It is not necessary to load all players in memory.
|
||||
// this.playersDatabase.loadObjects().forEach(this::loadPlayerData);
|
||||
loadAndValidate();
|
||||
}
|
||||
|
||||
|
||||
@ -265,13 +263,6 @@ public class ChallengesManager
|
||||
User user,
|
||||
boolean silent)
|
||||
{
|
||||
if (challenge == null)
|
||||
{
|
||||
this.addon.logError(
|
||||
"Tried to load NULL element from Database. One challenge is broken and will not work.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.challengeCacheData.containsKey(challenge.getUniqueId()))
|
||||
{
|
||||
if (!overwrite)
|
||||
@ -279,7 +270,7 @@ public class ChallengesManager
|
||||
if (!silent)
|
||||
{
|
||||
user.sendMessage("challenges.messages.load-skipping",
|
||||
"[value]", challenge.getFriendlyName());
|
||||
VALUE, challenge.getFriendlyName());
|
||||
}
|
||||
|
||||
return false;
|
||||
@ -289,7 +280,7 @@ public class ChallengesManager
|
||||
if (!silent)
|
||||
{
|
||||
user.sendMessage("challenges.messages.load-overwriting",
|
||||
"[value]", challenge.getFriendlyName());
|
||||
VALUE, challenge.getFriendlyName());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -298,7 +289,7 @@ public class ChallengesManager
|
||||
if (!silent)
|
||||
{
|
||||
user.sendMessage("challenges.messages.load-add",
|
||||
"[value]", challenge.getFriendlyName());
|
||||
VALUE, challenge.getFriendlyName());
|
||||
}
|
||||
}
|
||||
|
||||
@ -333,19 +324,12 @@ public class ChallengesManager
|
||||
User user,
|
||||
boolean silent)
|
||||
{
|
||||
if (level == null)
|
||||
{
|
||||
this.addon.logError(
|
||||
"Tried to load NULL element from Database. One level is broken and will not work.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!this.isValidLevel(level))
|
||||
{
|
||||
if (user != null)
|
||||
{
|
||||
user.sendMessage("challenges.errors.load-error",
|
||||
"[value]", level.getFriendlyName());
|
||||
VALUE, level.getFriendlyName());
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -363,7 +347,7 @@ public class ChallengesManager
|
||||
if (!silent)
|
||||
{
|
||||
user.sendMessage("challenges.messages.load-skipping",
|
||||
"[value]", level.getFriendlyName());
|
||||
VALUE, level.getFriendlyName());
|
||||
}
|
||||
|
||||
return false;
|
||||
@ -373,7 +357,7 @@ public class ChallengesManager
|
||||
if (!silent)
|
||||
{
|
||||
user.sendMessage("challenges.messages.load-overwriting",
|
||||
"[value]", level.getFriendlyName());
|
||||
VALUE, level.getFriendlyName());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -382,7 +366,7 @@ public class ChallengesManager
|
||||
if (!silent)
|
||||
{
|
||||
user.sendMessage("challenges.messages.load-add",
|
||||
"[value]", level.getFriendlyName());
|
||||
VALUE, level.getFriendlyName());
|
||||
}
|
||||
}
|
||||
|
||||
@ -416,15 +400,12 @@ public class ChallengesManager
|
||||
*/
|
||||
public void removeFromCache(UUID playerID)
|
||||
{
|
||||
if (!this.settings.isStoreAsIslandData())
|
||||
{
|
||||
if (this.playerCacheData.containsKey(playerID.toString()))
|
||||
if (!this.settings.isStoreAsIslandData() && this.playerCacheData.containsKey(playerID.toString()))
|
||||
{
|
||||
// save before remove
|
||||
this.savePlayerData(playerID.toString());
|
||||
this.playerCacheData.remove(playerID.toString());
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: It would be necessary to remove also data, if they stores islands.
|
||||
// Unfortunately, I do not know all worlds. Checking everything would be bad. Probably, I could
|
||||
@ -1082,6 +1063,7 @@ public class ChallengesManager
|
||||
* @param gameMode - World Name where levels should be searched.
|
||||
* @return Level status - how many challenges still to do on which level
|
||||
*/
|
||||
@NonNull
|
||||
private List<LevelStatus> getAllChallengeLevelStatus(String storageDataID, String gameMode)
|
||||
{
|
||||
this.addPlayerData(storageDataID);
|
||||
@ -1117,7 +1099,6 @@ public class ChallengesManager
|
||||
|
||||
previousLevel = level;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -1129,6 +1110,7 @@ public class ChallengesManager
|
||||
* @param level Level which status must be calculated.
|
||||
* @return LevelStatus of given level.
|
||||
*/
|
||||
@Nullable
|
||||
private LevelStatus getChallengeLevelStatus(@NonNull String storageDataID, World world, @NonNull ChallengeLevel level)
|
||||
{
|
||||
this.addPlayerData(storageDataID);
|
||||
@ -1162,29 +1144,6 @@ public class ChallengesManager
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check is playerData can see given level.
|
||||
* TODO: not an optimal way. Faster would be to check previous level challenges.
|
||||
* @param storageDataID - playerData ID
|
||||
* @param level - level
|
||||
* @return true if level is unlocked
|
||||
*/
|
||||
private boolean isLevelUnlocked(@NonNull String storageDataID,
|
||||
World world,
|
||||
ChallengeLevel level)
|
||||
{
|
||||
this.addPlayerData(storageDataID);
|
||||
|
||||
return this.islandWorldManager.getAddon(world).filter(gameMode ->
|
||||
this.getAllChallengeLevelStatus(storageDataID, gameMode.getDescription().getName()).
|
||||
stream().
|
||||
filter(LevelStatus::isUnlocked).
|
||||
anyMatch(lv -> lv.getLevel().equals(level))).
|
||||
isPresent();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This method returns if given user has been already completed given level.
|
||||
* @param levelID Level that must be checked.
|
||||
@ -1316,13 +1275,13 @@ public class ChallengesManager
|
||||
String storageID = this.getDataUniqueID(userID, Util.getWorld(world));
|
||||
this.setChallengeComplete(storageID, challenge.getUniqueId(), completionCount);
|
||||
this.addLogEntry(storageID, new LogEntry.Builder("COMPLETE").
|
||||
data("user-id", userID.toString()).
|
||||
data("challenge-id", challenge.getUniqueId()).
|
||||
data(USER_ID, userID.toString()).
|
||||
data(CHALLENGE_ID, challenge.getUniqueId()).
|
||||
data("completion-count", Integer.toString(completionCount)).
|
||||
build());
|
||||
|
||||
// Fire event that user completes challenge
|
||||
Bukkit.getServer().getPluginManager().callEvent(
|
||||
Bukkit.getPluginManager().callEvent(
|
||||
new ChallengeCompletedEvent(challenge.getUniqueId(),
|
||||
userID,
|
||||
false,
|
||||
@ -1343,13 +1302,13 @@ public class ChallengesManager
|
||||
|
||||
this.setChallengeComplete(storageID, challenge.getUniqueId());
|
||||
this.addLogEntry(storageID, new LogEntry.Builder("COMPLETE").
|
||||
data("user-id", userID.toString()).
|
||||
data("challenge-id", challenge.getUniqueId()).
|
||||
data("admin-id", adminID == null ? "OP" : adminID.toString()).
|
||||
data(USER_ID, userID.toString()).
|
||||
data(CHALLENGE_ID, challenge.getUniqueId()).
|
||||
data(ADMIN_ID, adminID == null ? "OP" : adminID.toString()).
|
||||
build());
|
||||
|
||||
// Fire event that admin completes user challenge
|
||||
Bukkit.getServer().getPluginManager().callEvent(
|
||||
Bukkit.getPluginManager().callEvent(
|
||||
new ChallengeCompletedEvent(challenge.getUniqueId(),
|
||||
userID,
|
||||
true,
|
||||
@ -1368,18 +1327,18 @@ public class ChallengesManager
|
||||
String storageID = this.getDataUniqueID(userID, Util.getWorld(world));
|
||||
|
||||
this.resetChallenge(storageID, challenge.getUniqueId());
|
||||
this.addLogEntry(storageID, new LogEntry.Builder("RESET").
|
||||
data("user-id", userID.toString()).
|
||||
data("challenge-id", challenge.getUniqueId()).
|
||||
data("admin-id", adminID == null ? "RESET" : adminID.toString()).
|
||||
this.addLogEntry(storageID, new LogEntry.Builder(RESET).
|
||||
data(USER_ID, userID.toString()).
|
||||
data(CHALLENGE_ID, challenge.getUniqueId()).
|
||||
data(ADMIN_ID, adminID == null ? RESET : adminID.toString()).
|
||||
build());
|
||||
|
||||
// Fire event that admin resets user challenge
|
||||
Bukkit.getServer().getPluginManager().callEvent(
|
||||
Bukkit.getPluginManager().callEvent(
|
||||
new ChallengeResetEvent(challenge.getUniqueId(),
|
||||
userID,
|
||||
true,
|
||||
"RESET"));
|
||||
RESET));
|
||||
}
|
||||
|
||||
|
||||
@ -1407,12 +1366,12 @@ public class ChallengesManager
|
||||
this.islandWorldManager.getAddon(world).ifPresent(gameMode -> {
|
||||
this.resetAllChallenges(storageID, gameMode.getDescription().getName());
|
||||
this.addLogEntry(storageID, new LogEntry.Builder("RESET_ALL").
|
||||
data("user-id", userID.toString()).
|
||||
data("admin-id", adminID == null ? "ISLAND_RESET" : adminID.toString()).
|
||||
data(USER_ID, userID.toString()).
|
||||
data(ADMIN_ID, adminID == null ? "ISLAND_RESET" : adminID.toString()).
|
||||
build());
|
||||
|
||||
// Fire event that admin resets user challenge
|
||||
Bukkit.getServer().getPluginManager().callEvent(
|
||||
Bukkit.getPluginManager().callEvent(
|
||||
new ChallengeResetAllEvent(gameMode.getDescription().getName(),
|
||||
userID,
|
||||
adminID != null,
|
||||
@ -1468,11 +1427,19 @@ public class ChallengesManager
|
||||
* @param world World where level must be checked.
|
||||
* @param level Level that must be checked.
|
||||
* @param user User who need to be checked.
|
||||
* @return true, if level is already completed.
|
||||
* @return true, if level is unlocked.
|
||||
*/
|
||||
public boolean isLevelUnlocked(User user, World world, ChallengeLevel level)
|
||||
{
|
||||
return this.isLevelUnlocked(this.getDataUniqueID(user, Util.getWorld(world)), world, level);
|
||||
String storageDataID = this.getDataUniqueID(user, Util.getWorld(world));
|
||||
this.addPlayerData(storageDataID);
|
||||
|
||||
return this.islandWorldManager.getAddon(world).filter(gameMode -> this.getAllChallengeLevelStatus(storageDataID, gameMode.getDescription().getName()).
|
||||
stream().
|
||||
filter(LevelStatus::isUnlocked).
|
||||
anyMatch(lv -> lv.getLevel().equals(level))).
|
||||
isPresent();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -1488,11 +1455,11 @@ public class ChallengesManager
|
||||
|
||||
this.setLevelComplete(storageID, level.getUniqueId());
|
||||
this.addLogEntry(storageID, new LogEntry.Builder("COMPLETE_LEVEL").
|
||||
data("user-id", user.getUniqueId().toString()).
|
||||
data(USER_ID, user.getUniqueId().toString()).
|
||||
data("level", level.getUniqueId()).build());
|
||||
|
||||
// Fire event that user completes level
|
||||
Bukkit.getServer().getPluginManager().callEvent(
|
||||
Bukkit.getPluginManager().callEvent(
|
||||
new LevelCompletedEvent(level.getUniqueId(),
|
||||
user.getUniqueId(),
|
||||
false));
|
||||
@ -1514,14 +1481,15 @@ public class ChallengesManager
|
||||
|
||||
/**
|
||||
* This method returns LevelStatus object for given challenge level.
|
||||
* @param uniqueId UUID of user who need to be validated.
|
||||
* @param world World where level must be validated.
|
||||
* @param level Level that must be validated.
|
||||
* @param user User who need to be validated.
|
||||
* @return LevelStatus of given level.
|
||||
*/
|
||||
public LevelStatus getChallengeLevelStatus(UUID user, World world, ChallengeLevel level)
|
||||
@Nullable
|
||||
public LevelStatus getChallengeLevelStatus(UUID uniqueId, World world, ChallengeLevel level)
|
||||
{
|
||||
return this.getChallengeLevelStatus(this.getDataUniqueID(user, Util.getWorld(world)), world, level);
|
||||
return this.getChallengeLevelStatus(this.getDataUniqueID(uniqueId, Util.getWorld(world)), world, level);
|
||||
}
|
||||
|
||||
|
||||
@ -1532,6 +1500,7 @@ public class ChallengesManager
|
||||
* @param world - World where levels should be searched.
|
||||
* @return Level status - how many challenges still to do on which level
|
||||
*/
|
||||
@NonNull
|
||||
public List<LevelStatus> getAllChallengeLevelStatus(User user, World world)
|
||||
{
|
||||
return this.islandWorldManager.getAddon(world).map(gameMode ->
|
||||
@ -1621,6 +1590,7 @@ public class ChallengesManager
|
||||
* @param name - unique name of challenge
|
||||
* @return - challenge or null if it does not exist
|
||||
*/
|
||||
@Nullable
|
||||
public Challenge getChallenge(String name)
|
||||
{
|
||||
if (this.challengeCacheData.containsKey(name))
|
||||
@ -1658,30 +1628,7 @@ public class ChallengesManager
|
||||
*/
|
||||
public boolean containsChallenge(String name)
|
||||
{
|
||||
if (this.challengeCacheData.containsKey(name))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// check database.
|
||||
if (this.challengeDatabase.objectExists(name))
|
||||
{
|
||||
Challenge challenge = this.challengeDatabase.loadObject(name);
|
||||
|
||||
if (challenge != null)
|
||||
{
|
||||
this.challengeCacheData.put(name, challenge);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.addon.logError("Tried to load NULL challenge object!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return getChallenge(name) != null;
|
||||
}
|
||||
|
||||
|
||||
@ -1691,6 +1638,7 @@ public class ChallengesManager
|
||||
* @param requirements - requirements object, as it is not changeable anymore.
|
||||
* @return Challenge that is currently created.
|
||||
*/
|
||||
@Nullable
|
||||
public Challenge createChallenge(String uniqueID, Challenge.ChallengeType type, Requirements requirements)
|
||||
{
|
||||
if (!this.containsChallenge(uniqueID))
|
||||
@ -1769,6 +1717,7 @@ public class ChallengesManager
|
||||
* @param challenge - challenge which level must be returned.
|
||||
* @return - challenge level or null if it does not exist
|
||||
*/
|
||||
@Nullable
|
||||
public ChallengeLevel getLevel(Challenge challenge)
|
||||
{
|
||||
if (!challenge.getLevel().equals(FREE))
|
||||
@ -1786,6 +1735,7 @@ public class ChallengesManager
|
||||
* @param name - unique name of challenge level
|
||||
* @return - challenge level or null if it does not exist
|
||||
*/
|
||||
@Nullable
|
||||
public ChallengeLevel getLevel(String name)
|
||||
{
|
||||
if (this.levelCacheData.containsKey(name))
|
||||
@ -1853,7 +1803,7 @@ public class ChallengesManager
|
||||
/**
|
||||
* This method adds given challenge to given challenge level.
|
||||
* @param newChallenge Challenge who must change owner.
|
||||
* @param newLevel Level who must add new challenge
|
||||
* @param newLevel Level to add to - must exist already
|
||||
*/
|
||||
public void addChallengeToLevel(Challenge newChallenge, ChallengeLevel newLevel)
|
||||
{
|
||||
@ -1869,7 +1819,7 @@ public class ChallengesManager
|
||||
{
|
||||
ChallengeLevel oldLevel = this.getLevel(newChallenge.getLevel());
|
||||
|
||||
if (!oldLevel.equals(newLevel))
|
||||
if (oldLevel == null || !oldLevel.equals(newLevel))
|
||||
{
|
||||
this.removeChallengeFromLevel(newChallenge, newLevel);
|
||||
newLevel.getChallenges().add(newChallenge.getUniqueId());
|
||||
@ -1904,6 +1854,7 @@ public class ChallengesManager
|
||||
* @param uniqueID - new ID for challenge level.
|
||||
* @return ChallengeLevel that is currently created.
|
||||
*/
|
||||
@Nullable
|
||||
public ChallengeLevel createLevel(String uniqueID, World world)
|
||||
{
|
||||
if (!this.containsLevel(uniqueID))
|
||||
|
@ -1,7 +1,6 @@
|
||||
package world.bentobox.challenges.commands;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
@ -27,10 +26,7 @@ public class ChallengesCommand extends CompositeCommand
|
||||
@Override
|
||||
public boolean canExecute(User user, String label, List<String> args)
|
||||
{
|
||||
Optional<GameModeAddon> optionalAddon = this.getAddon().getPlugin().getIWM().getAddon(this.getWorld());
|
||||
|
||||
if (!optionalAddon.isPresent())
|
||||
{
|
||||
if (!getIWM().inWorld(getWorld())) {
|
||||
// Not a GameMode world.
|
||||
user.sendMessage("general.errors.wrong-world");
|
||||
return false;
|
||||
@ -39,13 +35,14 @@ public class ChallengesCommand extends CompositeCommand
|
||||
if (!((ChallengesAddon) this.getAddon()).getChallengesManager().hasAnyChallengeData(this.getWorld()))
|
||||
{
|
||||
// Do not open gui if there is no challenges.
|
||||
|
||||
this.getAddon().getLogger().severe("There are no challenges set up in " + this.getWorld() + "!");
|
||||
this.getAddon().logError("There are no challenges set up in " + this.getWorld() + "!");
|
||||
|
||||
// Show admin better explanation.
|
||||
if (user.isOp() || user.hasPermission(this.getPermissionPrefix() + "admin.challenges"))
|
||||
{
|
||||
String topLabel = optionalAddon.get().getAdminCommand().orElseGet(this::getParent).getTopLabel();
|
||||
String topLabel = getIWM().getAddon(this.getWorld())
|
||||
.map(GameModeAddon::getAdminCommand)
|
||||
.map(optionalAdminCommand -> optionalAdminCommand.map(ac -> ac.getTopLabel()).orElse(this.getTopLabel())).orElse(this.getTopLabel());
|
||||
user.sendMessage("challenges.errors.no-challenges-admin", "[command]", topLabel + " challenges");
|
||||
}
|
||||
else
|
||||
@ -56,7 +53,7 @@ public class ChallengesCommand extends CompositeCommand
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.getPlugin().getIslands().getIsland(this.getWorld(), user.getUniqueId()) == null)
|
||||
if (this.getIslands().getIsland(this.getWorld(), user) == null)
|
||||
{
|
||||
// Do not open gui if there is no island for this player.
|
||||
user.sendMessage("general.errors.no-island");
|
||||
|
@ -58,7 +58,7 @@ public class CompleteChallengeCommand extends CompositeCommand
|
||||
this.showHelp(this, user);
|
||||
return false;
|
||||
}
|
||||
else if (!args.get(0).isEmpty())
|
||||
else
|
||||
{
|
||||
// Add world name back at the start
|
||||
String challengeName = Utils.getGameMode(this.getWorld()) + "_" + args.get(0);
|
||||
@ -92,9 +92,6 @@ public class CompleteChallengeCommand extends CompositeCommand
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
this.showHelp(this, user);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@ -104,6 +101,8 @@ public class CompleteChallengeCommand extends CompositeCommand
|
||||
@Override
|
||||
public Optional<List<String>> tabComplete(User user, String alias, List<String> args)
|
||||
{
|
||||
if (args.isEmpty()) return Optional.empty();
|
||||
|
||||
String lastString = args.get(args.size() - 1);
|
||||
|
||||
final List<String> returnList = new ArrayList<>();
|
||||
@ -112,11 +111,12 @@ public class CompleteChallengeCommand extends CompositeCommand
|
||||
switch (size)
|
||||
{
|
||||
case 3:
|
||||
|
||||
// Create suggestions with all challenges that is available for users.
|
||||
returnList.addAll(this.addon.getChallengesManager().getAllChallengesNames(this.getWorld()).stream().
|
||||
filter(challenge -> challenge.startsWith(Utils.getGameMode(this.getWorld()) + "_")).
|
||||
map(challenge -> challenge.substring(Utils.getGameMode(this.getWorld()).length() + 1)).
|
||||
collect(Collectors.toList()));
|
||||
|
||||
break;
|
||||
case 4:
|
||||
// Suggest a number of completions.
|
||||
|
@ -13,6 +13,7 @@ import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.eclipse.jdt.annotation.NonNull;
|
||||
|
||||
import com.google.gson.annotations.Expose;
|
||||
import com.google.gson.annotations.JsonAdapter;
|
||||
@ -717,7 +718,7 @@ public class Challenge implements DataObject
|
||||
* This method sets the level value.
|
||||
* @param level the level new value.
|
||||
*/
|
||||
public void setLevel(String level)
|
||||
public void setLevel(@NonNull String level)
|
||||
{
|
||||
this.level = level;
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import org.bukkit.Material;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.inventory.InventoryCloseEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
|
||||
import world.bentobox.bentobox.api.panels.PanelItem;
|
||||
import world.bentobox.bentobox.api.panels.PanelListener;
|
||||
@ -242,6 +243,7 @@ public class EditLoreGUI extends CommonGUI
|
||||
* @param loreElement String that represents current lore element.
|
||||
* @return PanelItem.
|
||||
*/
|
||||
@Nullable
|
||||
private PanelItem getLoreButton(String loreElement)
|
||||
{
|
||||
switch (this.lore)
|
||||
@ -460,7 +462,7 @@ public class EditLoreGUI extends CommonGUI
|
||||
for (String value : values)
|
||||
{
|
||||
PanelItem item = EditLoreGUI.this.getLoreButton(value);
|
||||
|
||||
if (item != null) {
|
||||
item.setClickHandler((panel, user1, clickType, slot) -> {
|
||||
selectedElement.accept(value);
|
||||
return true;
|
||||
@ -468,6 +470,7 @@ public class EditLoreGUI extends CommonGUI
|
||||
|
||||
panelBuilder.item(currentIndex++, item);
|
||||
|
||||
|
||||
// Border element
|
||||
if (currentIndex % 9 == 8)
|
||||
{
|
||||
@ -486,6 +489,7 @@ public class EditLoreGUI extends CommonGUI
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
panelBuilder.build();
|
||||
}
|
||||
@ -515,7 +519,7 @@ public class EditLoreGUI extends CommonGUI
|
||||
|
||||
String value = values.get(counter);
|
||||
PanelItem item = EditLoreGUI.this.getLoreButton(value);
|
||||
|
||||
if (item != null) {
|
||||
item.setClickHandler((panel, user1, clickType, slot) -> {
|
||||
selectedElement.accept(value, counter);
|
||||
return true;
|
||||
@ -541,6 +545,7 @@ public class EditLoreGUI extends CommonGUI
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
panelBuilder.build();
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ public class ChallengesGUI extends CommonGUI
|
||||
// Do not open gui if there is no challenges.
|
||||
if (!this.challengesManager.hasAnyChallengeData(this.world))
|
||||
{
|
||||
this.addon.getLogger().severe("There are no challenges set up!");
|
||||
this.addon.logError("There are no challenges set up!");
|
||||
this.user.sendMessage("challenges.errors.no-challenges");
|
||||
return;
|
||||
}
|
||||
|
@ -1,6 +1,3 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package world.bentobox.challenges.tasks;
|
||||
|
||||
|
||||
@ -16,6 +13,7 @@ import java.util.PriorityQueue;
|
||||
import java.util.Queue;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Material;
|
||||
@ -178,7 +176,7 @@ public class TryToComplete
|
||||
this.permissionPrefix = permissionPrefix;
|
||||
this.user = user;
|
||||
this.manager = addon.getChallengesManager();
|
||||
// To avoid any modifications that may accure to challenges in current completion
|
||||
// To avoid any modifications that may occur to challenges in current completion
|
||||
// just clone it.
|
||||
this.challenge = challenge.clone();
|
||||
this.topLabel = topLabel;
|
||||
@ -239,7 +237,7 @@ public class TryToComplete
|
||||
* This method checks if challenge can be done, and complete it, if it is possible.
|
||||
* @return ChallengeResult object, that contains completion status.
|
||||
*/
|
||||
public ChallengeResult build(int maxTimes)
|
||||
ChallengeResult build(int maxTimes)
|
||||
{
|
||||
// Check if can complete challenge
|
||||
ChallengeResult result = this.checkIfCanCompleteChallenge(maxTimes);
|
||||
@ -304,7 +302,7 @@ public class TryToComplete
|
||||
|
||||
if (this.addon.getChallengesSettings().isBroadcastMessages())
|
||||
{
|
||||
for (Player player : this.addon.getServer().getOnlinePlayers())
|
||||
for (Player player : Bukkit.getOnlinePlayers())
|
||||
{
|
||||
// Only other players should see message.
|
||||
if (!player.getUniqueId().equals(this.user.getUniqueId()))
|
||||
@ -349,7 +347,7 @@ public class TryToComplete
|
||||
if (this.addon.isEconomyProvided())
|
||||
{
|
||||
this.addon.getEconomyProvider().deposit(this.user,
|
||||
this.challenge.getRepeatMoneyReward() * rewardFactor);
|
||||
(double)this.challenge.getRepeatMoneyReward() * rewardFactor);
|
||||
}
|
||||
|
||||
// Experience Repeat Reward
|
||||
@ -521,7 +519,6 @@ public class TryToComplete
|
||||
ChallengeResult result;
|
||||
|
||||
ChallengeType type = this.challenge.getChallengeType();
|
||||
|
||||
// Check the world
|
||||
if (!this.challenge.isDeployed())
|
||||
{
|
||||
@ -609,7 +606,6 @@ public class TryToComplete
|
||||
{
|
||||
result.setCompleted(this.manager.isChallengeComplete(this.user, this.world, this.challenge));
|
||||
}
|
||||
|
||||
// Everything fails till this point.
|
||||
return result;
|
||||
}
|
||||
@ -882,6 +878,11 @@ public class TryToComplete
|
||||
|
||||
Island island = this.addon.getIslands().getIsland(this.world, this.user);
|
||||
|
||||
if (island == null) {
|
||||
// Just in case. Should never hit because there is a check if the player is on this island further up
|
||||
return EMPTY_RESULT;
|
||||
}
|
||||
|
||||
if (boundingBox.getMinX() < island.getMinX())
|
||||
{
|
||||
boundingBox.expand(BlockFace.EAST, Math.abs(island.getMinX() - boundingBox.getMinX()));
|
||||
@ -1316,7 +1317,7 @@ public class TryToComplete
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Section: Private classes
|
||||
// Section: Result classes
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
|
||||
@ -1325,7 +1326,7 @@ public class TryToComplete
|
||||
*
|
||||
* @author tastybento
|
||||
*/
|
||||
private class ChallengeResult
|
||||
class ChallengeResult
|
||||
{
|
||||
/**
|
||||
* This method sets that challenge meets all requirements at least once.
|
||||
|
@ -26,7 +26,7 @@ public class Utils
|
||||
{
|
||||
List<ItemStack> returnItems = new ArrayList<>(requiredItems.size());
|
||||
|
||||
// Group all equal items in singe stack, as otherwise it will be too complicated to check if all
|
||||
// Group all equal items in single stack, as otherwise it will be too complicated to check if all
|
||||
// items are in players inventory.
|
||||
for (ItemStack item : requiredItems)
|
||||
{
|
||||
|
@ -126,7 +126,6 @@ public class WebManager
|
||||
* @param user User who inits request.
|
||||
* @param world Target world where challenges should be loaded.
|
||||
* @param entry Entry that contains information about requested object.
|
||||
* @return {@code true} if request was successful, {@code false} otherwise.
|
||||
*/
|
||||
public void requestEntryGitHubData(User user, World world, LibraryEntry entry)
|
||||
{
|
||||
|
@ -1,117 +1,414 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package world.bentobox.challenges;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarOutputStream;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.UnsafeValues;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemFactory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.PotionMeta;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.potion.PotionData;
|
||||
import org.bukkit.potion.PotionType;
|
||||
import org.junit.BeforeClass;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
import org.eclipse.jdt.annotation.NonNull;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.reflect.Whitebox;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
import world.bentobox.challenges.database.object.Challenge;
|
||||
import world.bentobox.challenges.database.object.Challenge.ChallengeType;
|
||||
import world.bentobox.bentobox.BentoBox;
|
||||
import world.bentobox.bentobox.Settings;
|
||||
import world.bentobox.bentobox.api.addons.Addon;
|
||||
import world.bentobox.bentobox.api.addons.Addon.State;
|
||||
import world.bentobox.bentobox.api.addons.AddonDescription;
|
||||
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.configuration.Config;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
import world.bentobox.bentobox.database.DatabaseSetup.DatabaseType;
|
||||
import world.bentobox.bentobox.database.objects.Island;
|
||||
import world.bentobox.bentobox.managers.AddonsManager;
|
||||
import world.bentobox.bentobox.managers.CommandsManager;
|
||||
import world.bentobox.bentobox.managers.FlagsManager;
|
||||
import world.bentobox.bentobox.managers.IslandWorldManager;
|
||||
import world.bentobox.bentobox.managers.IslandsManager;
|
||||
|
||||
/**
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({Bukkit.class, BentoBox.class, User.class, Config.class })
|
||||
public class ChallengesAddonTest {
|
||||
|
||||
@Mock
|
||||
private User user;
|
||||
@Mock
|
||||
private IslandsManager im;
|
||||
@Mock
|
||||
private Island island;
|
||||
|
||||
private ChallengesAddon addon;
|
||||
@Mock
|
||||
private BentoBox plugin;
|
||||
@Mock
|
||||
private FlagsManager fm;
|
||||
@Mock
|
||||
private Settings settings;
|
||||
@Mock
|
||||
private GameModeAddon gameMode;
|
||||
@Mock
|
||||
private AddonsManager am;
|
||||
@Mock
|
||||
private BukkitScheduler scheduler;
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@BeforeClass
|
||||
public static void setUpBeforeClass() throws Exception {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
// Set up plugin
|
||||
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
|
||||
when(plugin.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
||||
//when(plugin.isEnabled()).thenReturn(true);
|
||||
// Command manager
|
||||
CommandsManager cm = mock(CommandsManager.class);
|
||||
when(plugin.getCommandsManager()).thenReturn(cm);
|
||||
|
||||
// Player
|
||||
Player p = mock(Player.class);
|
||||
// Sometimes use Mockito.withSettings().verboseLogging()
|
||||
when(user.isOp()).thenReturn(false);
|
||||
UUID uuid = UUID.randomUUID();
|
||||
when(user.getUniqueId()).thenReturn(uuid);
|
||||
when(user.getPlayer()).thenReturn(p);
|
||||
when(user.getName()).thenReturn("tastybento");
|
||||
User.setPlugin(plugin);
|
||||
|
||||
// Island World Manager
|
||||
IslandWorldManager iwm = mock(IslandWorldManager.class);
|
||||
when(plugin.getIWM()).thenReturn(iwm);
|
||||
|
||||
|
||||
// Player has island to begin with
|
||||
island = mock(Island.class);
|
||||
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island);
|
||||
when(plugin.getIslands()).thenReturn(im);
|
||||
|
||||
// Locales
|
||||
// Return the reference (USE THIS IN THE FUTURE)
|
||||
when(user.getTranslation(Mockito.anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
|
||||
|
||||
// Server
|
||||
PowerMockito.mockStatic(Bukkit.class);
|
||||
Server server = mock(Server.class);
|
||||
World world = mock(World.class);
|
||||
world = mock(World.class);
|
||||
Mockito.when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
||||
Mockito.when(server.getWorld("world")).thenReturn(world);
|
||||
Mockito.when(server.getVersion()).thenReturn("BSB_Mocking");
|
||||
|
||||
PluginManager pluginManager = mock(PluginManager.class);
|
||||
when(server.getPluginManager()).thenReturn(pluginManager);
|
||||
|
||||
ItemFactory itemFactory = mock(ItemFactory.class);
|
||||
when(server.getItemFactory()).thenReturn(itemFactory);
|
||||
|
||||
Bukkit.setServer(server);
|
||||
|
||||
PotionMeta potionMeta = mock(PotionMeta.class);
|
||||
when(itemFactory.getItemMeta(any())).thenReturn(potionMeta);
|
||||
|
||||
OfflinePlayer offlinePlayer = mock(OfflinePlayer.class);
|
||||
when(Bukkit.getOfflinePlayer(any(UUID.class))).thenReturn(offlinePlayer);
|
||||
when(offlinePlayer.getName()).thenReturn("tastybento");
|
||||
|
||||
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
|
||||
when(Bukkit.getServer()).thenReturn(server);
|
||||
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
||||
when(Bukkit.getPluginManager()).thenReturn(mock(PluginManager.class));
|
||||
|
||||
// Addon
|
||||
addon = new ChallengesAddon();
|
||||
File jFile = new File("addon.jar");
|
||||
List<String> lines = Arrays.asList("# ChallengesAddon Configuration", "uniqueId: config");
|
||||
Path path = Paths.get("config.yml");
|
||||
Files.write(path, lines, Charset.forName("UTF-8"));
|
||||
try (JarOutputStream tempJarOutputStream = new JarOutputStream(new FileOutputStream(jFile))) {
|
||||
//Added the new files to the jar.
|
||||
try (FileInputStream fis = new FileInputStream(path.toFile())) {
|
||||
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesRead = 0;
|
||||
JarEntry entry = new JarEntry(path.toString());
|
||||
tempJarOutputStream.putNextEntry(entry);
|
||||
while((bytesRead = fis.read(buffer)) != -1) {
|
||||
tempJarOutputStream.write(buffer, 0, bytesRead);
|
||||
}
|
||||
}
|
||||
}
|
||||
File dataFolder = new File("addons/Challenges");
|
||||
addon.setDataFolder(dataFolder);
|
||||
addon.setFile(jFile);
|
||||
AddonDescription desc = new AddonDescription.Builder("bentobox", "challenges", "1.3").description("test").authors("BONNe").build();
|
||||
addon.setDescription(desc);
|
||||
// Addons manager
|
||||
when(plugin.getAddonsManager()).thenReturn(am);
|
||||
// One game mode
|
||||
when(am.getGameModeAddons()).thenReturn(Collections.singletonList(gameMode));
|
||||
AddonDescription desc2 = new AddonDescription.Builder("bentobox", "BSkyBlock", "1.3").description("test").authors("tasty").build();
|
||||
when(gameMode.getDescription()).thenReturn(desc2);
|
||||
|
||||
// Player command
|
||||
CompositeCommand cmd = mock(CompositeCommand.class);
|
||||
@NonNull
|
||||
Optional<CompositeCommand> opCmd = Optional.of(cmd);
|
||||
when(gameMode.getPlayerCommand()).thenReturn(opCmd);
|
||||
// Admin command
|
||||
when(gameMode.getAdminCommand()).thenReturn(opCmd);
|
||||
|
||||
// Flags manager
|
||||
when(plugin.getFlagsManager()).thenReturn(fm);
|
||||
when(fm.getFlags()).thenReturn(Collections.emptyList());
|
||||
|
||||
// The database type has to be created one line before the thenReturn() to work!
|
||||
when(plugin.getSettings()).thenReturn(settings);
|
||||
DatabaseType value = DatabaseType.JSON;
|
||||
when(settings.getDatabaseType()).thenReturn(value);
|
||||
|
||||
// Bukkit
|
||||
PowerMockito.mockStatic(Bukkit.class);
|
||||
when(Bukkit.getScheduler()).thenReturn(scheduler);
|
||||
ItemMeta meta = mock(ItemMeta.class);
|
||||
ItemFactory itemFactory = mock(ItemFactory.class);
|
||||
when(itemFactory.getItemMeta(any())).thenReturn(meta);
|
||||
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
|
||||
UnsafeValues unsafe = mock(UnsafeValues.class);
|
||||
when(unsafe.getDataVersion()).thenReturn(777);
|
||||
when(Bukkit.getUnsafe()).thenReturn(unsafe);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
new File("addon.jar").delete();
|
||||
new File("config.yml").delete();
|
||||
deleteAll(new File("addons"));
|
||||
deleteAll(new File("database"));
|
||||
|
||||
}
|
||||
|
||||
private void deleteAll(File file) throws IOException {
|
||||
if (file.exists()) {
|
||||
Files.walk(file.toPath())
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.map(Path::toFile)
|
||||
.forEach(File::delete);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesAddon#onLoad()}.
|
||||
*/
|
||||
@Test
|
||||
public void test() {
|
||||
public void testOnLoad() {
|
||||
addon.onLoad();
|
||||
// Check that config.yml file has been saved
|
||||
File check = new File("addons/Challenges","config.yml");
|
||||
assertTrue(check.exists());
|
||||
}
|
||||
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
Challenge challenges = new Challenge();
|
||||
challenges.setChallengeType(ChallengeType.ISLAND);
|
||||
Map<Material, Integer> map = new HashMap<>();
|
||||
map.put(Material.DIRT, 5);
|
||||
map.put(Material.ACACIA_FENCE_GATE, 3);
|
||||
challenges.setRequiredBlocks(map);
|
||||
challenges.setIcon(new ItemStack(Material.ACACIA_FENCE_GATE));
|
||||
List<ItemStack> requiredItems = new ArrayList<>();
|
||||
ItemStack result = new ItemStack(Material.POTION, 55);
|
||||
ItemStack result2 = new ItemStack(Material.SPLASH_POTION, 22);
|
||||
ItemStack result3 = new ItemStack(Material.LINGERING_POTION, 11);
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesAddon#onEnable()}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnEnableDisabledPlugin() {
|
||||
when(plugin.isEnabled()).thenReturn(false);
|
||||
addon.onEnable();
|
||||
verify(plugin).logError("[challenges] BentoBox is not available or disabled!");
|
||||
assertEquals(Addon.State.DISABLED, addon.getState());
|
||||
}
|
||||
|
||||
PotionMeta potionMeta = (PotionMeta) result.getItemMeta();
|
||||
PotionData potionData = new PotionData(PotionType.FIRE_RESISTANCE, true, false);
|
||||
potionMeta.setBasePotionData(potionData);
|
||||
result.setItemMeta(potionMeta);
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesAddon#onEnable()}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnEnableDisabledAddon() {
|
||||
when(plugin.isEnabled()).thenReturn(true);
|
||||
addon.setState(State.DISABLED);
|
||||
addon.onEnable();
|
||||
verify(plugin).logError("[challenges] Challenges Addon is not available or disabled!");
|
||||
}
|
||||
|
||||
PotionMeta potionMeta2 = (PotionMeta) result2.getItemMeta();
|
||||
PotionData potionData2 = new PotionData(PotionType.SPEED, true, false);
|
||||
potionMeta2.setBasePotionData(potionData2);
|
||||
potionMeta2.addEnchant(Enchantment.BINDING_CURSE, 1, true);
|
||||
result2.setItemMeta(potionMeta2);
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesAddon#onEnable()}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnEnableIncompatibleDatabase() {
|
||||
// The database type has to be created one line before the thenReturn() to work!
|
||||
DatabaseType value = DatabaseType.YAML;
|
||||
when(settings.getDatabaseType()).thenReturn(value);
|
||||
when(plugin.isEnabled()).thenReturn(true);
|
||||
addon.setState(State.LOADED);
|
||||
addon.onEnable();
|
||||
verify(plugin).logError("[challenges] BentoBox database is not compatible with Challenges Addon.");
|
||||
verify(plugin).logError("[challenges] Please use JSON based database type.");
|
||||
assertEquals(State.INCOMPATIBLE, addon.getState());
|
||||
}
|
||||
|
||||
requiredItems.add(result);
|
||||
requiredItems.add(result2);
|
||||
requiredItems.add(result3);
|
||||
challenges.setRequiredItems(requiredItems);
|
||||
String json = gson.toJson(challenges);
|
||||
|
||||
Logger.getAnonymousLogger().info(json);
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesAddon#onEnable()}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnEnableHooked() {
|
||||
addon.onLoad();
|
||||
when(plugin.isEnabled()).thenReturn(true);
|
||||
addon.setState(State.LOADED);
|
||||
addon.onEnable();
|
||||
verify(plugin).logWarning("[challenges] Level add-on not found so level challenges will not work!");
|
||||
verify(plugin).logWarning("[challenges] Economy plugin not found so money options will not work!");
|
||||
verify(plugin).log("[challenges] Loading challenges...");
|
||||
verify(plugin, never()).logError("Challenges could not hook into AcidIsland or BSkyBlock so will not do anything!");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesAddon#onEnable()}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnEnableNotHooked() {
|
||||
addon.onLoad();
|
||||
when(am.getGameModeAddons()).thenReturn(Collections.emptyList());
|
||||
when(plugin.isEnabled()).thenReturn(true);
|
||||
addon.setState(State.LOADED);
|
||||
addon.onEnable();
|
||||
verify(plugin).log("[challenges] Loading challenges...");
|
||||
verify(plugin).logError("[challenges] Challenges could not hook into AcidIsland or BSkyBlock so will not do anything!");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesAddon#onReload()}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnReloadNotHooked() {
|
||||
addon.onReload();
|
||||
verify(plugin, never()).log(anyString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesAddon#onDisable()}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnDisable() {
|
||||
this.testOnEnableHooked();
|
||||
addon.onDisable();
|
||||
|
||||
// Verify database saved exists
|
||||
File chDir = new File("database", "Challenge");
|
||||
assertTrue(chDir.exists());
|
||||
File lvDir = new File("database", "ChallengeLevel");
|
||||
assertTrue(lvDir.exists());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesAddon#getChallengesManager()}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetChallengesManager() {
|
||||
assertNull(addon.getChallengesManager());
|
||||
this.testOnEnableHooked();
|
||||
assertNotNull(addon.getChallengesManager());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesAddon#getPermissionPrefix()}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetPermissionPrefix() {
|
||||
assertEquals("addon.", addon.getPermissionPrefix());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesAddon#getImportManager()}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetImportManager() {
|
||||
assertNull(addon.getImportManager());
|
||||
this.testOnEnableHooked();
|
||||
assertNotNull(addon.getImportManager());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesAddon#getWebManager()}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetWebManager() {
|
||||
assertNull(addon.getWebManager());
|
||||
this.testOnEnableHooked();
|
||||
assertNotNull(addon.getWebManager());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesAddon#getChallengesSettings()}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetChallengesSettings() {
|
||||
assertNull(addon.getChallengesSettings());
|
||||
addon.onLoad();
|
||||
assertNotNull(addon.getChallengesSettings());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesAddon#isEconomyProvided()}.
|
||||
*/
|
||||
@Test
|
||||
public void testIsEconomyProvided() {
|
||||
assertFalse(addon.isEconomyProvided());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesAddon#getEconomyProvider()}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetEconomyProvider() {
|
||||
assertNull(addon.getEconomyProvider());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesAddon#isLevelProvided()}.
|
||||
*/
|
||||
@Test
|
||||
public void testIsLevelProvided() {
|
||||
assertFalse(addon.isLevelProvided());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesAddon#getLevelAddon()}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetLevelAddon() {
|
||||
assertNull(addon.getLevelAddon());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,870 @@
|
||||
package world.bentobox.challenges;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.UnsafeValues;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.inventory.ItemFactory;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.eclipse.jdt.annotation.NonNull;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.reflect.Whitebox;
|
||||
|
||||
import world.bentobox.bentobox.BentoBox;
|
||||
import world.bentobox.bentobox.api.addons.AddonDescription;
|
||||
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
import world.bentobox.bentobox.database.DatabaseSetup.DatabaseType;
|
||||
import world.bentobox.bentobox.managers.IslandWorldManager;
|
||||
import world.bentobox.bentobox.managers.PlaceholdersManager;
|
||||
import world.bentobox.bentobox.util.Util;
|
||||
import world.bentobox.challenges.config.Settings;
|
||||
import world.bentobox.challenges.database.object.Challenge;
|
||||
import world.bentobox.challenges.database.object.Challenge.ChallengeType;
|
||||
import world.bentobox.challenges.database.object.ChallengeLevel;
|
||||
import world.bentobox.challenges.events.ChallengeCompletedEvent;
|
||||
import world.bentobox.challenges.events.ChallengeResetAllEvent;
|
||||
import world.bentobox.challenges.events.ChallengeResetEvent;
|
||||
import world.bentobox.challenges.events.LevelCompletedEvent;
|
||||
import world.bentobox.challenges.utils.LevelStatus;
|
||||
|
||||
/**
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({Bukkit.class, BentoBox.class, Util.class})
|
||||
public class ChallengesManagerTest {
|
||||
|
||||
// Constants
|
||||
private static final String GAME_MODE_NAME = "BSkyBlock";
|
||||
|
||||
// Mocks
|
||||
@Mock
|
||||
private ChallengesAddon addon;
|
||||
@Mock
|
||||
private Settings settings;
|
||||
@Mock
|
||||
private IslandWorldManager iwm;
|
||||
@Mock
|
||||
private Server server;
|
||||
@Mock
|
||||
private PluginManager pim;
|
||||
@Mock
|
||||
private ItemFactory itemFactory;
|
||||
@Mock
|
||||
private User user;
|
||||
@Mock
|
||||
private World world;
|
||||
@Mock
|
||||
private GameModeAddon gameModeAddon;
|
||||
@Mock
|
||||
private PlaceholdersManager plhm;
|
||||
|
||||
// Variable fields
|
||||
private ChallengesManager cm;
|
||||
private File database;
|
||||
private String uuid;
|
||||
private Challenge challenge;
|
||||
private @NonNull ChallengeLevel level;
|
||||
private UUID playerID = UUID.randomUUID();
|
||||
private String cName;
|
||||
private String levelName;
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
// Set up plugin
|
||||
BentoBox plugin = mock(BentoBox.class);
|
||||
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
|
||||
when(addon.getPlugin()).thenReturn(plugin);
|
||||
|
||||
// IWM
|
||||
when(plugin.getIWM()).thenReturn(iwm);
|
||||
when(iwm.inWorld(any(World.class))).thenReturn(true);
|
||||
|
||||
// Placeholders
|
||||
when(plugin.getPlaceholdersManager()).thenReturn(plhm);
|
||||
|
||||
// Settings for Database
|
||||
world.bentobox.bentobox.Settings s = mock(world.bentobox.bentobox.Settings.class);
|
||||
when(plugin.getSettings()).thenReturn(s);
|
||||
when(s.getDatabaseType()).thenReturn(DatabaseType.JSON);
|
||||
|
||||
// Addon Settings
|
||||
when(addon.getChallengesSettings()).thenReturn(settings);
|
||||
when(settings.isStoreHistory()).thenReturn(true);
|
||||
when(settings.getLifeSpan()).thenReturn(10);
|
||||
|
||||
// Database
|
||||
database = new File("database");
|
||||
tearDown();
|
||||
|
||||
// Bukkit
|
||||
PowerMockito.mockStatic(Bukkit.class);
|
||||
when(Bukkit.getServer()).thenReturn(server);
|
||||
when(Bukkit.getPluginManager()).thenReturn(pim);
|
||||
when(Bukkit.getWorld(anyString())).thenReturn(world);
|
||||
|
||||
ItemMeta meta = mock(ItemMeta.class);
|
||||
when(itemFactory.getItemMeta(any())).thenReturn(meta);
|
||||
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
|
||||
UnsafeValues unsafe = mock(UnsafeValues.class);
|
||||
when(unsafe.getDataVersion()).thenReturn(777);
|
||||
when(Bukkit.getUnsafe()).thenReturn(unsafe);
|
||||
|
||||
// Challenge
|
||||
challenge = new Challenge();
|
||||
uuid = UUID.randomUUID().toString();
|
||||
challenge.setUniqueId(GAME_MODE_NAME + "_" + uuid);
|
||||
challenge.setFriendlyName("name");
|
||||
challenge.setLevel(GAME_MODE_NAME + "_novice");
|
||||
challenge.setDescription(Collections.singletonList("A description"));
|
||||
|
||||
// Challenge Level
|
||||
level = new ChallengeLevel();
|
||||
levelName = GAME_MODE_NAME + "_novice";
|
||||
level.setUniqueId(levelName);
|
||||
level.setFriendlyName("Novice");
|
||||
|
||||
// User
|
||||
when(user.getUniqueId()).thenReturn(playerID);
|
||||
|
||||
// Util
|
||||
PowerMockito.mockStatic(Util.class);
|
||||
when(Util.getWorld(any())).thenReturn(world);
|
||||
|
||||
// Addon
|
||||
AddonDescription desc = new AddonDescription.Builder("main", GAME_MODE_NAME, "1.0").build();
|
||||
when(gameModeAddon.getDescription()).thenReturn(desc);
|
||||
Optional<GameModeAddon> opAddon = Optional.of(gameModeAddon);
|
||||
when(iwm.getAddon(any())).thenReturn(opAddon);
|
||||
|
||||
// Challenge name
|
||||
cName = GAME_MODE_NAME + "_" + uuid;
|
||||
|
||||
// Class under test
|
||||
cm = new ChallengesManager(addon);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
// Clean up JSON database
|
||||
// Clean up file system
|
||||
if (database.exists()) {
|
||||
Files.walk(database.toPath())
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.map(Path::toFile)
|
||||
.forEach(File::delete);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#load()}.
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
@Test
|
||||
public void testLoad() throws InterruptedException {
|
||||
verify(addon).log("Loading challenges...");
|
||||
verify(addon, never()).logError(anyString());
|
||||
this.testSaveLevel();
|
||||
this.testSaveChallenge();
|
||||
cm.load();
|
||||
verify(addon, times(2)).log("Loading challenges...");
|
||||
verify(addon, never()).logError(anyString());
|
||||
assertTrue(cm.containsChallenge(cName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#reload()}.
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
@Test
|
||||
public void testReload() throws InterruptedException {
|
||||
cm.reload();
|
||||
verify(addon).log("Reloading challenges...");
|
||||
this.testSaveLevel();
|
||||
this.testSaveChallenge();
|
||||
cm.reload();
|
||||
verify(addon, times(2)).log("Reloading challenges...");
|
||||
verify(addon, never()).logError(anyString());
|
||||
assertTrue(cm.containsChallenge(cName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#loadChallenge(world.bentobox.challenges.database.object.Challenge, boolean, world.bentobox.bentobox.api.user.User, boolean)}.
|
||||
*/
|
||||
@Test
|
||||
public void testLoadChallengeNoOverwriteSilent() {
|
||||
// load once
|
||||
assertTrue(cm.loadChallenge(challenge, false, user, true));
|
||||
// load twice - no overwrite
|
||||
assertFalse(cm.loadChallenge(challenge, false, user, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#loadChallenge(world.bentobox.challenges.database.object.Challenge, boolean, world.bentobox.bentobox.api.user.User, boolean)}.
|
||||
*/
|
||||
@Test
|
||||
public void testLoadChallengeNoOverwriteNotSilent() {
|
||||
// load once
|
||||
assertTrue(cm.loadChallenge(challenge, false, user, true));
|
||||
// load twice - no overwrite, not silent
|
||||
assertFalse(cm.loadChallenge(challenge, false, user, false));
|
||||
verify(user).sendMessage("challenges.messages.load-skipping", "[value]", "name");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#loadChallenge(world.bentobox.challenges.database.object.Challenge, boolean, world.bentobox.bentobox.api.user.User, boolean)}.
|
||||
*/
|
||||
@Test
|
||||
public void testLoadChallengeOverwriteSilent() {
|
||||
// load once
|
||||
assertTrue(cm.loadChallenge(challenge, false, user, true));
|
||||
// overwrite
|
||||
assertTrue(cm.loadChallenge(challenge, true, user, true));
|
||||
verify(user, never()).sendMessage(anyString(), anyString(), anyString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#loadChallenge(world.bentobox.challenges.database.object.Challenge, boolean, world.bentobox.bentobox.api.user.User, boolean)}.
|
||||
*/
|
||||
@Test
|
||||
public void testLoadChallengeOverwriteNotSilent() {
|
||||
// load once
|
||||
assertTrue(cm.loadChallenge(challenge, false, user, true));
|
||||
// overwrite not silent
|
||||
assertTrue(cm.loadChallenge(challenge, true, user, false));
|
||||
verify(user).sendMessage("challenges.messages.load-overwriting", "[value]", "name");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#loadLevel(world.bentobox.challenges.database.object.ChallengeLevel, boolean, world.bentobox.bentobox.api.user.User, boolean)}.
|
||||
*/
|
||||
@Test
|
||||
public void testLoadLevelNoOverwriteSilent() {
|
||||
// load once
|
||||
assertTrue(cm.loadLevel(level, false, user, true));
|
||||
// load twice - no overwrite
|
||||
assertFalse(cm.loadLevel(level, false, user, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#loadLevel(world.bentobox.challenges.database.object.ChallengeLevel, boolean, world.bentobox.bentobox.api.user.User, boolean)}.
|
||||
*/
|
||||
@Test
|
||||
public void testLoadLevelNoOverwriteNotSilent() {
|
||||
// load once
|
||||
assertTrue(cm.loadLevel(level, false, user, true));
|
||||
// load twice - no overwrite, not silent
|
||||
assertFalse(cm.loadLevel(level, false, user, false));
|
||||
verify(user).sendMessage("challenges.messages.load-skipping", "[value]", "Novice");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#loadLevel(world.bentobox.challenges.database.object.ChallengeLevel, boolean, world.bentobox.bentobox.api.user.User, boolean)}.
|
||||
*/
|
||||
@Test
|
||||
public void testLoadLevelOverwriteSilent() {
|
||||
// load once
|
||||
assertTrue(cm.loadLevel(level, false, user, true));
|
||||
// overwrite
|
||||
assertTrue(cm.loadLevel(level, true, user, true));
|
||||
verify(user, never()).sendMessage(anyString(), anyString(), anyString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#loadLevel(world.bentobox.challenges.database.object.ChallengeLevel, boolean, world.bentobox.bentobox.api.user.User, boolean)}.
|
||||
*/
|
||||
@Test
|
||||
public void testLoadLevelOverwriteNotSilent() {
|
||||
// load once
|
||||
assertTrue(cm.loadLevel(level, false, user, true));
|
||||
// overwrite not silent
|
||||
assertTrue(cm.loadLevel(level, true, user, false));
|
||||
verify(user).sendMessage("challenges.messages.load-overwriting", "[value]", "Novice");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#removeFromCache(java.util.UUID)}.
|
||||
*/
|
||||
@Test
|
||||
public void testRemoveFromCache() {
|
||||
cm.removeFromCache(playerID);
|
||||
verify(settings).isStoreAsIslandData();
|
||||
// TODO there should be a test where isStoreAsIslandData returns true
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#wipeDatabase(boolean)}.
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
@Test
|
||||
public void testWipeDatabase() throws InterruptedException {
|
||||
// Create some database
|
||||
this.testLoad();
|
||||
|
||||
// Verify file exists
|
||||
File chDir = new File(database, "Challenge");
|
||||
File check = new File(chDir, cName + ".json");
|
||||
assertTrue(check.exists());
|
||||
|
||||
File lvDir = new File(database, "ChallengeLevel");
|
||||
File checkLv = new File(lvDir, levelName + ".json");
|
||||
assertTrue(checkLv.exists());
|
||||
|
||||
cm.setChallengeComplete(user, world, challenge, 20);
|
||||
//cm.save();
|
||||
File plData = new File(database, "ChallengesPlayerData");
|
||||
File checkPd = new File(plData, playerID.toString() + ".json");
|
||||
assertTrue(checkPd.exists());
|
||||
|
||||
// Wipe it
|
||||
cm.wipeDatabase(false);
|
||||
|
||||
// Verify
|
||||
assertFalse(check.exists());
|
||||
assertFalse(checkLv.exists());
|
||||
assertTrue(checkPd.exists());
|
||||
|
||||
cm.wipeDatabase(true);
|
||||
assertFalse(checkPd.exists());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#wipePlayers()}.
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
@Test
|
||||
public void testWipePlayers() throws InterruptedException {
|
||||
this.testLoad();
|
||||
cm.setChallengeComplete(user, world, challenge, 20);
|
||||
cm.save();
|
||||
File plData = new File(database, "ChallengesPlayerData");
|
||||
File checkLv = new File(plData, playerID.toString() + ".json");
|
||||
assertTrue(checkLv.exists());
|
||||
cm.wipePlayers();
|
||||
assertFalse(checkLv.exists());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#migrateDatabase(world.bentobox.bentobox.api.user.User, org.bukkit.World)}.
|
||||
*/
|
||||
@Test
|
||||
public void testMigrateDatabase() {
|
||||
cm.migrateDatabase(user, world);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#save()}.
|
||||
*/
|
||||
@Test
|
||||
public void testSave() {
|
||||
cm.save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#saveChallenge(world.bentobox.challenges.database.object.Challenge)}.
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
@Test
|
||||
public void testSaveChallenge() throws InterruptedException {
|
||||
// Async - may not happen quickly
|
||||
cm.saveChallenge(challenge);
|
||||
Thread.sleep(500);
|
||||
File chDir = new File(database, "Challenge");
|
||||
assertTrue(chDir.exists());
|
||||
File check = new File(chDir, cName + ".json");
|
||||
assertTrue(check.exists());
|
||||
// Remove icon becauseit has mockito meta in it
|
||||
removeLine(check);
|
||||
}
|
||||
|
||||
private boolean removeLine(File inputFile) {
|
||||
File tempFile = new File("myTempFile.json");
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(inputFile))) {
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile))) {
|
||||
|
||||
String lineToRemove = "\"icon";
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
if(trimmedLine.startsWith(lineToRemove)) continue;
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return tempFile.renameTo(inputFile);
|
||||
}
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#saveLevel(world.bentobox.challenges.database.object.ChallengeLevel)}.
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
@Test
|
||||
public void testSaveLevel() throws InterruptedException {
|
||||
cm.saveLevel(level);
|
||||
Thread.sleep(500);
|
||||
File chDir = new File(database, "ChallengeLevel");
|
||||
assertTrue(chDir.exists());
|
||||
File check = new File(chDir, GAME_MODE_NAME + "_novice.json");
|
||||
assertTrue(check.exists());
|
||||
// Remove icon becauseit has mockito meta in it
|
||||
removeLine(check);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#isChallengeComplete(world.bentobox.bentobox.api.user.User, org.bukkit.World, world.bentobox.challenges.database.object.Challenge)}.
|
||||
*/
|
||||
@Test
|
||||
public void testIsChallengeCompleteUserWorldChallenge() {
|
||||
assertFalse(cm.isChallengeComplete(user, world, challenge));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#isChallengeComplete(java.util.UUID, org.bukkit.World, world.bentobox.challenges.database.object.Challenge)}.
|
||||
*/
|
||||
@Test
|
||||
public void testIsChallengeCompleteUUIDWorldChallenge() {
|
||||
assertFalse(cm.isChallengeComplete(playerID, world, challenge));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#isChallengeComplete(java.util.UUID, org.bukkit.World, java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testIsChallengeCompleteUUIDWorldString() {
|
||||
assertFalse(cm.isChallengeComplete(playerID, world, "Novice"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#setChallengeComplete(world.bentobox.bentobox.api.user.User, org.bukkit.World, world.bentobox.challenges.database.object.Challenge, int)}.
|
||||
*/
|
||||
@Test
|
||||
public void testSetChallengeCompleteUserWorldChallengeInt() {
|
||||
cm.setChallengeComplete(user, world, challenge, 3);
|
||||
assertTrue(cm.isChallengeComplete(user, world, challenge));
|
||||
verify(pim).callEvent(any(ChallengeCompletedEvent.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#setChallengeComplete(java.util.UUID, org.bukkit.World, world.bentobox.challenges.database.object.Challenge, int)}.
|
||||
*/
|
||||
@Test
|
||||
public void testSetChallengeCompleteUUIDWorldChallengeInt() {
|
||||
cm.setChallengeComplete(playerID, world, challenge, 3);
|
||||
assertTrue(cm.isChallengeComplete(playerID, world, challenge));
|
||||
verify(pim).callEvent(any(ChallengeCompletedEvent.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#setChallengeComplete(java.util.UUID, org.bukkit.World, world.bentobox.challenges.database.object.Challenge, java.util.UUID)}.
|
||||
*/
|
||||
@Test
|
||||
public void testSetChallengeCompleteUUIDWorldChallengeUUID() {
|
||||
UUID adminID = UUID.randomUUID();
|
||||
cm.setChallengeComplete(playerID, world, challenge, adminID);
|
||||
assertTrue(cm.isChallengeComplete(playerID, world, challenge));
|
||||
verify(pim).callEvent(any(ChallengeCompletedEvent.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#resetChallenge(java.util.UUID, org.bukkit.World, world.bentobox.challenges.database.object.Challenge, java.util.UUID)}.
|
||||
*/
|
||||
@Test
|
||||
public void testResetChallenge() {
|
||||
cm.setChallengeComplete(user, world, challenge, 3);
|
||||
assertTrue(cm.isChallengeComplete(user, world, challenge));
|
||||
cm.resetChallenge(playerID, world, challenge, playerID);
|
||||
assertFalse(cm.isChallengeComplete(user, world, challenge));
|
||||
verify(pim).callEvent(any(ChallengeResetEvent.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#resetAllChallenges(world.bentobox.bentobox.api.user.User, org.bukkit.World)}.
|
||||
*/
|
||||
@Test
|
||||
public void testResetAllChallengesUserWorld() {
|
||||
cm.setChallengeComplete(user, world, challenge, 3);
|
||||
assertTrue(cm.isChallengeComplete(user, world, challenge));
|
||||
cm.resetAllChallenges(user, world);
|
||||
assertFalse(cm.isChallengeComplete(user, world, challenge));
|
||||
verify(pim).callEvent(any(ChallengeResetAllEvent.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#resetAllChallenges(java.util.UUID, org.bukkit.World, java.util.UUID)}.
|
||||
*/
|
||||
@Test
|
||||
public void testResetAllChallengesUUIDWorldUUID() {
|
||||
cm.setChallengeComplete(user, world, challenge, 3);
|
||||
assertTrue(cm.isChallengeComplete(user, world, challenge));
|
||||
cm.resetAllChallenges(playerID, world, playerID);
|
||||
assertFalse(cm.isChallengeComplete(user, world, challenge));
|
||||
verify(pim).callEvent(any(ChallengeResetAllEvent.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#getChallengeTimes(world.bentobox.bentobox.api.user.User, org.bukkit.World, world.bentobox.challenges.database.object.Challenge)}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetChallengeTimesUserWorldChallenge() {
|
||||
assertEquals(0L, cm.getChallengeTimes(user, world, challenge));
|
||||
cm.setChallengeComplete(user, world, challenge, 6);
|
||||
assertEquals(6L, cm.getChallengeTimes(user, world, challenge));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#getChallengeTimes(world.bentobox.bentobox.api.user.User, org.bukkit.World, java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetChallengeTimesUserWorldString() {
|
||||
assertEquals(0L, cm.getChallengeTimes(user, world, cName));
|
||||
cm.setChallengeComplete(user, world, challenge, 6);
|
||||
assertEquals(6L, cm.getChallengeTimes(user, world, cName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#isLevelCompleted(world.bentobox.bentobox.api.user.User, org.bukkit.World, world.bentobox.challenges.database.object.ChallengeLevel)}.
|
||||
*/
|
||||
@Test
|
||||
public void testIsLevelCompleted() {
|
||||
assertFalse(cm.isLevelCompleted(user, world, level));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#isLevelUnlocked(world.bentobox.bentobox.api.user.User, org.bukkit.World, world.bentobox.challenges.database.object.ChallengeLevel)}.
|
||||
*/
|
||||
@Test
|
||||
public void testIsLevelUnlocked() {
|
||||
assertFalse(cm.isLevelUnlocked(user, world, level));
|
||||
this.testLoadLevelNoOverwriteSilent();
|
||||
assertTrue(cm.isLevelUnlocked(user, world, level));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#setLevelComplete(world.bentobox.bentobox.api.user.User, org.bukkit.World, world.bentobox.challenges.database.object.ChallengeLevel)}.
|
||||
*/
|
||||
@Test
|
||||
public void testSetLevelComplete() {
|
||||
assertFalse(cm.isLevelCompleted(user, world, level));
|
||||
cm.setLevelComplete(user, world, level);
|
||||
assertTrue(cm.isLevelCompleted(user, world, level));
|
||||
verify(pim).callEvent(any(LevelCompletedEvent.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#validateLevelCompletion(world.bentobox.bentobox.api.user.User, org.bukkit.World, world.bentobox.challenges.database.object.ChallengeLevel)}.
|
||||
*/
|
||||
@Test
|
||||
public void testValidateLevelCompletion() {
|
||||
assertTrue(cm.validateLevelCompletion(user, world, level));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#getChallengeLevelStatus(java.util.UUID, org.bukkit.World, world.bentobox.challenges.database.object.ChallengeLevel)}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetChallengeLevelStatus() {
|
||||
this.testLoadLevelNoOverwriteSilent();
|
||||
LevelStatus cls = cm.getChallengeLevelStatus(playerID, world, level);
|
||||
assertTrue(cls.getNumberOfChallengesStillToDo() == 0);
|
||||
assertEquals(level, cls.getLevel());
|
||||
assertTrue(cls.isComplete());
|
||||
assertTrue(cls.isUnlocked());
|
||||
assertEquals("BSkyBlock_novice", cls.getLevel().getUniqueId());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#getAllChallengeLevelStatus(world.bentobox.bentobox.api.user.User, org.bukkit.World)}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetAllChallengeLevelStatus() {
|
||||
this.testLoadLevelNoOverwriteSilent();
|
||||
List<LevelStatus> list = cm.getAllChallengeLevelStatus(user, world);
|
||||
assertTrue(list.size() == 1);
|
||||
LevelStatus cls = list.get(0);
|
||||
assertTrue(cls.getNumberOfChallengesStillToDo() == 0);
|
||||
assertEquals(level, cls.getLevel());
|
||||
assertTrue(cls.isComplete());
|
||||
assertTrue(cls.isUnlocked());
|
||||
assertEquals("BSkyBlock_novice", cls.getLevel().getUniqueId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#getAllChallengesNames(org.bukkit.World)}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetAllChallengesNames() {
|
||||
assertTrue(cm.getAllChallengesNames(world).isEmpty());
|
||||
cm.saveChallenge(challenge);
|
||||
cm.loadChallenge(challenge, false, user, true);
|
||||
List<String> list = cm.getAllChallengesNames(world);
|
||||
assertFalse(list.isEmpty());
|
||||
assertEquals(cName, list.get(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#getAllChallenges(org.bukkit.World)}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetAllChallenges() {
|
||||
assertTrue(cm.getAllChallenges(world).isEmpty());
|
||||
cm.saveChallenge(challenge);
|
||||
cm.loadChallenge(challenge, false, user, true);
|
||||
List<Challenge> list = cm.getAllChallenges(world);
|
||||
assertFalse(list.isEmpty());
|
||||
assertEquals(challenge, list.get(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#getFreeChallenges(org.bukkit.World)}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetFreeChallenges() {
|
||||
// Empty
|
||||
assertTrue(cm.getFreeChallenges(world).isEmpty());
|
||||
// One normal
|
||||
cm.saveChallenge(challenge);
|
||||
cm.loadChallenge(challenge, false, user, true);
|
||||
assertTrue(cm.getFreeChallenges(world).isEmpty());
|
||||
// One free
|
||||
challenge.setLevel("");
|
||||
cm.saveChallenge(challenge);
|
||||
cm.loadChallenge(challenge, false, user, true);
|
||||
List<Challenge> list = cm.getFreeChallenges(world);
|
||||
assertFalse(list.isEmpty());
|
||||
assertEquals(challenge, list.get(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#getLevelChallenges(world.bentobox.challenges.database.object.ChallengeLevel)}.
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
@Test
|
||||
public void testGetLevelChallenges() throws InterruptedException {
|
||||
assertTrue(cm.getLevelChallenges(level).isEmpty());
|
||||
// make some challenges
|
||||
this.testSaveLevel();
|
||||
this.testSaveChallenge();
|
||||
level.setChallenges(Collections.singleton(challenge.getUniqueId()));
|
||||
// Test again
|
||||
List<Challenge> list = cm.getLevelChallenges(level);
|
||||
assertFalse(list.isEmpty());
|
||||
assertEquals(challenge, list.get(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#getChallenge(java.lang.String)}.
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
@Test
|
||||
public void testGetChallenge() throws InterruptedException {
|
||||
assertNull(cm.getChallenge(cName));
|
||||
this.testSaveLevel();
|
||||
this.testSaveChallenge();
|
||||
Challenge ch = cm.getChallenge(cName);
|
||||
assertNotNull(ch);
|
||||
assertEquals(cName, ch.getUniqueId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#containsChallenge(java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testContainsChallenge() {
|
||||
assertFalse(cm.containsChallenge("no-such-challenge"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#createChallenge(java.lang.String, world.bentobox.challenges.database.object.Challenge.ChallengeType, world.bentobox.challenges.database.object.requirements.Requirements)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCreateChallenge() {
|
||||
@Nullable
|
||||
Challenge ch = cm.createChallenge("newChal", ChallengeType.ISLAND, null);
|
||||
assertEquals(ChallengeType.ISLAND, ch.getChallengeType());
|
||||
assertEquals("newChal", ch.getUniqueId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#deleteChallenge(world.bentobox.challenges.database.object.Challenge)}.
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
@Test
|
||||
public void testDeleteChallenge() throws InterruptedException {
|
||||
this.testSaveLevel();
|
||||
this.testSaveChallenge();
|
||||
Challenge ch = cm.getChallenge(cName);
|
||||
assertNotNull(ch);
|
||||
assertEquals(cName, ch.getUniqueId());
|
||||
cm.deleteChallenge(challenge);
|
||||
ch = cm.getChallenge(cName);
|
||||
assertNull(ch);
|
||||
verify(plhm).unregisterPlaceholder(eq("challenges_challenge_repetition_count_" + cName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#getLevels(org.bukkit.World)}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetLevels() {
|
||||
this.testGetLevelString();
|
||||
List<ChallengeLevel> lvs = cm.getLevels(world);
|
||||
assertFalse(lvs.isEmpty());
|
||||
assertEquals(level, lvs.get(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#getLevel(world.bentobox.challenges.database.object.Challenge)}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetLevelChallenge() {
|
||||
this.testGetLevelString();
|
||||
assertEquals(level, cm.getLevel(challenge));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#getLevel(java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetLevelString() {
|
||||
assertNull(cm.getLevel("dss"));
|
||||
cm.saveLevel(level);
|
||||
cm.loadLevel(level, false, user, true);
|
||||
assertEquals(level, cm.getLevel(levelName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#containsLevel(java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testContainsLevel() {
|
||||
this.testGetLevelString();
|
||||
assertFalse(cm.containsLevel("sdsd"));
|
||||
assertTrue(cm.containsLevel(levelName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#addChallengeToLevel(world.bentobox.challenges.database.object.Challenge, world.bentobox.challenges.database.object.ChallengeLevel)}.
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
@Test
|
||||
public void testAddChallengeToLevel() throws InterruptedException {
|
||||
this.testLoad();
|
||||
cm.deleteChallenge(challenge);
|
||||
assertFalse(cm.containsChallenge(cName));
|
||||
cm.addChallengeToLevel(challenge, level);
|
||||
assertEquals(level, cm.getLevel(challenge));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#removeChallengeFromLevel(world.bentobox.challenges.database.object.Challenge, world.bentobox.challenges.database.object.ChallengeLevel)}.
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
@Test
|
||||
public void testRemoveChallengeFromLevel() throws InterruptedException {
|
||||
this.testAddChallengeToLevel();
|
||||
cm.removeChallengeFromLevel(challenge, level);
|
||||
assertFalse(cm.containsChallenge(cName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#createLevel(java.lang.String, org.bukkit.World)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCreateLevel() {
|
||||
@Nullable
|
||||
ChallengeLevel cl = cm.createLevel("Expert", world);
|
||||
assertEquals("Expert", cl.getUniqueId());
|
||||
assertEquals(world.getName(), cl.getWorld());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#deleteChallengeLevel(world.bentobox.challenges.database.object.ChallengeLevel)}.
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
@Test
|
||||
public void testDeleteChallengeLevel() throws InterruptedException {
|
||||
this.testAddChallengeToLevel();
|
||||
assertTrue(cm.containsLevel(levelName));
|
||||
cm.deleteChallengeLevel(level);
|
||||
assertFalse(cm.containsLevel(levelName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#hasAnyChallengeData(org.bukkit.World)}.
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
@Test
|
||||
public void testHasAnyChallengeDataWorld() throws InterruptedException {
|
||||
assertFalse(cm.hasAnyChallengeData(world));
|
||||
this.testLoad();
|
||||
assertTrue(cm.hasAnyChallengeData(world));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.ChallengesManager#hasAnyChallengeData(java.lang.String)}.
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
@Test
|
||||
public void testHasAnyChallengeDataString() throws InterruptedException {
|
||||
assertFalse(cm.hasAnyChallengeData("BSkyBlock"));
|
||||
this.testLoad();
|
||||
assertTrue(cm.hasAnyChallengeData("BSkyBlock"));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,268 @@
|
||||
package world.bentobox.challenges.commands;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemFactory;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.eclipse.jdt.annotation.NonNull;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.reflect.Whitebox;
|
||||
|
||||
import world.bentobox.bentobox.BentoBox;
|
||||
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.localization.TextVariables;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
import world.bentobox.bentobox.database.objects.Island;
|
||||
import world.bentobox.bentobox.managers.CommandsManager;
|
||||
import world.bentobox.bentobox.managers.IslandWorldManager;
|
||||
import world.bentobox.bentobox.managers.IslandsManager;
|
||||
import world.bentobox.challenges.ChallengesAddon;
|
||||
import world.bentobox.challenges.ChallengesManager;
|
||||
import world.bentobox.challenges.config.Settings;
|
||||
import world.bentobox.challenges.config.SettingsUtils.VisibilityMode;
|
||||
|
||||
/**
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({Bukkit.class, BentoBox.class, ChatColor.class})
|
||||
public class ChallengesCommandTest {
|
||||
|
||||
@Mock
|
||||
private CompositeCommand ic;
|
||||
private UUID uuid;
|
||||
@Mock
|
||||
private User user;
|
||||
@Mock
|
||||
private IslandsManager im;
|
||||
@Mock
|
||||
private Island island;
|
||||
@Mock
|
||||
private ChallengesAddon addon;
|
||||
private ChallengesCommand cc;
|
||||
@Mock
|
||||
private World world;
|
||||
@Mock
|
||||
private ChallengesManager chm;
|
||||
@Mock
|
||||
private IslandWorldManager iwm;
|
||||
@Mock
|
||||
private GameModeAddon gameModeAddon;
|
||||
@Mock
|
||||
private Settings settings;
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
// Set up plugin
|
||||
BentoBox plugin = mock(BentoBox.class);
|
||||
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
|
||||
User.setPlugin(plugin);
|
||||
|
||||
// Command manager
|
||||
CommandsManager cm = mock(CommandsManager.class);
|
||||
when(plugin.getCommandsManager()).thenReturn(cm);
|
||||
// Addon
|
||||
when(ic.getAddon()).thenReturn(addon);
|
||||
when(ic.getPermissionPrefix()).thenReturn("bskyblock.");
|
||||
when(ic.getLabel()).thenReturn("island");
|
||||
when(ic.getTopLabel()).thenReturn("island");
|
||||
when(ic.getWorld()).thenReturn(world);
|
||||
when(ic.getTopLabel()).thenReturn("bsb");
|
||||
|
||||
// IWM friendly name
|
||||
when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock");
|
||||
when(iwm.inWorld(any(World.class))).thenReturn(true);
|
||||
Optional<GameModeAddon> optionalAddon = Optional.of(gameModeAddon);
|
||||
when(iwm.getAddon(any())).thenReturn(optionalAddon);
|
||||
when(plugin.getIWM()).thenReturn(iwm);
|
||||
|
||||
// Game Mode Addon
|
||||
@NonNull
|
||||
Optional<CompositeCommand> optionalAdmin = Optional.of(ic);
|
||||
when(gameModeAddon.getAdminCommand()).thenReturn(optionalAdmin);
|
||||
|
||||
// World
|
||||
when(world.toString()).thenReturn("world");
|
||||
|
||||
// Player
|
||||
Player p = mock(Player.class);
|
||||
// Sometimes use Mockito.withSettings().verboseLogging()
|
||||
when(user.isOp()).thenReturn(false);
|
||||
uuid = UUID.randomUUID();
|
||||
when(user.getUniqueId()).thenReturn(uuid);
|
||||
when(user.getPlayer()).thenReturn(p);
|
||||
when(user.getName()).thenReturn("tastybento");
|
||||
when(user.getPermissionValue(anyString(), anyInt())).thenReturn(-1);
|
||||
when(user.isPlayer()).thenReturn(true);
|
||||
|
||||
// Mock item factory (for itemstacks)
|
||||
PowerMockito.mockStatic(Bukkit.class);
|
||||
ItemFactory itemFactory = mock(ItemFactory.class);
|
||||
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
|
||||
ItemMeta itemMeta = mock(ItemMeta.class);
|
||||
when(itemFactory.getItemMeta(any())).thenReturn(itemMeta);
|
||||
|
||||
// Addon
|
||||
when(addon.getChallengesManager()).thenReturn(chm);
|
||||
when(chm.getAllChallengeLevelStatus(any(), any())).thenReturn(Collections.emptyList());
|
||||
// Challenges exist
|
||||
when(chm.hasAnyChallengeData(any(World.class))).thenReturn(true);
|
||||
|
||||
// ChatColor
|
||||
PowerMockito.mockStatic(ChatColor.class);
|
||||
when(ChatColor.translateAlternateColorCodes(any(char.class), anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
|
||||
|
||||
// Settings
|
||||
when(addon.getChallengesSettings()).thenReturn(settings);
|
||||
when(settings.getVisibilityMode()).thenReturn(VisibilityMode.VISIBLE);
|
||||
|
||||
// Island
|
||||
when(plugin.getIslands()).thenReturn(im);
|
||||
when(im.getIsland(any(), any(User.class))).thenReturn(island);
|
||||
|
||||
// Command under test
|
||||
cc = new ChallengesCommand(addon, ic);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.ChallengesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCanExecuteWrongWorld() {
|
||||
when(iwm.inWorld(any(World.class))).thenReturn(false);
|
||||
assertFalse(cc.canExecute(user, "challenges", Collections.emptyList()));
|
||||
verify(user).sendMessage("general.errors.wrong-world");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.ChallengesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCanExecuteNoChallenges() {
|
||||
when(chm.hasAnyChallengeData(any(World.class))).thenReturn(false);
|
||||
assertFalse(cc.canExecute(user, "challenges", Collections.emptyList()));
|
||||
verify(addon).logError("There are no challenges set up in world!");
|
||||
verify(user).sendMessage("challenges.errors.no-challenges");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.ChallengesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCanExecuteNoChallengesOp() {
|
||||
when(user.isOp()).thenReturn(true);
|
||||
when(chm.hasAnyChallengeData(any(World.class))).thenReturn(false);
|
||||
assertFalse(cc.canExecute(user, "challenges", Collections.emptyList()));
|
||||
verify(addon).logError("There are no challenges set up in world!");
|
||||
verify(user).sendMessage("challenges.errors.no-challenges-admin", "[command]", "bsb challenges");
|
||||
verify(user, never()).sendMessage("challenges.errors.no-challenges");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.ChallengesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCanExecuteNoChallengesHasPerm() {
|
||||
when(user.hasPermission(anyString())).thenReturn(true);
|
||||
when(chm.hasAnyChallengeData(any(World.class))).thenReturn(false);
|
||||
assertFalse(cc.canExecute(user, "challenges", Collections.emptyList()));
|
||||
verify(addon).logError("There are no challenges set up in world!");
|
||||
verify(user).sendMessage("challenges.errors.no-challenges-admin", "[command]", "bsb challenges");
|
||||
verify(user, never()).sendMessage("challenges.errors.no-challenges");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.ChallengesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCanExecuteNoAdminCommand() {
|
||||
when(gameModeAddon.getAdminCommand()).thenReturn(Optional.empty());
|
||||
when(user.isOp()).thenReturn(true);
|
||||
when(chm.hasAnyChallengeData(any(World.class))).thenReturn(false);
|
||||
assertFalse(cc.canExecute(user, "challenges", Collections.emptyList()));
|
||||
verify(addon).logError("There are no challenges set up in world!");
|
||||
verify(user).sendMessage("challenges.errors.no-challenges-admin", "[command]", "bsb challenges");
|
||||
verify(user, never()).sendMessage("challenges.errors.no-challenges");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.ChallengesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCanExecuteNoIsland() {
|
||||
when(im.getIsland(any(), any(User.class))).thenReturn(null);
|
||||
assertFalse(cc.canExecute(user, "challenges", Collections.emptyList()));
|
||||
verify(user).sendMessage("general.errors.no-island");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.ChallengesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCanExecuteSuccess() {
|
||||
assertTrue(cc.canExecute(user, "challenges", Collections.emptyList()));
|
||||
verify(user, never()).sendMessage(anyString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.ChallengesCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteUserStringListOfStringConsole() {
|
||||
User console = mock(User.class);
|
||||
assertFalse(cc.execute(console, "challenges", Collections.emptyList()));
|
||||
verify(console).sendMessage(eq("commands.help.header"), eq(TextVariables.LABEL), eq("BSkyBlock"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.ChallengesCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteUserStringListOfStringUser() {
|
||||
assertTrue(cc.execute(user, "challenges", Collections.emptyList()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.ChallengesCommand#setup()}.
|
||||
*/
|
||||
@Test
|
||||
public void testSetup() {
|
||||
assertEquals("bskyblock." + ChallengesCommand.CHALLENGE_COMMAND, cc.getPermission());
|
||||
assertEquals("challenges.commands.user.main.parameters", cc.getParameters());
|
||||
assertEquals("challenges.commands.user.main.description", cc.getDescription());
|
||||
assertTrue(cc.isOnlyPlayer());
|
||||
// CompleteChallengeCommand
|
||||
assertEquals(1, cc.getSubCommands(true).size());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,342 @@
|
||||
package world.bentobox.challenges.commands;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemFactory;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.eclipse.jdt.annotation.NonNull;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.reflect.Whitebox;
|
||||
|
||||
import world.bentobox.bentobox.BentoBox;
|
||||
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.localization.TextVariables;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
import world.bentobox.bentobox.database.objects.Island;
|
||||
import world.bentobox.bentobox.managers.CommandsManager;
|
||||
import world.bentobox.bentobox.managers.IslandWorldManager;
|
||||
import world.bentobox.bentobox.managers.IslandsManager;
|
||||
import world.bentobox.bentobox.util.Util;
|
||||
import world.bentobox.challenges.ChallengesAddon;
|
||||
import world.bentobox.challenges.ChallengesManager;
|
||||
import world.bentobox.challenges.config.Settings;
|
||||
import world.bentobox.challenges.config.SettingsUtils.VisibilityMode;
|
||||
import world.bentobox.challenges.database.object.Challenge;
|
||||
import world.bentobox.challenges.tasks.TryToComplete;
|
||||
import world.bentobox.challenges.utils.Utils;
|
||||
|
||||
/**
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({Bukkit.class, BentoBox.class, ChatColor.class, Utils.class, TryToComplete.class, Util.class})
|
||||
public class CompleteChallengeCommandTest {
|
||||
|
||||
@Mock
|
||||
private CompositeCommand ic;
|
||||
private UUID uuid;
|
||||
@Mock
|
||||
private User user;
|
||||
@Mock
|
||||
private IslandsManager im;
|
||||
@Mock
|
||||
private Island island;
|
||||
@Mock
|
||||
private ChallengesAddon addon;
|
||||
|
||||
private CompleteChallengeCommand cc;
|
||||
@Mock
|
||||
private World world;
|
||||
@Mock
|
||||
private ChallengesManager chm;
|
||||
@Mock
|
||||
private IslandWorldManager iwm;
|
||||
@Mock
|
||||
private GameModeAddon gameModeAddon;
|
||||
@Mock
|
||||
private Settings settings;
|
||||
@Mock
|
||||
private Challenge challenge;
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
// Set up plugin
|
||||
BentoBox plugin = mock(BentoBox.class);
|
||||
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
|
||||
User.setPlugin(plugin);
|
||||
|
||||
// Command manager
|
||||
CommandsManager cm = mock(CommandsManager.class);
|
||||
when(plugin.getCommandsManager()).thenReturn(cm);
|
||||
// Addon
|
||||
when(ic.getAddon()).thenReturn(addon);
|
||||
when(ic.getPermissionPrefix()).thenReturn("bskyblock.");
|
||||
when(ic.getLabel()).thenReturn("island");
|
||||
when(ic.getTopLabel()).thenReturn("island");
|
||||
when(ic.getWorld()).thenReturn(world);
|
||||
when(ic.getTopLabel()).thenReturn("bsb");
|
||||
|
||||
// IWM friendly name
|
||||
when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock");
|
||||
when(iwm.inWorld(any(World.class))).thenReturn(true);
|
||||
Optional<GameModeAddon> optionalAddon = Optional.of(gameModeAddon);
|
||||
when(iwm.getAddon(any())).thenReturn(optionalAddon);
|
||||
when(plugin.getIWM()).thenReturn(iwm);
|
||||
|
||||
// Game Mode Addon
|
||||
@NonNull
|
||||
Optional<CompositeCommand> optionalAdmin = Optional.of(ic);
|
||||
when(gameModeAddon.getAdminCommand()).thenReturn(optionalAdmin);
|
||||
|
||||
// World
|
||||
when(world.toString()).thenReturn("world");
|
||||
|
||||
// Player
|
||||
Player p = mock(Player.class);
|
||||
// Sometimes use Mockito.withSettings().verboseLogging()
|
||||
when(user.isOp()).thenReturn(false);
|
||||
uuid = UUID.randomUUID();
|
||||
when(user.getUniqueId()).thenReturn(uuid);
|
||||
when(user.getPlayer()).thenReturn(p);
|
||||
when(user.getName()).thenReturn("tastybento");
|
||||
when(user.getPermissionValue(anyString(), anyInt())).thenReturn(-1);
|
||||
when(user.isPlayer()).thenReturn(true);
|
||||
|
||||
// Mock item factory (for itemstacks)
|
||||
PowerMockito.mockStatic(Bukkit.class);
|
||||
ItemFactory itemFactory = mock(ItemFactory.class);
|
||||
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
|
||||
ItemMeta itemMeta = mock(ItemMeta.class);
|
||||
when(itemFactory.getItemMeta(any())).thenReturn(itemMeta);
|
||||
|
||||
// Addon & Challenge Manager
|
||||
when(addon.getChallengesManager()).thenReturn(chm);
|
||||
when(chm.getAllChallengeLevelStatus(any(), any())).thenReturn(Collections.emptyList());
|
||||
// Challenges exist
|
||||
when(chm.hasAnyChallengeData(any(World.class))).thenReturn(true);
|
||||
// Challenges
|
||||
when(chm.getChallenge(anyString())).thenReturn(challenge);
|
||||
List<String> nameList = Arrays.asList("world_maker", "world_placer", "bad_challenge_name", "world_breaker");
|
||||
when(chm.getAllChallengesNames(any())).thenReturn(nameList);
|
||||
|
||||
|
||||
// ChatColor
|
||||
PowerMockito.mockStatic(ChatColor.class);
|
||||
when(ChatColor.translateAlternateColorCodes(any(char.class), anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
|
||||
|
||||
// Settings
|
||||
when(addon.getChallengesSettings()).thenReturn(settings);
|
||||
when(settings.getVisibilityMode()).thenReturn(VisibilityMode.VISIBLE);
|
||||
|
||||
// Island
|
||||
when(plugin.getIslands()).thenReturn(im);
|
||||
when(im.getIsland(any(), any(User.class))).thenReturn(island);
|
||||
|
||||
// Utils
|
||||
PowerMockito.mockStatic(Utils.class);
|
||||
when(Utils.getGameMode(any())).thenReturn("world");
|
||||
|
||||
// Try to complete
|
||||
PowerMockito.mockStatic(TryToComplete.class);
|
||||
// All challenges are successful!
|
||||
when(TryToComplete.complete(any(), any(), any(), any(), anyString(), anyString(), anyInt())).thenReturn(true);
|
||||
|
||||
// Util
|
||||
PowerMockito.mockStatic(Util.class);
|
||||
when(Util.tabLimit(any(), any())).thenAnswer((Answer<List<String>>) invocation -> (List<String>)invocation.getArgument(0, List.class));
|
||||
|
||||
// Command under test
|
||||
cc = new CompleteChallengeCommand(addon, ic);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#CompleteChallengeCommand(world.bentobox.bentobox.api.addons.Addon, world.bentobox.bentobox.api.commands.CompositeCommand)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCompleteChallengeCommand() {
|
||||
assertEquals("complete", cc.getLabel());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#setup()}.
|
||||
*/
|
||||
@Test
|
||||
public void testSetup() {
|
||||
assertEquals("bskyblock.complete", cc.getPermission());
|
||||
assertEquals("challenges.commands.user.complete.parameters", cc.getParameters());
|
||||
assertEquals("challenges.commands.user.complete.description", cc.getDescription());
|
||||
assertTrue(cc.isOnlyPlayer());
|
||||
// No sub commands
|
||||
assertEquals(0, cc.getSubCommands(true).size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteUserStringListOfStringNoArgs() {
|
||||
assertFalse(cc.execute(user, "complete", Collections.emptyList()));
|
||||
verify(user).sendMessage(eq("challenges.errors.no-name"));
|
||||
verify(user).sendMessage(eq("commands.help.header"), eq(TextVariables.LABEL), eq("BSkyBlock"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteUserStringListOfStringUnknownChallenge() {
|
||||
when(chm.getChallenge(anyString())).thenReturn(null);
|
||||
assertFalse(cc.execute(user, "complete", Collections.singletonList("mychal")));
|
||||
verify(user).sendMessage(eq("challenges.errors.unknown-challenge"));
|
||||
verify(user).sendMessage(eq("commands.help.header"), eq(TextVariables.LABEL), eq("BSkyBlock"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteUserStringListOfStringKnownChallengeFail() {
|
||||
when(TryToComplete.complete(any(), any(), any(), any(), anyString(), anyString(), anyInt())).thenReturn(false);
|
||||
assertFalse(cc.execute(user, "complete", Collections.singletonList("mychal")));
|
||||
verify(user, never()).sendMessage(any());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteUserStringListOfStringKnownChallengeSuccess() {
|
||||
assertTrue(cc.execute(user, "complete", Collections.singletonList("mychal")));
|
||||
verify(user, never()).sendMessage(any());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteUserStringListOfStringKnownChallengeSuccessMultipleTimesNoPerm() {
|
||||
assertTrue(cc.execute(user, "complete", Arrays.asList("mychal", "5")));
|
||||
verify(user).sendMessage(eq("challenges.error.no-multiple-permission"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteUserStringListOfStringKnownChallengeSuccessMultipleTimesHasPerm() {
|
||||
when(user.hasPermission(anyString())).thenReturn(true);
|
||||
assertTrue(cc.execute(user, "complete", Arrays.asList("mychal", "5")));
|
||||
verify(user, never()).sendMessage(any());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testTabCompleteUserStringListOfStringNoArgs() {
|
||||
cc.tabComplete(user, "complete", Collections.emptyList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testTabCompleteUserStringListOfStringOneArg() {
|
||||
List<String> list = cc.tabComplete(user, "complete", Collections.singletonList("arg")).get();
|
||||
assertFalse(list.isEmpty());
|
||||
assertEquals("help", list.get(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testTabCompleteUserStringListOfStringTwoArgs() {
|
||||
List<String> list = cc.tabComplete(user, "complete", Arrays.asList("arg1", "arg2")).get();
|
||||
assertFalse(list.isEmpty());
|
||||
assertEquals("help", list.get(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testTabCompleteUserStringListOfStringThreeArgs() {
|
||||
List<String> list = cc.tabComplete(user, "complete", Arrays.asList("arg1", "arg2", "arg3")).get();
|
||||
assertFalse(list.isEmpty());
|
||||
assertEquals("maker", list.get(0));
|
||||
assertEquals("placer", list.get(1));
|
||||
assertEquals("breaker", list.get(2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testTabCompleteUserStringListOfStringFourArgs() {
|
||||
List<String> list = cc.tabComplete(user, "complete", Arrays.asList("arg1", "arg2", "arg3", "arg4")).get();
|
||||
assertTrue(list.isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testTabCompleteUserStringListOfStringFourArgsNumber() {
|
||||
List<String> list = cc.tabComplete(user, "complete", Arrays.asList("arg1", "arg2", "arg3", "4")).get();
|
||||
assertFalse(list.isEmpty());
|
||||
assertEquals("<number>", list.get(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testTabCompleteUserStringListOfStringFiveArgs() {
|
||||
List<String> list = cc.tabComplete(user, "complete", Arrays.asList("arg1", "arg2", "arg23", "arg4", "arg5")).get();
|
||||
assertFalse(list.isEmpty());
|
||||
assertEquals("help", list.get(0));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,392 @@
|
||||
package world.bentobox.challenges.panel.user;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.World.Environment;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemFactory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.reflect.Whitebox;
|
||||
|
||||
import world.bentobox.bentobox.BentoBox;
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
import world.bentobox.bentobox.managers.BlueprintsManager;
|
||||
import world.bentobox.bentobox.managers.IslandWorldManager;
|
||||
import world.bentobox.bentobox.managers.IslandsManager;
|
||||
import world.bentobox.challenges.ChallengesAddon;
|
||||
import world.bentobox.challenges.ChallengesManager;
|
||||
import world.bentobox.challenges.config.Settings;
|
||||
import world.bentobox.challenges.config.SettingsUtils.VisibilityMode;
|
||||
import world.bentobox.challenges.database.object.Challenge;
|
||||
import world.bentobox.challenges.database.object.Challenge.ChallengeType;
|
||||
import world.bentobox.challenges.database.object.ChallengeLevel;
|
||||
import world.bentobox.challenges.utils.LevelStatus;
|
||||
|
||||
/**
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({Bukkit.class, BentoBox.class, ChatColor.class})
|
||||
public class ChallengesGUITest {
|
||||
|
||||
@Mock
|
||||
private User user;
|
||||
@Mock
|
||||
private IslandsManager im;
|
||||
@Mock
|
||||
private IslandWorldManager iwm;
|
||||
@Mock
|
||||
private BentoBox plugin;
|
||||
@Mock
|
||||
private Settings settings;
|
||||
@Mock
|
||||
private CompositeCommand ic;
|
||||
@Mock
|
||||
private BlueprintsManager bpm;
|
||||
@Mock
|
||||
private Inventory inv;
|
||||
@Mock
|
||||
private ItemMeta meta;
|
||||
@Mock
|
||||
private ChallengesAddon addon;
|
||||
@Mock
|
||||
private World world;
|
||||
|
||||
private ChallengesGUI cg;
|
||||
|
||||
@Mock
|
||||
private ChallengesManager chm;
|
||||
private UUID uuid;
|
||||
@Mock
|
||||
private Challenge challenge1;
|
||||
@Mock
|
||||
private Challenge challenge2;
|
||||
@Mock
|
||||
private Challenge challenge3;
|
||||
@Mock
|
||||
private Challenge challenge4;
|
||||
@Mock
|
||||
private LevelStatus levelStatus;
|
||||
|
||||
private List<Challenge> freeChalls = new ArrayList<>();
|
||||
private List<Challenge> levelChalls = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
// Set up plugin
|
||||
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
|
||||
|
||||
PowerMockito.mockStatic(Bukkit.class);
|
||||
// Item Factory (needed for ItemStack)
|
||||
ItemFactory itemF = mock(ItemFactory.class);
|
||||
when(itemF.getItemMeta(Mockito.any())).thenReturn(meta);
|
||||
when(Bukkit.getItemFactory()).thenReturn(itemF);
|
||||
// Inventory
|
||||
when(Bukkit.createInventory(eq(null), anyInt(), anyString())).thenReturn(inv);
|
||||
|
||||
// Addon
|
||||
when(addon.getChallengesManager()).thenReturn(chm);
|
||||
// Levels
|
||||
when(levelStatus.isUnlocked()).thenReturn(true);
|
||||
ChallengeLevel level = mock(ChallengeLevel.class);
|
||||
when(level.getFriendlyName()).thenReturn("Novice");
|
||||
when(level.getUniqueId()).thenReturn("novice");
|
||||
when(level.getIcon()).thenReturn(new ItemStack(Material.BIRCH_BOAT));
|
||||
when(level.getLockedIcon()).thenReturn(new ItemStack(Material.DARK_OAK_BOAT));
|
||||
when(levelStatus.getLevel()).thenReturn(level);
|
||||
List<LevelStatus> levels = Collections.singletonList(levelStatus);
|
||||
when(chm.getAllChallengeLevelStatus(any(), any())).thenReturn(levels);
|
||||
when(chm.getChallengeLevelStatus(any(), any(), any())).thenReturn(levelStatus);
|
||||
// Challenges exist
|
||||
when(chm.hasAnyChallengeData(any(World.class))).thenReturn(true);
|
||||
|
||||
// Free challenges - have more than 18 so that the special processing kicks in
|
||||
when(chm.getFreeChallenges(any())).thenReturn(freeChalls);
|
||||
when(challenge1.isDeployed()).thenReturn(true);
|
||||
when(challenge2.isDeployed()).thenReturn(true);
|
||||
// 1 is repeatable, 2 is not
|
||||
when(challenge1.isRepeatable()).thenReturn(true);
|
||||
|
||||
// Level challenges
|
||||
when(chm.getLevelChallenges(any())).thenReturn(levelChalls);
|
||||
// ChatColor
|
||||
PowerMockito.mockStatic(ChatColor.class);
|
||||
when(ChatColor.translateAlternateColorCodes(any(char.class), anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
|
||||
// Settings
|
||||
when(addon.getChallengesSettings()).thenReturn(settings);
|
||||
when(settings.getVisibilityMode()).thenReturn(VisibilityMode.VISIBLE);
|
||||
when(settings.isFreeChallengesFirst()).thenReturn(false);
|
||||
when(settings.isRemoveCompleteOneTimeChallenges()).thenReturn(false);
|
||||
|
||||
// Player
|
||||
Player p = mock(Player.class);
|
||||
when(user.isOp()).thenReturn(false);
|
||||
uuid = UUID.randomUUID();
|
||||
when(user.getUniqueId()).thenReturn(uuid);
|
||||
when(user.getPlayer()).thenReturn(p);
|
||||
when(user.getName()).thenReturn("tastybento");
|
||||
when(user.getPermissionValue(anyString(), anyInt())).thenReturn(-1);
|
||||
when(user.isPlayer()).thenReturn(true);
|
||||
when(user.getTranslation(anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
|
||||
|
||||
cg = new ChallengesGUI(addon, world, user, "island", "bskyblock.");
|
||||
}
|
||||
|
||||
private void addLevelChallenges(int number) {
|
||||
for (int i = 0; i < number; i++) {
|
||||
Challenge c = mock(Challenge.class);
|
||||
when(c.isRepeatable()).thenReturn(true);
|
||||
when(c.getUniqueId()).thenReturn(String.valueOf(i) + "unique");
|
||||
when(c.getIcon()).thenReturn(new ItemStack(Material.EMERALD));
|
||||
when(c.getFriendlyName()).thenReturn(String.valueOf(i) + "name");
|
||||
when(c.getChallengeType()).thenReturn(ChallengeType.INVENTORY);
|
||||
when(c.getDescription()).thenReturn(Collections.singletonList("Description"));
|
||||
when(c.getEnvironment()).thenReturn(Collections.singleton(Environment.NORMAL));
|
||||
when(c.getLevel()).thenReturn("Novice");
|
||||
when(c.getRewardItems()).thenReturn(Collections.emptyList());
|
||||
when(c.isDeployed()).thenReturn(true);
|
||||
levelChalls.add(c);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void addFreeChallenges(int number) {
|
||||
for (int i = 0; i < number; i++) {
|
||||
Challenge c = mock(Challenge.class);
|
||||
when(c.getUniqueId()).thenReturn(String.valueOf(i) + "unique");
|
||||
when(c.getIcon()).thenReturn(new ItemStack(Material.DIAMOND));
|
||||
when(c.getFriendlyName()).thenReturn(String.valueOf(i) + "name");
|
||||
when(c.getChallengeType()).thenReturn(ChallengeType.INVENTORY);
|
||||
when(c.getDescription()).thenReturn(Collections.singletonList("Description"));
|
||||
when(c.getEnvironment()).thenReturn(Collections.singleton(Environment.NORMAL));
|
||||
when(c.getLevel()).thenReturn("Novice");
|
||||
when(c.getRewardItems()).thenReturn(Collections.emptyList());
|
||||
when(c.isDeployed()).thenReturn(true);
|
||||
when(c.isRepeatable()).thenReturn(true);
|
||||
freeChalls.add(c);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.panel.user.ChallengesGUI#build()}.
|
||||
*/
|
||||
@Test
|
||||
public void testBuildNoChallenges() {
|
||||
when(chm.hasAnyChallengeData(any(World.class))).thenReturn(false);
|
||||
cg.build();
|
||||
verify(addon).logError("There are no challenges set up!");
|
||||
verify(user).sendMessage("challenges.errors.no-challenges");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.panel.user.ChallengesGUI#build()}.
|
||||
*/
|
||||
@Test
|
||||
public void testBuild0Free0LevelChallenge() {
|
||||
when(settings.isFreeChallengesFirst()).thenReturn(true);
|
||||
cg.build();
|
||||
verify(user).getTranslation("challenges.gui.title.challenges");
|
||||
ArgumentCaptor<Integer> argument = ArgumentCaptor.forClass(Integer.class);
|
||||
ArgumentCaptor<ItemStack> argument2 = ArgumentCaptor.forClass(ItemStack.class);
|
||||
verify(inv).setItem(argument.capture(), argument2.capture());
|
||||
// Level
|
||||
assertTrue(argument.getAllValues().get(0) == 0);
|
||||
assertEquals(Material.BIRCH_BOAT, argument2.getAllValues().get(0).getType());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.panel.user.ChallengesGUI#build()}.
|
||||
*/
|
||||
@Test
|
||||
public void testBuild10Free10LevelChallenge() {
|
||||
addFreeChallenges(10);
|
||||
addLevelChallenges(10);
|
||||
when(settings.isFreeChallengesFirst()).thenReturn(true);
|
||||
cg.build();
|
||||
verify(user).getTranslation("challenges.gui.title.challenges");
|
||||
ArgumentCaptor<Integer> argument = ArgumentCaptor.forClass(Integer.class);
|
||||
ArgumentCaptor<ItemStack> argument2 = ArgumentCaptor.forClass(ItemStack.class);
|
||||
verify(inv, times(21)).setItem(argument.capture(), argument2.capture());
|
||||
List<ItemStack> values = argument2.getAllValues();
|
||||
// Free challenges
|
||||
for (int i = 0; i < 10; i++) {
|
||||
assertEquals("Failed on " + i, Material.DIAMOND, values.get(i).getType());
|
||||
}
|
||||
// Level challenges
|
||||
for (int i = 11; i < 20; i++) {
|
||||
assertEquals("Failed on " + i, Material.EMERALD, values.get(i).getType());
|
||||
}
|
||||
// Level icons
|
||||
assertTrue(argument.getAllValues().get(20) == 36);
|
||||
assertEquals(Material.BIRCH_BOAT, argument2.getAllValues().get(20).getType());
|
||||
|
||||
}
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.panel.user.ChallengesGUI#build()}.
|
||||
*/
|
||||
@Test
|
||||
public void testBuild20Free20LevelChallenge() {
|
||||
addFreeChallenges(20);
|
||||
addLevelChallenges(20);
|
||||
when(settings.isFreeChallengesFirst()).thenReturn(true);
|
||||
cg.build();
|
||||
verify(user).getTranslation("challenges.gui.title.challenges");
|
||||
ArgumentCaptor<Integer> argument = ArgumentCaptor.forClass(Integer.class);
|
||||
ArgumentCaptor<ItemStack> argument2 = ArgumentCaptor.forClass(ItemStack.class);
|
||||
verify(inv, times(38)).setItem(argument.capture(), argument2.capture());
|
||||
List<ItemStack> values = argument2.getAllValues();
|
||||
// Free challenges
|
||||
for (int i = 0; i < 18; i++) {
|
||||
assertEquals("Failed on " + i, Material.DIAMOND, values.get(i).getType());
|
||||
}
|
||||
// Next page
|
||||
assertTrue(argument.getAllValues().get(18) == 18);
|
||||
assertEquals(Material.OAK_SIGN, argument2.getAllValues().get(18).getType());
|
||||
// Level challenges
|
||||
for (int i = 19; i < 37; i++) {
|
||||
assertEquals("Failed on " + i, Material.EMERALD, values.get(i).getType());
|
||||
}
|
||||
// Next page
|
||||
assertTrue(argument.getAllValues().get(37) == 45);
|
||||
assertEquals(Material.OAK_SIGN, argument2.getAllValues().get(37).getType());
|
||||
}
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.panel.user.ChallengesGUI#build()}.
|
||||
*/
|
||||
@Test
|
||||
public void testBuildFreeChallenges10Free20LevelChallenge() {
|
||||
addFreeChallenges(10);
|
||||
addLevelChallenges(20);
|
||||
when(settings.isFreeChallengesFirst()).thenReturn(true);
|
||||
cg.build();
|
||||
verify(user).getTranslation("challenges.gui.title.challenges");
|
||||
ArgumentCaptor<Integer> argument = ArgumentCaptor.forClass(Integer.class);
|
||||
ArgumentCaptor<ItemStack> argument2 = ArgumentCaptor.forClass(ItemStack.class);
|
||||
verify(inv, times(30)).setItem(argument.capture(), argument2.capture());
|
||||
List<ItemStack> values = argument2.getAllValues();
|
||||
// Free challenges
|
||||
for (int i = 0; i < 10; i++) {
|
||||
assertEquals("Failed on " + i, Material.DIAMOND, values.get(i).getType());
|
||||
}
|
||||
// Level challenges
|
||||
for (int i = 10; i < 27; i++) {
|
||||
assertEquals("Failed on " + i, Material.EMERALD, values.get(i).getType());
|
||||
}
|
||||
// Next page
|
||||
assertTrue(argument.getAllValues().get(28) == 36);
|
||||
assertEquals(Material.OAK_SIGN, argument2.getAllValues().get(28).getType());
|
||||
// Level
|
||||
assertTrue(argument.getAllValues().get(29) == 45);
|
||||
assertEquals(Material.BIRCH_BOAT, argument2.getAllValues().get(29).getType());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.panel.user.ChallengesGUI#build()}.
|
||||
*/
|
||||
@Test
|
||||
public void testBuildFreeChallenges20Free10LevelChallenge() {
|
||||
addFreeChallenges(20);
|
||||
addLevelChallenges(10);
|
||||
when(settings.isFreeChallengesFirst()).thenReturn(true);
|
||||
cg.build();
|
||||
verify(user).getTranslation("challenges.gui.title.challenges");
|
||||
ArgumentCaptor<Integer> argument = ArgumentCaptor.forClass(Integer.class);
|
||||
ArgumentCaptor<ItemStack> argument2 = ArgumentCaptor.forClass(ItemStack.class);
|
||||
verify(inv, times(30)).setItem(argument.capture(), argument2.capture());
|
||||
|
||||
List<ItemStack> values = argument2.getAllValues();
|
||||
// Free challenges
|
||||
for (int i = 0; i < 18; i++) {
|
||||
assertEquals("Failed on " + i, Material.DIAMOND, values.get(i).getType());
|
||||
}
|
||||
// Next page
|
||||
assertTrue(argument.getAllValues().get(18) == 18);
|
||||
assertEquals(Material.OAK_SIGN, argument2.getAllValues().get(18).getType());
|
||||
// Level challenges
|
||||
for (int i = 19; i < 29; i++) {
|
||||
assertEquals("Failed on " + i, Material.EMERALD, values.get(i).getType());
|
||||
}
|
||||
|
||||
// Level
|
||||
assertTrue(argument.getAllValues().get(29) == 45);
|
||||
assertEquals(Material.BIRCH_BOAT, argument2.getAllValues().get(29).getType());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.panel.user.ChallengesGUI#build()}.
|
||||
*/
|
||||
@Test
|
||||
public void testBuildFreeChallengesLast20Free10LevelChallenge() {
|
||||
addFreeChallenges(20);
|
||||
addLevelChallenges(10);
|
||||
when(settings.isFreeChallengesFirst()).thenReturn(false);
|
||||
cg.build();
|
||||
verify(user).getTranslation("challenges.gui.title.challenges");
|
||||
ArgumentCaptor<Integer> argument = ArgumentCaptor.forClass(Integer.class);
|
||||
ArgumentCaptor<ItemStack> argument2 = ArgumentCaptor.forClass(ItemStack.class);
|
||||
verify(inv, times(30)).setItem(argument.capture(), argument2.capture());
|
||||
List<ItemStack> values = argument2.getAllValues();
|
||||
|
||||
// Level challenges
|
||||
for (int i = 0; i < 10; i++) {
|
||||
assertEquals("Failed on " + i, Material.EMERALD, values.get(i).getType());
|
||||
}
|
||||
// Next page
|
||||
assertTrue(argument.getAllValues().get(10) == 18);
|
||||
assertEquals(Material.BIRCH_BOAT, argument2.getAllValues().get(10).getType());
|
||||
// Free challenges
|
||||
for (int i = 11; i < 29; i++) {
|
||||
assertEquals("Failed on " + i, Material.DIAMOND, values.get(i).getType());
|
||||
}
|
||||
|
||||
// Level
|
||||
assertTrue(argument.getAllValues().get(29) == 45);
|
||||
assertEquals(Material.OAK_SIGN, argument2.getAllValues().get(29).getType());
|
||||
}
|
||||
|
||||
}
|
@ -1,233 +1,618 @@
|
||||
package world.bentobox.challenges.tasks;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.World.Environment;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemFactory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
import org.bukkit.util.BoundingBox;
|
||||
import org.eclipse.jdt.annotation.NonNull;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.reflect.Whitebox;
|
||||
|
||||
import world.bentobox.bentobox.BentoBox;
|
||||
import world.bentobox.bentobox.api.addons.AddonDescription;
|
||||
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
||||
import world.bentobox.bentobox.api.configuration.WorldSettings;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
import world.bentobox.bentobox.database.objects.Island;
|
||||
import world.bentobox.bentobox.managers.AddonsManager;
|
||||
import world.bentobox.bentobox.managers.IslandWorldManager;
|
||||
import world.bentobox.bentobox.managers.IslandsManager;
|
||||
import world.bentobox.bentobox.managers.LocalesManager;
|
||||
import world.bentobox.bentobox.managers.PlaceholdersManager;
|
||||
import world.bentobox.bentobox.util.Util;
|
||||
import world.bentobox.challenges.ChallengesAddon;
|
||||
import world.bentobox.challenges.ChallengesManager;
|
||||
import world.bentobox.challenges.config.Settings;
|
||||
import world.bentobox.challenges.database.object.Challenge;
|
||||
import world.bentobox.challenges.database.object.Challenge.ChallengeType;
|
||||
import world.bentobox.challenges.database.object.ChallengeLevel;
|
||||
import world.bentobox.challenges.database.object.requirements.InventoryRequirements;
|
||||
import world.bentobox.challenges.database.object.requirements.IslandRequirements;
|
||||
import world.bentobox.challenges.tasks.TryToComplete.ChallengeResult;
|
||||
import world.bentobox.challenges.utils.Utils;
|
||||
|
||||
|
||||
/**
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({ Bukkit.class})
|
||||
@PrepareForTest({Bukkit.class, BentoBox.class, Util.class, Utils.class})
|
||||
public class TryToCompleteTest {
|
||||
|
||||
private User user;
|
||||
ItemStack[] stacks = { new ItemStack(Material.PAPER, 32),
|
||||
new ItemStack(Material.ACACIA_BOAT),
|
||||
null,
|
||||
null,
|
||||
new ItemStack(Material.CACTUS, 32),
|
||||
new ItemStack(Material.CACTUS, 32),
|
||||
new ItemStack(Material.CACTUS, 32),
|
||||
new ItemStack(Material.BRICK_STAIRS, 64),
|
||||
new ItemStack(Material.BRICK_STAIRS, 64),
|
||||
new ItemStack(Material.BRICK_STAIRS, 5),
|
||||
new ItemStack(Material.GOLD_BLOCK, 32)
|
||||
};
|
||||
List<ItemStack> required;
|
||||
// Constants
|
||||
private static final String GAME_MODE_NAME = "BSkyBlock";
|
||||
private static final String[] NAMES = {"adam", "ben", "cara", "dave", "ed", "frank", "freddy", "george", "harry", "ian", "joe"};
|
||||
|
||||
private TryToComplete ttc;
|
||||
private Challenge challenge;
|
||||
private @NonNull ChallengeLevel level;
|
||||
@Mock
|
||||
private ChallengesAddon addon;
|
||||
private PlayerInventory inv;
|
||||
@Mock
|
||||
private User user;
|
||||
@Mock
|
||||
private World world;
|
||||
private String topLabel = "island";
|
||||
private String permissionPrefix = "perm.";
|
||||
private String cName;
|
||||
private String levelName;
|
||||
@Mock
|
||||
private ChallengesManager cm;
|
||||
@Mock
|
||||
private BentoBox plugin;
|
||||
@Mock
|
||||
private GameModeAddon gameMode;
|
||||
@Mock
|
||||
private AddonsManager am;
|
||||
@Mock
|
||||
private IslandsManager im;
|
||||
@Mock
|
||||
private Island island;
|
||||
@Mock
|
||||
private Player player;
|
||||
@Mock
|
||||
private Settings settings;
|
||||
@Mock
|
||||
private WorldSettings mySettings;
|
||||
private Map<String, Boolean> map;
|
||||
@Mock
|
||||
private @Nullable PlayerInventory inv;
|
||||
private ItemStack[] contents = {};
|
||||
@Mock
|
||||
private BoundingBox bb;
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
Server server = mock(Server.class);
|
||||
PowerMockito.mockStatic(Bukkit.class);
|
||||
when(Bukkit.getServer()).thenReturn(server);
|
||||
when(Bukkit.getBukkitVersion()).thenReturn("1.13.2");
|
||||
// Set up plugin
|
||||
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
|
||||
when(addon.getPlugin()).thenReturn(plugin);
|
||||
// World
|
||||
when(user.getWorld()).thenReturn(world);
|
||||
when(world.getEnvironment()).thenReturn(Environment.NORMAL);
|
||||
|
||||
user = mock(User.class);
|
||||
inv = mock(PlayerInventory.class);
|
||||
when(inv.getContents()).thenReturn(stacks);
|
||||
// Addons manager
|
||||
when(plugin.getAddonsManager()).thenReturn(am);
|
||||
// One game mode
|
||||
when(am.getGameModeAddons()).thenReturn(Collections.singletonList(gameMode));
|
||||
AddonDescription desc2 = new AddonDescription.Builder("bentobox", GAME_MODE_NAME, "1.3").description("test").authors("tasty").build();
|
||||
when(gameMode.getDescription()).thenReturn(desc2);
|
||||
|
||||
// Challenge Level
|
||||
level = new ChallengeLevel();
|
||||
levelName = GAME_MODE_NAME + "_novice";
|
||||
level.setUniqueId(levelName);
|
||||
level.setFriendlyName("Novice");
|
||||
// Set up challenge
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
challenge = new Challenge();
|
||||
challenge.setUniqueId(GAME_MODE_NAME + "_" + uuid);
|
||||
challenge.setFriendlyName("name");
|
||||
challenge.setLevel(GAME_MODE_NAME + "_novice");
|
||||
challenge.setDescription(Collections.singletonList("A description"));
|
||||
challenge.setChallengeType(ChallengeType.INVENTORY);
|
||||
challenge.setDeployed(true);
|
||||
challenge.setIcon(new ItemStack(Material.EMERALD));
|
||||
challenge.setEnvironment(Collections.singleton(World.Environment.NORMAL));
|
||||
challenge.setLevel(levelName);
|
||||
challenge.setRepeatable(true);
|
||||
challenge.setMaxTimes(10);
|
||||
InventoryRequirements req = new InventoryRequirements();
|
||||
|
||||
challenge.setRequirements(req);
|
||||
// Util
|
||||
PowerMockito.mockStatic(Util.class);
|
||||
when(Util.getWorld(any())).thenReturn(world);
|
||||
when(Util.prettifyText(anyString())).thenCallRealMethod();
|
||||
|
||||
// Island World Manager
|
||||
IslandWorldManager iwm = mock(IslandWorldManager.class);
|
||||
when(plugin.getIWM()).thenReturn(iwm);
|
||||
Optional<GameModeAddon> optionalGameMode = Optional.of(gameMode);
|
||||
when(iwm.getAddon(any())).thenReturn(optionalGameMode);
|
||||
when(iwm.getIslandDistance(any())).thenReturn(400);
|
||||
|
||||
// Island Manager
|
||||
when(addon.getIslands()).thenReturn(im);
|
||||
Optional<Island> opIsland = Optional.of(island);
|
||||
when(im.getIslandAt(any())).thenReturn(opIsland);
|
||||
when(im.getIsland(any(), any(User.class))).thenReturn(island);
|
||||
// Player is on island
|
||||
when(im.locationIsOnIsland(any(), any())).thenReturn(true);
|
||||
// Island flags - everything is allowed by default
|
||||
when(island.isAllowed(any(), any())).thenReturn(true);
|
||||
// Island
|
||||
|
||||
@Nullable
|
||||
Location loc = mock(Location.class);
|
||||
when(loc.toString()).thenReturn("center");
|
||||
when(island.getCenter()).thenReturn(loc);
|
||||
|
||||
// Challenges Manager
|
||||
when(addon.getChallengesManager()).thenReturn(cm);
|
||||
// All levels unlocked by default
|
||||
when(cm.isLevelUnlocked(any(), any(), any())).thenReturn(true);
|
||||
// Player has done this challenge 3 times (default max is 10)
|
||||
when(cm.getChallengeTimes(any(), any(), any(Challenge.class))).thenReturn(3L);
|
||||
|
||||
// User has all perms by default
|
||||
when(user.hasPermission(anyString())).thenReturn(true);
|
||||
when(user.getPlayer()).thenReturn(player);
|
||||
when(user.getTranslation(Mockito.anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
|
||||
when(user.getName()).thenReturn("tastybento");
|
||||
@Nullable
|
||||
Location userLoc = mock(Location.class);
|
||||
when(userLoc.toString()).thenReturn("location");
|
||||
when(user.getLocation()).thenReturn(userLoc);
|
||||
when(user.getInventory()).thenReturn(inv);
|
||||
addon = mock(ChallengesAddon.class);
|
||||
required = new ArrayList<>();
|
||||
when(inv.getContents()).thenReturn(contents);
|
||||
when(player.getBoundingBox()).thenReturn(bb);
|
||||
when(bb.clone()).thenReturn(bb);
|
||||
when(bb.toString()).thenReturn("BoundingBox");
|
||||
// Locales
|
||||
User.setPlugin(plugin);
|
||||
LocalesManager lm = mock(LocalesManager.class);
|
||||
when(plugin.getLocalesManager()).thenReturn(lm);
|
||||
when(lm.get(any(), any())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
|
||||
PlaceholdersManager phm = mock(PlaceholdersManager.class);
|
||||
when(plugin.getPlaceholdersManager()).thenReturn(phm);
|
||||
when(phm.replacePlaceholders(any(), any())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
|
||||
|
||||
// Survival by default
|
||||
when(player.getGameMode()).thenReturn(GameMode.SURVIVAL);
|
||||
|
||||
// Addon
|
||||
when(addon.getChallengesSettings()).thenReturn(settings);
|
||||
when(settings.isBroadcastMessages()).thenReturn(true);
|
||||
|
||||
// Bukkit - online players
|
||||
Map<UUID, String> online = new HashMap<>();
|
||||
|
||||
Set<Player> onlinePlayers = new HashSet<>();
|
||||
for (int j = 0; j < NAMES.length; j++) {
|
||||
Player p1 = mock(Player.class);
|
||||
UUID uuid2 = UUID.randomUUID();
|
||||
when(p1.getUniqueId()).thenReturn(uuid2);
|
||||
when(p1.getName()).thenReturn(NAMES[j]);
|
||||
online.put(uuid2, NAMES[j]);
|
||||
onlinePlayers.add(p1);
|
||||
}
|
||||
PowerMockito.mockStatic(Bukkit.class);
|
||||
when(Bukkit.getOnlinePlayers()).then((Answer<Set<Player>>) invocation -> onlinePlayers);
|
||||
|
||||
// World settings
|
||||
map = new HashMap<>();
|
||||
when(mySettings.getWorldFlags()).thenReturn(map);
|
||||
when(iwm.getWorldSettings(any())).thenReturn(mySettings);
|
||||
ChallengesAddon.CHALLENGES_WORLD_PROTECTION.setSetting(world, true);
|
||||
|
||||
// ItemFactory
|
||||
ItemFactory itemFactory = mock(ItemFactory.class);
|
||||
when(server.getItemFactory()).thenReturn(itemFactory);
|
||||
|
||||
// Test will not work with items that has meta data.
|
||||
when(itemFactory.getItemMeta(any())).thenReturn(null);
|
||||
when(itemFactory.equals(null, null)).thenReturn(true);
|
||||
|
||||
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
|
||||
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link TryToComplete#removeItems(java.util.List, int)}.
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@Test
|
||||
public void testRemoveItemsSuccess() {
|
||||
Material requiredMaterial = Material.PAPER;
|
||||
int requiredQuantity = 21;
|
||||
|
||||
this.required.add(new ItemStack(requiredMaterial, requiredQuantity));
|
||||
TryToComplete x = new TryToComplete(this.addon);
|
||||
x.user(this.user);
|
||||
Map<ItemStack, Integer> removed = x.removeItems(this.required, 1);
|
||||
|
||||
assertEquals((int) removed.getOrDefault(new ItemStack(requiredMaterial, 1), 0), requiredQuantity);
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link TryToComplete#removeItems(java.util.List, int)}.
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#TryToComplete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testRemoveItemsMax() {
|
||||
Material requiredMaterial = Material.PAPER;
|
||||
int requiredQuantity = 50;
|
||||
public void testTryToCompleteChallengesAddonUserChallengeWorldStringString() {
|
||||
ttc = new TryToComplete(addon,
|
||||
user,
|
||||
challenge,
|
||||
world,
|
||||
topLabel,
|
||||
permissionPrefix);
|
||||
verify(addon).getChallengesManager();
|
||||
|
||||
this.required.add(new ItemStack(requiredMaterial, requiredQuantity));
|
||||
TryToComplete x = new TryToComplete(this.addon);
|
||||
x.user(this.user);
|
||||
Map<ItemStack, Integer> removed = x.removeItems(this.required, 1);
|
||||
|
||||
assertNotEquals((int) removed.getOrDefault(new ItemStack(requiredMaterial, 1), 0), requiredQuantity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link TryToComplete#removeItems(java.util.List, int)}.
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testRemoveItemsZero() {
|
||||
Material requiredMaterial = Material.PAPER;
|
||||
int requiredQuantity = 0;
|
||||
|
||||
this.required.add(new ItemStack(requiredMaterial, requiredQuantity));
|
||||
TryToComplete x = new TryToComplete(this.addon);
|
||||
x.user(this.user);
|
||||
Map<ItemStack, Integer> removed = x.removeItems(this.required, 1);
|
||||
|
||||
assertTrue(removed.isEmpty());
|
||||
public void testCompleteChallengesAddonUserChallengeWorldStringStringNotDeployed() {
|
||||
challenge.setDeployed(false);
|
||||
assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix));
|
||||
verify(user).sendMessage("challenges.errors.not-deployed");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link TryToComplete#removeItems(java.util.List, int)}.
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testRemoveItemsSuccessMultiple() {
|
||||
required.add(new ItemStack(Material.PAPER, 11));
|
||||
required.add(new ItemStack(Material.PAPER, 5));
|
||||
required.add(new ItemStack(Material.PAPER, 5));
|
||||
TryToComplete x = new TryToComplete(addon);
|
||||
x.user(user);
|
||||
Map<ItemStack, Integer> removed = x.removeItems(required, 1);
|
||||
|
||||
assertEquals((int) removed.getOrDefault(new ItemStack(Material.PAPER, 1), 0), 21);
|
||||
public void testCompleteChallengesAddonUserChallengeWorldStringStringWrongWorld() {
|
||||
challenge.setUniqueId("test");
|
||||
assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix));
|
||||
verify(user).sendMessage("general.errors.wrong-world");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link TryToComplete#removeItems(java.util.List, int)}.
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testRemoveItemsSuccessMultipleOther() {
|
||||
required.add(new ItemStack(Material.CACTUS, 5));
|
||||
required.add(new ItemStack(Material.PAPER, 11));
|
||||
required.add(new ItemStack(Material.PAPER, 5));
|
||||
required.add(new ItemStack(Material.PAPER, 5));
|
||||
required.add(new ItemStack(Material.CACTUS, 5));
|
||||
TryToComplete x = new TryToComplete(addon);
|
||||
x.user(user);
|
||||
Map<ItemStack, Integer> removed = x.removeItems(required, 1);
|
||||
|
||||
assertEquals((int) removed.getOrDefault(new ItemStack(Material.PAPER, 1), 0), 21);
|
||||
assertEquals((int) removed.getOrDefault(new ItemStack(Material.CACTUS, 1), 0), 10);
|
||||
public void testCompleteChallengesAddonUserChallengeWorldStringStringNotOnIsland() {
|
||||
ChallengesAddon.CHALLENGES_WORLD_PROTECTION.setSetting(world, true);
|
||||
when(im.locationIsOnIsland(any(Player.class), any(Location.class))).thenReturn(false);
|
||||
assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix));
|
||||
verify(user).sendMessage("challenges.errors.not-on-island");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link TryToComplete#removeItems(java.util.List, int)}.
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testRemoveItemsMultipleOtherFail() {
|
||||
required.add(new ItemStack(Material.ACACIA_FENCE, 5));
|
||||
required.add(new ItemStack(Material.ARROW, 11));
|
||||
required.add(new ItemStack(Material.STONE, 5));
|
||||
required.add(new ItemStack(Material.BAKED_POTATO, 5));
|
||||
required.add(new ItemStack(Material.GHAST_SPAWN_EGG, 5));
|
||||
TryToComplete x = new TryToComplete(addon);
|
||||
x.user(user);
|
||||
Map<ItemStack, Integer> removed = x.removeItems(required, 1);
|
||||
assertTrue(removed.isEmpty());
|
||||
public void testCompleteChallengesAddonUserChallengeWorldStringStringNotOnIslandButOk() {
|
||||
ChallengesAddon.CHALLENGES_WORLD_PROTECTION.setSetting(world, false);
|
||||
when(im.locationIsOnIsland(any(Player.class), any(Location.class))).thenReturn(false);
|
||||
assertTrue(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix));
|
||||
verify(user).sendMessage("challenges.messages.you-completed-challenge", "[value]", "name");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link TryToComplete#removeItems(java.util.List, int)}.
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testRemoveItemsFail() {
|
||||
ItemStack input = new ItemStack(Material.GOLD_BLOCK, 55);
|
||||
required.add(input);
|
||||
TryToComplete x = new TryToComplete(addon);
|
||||
x.user(user);
|
||||
Map<ItemStack, Integer> removed = x.removeItems(required, 1);
|
||||
|
||||
// It will remove 32, but not any more
|
||||
assertEquals((int) removed.getOrDefault(new ItemStack(Material.GOLD_BLOCK, 1), 0), 32);
|
||||
|
||||
// An error will be thrown
|
||||
Mockito.verify(addon, Mockito.times(1)).logError(Mockito.anyString());
|
||||
public void testCompleteChallengesAddonUserChallengeWorldStringStringLevelNotUnlocked() {
|
||||
when(cm.isLevelUnlocked(any(), any(), any())).thenReturn(false);
|
||||
assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix));
|
||||
verify(user).sendMessage("challenges.errors.challenge-level-not-available");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Test method for {@link TryToComplete#removeItems(java.util.List, int)}.
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testRequireTwoStacks() {
|
||||
required.add(new ItemStack(Material.BRICK_STAIRS, 64));
|
||||
required.add(new ItemStack(Material.BRICK_STAIRS, 64));
|
||||
|
||||
TryToComplete x = new TryToComplete(addon);
|
||||
x.user(user);
|
||||
Map<ItemStack, Integer> removed = x.removeItems(required, 1);
|
||||
|
||||
// It should remove both stacks
|
||||
assertEquals((int) removed.getOrDefault(new ItemStack(Material.BRICK_STAIRS, 1), 0), 128);
|
||||
public void testCompleteChallengesAddonUserChallengeWorldStringStringNotRepeatable() {
|
||||
challenge.setRepeatable(false);
|
||||
when(cm.isChallengeComplete(any(User.class), any(), any())).thenReturn(true);
|
||||
assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix));
|
||||
verify(user).sendMessage("challenges.errors.not-repeatable");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test method for {@link TryToComplete#removeItems(java.util.List, int)}.
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testFactorStacks() {
|
||||
required.add(new ItemStack(Material.BRICK_STAIRS, 32));
|
||||
|
||||
TryToComplete x = new TryToComplete(addon);
|
||||
x.user(user);
|
||||
Map<ItemStack, Integer> removed = x.removeItems(required, 4);
|
||||
|
||||
// It should remove both stacks
|
||||
assertEquals((int) removed.getOrDefault(new ItemStack(Material.BRICK_STAIRS, 1), 0), 128);
|
||||
}
|
||||
public void testCompleteChallengesAddonUserChallengeWorldStringStringNotRepeatableFirstTime() {
|
||||
challenge.setRepeatable(false);
|
||||
challenge.setMaxTimes(0);
|
||||
when(cm.getChallengeTimes(any(), any(), any(Challenge.class))).thenReturn(0L);
|
||||
assertTrue(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix));
|
||||
verify(user).sendMessage("challenges.messages.you-completed-challenge", "[value]", "name");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCompleteChallengesAddonUserChallengeWorldStringStringNoRank() {
|
||||
when(island.isAllowed(any(), any())).thenReturn(false);
|
||||
assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix));
|
||||
verify(user).sendMessage("challenges.errors.no-rank");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String, int)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCompleteChallengesAddonUserChallengeWorldStringStringIntZero() {
|
||||
assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix, 0));
|
||||
verify(user).sendMessage("challenges.errors.not-valid-integer");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String, int)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCompleteChallengesAddonUserChallengeWorldStringStringIntNegative() {
|
||||
assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix, -10));
|
||||
verify(user).sendMessage("challenges.errors.not-valid-integer");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String, int)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCompleteChallengesAddonUserChallengeWorldStringStringIntPositiveWrongEnvinonment() {
|
||||
challenge.setEnvironment(Collections.singleton(Environment.NETHER));
|
||||
assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix, 100));
|
||||
verify(user).sendMessage("challenges.errors.wrong-environment");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String, int)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCompleteChallengesAddonUserChallengeWorldStringStringIntPositiveNoPerm() {
|
||||
InventoryRequirements req = new InventoryRequirements();
|
||||
req.setRequiredPermissions(Collections.singleton("perm-you-dont-have"));
|
||||
when(user.hasPermission(anyString())).thenReturn(false);
|
||||
challenge.setRequirements(req);
|
||||
assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix, 100));
|
||||
verify(user).sendMessage("general.errors.no-permission");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCompleteChallengesAddonUserChallengeWorldStringStringSuccess() {
|
||||
assertTrue(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix));
|
||||
verify(user).sendMessage("challenges.messages.you-completed-challenge", "[value]", "name");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCompleteChallengesAddonUserChallengeWorldStringStringSuccessSingleReq() {
|
||||
InventoryRequirements req = new InventoryRequirements();
|
||||
req.setRequiredItems(Collections.singletonList(new ItemStack(Material.EMERALD_BLOCK)));
|
||||
challenge.setRequirements(req);
|
||||
assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix));
|
||||
verify(user).sendMessage("challenges.errors.not-enough-items", "[items]", "Emerald Block");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCompleteChallengesAddonUserChallengeWorldStringStringSuccessMultipleReq() {
|
||||
|
||||
InventoryRequirements req = new InventoryRequirements();
|
||||
ItemStack itemStackMock = mock(ItemStack.class);
|
||||
when(itemStackMock.getAmount()).thenReturn(3);
|
||||
when(itemStackMock.getType()).thenReturn(Material.EMERALD_BLOCK);
|
||||
when(itemStackMock.clone()).thenReturn(itemStackMock);
|
||||
|
||||
ItemStack itemStackMock2 = mock(ItemStack.class);
|
||||
when(itemStackMock2.getType()).thenReturn(Material.ENCHANTED_BOOK);
|
||||
when(itemStackMock2.getAmount()).thenReturn(10);
|
||||
when(itemStackMock2.clone()).thenReturn(itemStackMock2);
|
||||
|
||||
ItemStack itemStackMock3 = mock(ItemStack.class);
|
||||
when(itemStackMock3.getType()).thenReturn(Material.EMERALD_BLOCK);
|
||||
when(itemStackMock3.getAmount()).thenReturn(15);
|
||||
when(itemStackMock3.clone()).thenReturn(itemStackMock3);
|
||||
// itemStackMock and 3 are same type
|
||||
when(itemStackMock3.isSimilar(eq(itemStackMock))).thenReturn(true);
|
||||
when(itemStackMock.isSimilar(eq(itemStackMock3))).thenReturn(true);
|
||||
|
||||
req.setRequiredItems(Arrays.asList(itemStackMock , itemStackMock2));
|
||||
challenge.setRequirements(req);
|
||||
ItemStack[] newContents = {itemStackMock3};
|
||||
when(inv.getContents()).thenReturn(newContents);
|
||||
|
||||
assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix));
|
||||
// Sufficient emerald blocks
|
||||
verify(user, never()).sendMessage("challenges.errors.not-enough-items", "[items]", "Emerald Block");
|
||||
// Not enough books
|
||||
verify(user).sendMessage("challenges.errors.not-enough-items", "[items]", "Enchanted Book");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCompleteChallengesAddonUserChallengeWorldStringStringSuccessCreative() {
|
||||
when(player.getGameMode()).thenReturn(GameMode.CREATIVE);
|
||||
assertTrue(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix));
|
||||
verify(user).sendMessage("challenges.messages.you-completed-challenge", "[value]", "name");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCompleteChallengesAddonUserChallengeWorldStringStringIslandBBTooLarge() {
|
||||
challenge.setChallengeType(ChallengeType.ISLAND);
|
||||
IslandRequirements req = new IslandRequirements();
|
||||
req.setSearchRadius(1);
|
||||
challenge.setRequirements(req);
|
||||
// Trigger big bounding box error
|
||||
when(bb.getWidthX()).thenReturn(50000D);
|
||||
assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix));
|
||||
verify(addon).logError("BoundingBox is larger than SearchRadius. | BoundingBox: BoundingBox | Search Distance: 1 | Location: location | Center: center | Range: 0");
|
||||
verify(bb).expand(1);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCompleteChallengesAddonUserChallengeWorldStringStringIslandSuccessNoEntities() {
|
||||
challenge.setChallengeType(ChallengeType.ISLAND);
|
||||
IslandRequirements req = new IslandRequirements();
|
||||
req.setSearchRadius(1);
|
||||
challenge.setRequirements(req);
|
||||
assertTrue(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix));
|
||||
verify(user).sendMessage("challenges.messages.you-completed-challenge", "[value]", "name");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCompleteChallengesAddonUserChallengeWorldStringStringIslandFailEntities() {
|
||||
challenge.setChallengeType(ChallengeType.ISLAND);
|
||||
IslandRequirements req = new IslandRequirements();
|
||||
Map<EntityType, Integer> requiredEntities = Collections.singletonMap(EntityType.GHAST, 3);
|
||||
req.setRequiredEntities(requiredEntities);
|
||||
req.setSearchRadius(1);
|
||||
challenge.setRequirements(req);
|
||||
assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix));
|
||||
verify(user).sendMessage("challenges.errors.you-still-need", "[amount]", "3", "[item]", "Ghast");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCompleteChallengesAddonUserChallengeWorldStringStringIslandFailMultipleEntities() {
|
||||
challenge.setChallengeType(ChallengeType.ISLAND);
|
||||
IslandRequirements req = new IslandRequirements();
|
||||
Map<EntityType, Integer> requiredEntities = new HashMap<>();
|
||||
requiredEntities.put(EntityType.GHAST, 3);
|
||||
requiredEntities.put(EntityType.CHICKEN, 5);
|
||||
requiredEntities.put(EntityType.PUFFERFISH, 1);
|
||||
req.setRequiredEntities(requiredEntities);
|
||||
req.setSearchRadius(1);
|
||||
challenge.setRequirements(req);
|
||||
assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix));
|
||||
verify(user).sendMessage("challenges.errors.you-still-need", "[amount]", "3", "[item]", "Ghast");
|
||||
verify(user).sendMessage("challenges.errors.you-still-need", "[amount]", "1", "[item]", "Pufferfish");
|
||||
verify(user).sendMessage("challenges.errors.you-still-need", "[amount]", "5", "[item]", "Chicken");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCompleteChallengesAddonUserChallengeWorldStringStringIslandFailPartialMultipleEntities() {
|
||||
challenge.setChallengeType(ChallengeType.ISLAND);
|
||||
IslandRequirements req = new IslandRequirements();
|
||||
Map<EntityType, Integer> requiredEntities = new HashMap<>();
|
||||
requiredEntities.put(EntityType.GHAST, 3);
|
||||
requiredEntities.put(EntityType.CHICKEN, 5);
|
||||
requiredEntities.put(EntityType.PUFFERFISH, 1);
|
||||
req.setRequiredEntities(requiredEntities);
|
||||
req.setSearchRadius(1);
|
||||
challenge.setRequirements(req);
|
||||
Entity ent = mock(Entity.class);
|
||||
when(ent.getType()).thenReturn(EntityType.PUFFERFISH);
|
||||
Location loc = mock(Location.class);
|
||||
when(ent.getLocation()).thenReturn(loc);
|
||||
List<Entity> list = Collections.singletonList(ent);
|
||||
when(world.getNearbyEntities(any(BoundingBox.class))).thenReturn(list);
|
||||
assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix));
|
||||
verify(user).sendMessage("challenges.errors.you-still-need", "[amount]", "3", "[item]", "Ghast");
|
||||
verify(user, never()).sendMessage("challenges.errors.you-still-need", "[amount]", "1", "[item]", "Pufferfish");
|
||||
verify(user).sendMessage("challenges.errors.you-still-need", "[amount]", "5", "[item]", "Chicken");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCompleteChallengesAddonUserChallengeWorldStringStringIslandSuccess() {
|
||||
challenge.setChallengeType(ChallengeType.ISLAND);
|
||||
IslandRequirements req = new IslandRequirements();
|
||||
Map<EntityType, Integer> requiredEntities = new HashMap<>();
|
||||
requiredEntities.put(EntityType.PUFFERFISH, 1);
|
||||
req.setRequiredEntities(requiredEntities);
|
||||
req.setSearchRadius(1);
|
||||
challenge.setRequirements(req);
|
||||
Entity ent = mock(Entity.class);
|
||||
when(ent.getType()).thenReturn(EntityType.PUFFERFISH);
|
||||
Location loc = mock(Location.class);
|
||||
when(ent.getLocation()).thenReturn(loc);
|
||||
List<Entity> list = Collections.singletonList(ent);
|
||||
when(world.getNearbyEntities(any(BoundingBox.class))).thenReturn(list);
|
||||
assertTrue(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix));
|
||||
verify(user).sendMessage("challenges.messages.you-completed-challenge", "[value]", "name");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String, int)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCompleteChallengesAddonUserChallengeWorldStringStringIntMultipleTimesPositiveSuccess() {
|
||||
// Try to complete 10 times. Already done 3 times, and max is 10, so it should be only done 7 times
|
||||
assertTrue(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix, 10));
|
||||
verify(user).sendMessage("challenges.messages.you-repeated-challenge-multiple", "[value]", "name", "[count]", "7");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#build(int)}.
|
||||
*/
|
||||
@Test
|
||||
public void testBuild() {
|
||||
this.testTryToCompleteChallengesAddonUserChallengeWorldStringString();
|
||||
ChallengeResult result = this.ttc.build(10);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#removeItems(java.util.List, int)}.
|
||||
*/
|
||||
@Test
|
||||
public void testRemoveItems() {
|
||||
this.testTryToCompleteChallengesAddonUserChallengeWorldStringString();
|
||||
ttc.removeItems(Collections.emptyList(), 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,233 @@
|
||||
package world.bentobox.challenges.tasks;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.inventory.ItemFactory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
import world.bentobox.challenges.ChallengesAddon;
|
||||
|
||||
/**
|
||||
* @author tastybento
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({ Bukkit.class})
|
||||
public class TryToCompleteTestOld {
|
||||
|
||||
private User user;
|
||||
ItemStack[] stacks = { new ItemStack(Material.PAPER, 32),
|
||||
new ItemStack(Material.ACACIA_BOAT),
|
||||
null,
|
||||
null,
|
||||
new ItemStack(Material.CACTUS, 32),
|
||||
new ItemStack(Material.CACTUS, 32),
|
||||
new ItemStack(Material.CACTUS, 32),
|
||||
new ItemStack(Material.BRICK_STAIRS, 64),
|
||||
new ItemStack(Material.BRICK_STAIRS, 64),
|
||||
new ItemStack(Material.BRICK_STAIRS, 5),
|
||||
new ItemStack(Material.GOLD_BLOCK, 32)
|
||||
};
|
||||
List<ItemStack> required;
|
||||
private ChallengesAddon addon;
|
||||
private PlayerInventory inv;
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
Server server = mock(Server.class);
|
||||
PowerMockito.mockStatic(Bukkit.class);
|
||||
when(Bukkit.getServer()).thenReturn(server);
|
||||
when(Bukkit.getBukkitVersion()).thenReturn("1.13.2");
|
||||
|
||||
user = mock(User.class);
|
||||
inv = mock(PlayerInventory.class);
|
||||
when(inv.getContents()).thenReturn(stacks);
|
||||
when(user.getInventory()).thenReturn(inv);
|
||||
addon = mock(ChallengesAddon.class);
|
||||
required = new ArrayList<>();
|
||||
|
||||
ItemFactory itemFactory = mock(ItemFactory.class);
|
||||
when(server.getItemFactory()).thenReturn(itemFactory);
|
||||
|
||||
// Test will not work with items that has meta data.
|
||||
when(itemFactory.getItemMeta(any())).thenReturn(null);
|
||||
when(itemFactory.equals(null, null)).thenReturn(true);
|
||||
|
||||
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
|
||||
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link TryToComplete#removeItems(java.util.List, int)}.
|
||||
*/
|
||||
@Test
|
||||
public void testRemoveItemsSuccess() {
|
||||
Material requiredMaterial = Material.PAPER;
|
||||
int requiredQuantity = 21;
|
||||
|
||||
this.required.add(new ItemStack(requiredMaterial, requiredQuantity));
|
||||
TryToComplete x = new TryToComplete(this.addon);
|
||||
x.user(this.user);
|
||||
Map<ItemStack, Integer> removed = x.removeItems(this.required, 1);
|
||||
|
||||
assertEquals((int) removed.getOrDefault(new ItemStack(requiredMaterial, 1), 0), requiredQuantity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link TryToComplete#removeItems(java.util.List, int)}.
|
||||
*/
|
||||
@Test
|
||||
public void testRemoveItemsMax() {
|
||||
Material requiredMaterial = Material.PAPER;
|
||||
int requiredQuantity = 50;
|
||||
|
||||
this.required.add(new ItemStack(requiredMaterial, requiredQuantity));
|
||||
TryToComplete x = new TryToComplete(this.addon);
|
||||
x.user(this.user);
|
||||
Map<ItemStack, Integer> removed = x.removeItems(this.required, 1);
|
||||
|
||||
assertNotEquals((int) removed.getOrDefault(new ItemStack(requiredMaterial, 1), 0), requiredQuantity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link TryToComplete#removeItems(java.util.List, int)}.
|
||||
*/
|
||||
@Test
|
||||
public void testRemoveItemsZero() {
|
||||
Material requiredMaterial = Material.PAPER;
|
||||
int requiredQuantity = 0;
|
||||
|
||||
this.required.add(new ItemStack(requiredMaterial, requiredQuantity));
|
||||
TryToComplete x = new TryToComplete(this.addon);
|
||||
x.user(this.user);
|
||||
Map<ItemStack, Integer> removed = x.removeItems(this.required, 1);
|
||||
|
||||
assertTrue(removed.isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link TryToComplete#removeItems(java.util.List, int)}.
|
||||
*/
|
||||
@Test
|
||||
public void testRemoveItemsSuccessMultiple() {
|
||||
required.add(new ItemStack(Material.PAPER, 11));
|
||||
required.add(new ItemStack(Material.PAPER, 5));
|
||||
required.add(new ItemStack(Material.PAPER, 5));
|
||||
TryToComplete x = new TryToComplete(addon);
|
||||
x.user(user);
|
||||
Map<ItemStack, Integer> removed = x.removeItems(required, 1);
|
||||
|
||||
assertEquals((int) removed.getOrDefault(new ItemStack(Material.PAPER, 1), 0), 21);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link TryToComplete#removeItems(java.util.List, int)}.
|
||||
*/
|
||||
@Test
|
||||
public void testRemoveItemsSuccessMultipleOther() {
|
||||
required.add(new ItemStack(Material.CACTUS, 5));
|
||||
required.add(new ItemStack(Material.PAPER, 11));
|
||||
required.add(new ItemStack(Material.PAPER, 5));
|
||||
required.add(new ItemStack(Material.PAPER, 5));
|
||||
required.add(new ItemStack(Material.CACTUS, 5));
|
||||
TryToComplete x = new TryToComplete(addon);
|
||||
x.user(user);
|
||||
Map<ItemStack, Integer> removed = x.removeItems(required, 1);
|
||||
|
||||
assertEquals((int) removed.getOrDefault(new ItemStack(Material.PAPER, 1), 0), 21);
|
||||
assertEquals((int) removed.getOrDefault(new ItemStack(Material.CACTUS, 1), 0), 10);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link TryToComplete#removeItems(java.util.List, int)}.
|
||||
*/
|
||||
@Test
|
||||
public void testRemoveItemsMultipleOtherFail() {
|
||||
required.add(new ItemStack(Material.ACACIA_FENCE, 5));
|
||||
required.add(new ItemStack(Material.ARROW, 11));
|
||||
required.add(new ItemStack(Material.STONE, 5));
|
||||
required.add(new ItemStack(Material.BAKED_POTATO, 5));
|
||||
required.add(new ItemStack(Material.GHAST_SPAWN_EGG, 5));
|
||||
TryToComplete x = new TryToComplete(addon);
|
||||
x.user(user);
|
||||
Map<ItemStack, Integer> removed = x.removeItems(required, 1);
|
||||
assertTrue(removed.isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link TryToComplete#removeItems(java.util.List, int)}.
|
||||
*/
|
||||
@Test
|
||||
public void testRemoveItemsFail() {
|
||||
ItemStack input = new ItemStack(Material.GOLD_BLOCK, 55);
|
||||
required.add(input);
|
||||
TryToComplete x = new TryToComplete(addon);
|
||||
x.user(user);
|
||||
Map<ItemStack, Integer> removed = x.removeItems(required, 1);
|
||||
|
||||
// It will remove 32, but not any more
|
||||
assertEquals((int) removed.getOrDefault(new ItemStack(Material.GOLD_BLOCK, 1), 0), 32);
|
||||
|
||||
// An error will be thrown
|
||||
Mockito.verify(addon, Mockito.times(1)).logError(Mockito.anyString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Test method for {@link TryToComplete#removeItems(java.util.List, int)}.
|
||||
*/
|
||||
@Test
|
||||
public void testRequireTwoStacks() {
|
||||
required.add(new ItemStack(Material.BRICK_STAIRS, 64));
|
||||
required.add(new ItemStack(Material.BRICK_STAIRS, 64));
|
||||
|
||||
TryToComplete x = new TryToComplete(addon);
|
||||
x.user(user);
|
||||
Map<ItemStack, Integer> removed = x.removeItems(required, 1);
|
||||
|
||||
// It should remove both stacks
|
||||
assertEquals((int) removed.getOrDefault(new ItemStack(Material.BRICK_STAIRS, 1), 0), 128);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test method for {@link TryToComplete#removeItems(java.util.List, int)}.
|
||||
*/
|
||||
@Test
|
||||
public void testFactorStacks() {
|
||||
required.add(new ItemStack(Material.BRICK_STAIRS, 32));
|
||||
|
||||
TryToComplete x = new TryToComplete(addon);
|
||||
x.user(user);
|
||||
Map<ItemStack, Integer> removed = x.removeItems(required, 4);
|
||||
|
||||
// It should remove both stacks
|
||||
assertEquals((int) removed.getOrDefault(new ItemStack(Material.BRICK_STAIRS, 1), 0), 128);
|
||||
}
|
||||
}
|
||||
|
199
src/test/java/world/bentobox/challenges/utils/UtilsTest.java
Normal file
199
src/test/java/world/bentobox/challenges/utils/UtilsTest.java
Normal file
@ -0,0 +1,199 @@
|
||||
package world.bentobox.challenges.utils;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.inventory.ItemFactory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.reflect.Whitebox;
|
||||
|
||||
import world.bentobox.bentobox.BentoBox;
|
||||
import world.bentobox.bentobox.api.addons.AddonDescription;
|
||||
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
||||
import world.bentobox.bentobox.managers.IslandWorldManager;
|
||||
import world.bentobox.challenges.config.SettingsUtils.VisibilityMode;
|
||||
|
||||
/**
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({Bukkit.class})
|
||||
public class UtilsTest {
|
||||
|
||||
@Mock
|
||||
private IslandWorldManager iwm;
|
||||
@Mock
|
||||
private GameModeAddon gameModeAddon;
|
||||
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
// Set up plugin
|
||||
BentoBox plugin = mock(BentoBox.class);
|
||||
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
|
||||
|
||||
// Mock item factory (for itemstacks)
|
||||
PowerMockito.mockStatic(Bukkit.class);
|
||||
ItemFactory itemFactory = mock(ItemFactory.class);
|
||||
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
|
||||
|
||||
// IWM getAddon
|
||||
AddonDescription desc = new AddonDescription.Builder("main", "name", "1.0").build();
|
||||
when(gameModeAddon.getDescription()).thenReturn(desc);
|
||||
Optional<GameModeAddon> optionalAddon = Optional.of(gameModeAddon);
|
||||
when(iwm.getAddon(any())).thenReturn(optionalAddon);
|
||||
when(plugin.getIWM()).thenReturn(iwm);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.utils.Utils#groupEqualItems(java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testGroupEqualItemsEmpty() {
|
||||
assertTrue(Utils.groupEqualItems(Collections.emptyList()).isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.utils.Utils#groupEqualItems(java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testGroupEqualItems() {
|
||||
List<ItemStack> requiredItems = new ArrayList<>();
|
||||
// First item
|
||||
ItemStack is = mock(ItemStack.class);
|
||||
when(is.getAmount()).thenReturn(1);
|
||||
when(is.getType()).thenReturn(Material.ACACIA_FENCE);
|
||||
when(is.getMaxStackSize()).thenReturn(64);
|
||||
when(is.isSimilar(any())).thenReturn(true);
|
||||
when(is.clone()).thenReturn(is);
|
||||
requiredItems.add(is);
|
||||
for (int i = 0; i < 9; i++) {
|
||||
ItemStack is2 = mock(ItemStack.class);
|
||||
when(is2.getAmount()).thenReturn(1);
|
||||
when(is2.getType()).thenReturn(Material.ACACIA_FENCE);
|
||||
when(is2.getMaxStackSize()).thenReturn(64);
|
||||
when(is2.isSimilar(any())).thenReturn(true);
|
||||
when(is2.clone()).thenReturn(is);
|
||||
requiredItems.add(is2);
|
||||
}
|
||||
List<ItemStack> list = Utils.groupEqualItems(requiredItems);
|
||||
// Result should be two stacks stack of 64 doors and 36 doors
|
||||
assertEquals(1, list.size());
|
||||
verify(is, times(9)).setAmount(2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.utils.Utils#groupEqualItems(java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testGroupEqualItemsUnique() {
|
||||
List<ItemStack> requiredItems = new ArrayList<>();
|
||||
// First item
|
||||
ItemStack is = mock(ItemStack.class);
|
||||
when(is.getAmount()).thenReturn(1);
|
||||
when(is.getType()).thenReturn(Material.ACACIA_FENCE);
|
||||
when(is.getMaxStackSize()).thenReturn(64);
|
||||
when(is.isSimilar(any())).thenReturn(false);
|
||||
when(is.clone()).thenReturn(is);
|
||||
requiredItems.add(is);
|
||||
for (int i = 0; i < 9; i++) {
|
||||
ItemStack is2 = mock(ItemStack.class);
|
||||
when(is2.getAmount()).thenReturn(1);
|
||||
when(is2.getType()).thenReturn(Material.values()[i+20]);
|
||||
when(is2.getMaxStackSize()).thenReturn(64);
|
||||
when(is2.isSimilar(any())).thenReturn(false);
|
||||
when(is2.clone()).thenReturn(is);
|
||||
requiredItems.add(is2);
|
||||
}
|
||||
List<ItemStack> list = Utils.groupEqualItems(requiredItems);
|
||||
// Result should be two stacks stack of 64 doors and 36 doors
|
||||
assertEquals(10, list.size());
|
||||
verify(is, never()).setAmount(2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.utils.Utils#canIgnoreMeta(org.bukkit.Material)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCanIgnoreMeta() {
|
||||
assertTrue(Utils.canIgnoreMeta(Material.FIREWORK_ROCKET));
|
||||
assertTrue(Utils.canIgnoreMeta(Material.ENCHANTED_BOOK));
|
||||
assertTrue(Utils.canIgnoreMeta(Material.WRITTEN_BOOK));
|
||||
assertTrue(Utils.canIgnoreMeta(Material.FILLED_MAP));
|
||||
assertFalse(Utils.canIgnoreMeta(Material.CHISELED_RED_SANDSTONE));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.utils.Utils#getGameMode(org.bukkit.World)}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetGameModeNoGameMode() {
|
||||
when(iwm.getAddon(any())).thenReturn(Optional.empty());
|
||||
assertNull(Utils.getGameMode(mock(World.class)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.utils.Utils#getGameMode(org.bukkit.World)}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetGameMode() {
|
||||
assertEquals("name", Utils.getGameMode(mock(World.class)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.utils.Utils#getNextValue(T[], java.lang.Object)}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetNextValue() {
|
||||
assertEquals(VisibilityMode.HIDDEN, Utils.getNextValue(VisibilityMode.values(), VisibilityMode.VISIBLE));
|
||||
assertEquals(VisibilityMode.TOGGLEABLE, Utils.getNextValue(VisibilityMode.values(), VisibilityMode.HIDDEN));
|
||||
assertEquals(VisibilityMode.VISIBLE, Utils.getNextValue(VisibilityMode.values(), VisibilityMode.TOGGLEABLE));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.challenges.utils.Utils#getPreviousValue(T[], java.lang.Object)}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetPreviousValue() {
|
||||
assertEquals(VisibilityMode.TOGGLEABLE, Utils.getPreviousValue(VisibilityMode.values(), VisibilityMode.VISIBLE));
|
||||
assertEquals(VisibilityMode.VISIBLE, Utils.getPreviousValue(VisibilityMode.values(), VisibilityMode.HIDDEN));
|
||||
assertEquals(VisibilityMode.HIDDEN, Utils.getPreviousValue(VisibilityMode.values(), VisibilityMode.TOGGLEABLE));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user