Search in sources :

Example 1 with ArkEvent

use of com.alipay.sofa.ark.spi.event.ArkEvent in project sofa-ark by alipay.

the class ArkBootRunnerTest method test.

@Test
public void test() {
    Assert.assertNotNull(sampleService);
    Assert.assertNotNull(pluginManagerService);
    Assert.assertTrue("SampleService".equals(sampleService.say()));
    ArkBootRunner runner = new ArkBootRunner(ArkBootRunnerTest.class);
    Field field = ReflectionUtils.findField(ArkBootRunner.class, "runner");
    Assert.assertNotNull(field);
    ReflectionUtils.makeAccessible(field);
    BlockJUnit4ClassRunner springRunner = (BlockJUnit4ClassRunner) ReflectionUtils.getField(field, runner);
    Assert.assertTrue(springRunner.getClass().getCanonicalName().equals(SpringRunner.class.getCanonicalName()));
    ClassLoader loader = springRunner.getTestClass().getJavaClass().getClassLoader();
    Assert.assertTrue(loader.getClass().getCanonicalName().equals(TestClassLoader.class.getCanonicalName()));
    Assert.assertEquals(0, TestValueHolder.getTestValue());
    eventAdminService.sendEvent(new ArkEvent() {

        @Override
        public String getTopic() {
            return "test-event-A";
        }
    });
    Assert.assertEquals(10, TestValueHolder.getTestValue());
    eventAdminService.sendEvent(new ArkEvent() {

        @Override
        public String getTopic() {
            return "test-event-B";
        }
    });
    Assert.assertEquals(20, TestValueHolder.getTestValue());
}
Also used : Field(java.lang.reflect.Field) BlockJUnit4ClassRunner(org.junit.runners.BlockJUnit4ClassRunner) ArkBootRunner(com.alipay.sofa.ark.springboot.runner.ArkBootRunner) TestClassLoader(com.alipay.sofa.ark.container.test.TestClassLoader) ArkEvent(com.alipay.sofa.ark.spi.event.ArkEvent) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with ArkEvent

use of com.alipay.sofa.ark.spi.event.ArkEvent in project sofa-ark by alipay.

the class GlobalEventHandlerTest method testEvent.

@Test
public void testEvent() {
    Biz biz = new BizModel().setBizName("test-biz").setBizVersion("1.0.0").setBizState(BizState.RESOLVED);
    Plugin plugin = new PluginModel().setPluginName("test-plugin").setVersion("1.0.0");
    eventAdminService.sendEvent(new AfterBizStartupEvent(biz));
    Assert.assertTrue(result.size() == 3);
    Assert.assertTrue(result.contains("AbstractArkEvent->AfterBizStartupEvent"));
    eventAdminService.sendEvent(new BeforePluginStartupEvent(plugin));
    Assert.assertTrue(result.size() == 5);
    // test for ArkEvent.class.isAssignableFrom(event.getClass()
    eventAdminService.sendEvent(new ArkEvent() {

        @Override
        public String getTopic() {
            return "ark-event";
        }
    });
    Assert.assertTrue(result.size() == 7);
}
Also used : PluginModel(com.alipay.sofa.ark.container.model.PluginModel) BeforePluginStartupEvent(com.alipay.sofa.ark.spi.event.plugin.BeforePluginStartupEvent) Biz(com.alipay.sofa.ark.spi.model.Biz) AfterBizStartupEvent(com.alipay.sofa.ark.spi.event.biz.AfterBizStartupEvent) BizModel(com.alipay.sofa.ark.container.model.BizModel) ArkEvent(com.alipay.sofa.ark.spi.event.ArkEvent) AbstractArkEvent(com.alipay.sofa.ark.spi.event.AbstractArkEvent) Plugin(com.alipay.sofa.ark.spi.model.Plugin) Test(org.junit.Test) BaseTest(com.alipay.sofa.ark.container.BaseTest)

Example 3 with ArkEvent

use of com.alipay.sofa.ark.spi.event.ArkEvent in project sofa-ark by alipay.

the class ArkClientTest method testBizArguments.

@Test
public void testBizArguments() throws Throwable {
    EventAdminService eventAdminService = arkServiceContainer.getService(EventAdminService.class);
    List<String> topicList = new ArrayList<>();
    EventHandler eventHandler = new EventHandler<ArkEvent>() {

        @Override
        public void handleEvent(ArkEvent event) {
            topicList.add(event.getTopic());
        }

        @Override
        public int getPriority() {
            return 0;
        }
    };
    eventAdminService.register(eventHandler);
    File bizFile3 = ArkClient.createBizSaveFile("biz-demo", "3.0.0");
    FileUtils.copyInputStreamToFile(bizUrl3.openStream(), bizFile3);
    ArkClient.installBiz(bizFile3);
    // ArkClient.installBiz(new File(bizUrl3.getFile()));
    ArkClient.uninstallBiz("biz-demo", "3.0.0");
    File bizFile33 = ArkClient.createBizSaveFile("biz-demo", "3.0.0");
    FileUtils.copyInputStreamToFile(bizUrl3.openStream(), bizFile33);
    ArkClient.installBiz(bizFile33, new String[] { "demo" });
    ArkClient.uninstallBiz("biz-demo", "3.0.0");
    Assert.assertEquals("BEFORE-INVOKE-BIZ-START", topicList.get(0));
    Assert.assertEquals("No arguments", topicList.get(1));
    Assert.assertEquals("AFTER-INVOKE-BIZ-START", topicList.get(2));
    // new event
    Assert.assertEquals("BEFORE-RECYCLE-BIZ", topicList.get(4));
    Assert.assertEquals("demo", topicList.get(7));
}
Also used : EventAdminService(com.alipay.sofa.ark.spi.service.event.EventAdminService) ArrayList(java.util.ArrayList) EventHandler(com.alipay.sofa.ark.spi.service.event.EventHandler) ArkEvent(com.alipay.sofa.ark.spi.event.ArkEvent) File(java.io.File) Test(org.junit.Test) BaseTest(com.alipay.sofa.ark.container.BaseTest)

Aggregations

ArkEvent (com.alipay.sofa.ark.spi.event.ArkEvent)3 Test (org.junit.Test)3 BaseTest (com.alipay.sofa.ark.container.BaseTest)2 BizModel (com.alipay.sofa.ark.container.model.BizModel)1 PluginModel (com.alipay.sofa.ark.container.model.PluginModel)1 TestClassLoader (com.alipay.sofa.ark.container.test.TestClassLoader)1 AbstractArkEvent (com.alipay.sofa.ark.spi.event.AbstractArkEvent)1 AfterBizStartupEvent (com.alipay.sofa.ark.spi.event.biz.AfterBizStartupEvent)1 BeforePluginStartupEvent (com.alipay.sofa.ark.spi.event.plugin.BeforePluginStartupEvent)1 Biz (com.alipay.sofa.ark.spi.model.Biz)1 Plugin (com.alipay.sofa.ark.spi.model.Plugin)1 EventAdminService (com.alipay.sofa.ark.spi.service.event.EventAdminService)1 EventHandler (com.alipay.sofa.ark.spi.service.event.EventHandler)1 ArkBootRunner (com.alipay.sofa.ark.springboot.runner.ArkBootRunner)1 File (java.io.File)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 BlockJUnit4ClassRunner (org.junit.runners.BlockJUnit4ClassRunner)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1