From 8a4982e169b8a1a9f409cf52a2c681cf0126dd28 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Mon, 7 Oct 2013 21:50:07 +0200 Subject: [PATCH] Add the test plugin to the plugin manager, just in case. --- .../protocol/SimpleCraftBukkitITCase.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/ProtocolLib/src/test/java/com/comphenix/integration/protocol/SimpleCraftBukkitITCase.java b/ProtocolLib/src/test/java/com/comphenix/integration/protocol/SimpleCraftBukkitITCase.java index 05a8e4e3..1a3365c1 100644 --- a/ProtocolLib/src/test/java/com/comphenix/integration/protocol/SimpleCraftBukkitITCase.java +++ b/ProtocolLib/src/test/java/com/comphenix/integration/protocol/SimpleCraftBukkitITCase.java @@ -6,6 +6,8 @@ import static org.mockito.Mockito.when; import java.io.File; import java.io.IOException; import java.util.Collections; +import java.util.List; +import java.util.Map; import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; @@ -14,6 +16,7 @@ import org.bukkit.Bukkit; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginLoadOrder; +import org.bukkit.plugin.PluginManager; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; @@ -63,6 +66,7 @@ public class SimpleCraftBukkitITCase { Bukkit.getScheduler().callSyncMethod(FAKE_PLUGIN, new Callable() { @Override public Object call() throws Exception { + initializePlugin(FAKE_PLUGIN); return null; } }).get(TIMEOUT_MS, TimeUnit.MILLISECONDS); @@ -106,6 +110,27 @@ public class SimpleCraftBukkitITCase { FileUtils.copyFile(bestFile, new File(pluginDirectory, bestFile.getName())); } + /** + * Load a specific fake plugin. + * @param plugin - the plugin to load. + */ + @SuppressWarnings("unchecked") + private static void initializePlugin(Plugin plugin) { + PluginManager manager = Bukkit.getPluginManager(); + + try { + List plugins = (List) FieldUtils.readField(manager, "plugins", true); + Map lookupNames = (Map) FieldUtils.readField(manager, "lookupNames", true); + + /// Associate this plugin + plugins.add(plugin); + lookupNames.put(plugin.getName(), plugin); + + } catch (Exception e) { + throw new RuntimeException("Unable to access the fields of " + manager, e); + } + } + /** * Create a mockable plugin for all the tests. * @param fakePluginName - the fake plugin name.