/** * Initializes this plugin with the given variables. * * This method should never be called manually. * * @param loader PluginLoader that is responsible for this plugin * @param server Server instance that is running this plugin * @param description PluginDescriptionFile containing metadata on this plugin * @param dataFolder Folder containing the plugin's data * @param file File containing this plugin * @param classLoader ClassLoader which holds this plugin */ protected final void initialize(PluginLoader loader, Server server, PluginDescriptionFile description, File dataFolder, File file ) { //, //ClassLoader classLoader) { if (!initialized) { this.initialized = true; this.loader = loader; this.server = server; this.file = file; this.description = description; this.dataFolder = dataFolder; //this.classLoader = classLoader; this.config = YamlConfiguration.loadConfiguration(new File(dataFolder, "config.yml")); } }
@Test public void test_default_modules() throws Exception { PluginLoader loader = mock(PluginLoader.class); Server server = mock(Server.class); PluginDescriptionFile pdf = mock(PluginDescriptionFile.class); File file1 = new File("target" + File.separator + "testdatafolder"); PluginLogger logger = mock(PluginLogger.class); when(server.getLogger()).thenReturn(logger); TestPluginDefaultModules plugin = new TestPluginDefaultModules(loader, server, pdf, file1, file1); assertThat(plugin.injectedPlugin).isNull(); plugin.onLoad(); assertThat(plugin.injectedPlugin).isNull(); plugin.onEnable(); assertThat(plugin.injectedPlugin).isSameAs(plugin); }
@Test public void test_extra_modules() throws Exception { PluginLoader loader = mock(PluginLoader.class); Server server = mock(Server.class); PluginDescriptionFile pdf = mock(PluginDescriptionFile.class); File file1 = new File("target" + File.separator + "testdatafolder"); PluginLogger logger = mock(PluginLogger.class); when(server.getLogger()).thenReturn(logger); TestPluginExtraModules plugin = new TestPluginExtraModules(loader, server, pdf, file1, file1); assertThat(plugin.i).isNull(); plugin.onLoad(); assertThat(plugin.i).isNull(); plugin.onEnable(); assertThat(plugin.i).isInstanceOf(TestPluginExtraModules.TestConcrete.class); }
@Test public void test_override_modules() throws Exception { PluginLoader loader = mock(PluginLoader.class); Server server = mock(Server.class); PluginDescriptionFile pdf = mock(PluginDescriptionFile.class); File file1 = new File("target" + File.separator + "testdatafolder"); PluginLogger logger = mock(PluginLogger.class); when(server.getLogger()).thenReturn(logger); TestPluginReplaceModules plugin = new TestPluginReplaceModules(loader, server, pdf, file1, file1); assertThat(plugin.logger).isNull(); plugin.onLoad(); assertThat(plugin.logger).isNull(); plugin.onEnable(); assertThat(plugin.logger).isInstanceOf(TestPluginReplaceModules.TestPluginLogger.class); }
/** * @deprecated This method is intended for unit testing purposes when the * other {@linkplain #JavaPlugin(JavaPluginLoader, * PluginDescriptionFile, File, File) constructor} cannot be used. * <p> * Its existence may be temporary. */ @Deprecated protected JavaPlugin(final PluginLoader loader, final Server server, final PluginDescriptionFile description, final File dataFolder, final File file) { final ClassLoader classLoader = this.getClass().getClassLoader(); if (classLoader instanceof PluginClassLoader) { throw new IllegalStateException("Cannot use initialization constructor at runtime"); } init(loader, server, description, dataFolder, file, classLoader); }
/** * @deprecated This method is legacy and will be removed - it must be * replaced by the specially provided constructor(s). */ @Deprecated protected final void initialize(PluginLoader loader, Server server, PluginDescriptionFile description, File dataFolder, File file, ClassLoader classLoader) { if (server.getWarningState() == WarningState.OFF) { return; } getLogger().log(Level.WARNING, getClass().getName() + " is already initialized", server.getWarningState() == WarningState.DEFAULT ? null : new AuthorNagException("Explicit initialization")); }
final void init(PluginLoader loader, Server server, PluginDescriptionFile description, File dataFolder, File file, ClassLoader classLoader) { this.loader = loader; this.server = server; this.file = file; this.description = description; this.dataFolder = dataFolder; this.classLoader = classLoader; this.configFile = new File(dataFolder, "config.yml"); this.logger = new PluginLogger(this); if (description.isDatabaseEnabled()) { ServerConfig db = new ServerConfig(); db.setDefaultServer(false); db.setRegister(false); db.setClasses(getDatabaseClasses()); db.setName(description.getName()); server.configureDbConfig(db); DataSourceConfig ds = db.getDataSourceConfig(); ds.setUrl(replaceDatabaseString(ds.getUrl())); dataFolder.mkdirs(); ClassLoader previous = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(classLoader); ebean = EbeanServerFactory.create(db); Thread.currentThread().setContextClassLoader(previous); } }
@Test public void test_no_modules() throws Exception { PluginLoader loader = mock(PluginLoader.class); Server server = mock(Server.class); PluginDescriptionFile pdf = mock(PluginDescriptionFile.class); File file1 = new File("target" + File.separator + "testdatafolder"); PluginLogger logger = mock(PluginLogger.class); when(server.getLogger()).thenReturn(logger); TestPluginNoModules plugin = new TestPluginNoModules(loader, server, pdf, file1, file1); plugin.onLoad(); plugin.onEnable(); }
/** * Initializes this plugin with the given variables. * <p> * This method should never be called manually. * * @param loader PluginLoader that is responsible for this plugin * @param server Server instance that is running this plugin * @param description PluginDescriptionFile containing metadata on this plugin * @param dataFolder Folder containing the plugin's data * @param file File containing this plugin * @param classLoader ClassLoader which holds this plugin */ protected final void initialize(PluginLoader loader, Server server, PluginDescriptionFile description, File dataFolder, File file, ClassLoader classLoader) { if (!initialized) { this.initialized = true; this.loader = loader; this.server = server; this.file = file; this.description = description; this.dataFolder = dataFolder; this.classLoader = classLoader; this.configFile = new File(dataFolder, "config.yml"); this.logger = new PluginLogger(this); if (description.isDatabaseEnabled()) { ServerConfig db = new ServerConfig(); db.setDefaultServer(false); db.setRegister(false); db.setClasses(getDatabaseClasses()); db.setName(description.getName()); server.configureDbConfig(db); DataSourceConfig ds = db.getDataSourceConfig(); ds.setUrl(replaceDatabaseString(ds.getUrl())); dataFolder.mkdirs(); ClassLoader previous = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(classLoader); ebean = EbeanServerFactory.create(db); Thread.currentThread().setContextClassLoader(previous); } } }
@Override public void registerInterface(Class<? extends PluginLoader> loader) throws IllegalArgumentException { // TODO Auto-generated method stub throw new UnimplementedOperationException(); }
@Override public void registerInterface(Class<? extends PluginLoader> loader) throws IllegalArgumentException { delegate.registerInterface(loader); }
@SuppressWarnings({"resource", "deprecation"}) @Before public void before() throws Exception { final File dataDir = new File("target/classes/"); final File jar = new File("target/", "skript.jar"); assumeTrue(jar.exists()); final Logger l = Logger.getLogger(getClass().getCanonicalName()); l.setParent(SkriptLogger.LOGGER); l.setLevel(Level.WARNING); final Server s = createMock(Server.class); s.getLogger(); expectLastCall().andReturn(l).anyTimes(); s.isPrimaryThread(); expectLastCall().andReturn(true).anyTimes(); s.getName(); expectLastCall().andReturn("Whatever").anyTimes(); s.getVersion(); expectLastCall().andReturn("2.0").anyTimes(); s.getBukkitVersion(); expectLastCall().andReturn("2.0").anyTimes(); replay(s); Bukkit.setServer(s); final Skript skript = (Skript) ObjenesisHelper.newInstance(Skript.class); // bypass the class loader check final Field instance = Skript.class.getDeclaredField("instance"); instance.setAccessible(true); instance.set(null, skript); final PluginDescriptionFile pdf = new PluginDescriptionFile(new FileInputStream(new File(dataDir, "plugin.yml"))); // final void init(PluginLoader loader, Server server, PluginDescriptionFile description, File dataFolder, File file, ClassLoader classLoader) { final Method init = JavaPlugin.class.getDeclaredMethod("init", PluginLoader.class, Server.class, PluginDescriptionFile.class, File.class, File.class, ClassLoader.class); init.setAccessible(true); init.invoke(skript, new JavaPluginLoader(s), s, pdf, dataDir, jar, getClass().getClassLoader()); Skript.getAddonInstance().loadClasses("ch.njol.skript", "entity"); new JavaClasses(); new BukkitClasses(); new BukkitEventValues(); new SkriptClasses(); final Field r = Skript.class.getDeclaredField("acceptRegistrations"); r.setAccessible(true); r.set(null, false); Classes.onRegistrationsStop(); }
@Override public void registerInterface(Class<? extends PluginLoader> loader) throws IllegalArgumentException { pm.registerInterface(loader); }
void setLoader(PluginLoader loader) { this.loader = loader; }
@Override public PluginLoader getPluginLoader() { return loader; }
@Override public PluginLoader getPluginLoader() { return delegate.getPluginLoader(); }
@Override public PluginLoader getPluginLoader() { return plugin.getPluginLoader(); }
@Override public void registerInterface(Class<? extends PluginLoader> loader) throws IllegalArgumentException { }
public PluginLoader getPluginLoader() { return pluginLoader; }
@Override public void registerInterface(Class<? extends PluginLoader> loader) throws IllegalArgumentException { throw Pokkit.unsupported(); }
@Override public PluginLoader getPluginLoader() { return this; }