/*
 * Copyright (C) 2008 Google Inc.
 *
 * 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 com.google.inject.testing.guiceberry;

import com.google.common.base.Preconditions;

import junit.framework.TestCase;

import java.util.Random;

import javax.servlet.http.Cookie;

/**
 * A {@link TestId} is created by GuiceBerry (and made
 * available to @Inject in tests), and uniquely identify a test. This ID is
 * also set as a cookie so the server serving a request can be matched with 
 * the test that initiated this, thus making it possible to control (say) 
 * injections from within a Functional Test.
 * 
 * @author zorzella
 */
//@Immutable
public final class TestId implements Comparable<TestId>, CharSequence {

  public static final String COOKIE_NAME = "testid";
  
  private final String testCaseName;
  private final String testMethodName;
  private final long random;
  private final String asString;

  /**
   * @deprecated This method is going away soon. Inline it!
   */
  @Deprecated
  public TestId(TestCase testCase) {
    this(testCase.getClass().getName(), testCase.getName());
  }
  
  public TestId(String testCaseName, String testMethodName) {
    this(testCaseName, testMethodName, new Random().nextInt(1000));
  }

  private TestId(String testCaseName, String testMethodName, long random) {
    this.testCaseName = testCaseName; 
    this.testMethodName = testMethodName;
    this.random = random;
    this.asString = getAsString(testCaseName, testMethodName, random);
  }

  private static String getAsString(String testCaseName, String testMethodName,
      long random) {
    return testMethodName + ":" + testCaseName + ":" + random;
  }

  public TestId(Cookie cookie) {
    String[] parts = cookie.getValue().split(":");
    Preconditions.checkState(parts.length == 3);
    this.testMethodName = parts[0];
    this.testCaseName = parts[1];
    this.random = Long.parseLong(parts[2]);
    this.asString = getAsString(testCaseName, testMethodName, random);
  }

  @Override
  public String toString() {
    return asString;
  }
  
  public int compareTo(TestId that) {
    return this.asString.compareTo(that.asString);
  }
  
  @Override
  public boolean equals(Object that) {
    if (!(that instanceof TestId)) {
      return false;
    }
    return this.asString.equals(((TestId)that).asString);
  }
  
  @Override
  public int hashCode() {
    return this.asString.hashCode();
  }

  public char charAt(int index) {
    return asString.charAt(index);
  }

  public int length() {
    return asString.length();
  }

  public CharSequence subSequence(int start, int end) {
    return asString.subSequence(start, end);
  }
}

@@ -686,42 +686,8 @@
 s;%0A%0A
-import junit.framework.TestCase;%0A%0A
 impo
@@ -1388,199 +1388,8 @@
 g;%0A%0A
-  /**%0A   * @deprecated This method is going away soon. Inline it!%0A   */%0A  @Deprecated%0A  public TestId(TestCase testCase) %7B%0A    this(testCase.getClass().getName(), testCase.getName());%0A  %7D%0A  %0A
   pu

Remove TestId deprecated constructor