Search in sources :

Example 6 with Ordered

use of org.springframework.core.Ordered in project spring-boot by spring-projects.

the class SpringBootDependencyInjectionTestExecutionListenerTests method orderShouldBeSameAsDependencyInjectionTestExecutionListener.

@Test
public void orderShouldBeSameAsDependencyInjectionTestExecutionListener() throws Exception {
    Ordered injectionListener = new DependencyInjectionTestExecutionListener();
    assertThat(this.reportListener.getOrder()).isEqualTo(injectionListener.getOrder());
}
Also used : Ordered(org.springframework.core.Ordered) DependencyInjectionTestExecutionListener(org.springframework.test.context.support.DependencyInjectionTestExecutionListener) Test(org.junit.Test)

Example 7 with Ordered

use of org.springframework.core.Ordered in project spring-boot by spring-projects.

the class DispatcherTests method ordersMappers.

@Test
public void ordersMappers() throws Exception {
    HandlerMapper mapper1 = mock(HandlerMapper.class, withSettings().extraInterfaces(Ordered.class));
    HandlerMapper mapper2 = mock(HandlerMapper.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) mapper1).getOrder()).willReturn(1);
    given(((Ordered) mapper2).getOrder()).willReturn(2);
    List<HandlerMapper> mappers = Arrays.asList(mapper2, mapper1);
    Dispatcher dispatcher = new Dispatcher(AccessManager.PERMIT_ALL, mappers);
    dispatcher.handle(this.serverRequest, this.serverResponse);
    InOrder inOrder = inOrder(mapper1, mapper2);
    inOrder.verify(mapper1).getHandler(this.serverRequest);
    inOrder.verify(mapper2).getHandler(this.serverRequest);
}
Also used : InOrder(org.mockito.InOrder) Ordered(org.springframework.core.Ordered) Test(org.junit.Test)

Example 8 with Ordered

use of org.springframework.core.Ordered in project spring-boot by spring-projects.

the class ServletWebServerApplicationContextTests method servletContextInitializerBeans.

@Test
public 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);
    ordered.verify(initializer1).onStartup(servletContext);
    ordered.verify(initializer2).onStartup(servletContext);
}
Also used : InOrder(org.mockito.InOrder) Ordered(org.springframework.core.Ordered) ServletContext(javax.servlet.ServletContext) ServletContextInitializer(org.springframework.boot.web.servlet.ServletContextInitializer) Test(org.junit.Test)

Example 9 with Ordered

use of org.springframework.core.Ordered in project spring-boot by spring-projects.

the class ServletWebServerApplicationContextTests method servletAndFilterBeans.

@Test
public void servletAndFilterBeans() throws Exception {
    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);
    verify(factory.getServletContext()).addServlet("servletBean", servlet);
    verify(factory.getRegisteredServlet(0).getRegistration()).addMapping("/");
    ordered.verify(factory.getServletContext()).addFilter("filterBean1", filter1);
    ordered.verify(factory.getServletContext()).addFilter("filterBean2", filter2);
    verify(factory.getRegisteredFilter(0).getRegistration()).addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
    verify(factory.getRegisteredFilter(1).getRegistration()).addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
}
Also used : MockServletWebServerFactory(org.springframework.boot.web.servlet.server.MockServletWebServerFactory) InOrder(org.mockito.InOrder) Filter(javax.servlet.Filter) Ordered(org.springframework.core.Ordered) Servlet(javax.servlet.Servlet) ServletContext(javax.servlet.ServletContext) Test(org.junit.Test)

Example 10 with Ordered

use of org.springframework.core.Ordered in project spring-framework by spring-projects.

the class PostProcessorRegistrationDelegate method registerBeanPostProcessors.

public static void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
    String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
    // Register BeanPostProcessorChecker that logs an info message when
    // a bean is created during BeanPostProcessor instantiation, i.e. when
    // a bean is not eligible for getting processed by all BeanPostProcessors.
    int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
    beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
    // Separate between BeanPostProcessors that implement PriorityOrdered,
    // Ordered, and the rest.
    List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
    List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
    List<String> orderedPostProcessorNames = new ArrayList<>();
    List<String> nonOrderedPostProcessorNames = new ArrayList<>();
    for (String ppName : postProcessorNames) {
        if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
            BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
            priorityOrderedPostProcessors.add(pp);
            if (pp instanceof MergedBeanDefinitionPostProcessor) {
                internalPostProcessors.add(pp);
            }
        } else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
            orderedPostProcessorNames.add(ppName);
        } else {
            nonOrderedPostProcessorNames.add(ppName);
        }
    }
    // First, register the BeanPostProcessors that implement PriorityOrdered.
    sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
    registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
    // Next, register the BeanPostProcessors that implement Ordered.
    List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>();
    for (String ppName : orderedPostProcessorNames) {
        BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
        orderedPostProcessors.add(pp);
        if (pp instanceof MergedBeanDefinitionPostProcessor) {
            internalPostProcessors.add(pp);
        }
    }
    sortPostProcessors(beanFactory, orderedPostProcessors);
    registerBeanPostProcessors(beanFactory, orderedPostProcessors);
    // Now, register all regular BeanPostProcessors.
    List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
    for (String ppName : nonOrderedPostProcessorNames) {
        BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
        nonOrderedPostProcessors.add(pp);
        if (pp instanceof MergedBeanDefinitionPostProcessor) {
            internalPostProcessors.add(pp);
        }
    }
    registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
    // Finally, re-register all internal BeanPostProcessors.
    sortPostProcessors(beanFactory, internalPostProcessors);
    registerBeanPostProcessors(beanFactory, internalPostProcessors);
    // Re-register post-processor for detecting inner beans as ApplicationListeners,
    // moving it to the end of the processor chain (for picking up proxies etc).
    beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}
Also used : BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) ArrayList(java.util.ArrayList) Ordered(org.springframework.core.Ordered) PriorityOrdered(org.springframework.core.PriorityOrdered) MergedBeanDefinitionPostProcessor(org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor)

Aggregations

Ordered (org.springframework.core.Ordered)10 Test (org.junit.Test)6 InOrder (org.mockito.InOrder)5 ServletContext (javax.servlet.ServletContext)4 Servlet (javax.servlet.Servlet)3 MockServletWebServerFactory (org.springframework.boot.web.servlet.server.MockServletWebServerFactory)3 ArrayList (java.util.ArrayList)2 PriorityOrdered (org.springframework.core.PriorityOrdered)2 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Filter (javax.servlet.Filter)1 CodecFactory (org.grails.encoder.CodecFactory)1 StreamingEncoder (org.grails.encoder.StreamingEncoder)1 BeanFactoryPostProcessor (org.springframework.beans.factory.config.BeanFactoryPostProcessor)1 BeanPostProcessor (org.springframework.beans.factory.config.BeanPostProcessor)1 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)1 BeanDefinitionRegistryPostProcessor (org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor)1 MergedBeanDefinitionPostProcessor (org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor)1 ServletContextInitializer (org.springframework.boot.web.servlet.ServletContextInitializer)1 LiteDeviceDelegatingViewResolver (org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver)1