-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework hk2 to support mockbukkit tests
- Loading branch information
1 parent
e41641c
commit f7a82c9
Showing
30 changed files
with
237 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions
12
src/test/java/org/mvplugins/multiverse/inventories/MockBukkitTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/test/java/org/mvplugins/multiverse/inventories/TestWithMockBukkit.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)") | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/test/java/org/mvplugins/multiverse/inventories/mock/MVServerMock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.