Search in sources :

Example 1 with ApplicationContextBuilder

use of com.tvd12.ezyhttp.server.core.ApplicationContextBuilder in project ezyhttp by youngmonkeys.

the class ApplicationContextBuilderTest method test.

@Test
public void test() {
    // given
    EzyBeanContext internalBeanContext = EzyBeanContext.builder().addSingleton(new InternalSingleton1()).build();
    Properties properties = new Properties();
    properties.put("b", 2);
    ApplicationContext applicationContext = new ApplicationContextBuilder().scan("com.tvd12.ezyhttp.server.core.test.component", "com.tvd12.ezyhttp.server.core.test.config", "com.tvd12.ezyhttp.server.core.test.controller").scan(Arrays.asList("com.tvd12.ezyhttp.server.core.test.reflect", "com.tvd12.ezyhttp.server.core.test.request", "com.tvd12.ezyhttp.server.core.test.resources", "com.tvd12.ezyhttp.server.core.test.service")).addComponentClasses(Collections.singletonList(SourceService.class)).addPropertiesSources(Collections.singletonList("application3.yaml")).addPropertiesSource("application-enable.yaml").addProperty("a", "1").addProperties(properties).addProperties(Collections.singletonMap("c", "3")).beanContext(internalBeanContext).addSingleton(new InternalSingleton2()).addSingleton(Collections.singletonMap("internalSingleton3", new InternalSingleton3())).addSingleton(mock(AbsentMessageResolver.class)).addComponentClass(InternalSingleton1.class).addPropertiesSources().build();
    EzyBeanContext beanContext = applicationContext.getBeanContext();
    // when
    int actualOneProp = beanContext.getProperty("one", int.class);
    boolean managementEnable = beanContext.getProperty("management.enable", boolean.class);
    Boolean resourceEnable = beanContext.getProperty("resources.enable", boolean.class);
    Boolean resourceUploadEnable = beanContext.getProperty("resources.upload.enable", boolean.class);
    UserService userService = beanContext.getSingleton(UserService.class);
    UserService0 userService0 = beanContext.getSingleton(UserService0.class);
    ViewContextBuilder viewContextBuilder = beanContext.getSingleton(ViewContextBuilder.class);
    ResourceResolver resourceResolver = beanContext.getSingleton(ResourceResolver.class);
    ResourceDownloadManager resourceDownloadManager = beanContext.getSingleton(ResourceDownloadManager.class);
    Set<String> packagesToScan = beanContext.getPackagesToScan();
    EventService eventService = beanContext.getSingleton(EventService.class);
    SourceService sourceService = beanContext.getSingleton(SourceService.class);
    String helloValue = beanContext.getProperty("hello", String.class);
    InternalSingleton1 internalSingleton1 = beanContext.getBeanCast(InternalSingleton1.class);
    InternalSingleton2 internalSingleton2 = beanContext.getBeanCast(InternalSingleton2.class);
    InternalSingleton3 internalSingleton3 = beanContext.getBeanCast(InternalSingleton3.class);
    // then
    Asserts.assertEquals(1, actualOneProp);
    Asserts.assertTrue(managementEnable);
    Asserts.assertTrue(resourceEnable);
    Asserts.assertTrue(resourceUploadEnable);
    Asserts.assertNotNull(userService);
    Asserts.assertNotNull(userService0);
    Asserts.assertNotNull(viewContextBuilder);
    Asserts.assertNotNull(resourceDownloadManager);
    Asserts.assertEquals(4, resourceResolver.getResources().size());
    Asserts.assertNotNull(eventService);
    Asserts.assertNotNull(sourceService);
    Asserts.assertNotNull(helloValue);
    Asserts.assertEquals("1", beanContext.getProperty("a", String.class));
    Asserts.assertEquals(2, beanContext.getProperty("b", int.class));
    Asserts.assertEquals("3", beanContext.getProperty("c", String.class));
    Asserts.assertEquals("3", applicationContext.getProperty("c", String.class));
    Asserts.assertEquals(packagesToScan, Sets.newHashSet("com.tvd12.ezyhttp.server", "com.tvd12.ezyhttp.server.core.test", "com.tvd12.ezyhttp.server.core.test.component", "com.tvd12.ezyhttp.server.core.test.config", "com.tvd12.ezyhttp.server.core.test.controller", "com.tvd12.ezyhttp.server.core.test.event", "com.tvd12.ezyhttp.server.core.test.api", "com.tvd12.ezyhttp.server.core.test.reflect", "com.tvd12.ezyhttp.server.core.test.request", "com.tvd12.ezyhttp.server.core.test.resources", "com.tvd12.ezyhttp.server.core.test.service"));
    Asserts.assertNotNull(internalSingleton1);
    Asserts.assertNotNull(internalSingleton2);
    Asserts.assertNotNull(internalSingleton3);
    ComponentManager componentManager = ComponentManager.getInstance();
    Asserts.assertEquals(componentManager.getAsyncDefaultTimeout(), 10000);
    System.out.println(applicationContext);
    applicationContext.destroy();
}
Also used : EzyBeanContext(com.tvd12.ezyfox.bean.EzyBeanContext) UserService(com.tvd12.ezyhttp.server.core.test.service.UserService) EventService(com.tvd12.ezyhttp.server.core.test.event.EventService) Properties(java.util.Properties) ViewContextBuilder(com.tvd12.ezyhttp.server.core.view.ViewContextBuilder) ApplicationContext(com.tvd12.ezyhttp.server.core.ApplicationContext) ResourceDownloadManager(com.tvd12.ezyhttp.core.resources.ResourceDownloadManager) ResourceResolver(com.tvd12.ezyhttp.server.core.resources.ResourceResolver) ComponentManager(com.tvd12.ezyhttp.server.core.manager.ComponentManager) UserService0(com.tvd12.ezyhttp.server.core.test.service.UserService0) ApplicationContextBuilder(com.tvd12.ezyhttp.server.core.ApplicationContextBuilder) SourceService(com.tvd12.ezyhttp.server.core.test.event.SourceService) Test(org.testng.annotations.Test)

