Search in sources :

Example 1 with Ordered

use of cn.taketoday.core.Ordered in project today-infrastructure by TAKETODAY.

the class ServletWebServerApplicationContextTests method servletContextInitializerBeans.

@Test
void servletContextInitializerBeans() throws Exception {
    addWebServerFactoryBean();
    ServletContextInitializer initializer1 = mock(ServletContextInitializer.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) initializer1).getOrder()).willReturn(1);
    ServletContextInitializer initializer2 = mock(ServletContextInitializer.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) initializer2).getOrder()).willReturn(2);
    this.context.registerBeanDefinition("initializerBean2", beanDefinition(initializer2));
    this.context.registerBeanDefinition("initializerBean1", beanDefinition(initializer1));
    this.context.refresh();
    ServletContext servletContext = getWebServerFactory().getServletContext();
    InOrder ordered = inOrder(initializer1, initializer2);
    then(initializer1).should(ordered).onStartup(servletContext);
    then(initializer2).should(ordered).onStartup(servletContext);
}
Also used : InOrder(org.mockito.InOrder) Ordered(cn.taketoday.core.Ordered) ServletContext(jakarta.servlet.ServletContext) ServletContextInitializer(cn.taketoday.framework.web.servlet.ServletContextInitializer) Test(org.junit.jupiter.api.Test)

Example 2 with Ordered

use of cn.taketoday.core.Ordered in project today-infrastructure by TAKETODAY.

the class ServletWebServerApplicationContextTests method multipleServletBeansWithMainDispatcher.

@Test
void multipleServletBeansWithMainDispatcher() {
    addWebServerFactoryBean();
    Servlet servlet1 = mock(Servlet.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) servlet1).getOrder()).willReturn(1);
    Servlet servlet2 = mock(Servlet.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) servlet2).getOrder()).willReturn(2);
    this.context.registerBeanDefinition("servletBean2", beanDefinition(servlet2));
    this.context.registerBeanDefinition("dispatcherServlet", beanDefinition(servlet1));
    this.context.refresh();
    MockServletWebServerFactory factory = getWebServerFactory();
    ServletContext servletContext = factory.getServletContext();
    InOrder ordered = inOrder(servletContext);
    then(servletContext).should(ordered).addServlet("dispatcherServlet", servlet1);
    then(servletContext).should(ordered).addServlet("servletBean2", servlet2);
    then(factory.getRegisteredServlet(0).getRegistration()).should().addMapping("/");
    then(factory.getRegisteredServlet(1).getRegistration()).should().addMapping("/servletBean2/");
}
Also used : MockServletWebServerFactory(cn.taketoday.framework.web.servlet.server.MockServletWebServerFactory) InOrder(org.mockito.InOrder) Ordered(cn.taketoday.core.Ordered) Servlet(jakarta.servlet.Servlet) ServletContext(jakarta.servlet.ServletContext) Test(org.junit.jupiter.api.Test)

Example 3 with Ordered

use of cn.taketoday.core.Ordered in project today-infrastructure by TAKETODAY.

the class ServletWebServerApplicationContextTests method servletAndFilterBeans.

@Test
void servletAndFilterBeans() {
    addWebServerFactoryBean();
    Servlet servlet = mock(Servlet.class);
    Filter filter1 = mock(Filter.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) filter1).getOrder()).willReturn(1);
    Filter filter2 = mock(Filter.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) filter2).getOrder()).willReturn(2);
    this.context.registerBeanDefinition("servletBean", beanDefinition(servlet));
    this.context.registerBeanDefinition("filterBean2", beanDefinition(filter2));
    this.context.registerBeanDefinition("filterBean1", beanDefinition(filter1));
    this.context.refresh();
    MockServletWebServerFactory factory = getWebServerFactory();
    ServletContext servletContext = factory.getServletContext();
    InOrder ordered = inOrder(servletContext);
    then(factory.getServletContext()).should().addServlet("servletBean", servlet);
    then(factory.getRegisteredServlet(0).getRegistration()).should().addMapping("/");
    then(factory.getServletContext()).should(ordered).addFilter("filterBean1", filter1);
    then(factory.getServletContext()).should(ordered).addFilter("filterBean2", filter2);
    then(factory.getRegisteredFilter(0).getRegistration()).should().addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
    then(factory.getRegisteredFilter(1).getRegistration()).should().addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
}
Also used : MockServletWebServerFactory(cn.taketoday.framework.web.servlet.server.MockServletWebServerFactory) InOrder(org.mockito.InOrder) Filter(jakarta.servlet.Filter) Ordered(cn.taketoday.core.Ordered) Servlet(jakarta.servlet.Servlet) ServletContext(jakarta.servlet.ServletContext) Test(org.junit.jupiter.api.Test)

