// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package adaptorlib;

import static org.junit.Assert.*;

import org.junit.Test;

/**
 * Test cases for {@link SessionManager}.
 */
public class SessionManagerTest {
  private MockTimeProvider timeProvider = new MockTimeProvider();
  private SessionManager<Reference> sessionManager
      = new SessionManager<Reference>(
          timeProvider, new ReferenceClientStore(), 1000, 500);

  @Test
  public void testGetSessionNormal() {
    Reference ref1 = new Reference();
    Reference ref2 = new Reference();

    Session sess1 = sessionManager.getSession(ref1, false);
    assertNull(sess1);

    sess1 = sessionManager.getSession(ref1);
    Session sess2 = sessionManager.getSession(ref2);
    assertNotNull(sess1);
    assertNotNull(sess2);
    assertNotSame(sess1, sess2);
  }

  @Test
  public void testSessionExpires() {
    Reference ref1 = new Reference();
    Session sess1 = sessionManager.getSession(ref1);
    assertNotNull(sess1);

    timeProvider.time += 1000;
    sess1 = sessionManager.getSession(ref1, false);
    assertNull(sess1);
  }

  @Test
  public void testSessionExpiresDuringCreate() {
    Reference ref1 = new Reference();
    Session sess1 = sessionManager.getSession(ref1);
    assertNotNull(sess1);

    timeProvider.time += 1000;
    // Create a new session to allow checking for expired sessions.
    sessionManager.getSession(new Reference());
    assertEquals(1, sessionManager.getSessionCount());

    sess1 = sessionManager.getSession(ref1, false);
    assertNull(sess1);
  }

  @Test
  public void testSessionNotExpiredDuringCreate() {
    // Create a trash session, just so we can verify that the cleanup ran.
    sessionManager.getSession(new Reference());

    timeProvider.time += 500;
    Reference ref1 = new Reference();
    Session sess1 = sessionManager.getSession(ref1);
    assertNotNull(sess1);
    assertEquals(2, sessionManager.getSessionCount());

    timeProvider.time += 500;
    // Create a new session to allow checking for expired sessions.
    sessionManager.getSession(new Reference());
    assertEquals(2, sessionManager.getSessionCount());

    Session sess2 = sessionManager.getSession(ref1, false);
    assertSame(sess1, sess2);
  }

  @Test
  public void testCreateAfterSessionExpires() {
    Reference ref1 = new Reference();
    Session sess1 = sessionManager.getSession(ref1);
    assertNotNull(sess1);

    timeProvider.time += 1000;
    Session sess2 = sessionManager.getSession(ref1);
    assertNotSame(sess1, sess2);
  }

  private static class ReferenceClientStore
      implements SessionManager.ClientStore<Reference> {
    public String retrieve(Reference clientState) {
      return (String) clientState.ref;
    }

    public void store(Reference clientState, String value) {
      clientState.ref = value;
    }
  }

  private static class Reference {
    public Object ref;
  }
}

@@ -671,22 +671,106 @@
 ort 
-org.junit.Test
+com.sun.net.httpserver.HttpExchange;%0A%0Aimport org.junit.*;%0Aimport org.junit.rules.ExpectedException
 ;%0A%0A/
@@ -1073,16 +1073,87 @@
  500);%0A%0A
+  @Rule%0A  public ExpectedException thrown = ExpectedException.none();%0A%0A
   @Test%0A
@@ -3248,24 +3248,926 @@
 ess2);%0A  %7D%0A%0A
+  @Test%0A  public void testHttpExchangeClientStoreInitNull() %7B%0A    thrown.expect(NullPointerException.class);%0A    new SessionManager.HttpExchangeClientStore(null);%0A  %7D%0A%0A  @Test%0A  public void testHttpExchangeClientStoreCookies() %7B%0A    SessionManager.ClientStore%3CHttpExchange%3E clientStore1%0A        = new SessionManager.HttpExchangeClientStore(%22test1%22);%0A    SessionManager.ClientStore%3CHttpExchange%3E clientStore2%0A        = new SessionManager.HttpExchangeClientStore(%22test2%22);%0A    SessionManager.ClientStore%3CHttpExchange%3E clientStore3%0A        = new SessionManager.HttpExchangeClientStore(%22notfound%22);%0A    HttpExchange ex = new MockHttpExchange(%22http%22, %22GET%22, %22/%22, null);%0A    ex.getRequestHeaders().set(%22Cookie%22, %22test1=value1; value; test2=value2%22);%0A    assertEquals(%22value1%22, clientStore1.retrieve(ex));%0A    assertEquals(%22value2%22, clientStore2.retrieve(ex));%0A    assertNull(clientStore3.retrieve(ex));%0A  %7D%0A%0A
   private st

Add tests for HttpExchangeClientStore