Search in sources :

Example 11 with EzySimplePlugin

use of com.tvd12.ezyfoxserver.EzySimplePlugin in project ezyfox-server by youngmonkeys.

the class EzyZonesStarterTest method normalCaseTest.

@Test
public void normalCaseTest() {
    EzySimpleSettings settings = new EzySimpleSettings();
    EzySimpleZonesSetting zonesSetting = settings.getZones();
    EzySimpleZoneSetting zoneSetting = new EzySimpleZoneSetting();
    zoneSetting.setName("test");
    zonesSetting.setItem(zoneSetting);
    EzySimpleAppsSetting appsSetting = new EzySimpleAppsSetting();
    EzySimpleAppSetting appSetting = new EzySimpleAppSetting();
    appSetting.setName("apps");
    appSetting.setFolder("apps");
    appSetting.setEntryLoader(ExEntryLoader.class.getName());
    appsSetting.setItem(appSetting);
    zoneSetting.setApplications(appsSetting);
    EzySimplePluginsSetting pluginsSetting = new EzySimplePluginsSetting();
    EzySimplePluginSetting pluginSetting = new EzySimplePluginSetting();
    pluginSetting.setName("plugins");
    pluginSetting.setFolder("plugins");
    pluginSetting.setEntryLoader(ExPluginEntryLoader.class.getName());
    pluginsSetting.setItem(pluginSetting);
    zoneSetting.setPlugins(pluginsSetting);
    EzySimpleServer server = new EzySimpleServer();
    server.setSettings(settings);
    server.setConfig(new EzySimpleConfig());
    EzyServerContext serverContext = mock(EzyServerContext.class);
    when(serverContext.getServer()).thenReturn(server);
    EzySimpleZone zone = new EzySimpleZone();
    zone.setSetting(zoneSetting);
    EzyZoneContext zoneContext = mock(EzyZoneContext.class);
    when(zoneContext.getZone()).thenReturn(zone);
    when(serverContext.getZoneContext("test")).thenReturn(zoneContext);
    EzySimpleApplication app = new EzySimpleApplication();
    app.setSetting(appSetting);
    EzyAppContext appContext = mock(EzyAppContext.class);
    when(appContext.getApp()).thenReturn(app);
    when(zoneContext.getAppContext("apps")).thenReturn(appContext);
    EzySimplePlugin plugin = new EzySimplePlugin();
    plugin.setSetting(pluginSetting);
    EzyPluginContext pluginContext = mock(EzyPluginContext.class);
    when(pluginContext.getPlugin()).thenReturn(plugin);
    when(zoneContext.getPluginContext("plugins")).thenReturn(pluginContext);
    Map<String, ClassLoader> appClassLoaders = new HashMap<>();
    appClassLoaders.put("apps", new EzyAppClassLoader(new File("test-data"), getClass().getClassLoader()));
    server.setAppClassLoaders(appClassLoaders);
    EzyZonesStarter starter = EzyZonesStarter.builder().serverContext(serverContext).build();
    starter.start();
}
Also used : EzyAppClassLoader(com.tvd12.ezyfoxserver.ccl.EzyAppClassLoader) HashMap(java.util.HashMap) EzyAppClassLoader(com.tvd12.ezyfoxserver.ccl.EzyAppClassLoader) EzySimpleConfig(com.tvd12.ezyfoxserver.config.EzySimpleConfig) EzyZoneContext(com.tvd12.ezyfoxserver.context.EzyZoneContext) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzyAppContext(com.tvd12.ezyfoxserver.context.EzyAppContext) EzyPluginContext(com.tvd12.ezyfoxserver.context.EzyPluginContext) File(java.io.File) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 12 with EzySimplePlugin

use of com.tvd12.ezyfoxserver.EzySimplePlugin in project ezyfox-server by youngmonkeys.

the class EzySimplePluginContextTest method test.