Example 4 with Ordered

use of cn.taketoday.core.Ordered in project today-infrastructure by TAKETODAY.

the class ServletWebServerApplicationContextTests method multipleServletBeans.

@Test
void multipleServletBeans() {
    addWebServerFactoryBean();
    Servlet servlet1 = mock(Servlet.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) servlet1).getOrder()).willReturn(1);
    Servlet servlet2 = mock(Servlet.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) servlet2).getOrder()).willReturn(2);
    this.context.registerBeanDefinition("servletBean2", beanDefinition(servlet2));
    this.context.registerBeanDefinition("servletBean1", beanDefinition(servlet1));
    this.context.refresh();
    MockServletWebServerFactory factory = getWebServerFactory();
    ServletContext servletContext = factory.getServletContext();
    InOrder ordered = inOrder(servletContext);
    then(servletContext).should(ordered).addServlet("servletBean1", servlet1);
    then(servletContext).should(ordered).addServlet("servletBean2", servlet2);
    then(factory.getRegisteredServlet(0).getRegistration()).should().addMapping("/servletBean1/");
    then(factory.getRegisteredServlet(1).getRegistration()).should().addMapping("/servletBean2/");
}
Also used : MockServletWebServerFactory(cn.taketoday.framework.web.servlet.server.MockServletWebServerFactory) InOrder(org.mockito.InOrder) Ordered(cn.taketoday.core.Ordered) Servlet(jakarta.servlet.Servlet) ServletContext(jakarta.servlet.ServletContext) Test(org.junit.jupiter.api.Test)

Example 5 with Ordered

use of cn.taketoday.core.Ordered in project today-framework by TAKETODAY.

the class ServletWebServerApplicationContextTests method multipleServletBeans.

@Test
void multipleServletBeans() {
    addWebServerFactoryBean();
    Servlet servlet1 = mock(Servlet.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) servlet1).getOrder()).willReturn(1);
    Servlet servlet2 = mock(Servlet.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) servlet2).getOrder()).willReturn(2);
    this.context.registerBeanDefinition("servletBean2", beanDefinition(servlet2));
    this.context.registerBeanDefinition("servletBean1", beanDefinition(servlet1));
    this.context.refresh();
    MockServletWebServerFactory factory = getWebServerFactory();
    ServletContext servletContext = factory.getServletContext();
    InOrder ordered = inOrder(servletContext);
    then(servletContext).should(ordered).addServlet("servletBean1", servlet1);
    then(servletContext).should(ordered).addServlet("servletBean2", servlet2);
    then(factory.getRegisteredServlet(0).getRegistration()).should().addMapping("/servletBean1/");
    then(factory.getRegisteredServlet(1).getRegistration()).should().addMapping("/servletBean2/");
}
Also used : MockServletWebServerFactory(cn.taketoday.framework.web.servlet.server.MockServletWebServerFactory) InOrder(org.mockito.InOrder) Ordered(cn.taketoday.core.Ordered) Servlet(jakarta.servlet.Servlet) ServletContext(jakarta.servlet.ServletContext) Test(org.junit.jupiter.api.Test)

Aggregations

Ordered (cn.taketoday.core.Ordered)8 ServletContext (jakarta.servlet.ServletContext)8 Test (org.junit.jupiter.api.Test)8 InOrder (org.mockito.InOrder)8 MockServletWebServerFactory (cn.taketoday.framework.web.servlet.server.MockServletWebServerFactory)6 Servlet (jakarta.servlet.Servlet)6 ServletContextInitializer (cn.taketoday.framework.web.servlet.ServletContextInitializer)2 Filter (jakarta.servlet.Filter)2