Example 2 with ApplicationContextBuilder

use of com.tvd12.ezyhttp.server.core.ApplicationContextBuilder in project ezyhttp by youngmonkeys.

the class ApplicationContextBuilderTest method buildFailedDueToPackageToScans.

@Test
public void buildFailedDueToPackageToScans() {
    // given
    ApplicationContextBuilder builder = new ApplicationContextBuilder();
    // when
    Throwable e = Asserts.assertThrows(builder::build);
    // then
    Asserts.assertThat(e).isEqualsType(IllegalStateException.class);
}
Also used : ApplicationContextBuilder(com.tvd12.ezyhttp.server.core.ApplicationContextBuilder) Test(org.testng.annotations.Test)

Example 3 with ApplicationContextBuilder

use of com.tvd12.ezyhttp.server.core.ApplicationContextBuilder in project ezyhttp by youngmonkeys.

the class ApplicationContextBuilderTest method buildViewContextViewContextBuilderIsNull.

@Test
public void buildViewContextViewContextBuilderIsNull() {
    // given
    EzyBeanContext beanContext = mock(EzyBeanContext.class);
    ApplicationContextBuilder sut = new ApplicationContextBuilder();
    // when
    ViewContext actual = MethodInvoker.create().object(sut).method("buildViewContext").param(EzyBeanContext.class, beanContext).invoke(ViewContext.class);
    // then
    Asserts.assertNull(actual);
    verify(beanContext, times(1)).getSingleton(ViewContext.class);
    verify(beanContext, times(1)).getSingleton(ViewContextBuilder.class);
}
Also used : ViewContext(com.tvd12.ezyhttp.server.core.view.ViewContext) EzyBeanContext(com.tvd12.ezyfox.bean.EzyBeanContext) ApplicationContextBuilder(com.tvd12.ezyhttp.server.core.ApplicationContextBuilder) Test(org.testng.annotations.Test)

