Skip to content

Commit

Permalink
Merge pull request #3127 from Multiverse/ben/mv5/testssss
Browse files Browse the repository at this point in the history
More testsss
  • Loading branch information
benwoo1110 authored Nov 20, 2024
2 parents e889e40 + 0b89738 commit 1eeb265
Show file tree
Hide file tree
Showing 9 changed files with 358 additions and 32 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ dependencies {

// Tests
testImplementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:2.0.21'
testImplementation 'com.github.seeseemelk:MockBukkit-v1.21:3.133.0'
testImplementation 'org.mockbukkit.mockbukkit:mockbukkit-v1.21:4.3.1'
testImplementation('com.googlecode.json-simple:json-simple:1.1.1') {
exclude group: 'junit', module: 'junit'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,12 @@ private Attempt<CreateWorldOptions, CreateFailureReason> validateCreateWorldOpti
return worldActionResult(CreateFailureReason.WORLD_EXIST_LOADED, options.worldName());
} else if (getWorld(options.worldName()).isDefined()) {
return worldActionResult(CreateFailureReason.WORLD_EXIST_UNLOADED, options.worldName());
} else if (hasWorldFolder(options.worldName())) {
} else if (worldNameChecker.hasWorldFolder(options.worldName())) {
return worldActionResult(CreateFailureReason.WORLD_EXIST_FOLDER, options.worldName());
}
return worldActionResult(options);
}

private boolean hasWorldFolder(String worldName) {
File worldFolder = new File(Bukkit.getWorldContainer(), worldName);
return worldFolder.exists();
}