@Test
public void test() {
    EzyServerContext serverContext = mock(EzyServerContext.class);
    EzySimpleZone zone = new EzySimpleZone();
    EzyZoneContext zoneContext = mock(EzyZoneContext.class);
    when(zoneContext.getZone()).thenReturn(zone);
    when(zoneContext.getParent()).thenReturn(serverContext);
    EzySimplePlugin plugin = new EzySimplePlugin();
    EzySimplePluginSetting setting = new EzySimplePluginSetting();
    plugin.setSetting(setting);
    EzySimplePluginContext pluginContext = new EzySimplePluginContext();
    pluginContext.setParent(zoneContext);
    pluginContext.setPlugin(plugin);
    pluginContext.init();
    // noinspection EqualsWithItself
    assert pluginContext.equals(pluginContext);
    EzySimplePluginContext pluginContext2 = new EzySimplePluginContext();
    assert !pluginContext.equals(pluginContext2);
    assert pluginContext.cmd(EzyPluginResponse.class) != null;
    EzySimpleUser user = new EzySimpleUser();
    user.setName("test");
    EzyAbstractSession session = spy(EzyAbstractSession.class);
    user.addSession(session);
    EzyData data = EzyEntityFactory.newArrayBuilder().build();
    pluginContext.send(data, session, false);
}
Also used : EzySimpleZone(com.tvd12.ezyfoxserver.EzySimpleZone) EzySimpleUser(com.tvd12.ezyfoxserver.entity.EzySimpleUser) EzyZoneContext(com.tvd12.ezyfoxserver.context.EzyZoneContext) EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzySimplePluginContext(com.tvd12.ezyfoxserver.context.EzySimplePluginContext) EzyData(com.tvd12.ezyfox.entity.EzyData) EzySimplePlugin(com.tvd12.ezyfoxserver.EzySimplePlugin) EzyPluginResponse(com.tvd12.ezyfoxserver.command.EzyPluginResponse) EzySimplePluginSetting(com.tvd12.ezyfoxserver.setting.EzySimplePluginSetting) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 13 with EzySimplePlugin

use of com.tvd12.ezyfoxserver.EzySimplePlugin in project ezyfox-server by youngmonkeys.

the class EzyPluginHandleExceptionImplTest method handleExceptionWithHandlersButException.

@Test
public void handleExceptionWithHandlersButException() {
    // given
    EzySimplePlugin plugin = new EzySimplePlugin();
    EzySimplePluginSetting setting = new EzySimplePluginSetting();
    String pluginName = RandomUtil.randomShortAlphabetString();
    setting.setName(pluginName);
    plugin.setSetting(setting);
    EzyPluginHandleExceptionImpl sut = new EzyPluginHandleExceptionImpl(plugin);
    Logger logger = mock(Logger.class);
    FieldUtil.setFieldValue(sut, "logger", logger);
    Exception exception = new IllegalArgumentException("one");
    EzyExceptionHandler exceptionHandler = mock(EzyExceptionHandler.class);
    RuntimeException ex = new RuntimeException("just test");
    doThrow(ex).when(exceptionHandler).handleException(Thread.currentThread(), exception);
    plugin.getExceptionHandlers().addExceptionHandler(exceptionHandler);
    // when
    sut.handle(Thread.currentThread(), exception);
    // then
    verify(exceptionHandler, times(1)).handleException(Thread.currentThread(), exception);
    verify(logger, times(1)).warn("handle exception: {} on plugin: {} error", EzyStrings.exceptionToSimpleString(exception), pluginName, ex);
}
Also used : EzyPluginHandleExceptionImpl(com.tvd12.ezyfoxserver.command.impl.EzyPluginHandleExceptionImpl) EzyExceptionHandler(com.tvd12.ezyfox.util.EzyExceptionHandler) EzySimplePlugin(com.tvd12.ezyfoxserver.EzySimplePlugin) Logger(org.slf4j.Logger) EzySimplePluginSetting(com.tvd12.ezyfoxserver.setting.EzySimplePluginSetting) BaseTest(com.tvd12.test.base.BaseTest) Test(org.testng.annotations.Test)

Example 14 with EzySimplePlugin

use of com.tvd12.ezyfoxserver.EzySimplePlugin in project ezyfox-server by youngmonkeys.

the class EzyPluginHandleExceptionImplTest method handleExceptionWithEmptyHandlers.

@Test
public void handleExceptionWithEmptyHandlers() {
    // given
    EzySimplePlugin plugin = new EzySimplePlugin();
    EzySimplePluginSetting setting = new EzySimplePluginSetting();
    String pluginName = RandomUtil.randomShortAlphabetString();
    setting.setName(pluginName);
    plugin.setSetting(setting);
    EzyPluginHandleExceptionImpl sut = new EzyPluginHandleExceptionImpl(plugin);
    Logger logger = mock(Logger.class);
    FieldUtil.setFieldValue(sut, "logger", logger);
    // when
    Exception exception = new IllegalArgumentException("one");
    sut.handle(Thread.currentThread(), exception);
    // then
    verify(logger, times(1)).info("plugin: {} has no handler for exception:", pluginName, exception);
}
Also used : EzyPluginHandleExceptionImpl(com.tvd12.ezyfoxserver.command.impl.EzyPluginHandleExceptionImpl) EzySimplePlugin(com.tvd12.ezyfoxserver.EzySimplePlugin) Logger(org.slf4j.Logger) EzySimplePluginSetting(com.tvd12.ezyfoxserver.setting.EzySimplePluginSetting) BaseTest(com.tvd12.test.base.BaseTest) Test(org.testng.annotations.Test)