Example 4 with ApplicationContextBuilder

use of com.tvd12.ezyhttp.server.core.ApplicationContextBuilder in project ezyhttp by youngmonkeys.

the class ApplicationContextBuilderTest method getPropertiesMapNull.

@Test
public void getPropertiesMapNull() {
    // given
    EzyReflection reflection = mock(EzyReflection.class);
    when(reflection.getExtendsClass(EzyPropertiesMap.class)).thenReturn(null);
    ApplicationContextBuilder sut = new ApplicationContextBuilder();
    // when
    EzyPropertiesMap actual = MethodInvoker.create().object(sut).method("getPropertiesMap").param(EzyReflection.class, reflection).invoke(EzyPropertiesMap.class);
    // then
    Asserts.assertNull(actual);
}
Also used : EzyReflection(com.tvd12.ezyfox.reflect.EzyReflection) EzyPropertiesMap(com.tvd12.ezyfox.bean.EzyPropertiesMap) ApplicationContextBuilder(com.tvd12.ezyhttp.server.core.ApplicationContextBuilder) Test(org.testng.annotations.Test)

Example 5 with ApplicationContextBuilder

use of com.tvd12.ezyhttp.server.core.ApplicationContextBuilder in project ezyhttp by youngmonkeys.

the class ApplicationContextBuilderTest method buildViewContextTemplateResolverNotNullIsNull.

@Test
public void buildViewContextTemplateResolverNotNullIsNull() {
    // given
    EzyBeanContext beanContext = mock(EzyBeanContext.class);
    ViewContextBuilder viewContextBuilder = mock(ViewContextBuilder.class);
    when(beanContext.getSingleton(ViewContextBuilder.class)).thenReturn(viewContextBuilder);
    TemplateResolver templateResolver = mock(TemplateResolver.class);
    when(beanContext.getSingleton(TemplateResolver.class)).thenReturn(templateResolver);
    when(viewContextBuilder.templateResolver(templateResolver)).thenReturn(viewContextBuilder);
    ViewContext viewContext = mock(ViewContext.class);
    when(viewContextBuilder.build()).thenReturn(viewContext);
    EzySingletonFactory singletonFactory = mock(EzySingletonFactory.class);
    when(beanContext.getSingletonFactory()).thenReturn(singletonFactory);
    ViewDialect viewDialect = mock(ViewDialect.class);
    List<ViewDialect> viewDialects = Collections.singletonList(viewDialect);
    when(beanContext.getSingletonsOf(ViewDialect.class)).thenReturn(viewDialects);
    when(viewContextBuilder.viewDialects(viewDialects)).thenReturn(viewContextBuilder);
    ViewDecorator viewDecorator = mock(ViewDecorator.class);
    List<ViewDecorator> viewDecorators = Collections.singletonList(viewDecorator);
    when(beanContext.getSingletonsOf(ViewDecorator.class)).thenReturn(viewDecorators);
    when(viewContextBuilder.viewDecorators(viewDecorators)).thenReturn(viewContextBuilder);
    MessageProvider messageProvider = mock(MessageProvider.class);
    List<MessageProvider> messageProviders = Collections.singletonList(messageProvider);
    when(beanContext.getSingletonsOf(MessageProvider.class)).thenReturn(messageProviders);
    when(viewContextBuilder.messageProviders(messageProviders)).thenReturn(viewContextBuilder);
    AbsentMessageResolver absentMessageResolver = mock(AbsentMessageResolver.class);
    when(beanContext.getSingleton(AbsentMessageResolver.class)).thenReturn(absentMessageResolver);
    when(viewContextBuilder.absentMessageResolver(absentMessageResolver)).thenReturn(viewContextBuilder);
    ApplicationContextBuilder sut = new ApplicationContextBuilder();
    // when
    ViewContext actual = MethodInvoker.create().object(sut).method("buildViewContext").param(EzyBeanContext.class, beanContext).invoke(ViewContext.class);
    // then
    Asserts.assertEquals(viewContext, actual);
    verify(beanContext, times(1)).getSingleton(ViewContext.class);
    verify(beanContext, times(1)).getSingleton(ViewContextBuilder.class);
    verify(beanContext, times(1)).getSingleton(TemplateResolver.class);
    verify(beanContext, times(1)).getSingletonsOf(ViewDialect.class);
    verify(beanContext, times(1)).getSingletonsOf(ViewDecorator.class);
    verify(beanContext, times(1)).getSingleton(AbsentMessageResolver.class);
    verify(beanContext, times(1)).getSingletonsOf(MessageProvider.class);
    verify(viewContextBuilder, times(1)).templateResolver(templateResolver);
    verify(viewContextBuilder, times(1)).viewDialects(viewDialects);
    verify(viewContextBuilder, times(1)).viewDecorators(viewDecorators);
    verify(viewContextBuilder, times(1)).messageProviders(messageProviders);
    verify(viewContextBuilder, times(1)).absentMessageResolver(absentMessageResolver);
    verify(viewContextBuilder, times(1)).build();
    verify(singletonFactory, times(1)).addSingleton(viewContext);
}
Also used : MessageProvider(com.tvd12.ezyhttp.server.core.view.MessageProvider) AbsentMessageResolver(com.tvd12.ezyhttp.server.core.view.AbsentMessageResolver) ViewContext(com.tvd12.ezyhttp.server.core.view.ViewContext) EzyBeanContext(com.tvd12.ezyfox.bean.EzyBeanContext) ViewContextBuilder(com.tvd12.ezyhttp.server.core.view.ViewContextBuilder) EzySingletonFactory(com.tvd12.ezyfox.bean.EzySingletonFactory) TemplateResolver(com.tvd12.ezyhttp.server.core.view.TemplateResolver) ViewDialect(com.tvd12.ezyhttp.server.core.view.ViewDialect) ViewDecorator(com.tvd12.ezyhttp.server.core.view.ViewDecorator) ApplicationContextBuilder(com.tvd12.ezyhttp.server.core.ApplicationContextBuilder) Test(org.testng.annotations.Test)

