Search in sources :

Example 11 with Ordered

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

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(org.springframework.boot.web.servlet.server.MockServletWebServerFactory) InOrder(org.mockito.InOrder) Filter(jakarta.servlet.Filter) Ordered(org.springframework.core.Ordered) Servlet(jakarta.servlet.Servlet) ServletContext(jakarta.servlet.ServletContext) Test(org.junit.jupiter.api.Test)

Example 12 with Ordered

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

the class DispatcherTests method ordersMappers.

@Test
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);
    then(mapper1).should(inOrder).getHandler(this.serverRequest);
    then(mapper2).should(inOrder).getHandler(this.serverRequest);
}
Also used : InOrder(org.mockito.InOrder) Ordered(org.springframework.core.Ordered) Test(org.junit.jupiter.api.Test)

Example 13 with Ordered

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

the class SpringBootDependencyInjectionTestExecutionListenerTests method orderShouldBeSameAsDependencyInjectionTestExecutionListener.

@Test
void orderShouldBeSameAsDependencyInjectionTestExecutionListener() {
    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.jupiter.api.Test)

Example 14 with Ordered

use of org.springframework.core.Ordered in project spring-cloud-gateway by spring-cloud.

the class GatewayFilterSpec method filter.

public GatewayFilterSpec filter(GatewayFilter gatewayFilter, int order) {
    if (gatewayFilter instanceof Ordered) {
        this.routeBuilder.filter(gatewayFilter);
        log.warn("GatewayFilter already implements ordered " + gatewayFilter.getClass() + "ignoring order parameter: " + order);
        return this;
    }
    this.routeBuilder.filter(new OrderedGatewayFilter(gatewayFilter, order));
    return this;
}
Also used : OrderedGatewayFilter(org.springframework.cloud.gateway.filter.OrderedGatewayFilter) Ordered(org.springframework.core.Ordered)

Example 15 with Ordered

use of org.springframework.core.Ordered in project spring-cloud-gateway by spring-cloud.

the class GatewayFilterSpecTests method testFilter.

private void testFilter(Class<? extends GatewayFilter> type, GatewayFilter gatewayFilter, int order) {
    ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
    Route.Builder routeBuilder = Route.builder().id("123").uri("abc:123").predicate(exchange -> true);
    RouteLocatorBuilder.Builder routes = new RouteLocatorBuilder(context).routes();
    GatewayFilterSpec spec = new GatewayFilterSpec(routeBuilder, routes);
    spec.filter(gatewayFilter);
    Route route = routeBuilder.build();
    assertThat(route.getFilters()).hasSize(1);
    GatewayFilter filter = route.getFilters().get(0);
    assertThat(filter).isInstanceOf(type);
    Ordered ordered = (Ordered) filter;
    assertThat(ordered.getOrder()).isEqualTo(order);
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Ordered(org.springframework.core.Ordered) Route(org.springframework.cloud.gateway.route.Route) GatewayFilter(org.springframework.cloud.gateway.filter.GatewayFilter) OrderedGatewayFilter(org.springframework.cloud.gateway.filter.OrderedGatewayFilter)

Aggregations

Ordered (org.springframework.core.Ordered)18 Test (org.junit.jupiter.api.Test)7 InOrder (org.mockito.InOrder)5 ServletContext (jakarta.servlet.ServletContext)4 Servlet (jakarta.servlet.Servlet)3 ArrayList (java.util.ArrayList)3 MockServletWebServerFactory (org.springframework.boot.web.servlet.server.MockServletWebServerFactory)3 OrderedGatewayFilter (org.springframework.cloud.gateway.filter.OrderedGatewayFilter)2 ApplicationListener (org.springframework.context.ApplicationListener)2 PriorityOrdered (org.springframework.core.PriorityOrdered)2 Filter (jakarta.servlet.Filter)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Set (java.util.Set)1 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)1 Supplier (java.util.function.Supplier)1 CodecFactory (org.grails.encoder.CodecFactory)1 StreamingEncoder (org.grails.encoder.StreamingEncoder)1