Example 15 with EzySimplePlugin

use of com.tvd12.ezyfoxserver.EzySimplePlugin in project ezyfox-server by youngmonkeys.

the class EzyPluginsStarterTest method newAppEntryLoaderArgsNotNullTest.

@Test
public void newAppEntryLoaderArgsNotNullTest() {
    // given
    EzySimpleZoneContext zoneContext = EzyZoneContextsTest.newDefaultZoneContext();
    EzySimplePlugin plugin = new EzySimplePlugin();
    EzySimplePluginSetting pluginSetting = new EzySimplePluginSetting();
    pluginSetting.setName("abc");
    pluginSetting.setEntryLoader(InternalPluginEntryLoader.class);
    pluginSetting.setEntryLoaderArgs(new String[] { "Hello" });
    plugin.setSetting(pluginSetting);
    EzySimplePluginContext pluginContext = new EzySimplePluginContext();
    pluginContext.setPlugin(plugin);
    EzySimpleZoneSetting zoneSetting = new EzySimpleZoneSetting();
    EzySimplePluginsSetting pluginsSetting = new EzySimplePluginsSetting();
    pluginsSetting.setItem(pluginSetting);
    zoneSetting.setPlugins(pluginsSetting);
    zoneContext.addPluginContext(pluginSetting, pluginContext);
    EzySimpleZone zone = new EzySimpleZone();
    zone.setSetting(zoneSetting);
    zoneContext.setZone(zone);
    EzyPluginsStarter starter = new EzyPluginsStarter.Builder().zoneContext(zoneContext).build();
    // when
    starter.start();
    // then
    Asserts.assertNotNull(plugin.getEntry());
}
Also used : EzySimpleZone(com.tvd12.ezyfoxserver.EzySimpleZone) EzySimplePluginContext(com.tvd12.ezyfoxserver.context.EzySimplePluginContext) EzySimpleZoneContext(com.tvd12.ezyfoxserver.context.EzySimpleZoneContext) EzySimplePlugin(com.tvd12.ezyfoxserver.EzySimplePlugin) EzySimpleZoneSetting(com.tvd12.ezyfoxserver.setting.EzySimpleZoneSetting) EzyPluginsStarter(com.tvd12.ezyfoxserver.EzyPluginsStarter) EzySimplePluginsSetting(com.tvd12.ezyfoxserver.setting.EzySimplePluginsSetting) EzySimplePluginSetting(com.tvd12.ezyfoxserver.setting.EzySimplePluginSetting) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Aggregations

EzySimplePlugin (com.tvd12.ezyfoxserver.EzySimplePlugin)18 Test (org.testng.annotations.Test)16 BaseTest (com.tvd12.test.base.BaseTest)11 EzySimplePluginContext (com.tvd12.ezyfoxserver.context.EzySimplePluginContext)8 EzySimplePluginSetting (com.tvd12.ezyfoxserver.setting.EzySimplePluginSetting)8 EzySimpleZone (com.tvd12.ezyfoxserver.EzySimpleZone)7 EzySimpleServer (com.tvd12.ezyfoxserver.EzySimpleServer)6 EzySimpleServerContext (com.tvd12.ezyfoxserver.context.EzySimpleServerContext)6 EzySimpleZoneContext (com.tvd12.ezyfoxserver.context.EzySimpleZoneContext)6 EzySimpleUser (com.tvd12.ezyfoxserver.entity.EzySimpleUser)6 EzyEventControllers (com.tvd12.ezyfoxserver.wrapper.EzyEventControllers)6 EzyErrorScheduledExecutorService (com.tvd12.ezyfox.concurrent.EzyErrorScheduledExecutorService)5 EzyAbstractSession (com.tvd12.ezyfoxserver.entity.EzyAbstractSession)5 EzySimplePluginEntry (com.tvd12.ezyfoxserver.support.entry.EzySimplePluginEntry)5 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)5 EzyArray (com.tvd12.ezyfox.entity.EzyArray)4 EzyZoneContext (com.tvd12.ezyfoxserver.context.EzyZoneContext)4 EzyPluginRequestController (com.tvd12.ezyfoxserver.plugin.EzyPluginRequestController)4 EzyPluginHandleExceptionImpl (com.tvd12.ezyfoxserver.command.impl.EzyPluginHandleExceptionImpl)3 EzyServerContext (com.tvd12.ezyfoxserver.context.EzyServerContext)3