Aggregations

ApplicationContextBuilder (com.tvd12.ezyhttp.server.core.ApplicationContextBuilder)8 Test (org.testng.annotations.Test)8 EzyBeanContext (com.tvd12.ezyfox.bean.EzyBeanContext)6 ResourceResolver (com.tvd12.ezyhttp.server.core.resources.ResourceResolver)3 ViewContext (com.tvd12.ezyhttp.server.core.view.ViewContext)3 EzySingletonFactory (com.tvd12.ezyfox.bean.EzySingletonFactory)2 ResourceDownloadManager (com.tvd12.ezyhttp.core.resources.ResourceDownloadManager)2 ApplicationContext (com.tvd12.ezyhttp.server.core.ApplicationContext)2 ViewContextBuilder (com.tvd12.ezyhttp.server.core.view.ViewContextBuilder)2 EzyPackagesToScan (com.tvd12.ezyfox.annotation.EzyPackagesToScan)1 EzyPropertiesMap (com.tvd12.ezyfox.bean.EzyPropertiesMap)1 EzyReflection (com.tvd12.ezyfox.reflect.EzyReflection)1 ComponentClasses (com.tvd12.ezyhttp.server.core.annotation.ComponentClasses)1 ComponentsScan (com.tvd12.ezyhttp.server.core.annotation.ComponentsScan)1 PropertiesSources (com.tvd12.ezyhttp.server.core.annotation.PropertiesSources)1 ComponentManager (com.tvd12.ezyhttp.server.core.manager.ComponentManager)1 EventService (com.tvd12.ezyhttp.server.core.test.event.EventService)1 SourceService (com.tvd12.ezyhttp.server.core.test.event.SourceService)1 UserService (com.tvd12.ezyhttp.server.core.test.service.UserService)1 UserService0 (com.tvd12.ezyhttp.server.core.test.service.UserService0)1