public static void execute(Task task) {
    try {
      task.start();
      task.doPrivileged();
    } finally {
      task.stop();
    }
  }
@Test
  public void allow_everything_in_privileged_block_only() {
    UserSessionCatcherTask catcher = new UserSessionCatcherTask();

    DoPrivileged.execute(catcher);

    // verify the session used inside Privileged task
    assertThat(catcher.userSession.isLoggedIn()).isFalse();
    assertThat(catcher.userSession.hasComponentPermission("any permission", new ComponentDto())).isTrue();
    assertThat(catcher.userSession.isSystemAdministrator()).isTrue();
    assertThat(catcher.userSession.shouldResetPassword()).isFalse();
    assertThat(catcher.userSession.isActive()).isTrue();
    assertThat(catcher.userSession.hasChildProjectsPermission(USER, new ComponentDto().setUuid("uuid"))).isTrue();
    assertThat(catcher.userSession.hasPortfolioChildProjectsPermission(USER, new ComponentDto())).isTrue();
    assertThat(catcher.userSession.isAuthenticatedBrowserSession()).isFalse();

    // verify session in place after task is done
    assertThat(threadLocalUserSession.get()).isSameAs(session);
  }