package com.carrotsearch.randomizedtesting;

import java.util.Random;

/**
 * Context variables for an execution of a test suite (hooks and tests) running
 * under a {@link RandomizedRunner}.
 */
public final class RandomizedContext {
  /** Per thread context. */
  private final static ThreadLocal<RandomizedContext> context = new ThreadLocal<RandomizedContext>();

  /** @see #getTargetClass() */
  Class<?> targetClass;

  /** @see #getRandomness() */
  Randomness randomness;

  public RandomizedContext(Class<?> targetClass) {
    this.targetClass = targetClass;
  }
  
  /** The class (suite) being tested. */
  public Class<?> getTargetClass() {
    return targetClass;
  }

  /** Source of randomness for the context's thread. */
  public Randomness getRandomness() {
    return randomness;
  }

  /**
   * A shorthand for calling {@link #getRandomness()} and then {@link Randomness#getRandom()}. 
   */
  public Random getRandom() {
    return getRandomness().getRandom();
  }

  /**
   * Sets the context for the current thread.
   */
  static void setContext(RandomizedContext ctx) {
    context.set(ctx);
  }

  /**
   * Clear the context for the current thread.
   */
  static void clearContext() {
    setContext(null);
  }

  /**
   * @return Returns the context for the calling thread or throws an
   *         {@link IllegalStateException} if the thread is out of scope.
   */
  public static RandomizedContext current() {
    RandomizedContext ctx = context.get();
    if (ctx == null) {
      throw new IllegalStateException("No context information, is this test/ thread running under " +
      		RandomizedRunner.class + " runner? Add @RunWith(" + RandomizedRunner.class + ".class)" +
      				" to your test class");
    }
    return ctx;
  }
}


@@ -1084,24 +1084,136 @@
 text ctx) %7B%0A
+    if (context.get() != null) %7B%0A      throw new Error(%22Recursive context stack not implemented (yet).%22);%0A    %7D%0A
     context.
@@ -1222,16 +1222,16 @@
 t(ctx);%0A
-
   %7D%0A%0A  /
@@ -1321,26 +1321,27 @@
 ) %7B%0A    
-setC
+c
 ontext
+.set
 (null);%0A

Add an assertion about recursive context stacking.