private Attempt<LoadedMultiverseWorld, CreateFailureReason> createValidatedWorld(
CreateWorldOptions options) {
String parsedGenerator = parseGenerator(options.worldName(), options.generator());
Expand Down Expand Up @@ -530,9 +525,6 @@ private Attempt<CloneWorldOptions, CloneFailureReason> cloneWorldValidateWorld(
Logging.severe("Invalid world name: " + newWorldName);
return worldActionResult(CloneFailureReason.INVALID_WORLDNAME, newWorldName);
}
if (worldNameChecker.isValidWorldFolder(newWorldName)) {
return worldActionResult(CloneFailureReason.WORLD_EXIST_FOLDER, newWorldName);
}
if (isLoadedWorld(newWorldName)) {
Logging.severe("World already loaded when attempting to clone: " + newWorldName);
return worldActionResult(CloneFailureReason.WORLD_EXIST_LOADED, newWorldName);
Expand All @@ -541,6 +533,9 @@ private Attempt<CloneWorldOptions, CloneFailureReason> cloneWorldValidateWorld(
Logging.severe("World already exist unloaded: " + newWorldName);
return worldActionResult(CloneFailureReason.WORLD_EXIST_UNLOADED, newWorldName);
}
if (worldNameChecker.hasWorldFolder(newWorldName)) {
return worldActionResult(CloneFailureReason.WORLD_EXIST_FOLDER, newWorldName);
}
return worldActionResult(options);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Set;
import java.util.regex.Pattern;

import io.vavr.control.Option;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -45,17 +46,32 @@ public boolean isValidWorldName(@Nullable String worldName) {
*/
@NotNull
public NameStatus checkName(@Nullable String worldName) {
if (BLACKLIST_NAMES.contains(worldName)) {
return NameStatus.BLACKLISTED;
}
if (worldName == null || !WORLD_NAME_PATTERN.matcher(worldName).matches()) {
return NameStatus.INVALID_CHARS;
}
return NameStatus.VALID;
return Option.of(worldName).map(name -> {
if (name.isEmpty()) {
return NameStatus.EMPTY;
}
if (BLACKLIST_NAMES.contains(name)) {
return NameStatus.BLACKLISTED;
}
if (!WORLD_NAME_PATTERN.matcher(name).matches()) {
return NameStatus.INVALID_CHARS;
}
return NameStatus.VALID;
}).getOrElse(NameStatus.EMPTY);
}

/**
* Checks if a world name has a valid world folder.
* Check if a world name has a world folder directory. It may not contain valid world data.
*
* @param worldName The world name to check on.
* @return True if the folder exists, else false.
*/
public boolean hasWorldFolder(@Nullable String worldName) {
return checkFolder(worldName) != FolderStatus.DOES_NOT_EXIST;
}

/**
* Checks if a world name has a valid world folder with basic world data.
*
* @param worldName The world name to check on.
* @return True if check result is valid, else false.
Expand All @@ -65,7 +81,7 @@ public boolean isValidWorldFolder(@Nullable String worldName) {
}

/**
* Checks if a world folder is valid.
* Checks if a world folder is valid with basic world data.
*
* @param worldFolder The world folder to check on.
* @return True if check result is valid, else false.
Expand Down Expand Up @@ -132,6 +148,11 @@ public enum NameStatus {
*/
INVALID_CHARS,

/**
* Name string that is null or length 0.
*/
EMPTY,

/**
* Name not valid as it is deemed blacklisted.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
package org.mvplugins.multiverse.core

import be.seeseemelk.mockbukkit.MockBukkit
import be.seeseemelk.mockbukkit.ServerMock
import com.dumptruckman.minecraft.util.Logging
import org.mockbukkit.mockbukkit.MockBukkit
import org.mvplugins.multiverse.core.inject.PluginServiceLocator
import org.mvplugins.multiverse.core.mock.MVServerMock
import org.mvplugins.multiverse.core.utils.TestingMode
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.assertNotNull

/**
* Basic abstract test class that sets up MockBukkit and MultiverseCore.
*/
abstract class TestWithMockBukkit {

protected lateinit var server: ServerMock
protected lateinit var server: MVServerMock
protected lateinit var multiverseCore: MultiverseCore
protected lateinit var serviceLocator : PluginServiceLocator

@BeforeTest
fun setUpMockBukkit() {
TestingMode.enable()
server = MockBukkit.mock()
server = MockBukkit.mock(MVServerMock())
multiverseCore = MockBukkit.load(MultiverseCore::class.java)
Logging.setDebugLevel(3)
serviceLocator = multiverseCore.serviceLocator
assertNotNull(server.commandMap)
}

@AfterTest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.mvplugins.multiverse.core.commands

import be.seeseemelk.mockbukkit.entity.PlayerMock
import org.bukkit.Bukkit
import org.bukkit.ChatColor
import org.mockbukkit.mockbukkit.entity.PlayerMock
import org.mvplugins.multiverse.core.TestWithMockBukkit
import kotlin.test.BeforeTest
import kotlin.test.Test
Expand Down
53 changes: 53 additions & 0 deletions src/test/java/org/mvplugins/multiverse/core/mock/MVServerMock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.mvplugins.multiverse.core.mock;

import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.jetbrains.annotations.NotNull;
import org.mockbukkit.mockbukkit.ServerMock;
import org.mockbukkit.mockbukkit.command.CommandMapMock;
import org.mockbukkit.mockbukkit.world.WorldMock;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class MVServerMock extends ServerMock {

private final File worldContainer;

public MVServerMock() throws IOException {
super();
this.worldContainer = Files.createTempDirectory("world-container").toFile();
this.worldContainer.deleteOnExit();
System.out.println("Created test world folder: " + this.worldContainer.getAbsolutePath());
}

// This is required for acf reflection to work
@Override
public @NotNull CommandMapMock getCommandMap() {
return super.getCommandMap();
}

@Override
public @NotNull File getWorldContainer() {
return this.worldContainer;
}

@Override
public World createWorld(@NotNull WorldCreator creator) {
WorldMock world = new MVWorldMock(creator);
world.getWorldFolder().mkdirs();
createFile(new File(world.getWorldFolder(), "uid.dat"));
createFile(new File(world.getWorldFolder(), "level.dat"));
addWorld(world);
return world;
}

private void createFile(File file) {
try {
file.createNewFile();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
30 changes: 30 additions & 0 deletions src/test/java/org/mvplugins/multiverse/core/mock/MVWorldMock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.mvplugins.multiverse.core.mock;

import org.bukkit.WorldCreator;
import org.jetbrains.annotations.NotNull;
import org.mockbukkit.mockbukkit.MockBukkit;
import org.mockbukkit.mockbukkit.world.WorldMock;

import java.io.File;

public class MVWorldMock extends WorldMock {

private final File worldFolder;
private final boolean generateStructures;

public MVWorldMock(@NotNull WorldCreator creator) {
super(creator);
this.worldFolder = new File(MockBukkit.getMock().getWorldContainer(), getName());
this.generateStructures = creator.generateStructures();
}

@Override
public @NotNull File getWorldFolder() {
return this.worldFolder;
}

@Override
public boolean canGenerateStructures() {
return this.generateStructures;
}
}
Loading

0 comments on commit 1eeb265

Please sign in to comment.