### Question:
SpecificPageFilterFactory implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request; String selector = slingRequest.getRequestPathInfo().getSelectorString(); if (selector == null) { chain.doFilter(request, response); return; } WCMMode wcmMode = WCMMode.fromRequest(request); if (!WCMMode.DISABLED.equals(wcmMode)) { chain.doFilter(request, response); return; } Resource page = slingRequest.getResource(); LOGGER.debug("Checking sub-pages for {}", slingRequest.getRequestURI()); Resource subPage = UrlProviderImpl.toSpecificPage(page, selector); if (subPage != null) { RequestDispatcher dispatcher = slingRequest.getRequestDispatcher(subPage); dispatcher.forward(slingRequest, response); return; } chain.doFilter(request, response); }  @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); @Override void destroy();  }### Answer:
@Test public void testFilterForwardingWithStringProperty() throws IOException, ServletException { request.setResource(context.resourceResolver().resolve("/content/product-page")); MockRequestPathInfo pathInfo = (MockRequestPathInfo) request.getRequestPathInfo(); pathInfo.setSelectorString("productId1"); filter.doFilter(request, null, chain); ResourcePathMatcher matcher = new ResourcePathMatcher("/content/product-page/sub-page"); Mockito.verify(requestDispatcherFactory).getRequestDispatcher(argThat(matcher), isNull(RequestDispatcherOptions.class)); Mockito.verify(chain, Mockito.times(0)).doFilter(request, null); }
@Test public void testFilterNoopWcmmode() throws IOException, ServletException { request.setResource(context.resourceResolver().resolve("/content/product-page")); MockRequestPathInfo pathInfo = (MockRequestPathInfo) request.getRequestPathInfo(); pathInfo.setSelectorString("productId1"); request.setAttribute(WCMMode.class.getName(), WCMMode.EDIT); filter.doFilter(request, null, chain); Mockito.verify(requestDispatcherFactory, Mockito.times(0)).getRequestDispatcher(any(Resource.class), any()); Mockito.verify(chain).doFilter(request, null); }
@Test public void testFilterForwardingWithStringArrayProperty() throws IOException, ServletException { request.setResource(context.resourceResolver().resolve("/content/category-page")); MockRequestPathInfo pathInfo = (MockRequestPathInfo) request.getRequestPathInfo(); pathInfo.setSelectorString("categoryId2"); filter.doFilter(request, null, chain); ResourcePathMatcher matcher = new ResourcePathMatcher("/content/category-page/sub-page"); Mockito.verify(requestDispatcherFactory).getRequestDispatcher(argThat(matcher), isNull(RequestDispatcherOptions.class)); Mockito.verify(chain, Mockito.times(0)).doFilter(request, null); }
@Test public void testFilterForwardingNoMatch() throws IOException, ServletException { request.setResource(context.resourceResolver().resolve("/content/product-page")); MockRequestPathInfo pathInfo = (MockRequestPathInfo) request.getRequestPathInfo(); pathInfo.setSelectorString("productId3"); filter.doFilter(request, null, chain); Mockito.verify(requestDispatcherFactory, Mockito.times(0)).getRequestDispatcher(any(Resource.class), any()); Mockito.verify(chain).doFilter(request, null); }
@Test public void testFilterForwardingNoSelector() throws IOException, ServletException { request.setResource(context.resourceResolver().resolve("/content/product-page")); filter.doFilter(request, null, chain); Mockito.verify(requestDispatcherFactory, Mockito.times(0)).getRequestDispatcher(any(Resource.class), any()); Mockito.verify(chain).doFilter(request, null); }