Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework hk2 to support mockbukkit tests #558

Draft
wants to merge 1 commit into
base: MV5
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 56 additions & 18 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
id 'java-library'
id 'maven-publish'
id 'checkstyle'
id 'com.gradleup.shadow' version '8.3.5'
id "org.jetbrains.kotlin.jvm" version "2.0.21"
}

version = System.getenv('GITHUB_VERSION') ?: 'local'
Expand All @@ -14,9 +17,16 @@ compileJava {
targetCompatibility = JavaVersion.VERSION_17
}

// todo: Enable test when convert them to use mockbukkit like mv-core
compileTestJava {
enabled = false
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}

compileTestKotlin {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_21)
javaParameters.set(true)
}
}

repositories {
Expand Down Expand Up @@ -55,14 +65,19 @@ repositories {

dependencies {
// Spigot
implementation('org.spigotmc:spigot-api:1.21.4-R0.1-SNAPSHOT') {
compileOnly('org.spigotmc:spigot-api:1.21.3-R0.1-SNAPSHOT') {
exclude group: 'junit', module: 'junit'
}

// Core
// TODO update to correct version once we have it published
implementation 'org.mvplugins.multiverse.core:multiverse-core:5.0.0-SNAPSHOT'

// hk2 for annotation processing only
compileOnly('org.glassfish.hk2:hk2-api:3.0.3') {
exclude group: '*', module: '*'
}

// Config
api 'com.dumptruckman.minecraft:JsonConfiguration:1.2-SNAPSHOT'
api 'net.minidev:json-smart:2.5.1'
Expand All @@ -82,9 +97,15 @@ dependencies {
}

// Tests
testImplementation 'com.github.MilkBowl:VaultAPI:1.7.1'
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.mockito:mockito-core:3.11.2'
testImplementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:2.0.21'
testImplementation 'org.mockbukkit.mockbukkit:mockbukkit-v1.21:4.24.1'
testImplementation 'org.jetbrains.kotlin:kotlin-test'
testImplementation 'com.natpryce:hamkrest:1.8.0.1'
testImplementation 'org.mockito.kotlin:mockito-kotlin:4.1.0'

// Annotation Processors
annotationProcessor 'org.glassfish.hk2:hk2-metadata-generator:3.0.3'
testAnnotationProcessor 'org.glassfish.hk2:hk2-metadata-generator:3.0.3'
}


Expand All @@ -97,6 +118,12 @@ tasks.withType(JavaCompile).configureEach {
options.encoding = 'UTF-8'
}

tasks.withType(JavaCompile) {
configure(options) {
options.compilerArgs << '-Aorg.glassfish.hk2.metadata.location=META-INF/hk2-locator/Multiverse-Inventories'
}
}

tasks.withType(Javadoc).configureEach {
options.encoding = 'UTF-8'
}
Expand All @@ -109,6 +136,7 @@ configurations {
}
}


publishing {
publications {
maven(MavenPublication) {
Expand All @@ -128,6 +156,15 @@ publishing {
}


compileKotlin {
// We're not using Kotlin in the plugin itself, just tests!
enabled = false
}
configurations.findAll { !it.name.startsWith('test') }.each {
it.exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-jdk8'
}


processResources {
def props = [version: "${project.version}"]
inputs.properties props
Expand Down Expand Up @@ -162,23 +199,24 @@ shadowJar {
relocate 'com.dumptruckman.minecraft.util.DebugLog', 'org.mvplugins.multiverse.inventories.utils.DebugFileLogger'
relocate 'com.dumptruckman.bukkit.configuration', 'org.mvplugins.multiverse.inventories.utils.configuration'
relocate 'io.papermc.lib', 'org.mvplugins.multiverse.inventories.utils.paperlib'
relocate 'net.minidev.json', 'org.mvplugins.multiverse.inventories.utils.json'
relocate 'net.minidev', 'org.mvplugins.multiverse.inventories.utils.minidev'

configurations = [project.configurations.api]

archiveClassifier.set('')

dependencies {
exclude(dependency {
it.moduleGroup == 'org.jetbrains.kotlin'
})
exclude(dependency {
it.moduleGroup == 'org.jetbrains'
})
exclude(dependency {
it.moduleGroup == 'org.ow2.asm'
})
}
}

build.dependsOn shadowJar
jar.enabled = false

tasks.register('runHabitatGenerator', JavaExec) {
classpath = configurations["compileClasspath"]
mainClass.set('org.mvplugins.multiverse.external.jvnet.hk2.generator.HabitatGenerator')

args = [
'--file', "build/libs/multiverse-inventories-$version" + ".jar",
'--locator', 'Multiverse-Inventories',
]
}
tasks.named("build") { finalizedBy("runHabitatGenerator") }
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.mvplugins.multiverse.inventories;

import com.dumptruckman.minecraft.util.Logging;
import org.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.core.api.event.MVConfigReloadEvent;
import org.mvplugins.multiverse.core.api.event.MVDebugModeEvent;
import org.mvplugins.multiverse.core.api.event.MVDumpsDebugInfoEvent;
Expand Down Expand Up @@ -36,7 +37,6 @@
import org.bukkit.inventory.InventoryHolder;
import org.mvplugins.multiverse.external.jakarta.inject.Inject;
import org.mvplugins.multiverse.external.jetbrains.annotations.NotNull;
import org.mvplugins.multiverse.external.jvnet.hk2.annotations.Service;
import uk.co.tggl.pluckerpluck.multiinv.MultiInv;

import java.io.File;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.mvplugins.multiverse.core.inject.PluginServiceLocator;
import org.mvplugins.multiverse.external.jakarta.inject.Inject;
import org.mvplugins.multiverse.external.jakarta.inject.Provider;
import org.mvplugins.multiverse.external.jvnet.hk2.annotations.Service;
import org.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.external.vavr.control.Try;
import uk.co.tggl.pluckerpluck.multiinv.MultiInv;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.mvplugins.multiverse.inventories;

import org.bukkit.plugin.Plugin;
import org.mvplugins.multiverse.core.inject.binder.JavaPluginBinder;
import org.mvplugins.multiverse.core.submodules.MVPlugin;
import org.mvplugins.multiverse.external.glassfish.hk2.utilities.binding.ScopedBindingBuilder;
Expand All @@ -14,6 +15,6 @@ protected MultiverseInventoriesPluginBinder(@NotNull MultiverseInventories plugi
@Override
protected ScopedBindingBuilder<MultiverseInventories> bindPluginClass
(ScopedBindingBuilder<MultiverseInventories> bindingBuilder) {
return super.bindPluginClass(bindingBuilder).to(MVPlugin.class).to(MultiverseInventories.class);
return super.bindPluginClass(bindingBuilder).to(Plugin.class).to(MVPlugin.class).to(MultiverseInventories.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.mvplugins.multiverse.external.acf.commands.annotation.Subcommand;
import org.mvplugins.multiverse.external.jakarta.inject.Inject;
import org.mvplugins.multiverse.external.jetbrains.annotations.NotNull;
import org.mvplugins.multiverse.external.jvnet.hk2.annotations.Service;
import org.jvnet.hk2.annotations.Service;

@Service
@CommandAlias("mvinv")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.mvplugins.multiverse.external.acf.commands.annotation.Syntax;
import org.mvplugins.multiverse.external.jakarta.inject.Inject;
import org.mvplugins.multiverse.external.jetbrains.annotations.NotNull;
import org.mvplugins.multiverse.external.jvnet.hk2.annotations.Service;
import org.jvnet.hk2.annotations.Service;

import java.util.List;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.mvplugins.multiverse.external.acf.commands.annotation.Subcommand;
import org.mvplugins.multiverse.external.jakarta.inject.Inject;
import org.mvplugins.multiverse.external.jetbrains.annotations.NotNull;
import org.mvplugins.multiverse.external.jvnet.hk2.annotations.Service;
import org.jvnet.hk2.annotations.Service;

import java.util.Collection;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.mvplugins.multiverse.external.acf.commands.annotation.Subcommand;
import org.mvplugins.multiverse.external.jakarta.inject.Inject;
import org.mvplugins.multiverse.external.jetbrains.annotations.NotNull;
import org.mvplugins.multiverse.external.jvnet.hk2.annotations.Service;
import org.jvnet.hk2.annotations.Service;

@Service
@CommandAlias("mvinv")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.mvplugins.multiverse.external.acf.commands.annotation.Syntax;
import org.mvplugins.multiverse.external.jakarta.inject.Inject;
import org.mvplugins.multiverse.external.jetbrains.annotations.NotNull;
import org.mvplugins.multiverse.external.jvnet.hk2.annotations.Service;
import org.jvnet.hk2.annotations.Service;

@Service
@CommandAlias("mvinv")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.mvplugins.multiverse.inventories

import kotlin.test.Test
import kotlin.test.assertNotNull

open class MockBukkitTest : TestWithMockBukkit() {

@Test
fun `MockBukkit loads the plugin`() {
assertNotNull(multiverseInventories)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.mvplugins.multiverse.inventories

import com.dumptruckman.minecraft.util.Logging
import org.bukkit.Location
import org.bukkit.configuration.MemorySection
import org.bukkit.configuration.file.YamlConfiguration
import org.mockbukkit.mockbukkit.MockBukkit
import org.mvplugins.multiverse.core.MultiverseCore
import org.mvplugins.multiverse.core.inject.PluginServiceLocator
import org.mvplugins.multiverse.core.utils.TestingMode
import org.mvplugins.multiverse.inventories.mock.MVServerMock
import kotlin.test.*

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

protected lateinit var server: MVServerMock
protected lateinit var multiverseCore: MultiverseCore
protected lateinit var multiverseInventories: MultiverseInventories
protected lateinit var serviceLocator : PluginServiceLocator

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

@AfterTest
fun tearDownMockBukkit() {
MockBukkit.unmock()
}

fun getResourceAsText(path: String): String? = object {}.javaClass.getResource(path)?.readText()

fun assertConfigEquals(expectedPath: String, actualPath: String) {
val actualString = multiverseInventories.dataFolder.toPath().resolve(actualPath).toFile().readText()
val expectedString = getResourceAsText(expectedPath)
assertNotNull(expectedString)

val actualYaml = YamlConfiguration()
actualYaml.loadFromString(actualString)
val actualYamlKeys = HashSet(actualYaml.getKeys(true))

val expectedYaml = YamlConfiguration()
expectedYaml.loadFromString(expectedString)
val expectedYamlKeys = HashSet(expectedYaml.getKeys(true))

for (key in expectedYamlKeys) {
assertNotNull(actualYamlKeys.remove(key), "Key $key is missing in actual config")
val actualValue = actualYaml.get(key)
if (actualValue is MemorySection) {
continue
}
assertEquals(expectedYaml.get(key), actualYaml.get(key), "Value for $key is different.")
}
for (key in actualYamlKeys) {
assertNull(actualYaml.get(key), "Key $key is present in actual config when it should be empty.")
}

assertEquals(0, actualYamlKeys.size,
"Actual config has more keys than expected config. The following keys are missing: $actualYamlKeys")
}

fun assertLocationEquals(expected: Location?, actual: Location?) {
assertEquals(expected?.world, actual?.world, "Worlds don't match for location comparison ($expected, $actual)")
assertEquals(expected?.x, actual?.x, "X values don't match for location comparison ($expected, $actual)")
assertEquals(expected?.y, actual?.y, "Y values don't match for location comparison ($expected, $actual)")
assertEquals(expected?.z, actual?.z, "Z values don't match for location comparison ($expected, $actual)")
assertEquals(expected?.yaw, actual?.yaw, "Yaw values don't match for location comparison ($expected, $actual)")
assertEquals(expected?.pitch, actual?.pitch, "Pitch values don't match for location comparison ($expected, $actual)")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.mvplugins.multiverse.inventories.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);
}
}
}
Loading
Loading