/**
   * Test that executeAnonymousLogin will attempt to recover after an UnknownSessionException is
   * thrown.
   *
   * @throws Exception
   */
  @Test
  public void testExecuteAnonymousLoginForAnonUserWithInvalidSession() throws Exception {
    // ******
    // Delete the session directly to mimic what I think is the cause of the Unknown
    // SessionException
    // ******
    sessionDAO.delete(simpleSession);

    // Verify this does not throw an exception when the session is expired
    assertThat(
        "Anonymous user was not logged in after UnknownSessionException",
        callExecuteAnonymousLogin());
  }
  @Before
  public void bindSubjectToThread() {
    // setup a simple realm for authc
    SimpleAccountRealm simpleAccountRealm = new SimpleAccountRealm();
    simpleAccountRealm.addAccount("anonymous", "anonymous");
    DefaultSecurityManager securityManager = new DefaultSecurityManager();
    securityManager.setRealm(simpleAccountRealm);

    DefaultSessionManager sessionManager =
        (DefaultSessionManager) securityManager.getSessionManager();
    sessionDAO = new EnterpriseCacheSessionDAO();
    sessionManager.setSessionDAO(sessionDAO);

    simpleSession = new SimpleSession();
    sessionDAO.create(simpleSession);

    List<PrincipalCollection> principalCollectionList = new ArrayList<PrincipalCollection>();
    principalCollectionList.add(new SimplePrincipalCollection("other Principal", "some-realm"));

    simpleSession.setAttribute(
        DelegatingSubject.class.getName() + ".RUN_AS_PRINCIPALS_SESSION_KEY",
        principalCollectionList);

    DelegatingSession delegatingSession =
        new DelegatingSession(sessionManager, new DefaultSessionKey(simpleSession.getId()));

    // set the user

    subject =
        new DelegatingSubject(
            new SimplePrincipalCollection("anonymous", "realmName"),
            true,
            null,
            delegatingSession,
            securityManager);
    ThreadContext.bind(subject);
  }