@Test
  public void testConcurrentLoginWithDisallowingAll() throws Exception {
    MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
    filterConfig.addInitParameter("disallowConcurrentLoginUsernames", "*");
    filter.init(filterConfig);

    MockHttpServletRequest req = new MockHttpServletRequest(servletContext);
    MockHttpServletResponse res = new MockHttpServletResponse();

    MockHttpSession sessionOfJohn1 = new MockHttpSession(servletContext);
    req.setUserPrincipal(new TransientUser("john"));
    req.setSession(sessionOfJohn1);
    filter.doFilter(req, res, new MockFilterChain());
    assertFalse(sessionOfJohn1.isInvalid());

    MockHttpSession sessionOfJohn2 = new MockHttpSession(servletContext);
    req.setUserPrincipal(new TransientUser("john"));
    req.setSession(sessionOfJohn2);
    filter.doFilter(req, res, new MockFilterChain());
    assertFalse(sessionOfJohn2.isInvalid());

    req.setSession(sessionOfJohn1);
    filter.doFilter(req, res, new MockFilterChain());
    assertTrue(sessionOfJohn1.isInvalid());

    req.setSession(sessionOfJohn2);
    filter.doFilter(req, res, new MockFilterChain());
    assertFalse(sessionOfJohn2.isInvalid());
  }
Example #2
0
 protected void setUp() throws Exception {
   filter = new StaticFilter();
   MockFilterConfig config = new MockFilterConfig();
   config.addInitParameter("includes", "/scripts/*");
   filter.init(config);
 }
  public void testOpenPersistenceManagerInViewFilter() throws Exception {
    MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
    final PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock();
    MockControl pmControl = MockControl.createControl(PersistenceManager.class);
    PersistenceManager pm = (PersistenceManager) pmControl.getMock();

    pmf.getPersistenceManager();
    pmfControl.setReturnValue(pm, 1);
    pm.close();
    pmControl.setVoidCallable(1);
    pmfControl.replay();
    pmControl.replay();

    MockControl pmf2Control = MockControl.createControl(PersistenceManagerFactory.class);
    final PersistenceManagerFactory pmf2 = (PersistenceManagerFactory) pmf2Control.getMock();
    MockControl pm2Control = MockControl.createControl(PersistenceManager.class);
    PersistenceManager pm2 = (PersistenceManager) pm2Control.getMock();

    pmf2.getPersistenceManager();
    pmf2Control.setReturnValue(pm2, 1);
    pm2.close();
    pm2Control.setVoidCallable(1);
    pmf2Control.replay();
    pm2Control.replay();

    MockServletContext sc = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.getDefaultListableBeanFactory().registerSingleton("persistenceManagerFactory", pmf);
    wac.getDefaultListableBeanFactory().registerSingleton("myPersistenceManagerFactory", pmf2);
    wac.refresh();
    sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    MockHttpServletRequest request = new MockHttpServletRequest(sc);
    MockHttpServletResponse response = new MockHttpServletResponse();

    MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
    MockFilterConfig filterConfig2 = new MockFilterConfig(wac.getServletContext(), "filter2");
    filterConfig2.addInitParameter(
        "persistenceManagerFactoryBeanName", "myPersistenceManagerFactory");

    final OpenPersistenceManagerInViewFilter filter = new OpenPersistenceManagerInViewFilter();
    filter.init(filterConfig);
    final OpenPersistenceManagerInViewFilter filter2 = new OpenPersistenceManagerInViewFilter();
    filter2.init(filterConfig2);

    final FilterChain filterChain =
        new FilterChain() {
          public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
            assertTrue(TransactionSynchronizationManager.hasResource(pmf));
            servletRequest.setAttribute("invoked", Boolean.TRUE);
          }
        };

    final FilterChain filterChain2 =
        new FilterChain() {
          public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
              throws IOException, ServletException {
            assertTrue(TransactionSynchronizationManager.hasResource(pmf2));
            filter.doFilter(servletRequest, servletResponse, filterChain);
          }
        };

    FilterChain filterChain3 = new PassThroughFilterChain(filter2, filterChain2);

    assertFalse(TransactionSynchronizationManager.hasResource(pmf));
    assertFalse(TransactionSynchronizationManager.hasResource(pmf2));
    filter2.doFilter(request, response, filterChain3);
    assertFalse(TransactionSynchronizationManager.hasResource(pmf));
    assertFalse(TransactionSynchronizationManager.hasResource(pmf2));
    assertNotNull(request.getAttribute("invoked"));

    pmfControl.verify();
    pmControl.verify();
    pmf2Control.verify();
    pm2Control.verify();

    wac.close();
  }
  public void testInit() throws Exception {
    filterConfig.addInitParameter("pattern", ".*");
    filter.init(filterConfig);

    assertEquals(Pattern.compile(".*").pattern(), filter.getPattern().pattern());
  }