All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mockit.internal.state.TestRun Maven / Gradle / Ivy

Go to download

JMockit is a Java toolkit for developer (unit/integration) testing. It contains mocking APIs and other tools, supporting both JUnit and TestNG. The mocking APIs allow all kinds of Java code, without testability restrictions, to be tested in isolation from selected dependencies.

There is a newer version: 1.7
Show newest version
/*
 * Copyright (c) 2006-2013 Rogério Liesenfeld
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.internal.state;

import org.jetbrains.annotations.*;

import mockit.internal.mockups.*;
import mockit.internal.expectations.*;
import mockit.internal.expectations.mocking.*;

/**
 * A singleton which stores several data structures which in turn hold global state for individual test methods, test
 * classes, and for the test run as a whole.
 */
public final class TestRun
{
   private static final TestRun INSTANCE = new TestRun();
   private TestRun() {}

   // Fields with global state ////////////////////////////////////////////////////////////////////////////////////////

   private static final ThreadLocal noMockingCount = new ThreadLocal()
   {
      @Override
      protected Integer initialValue() { return 0; }

      @Override
      public void set(Integer valueToAdd)
      {
         super.set(get() + valueToAdd);
      }
   };

   // Used only by the Coverage tool:
   private int testId;

   @Nullable private Class currentTestClass;
   @Nullable private Object currentTestInstance;
   @Nullable private SavePoint savePointForTestMethod;
   @Nullable private SavePoint savePointForTestClass;
   @Nullable private SharedFieldTypeRedefinitions sharedFieldTypeRedefinitions;

   @NotNull private final MockFixture mockFixture = new MockFixture();

   @NotNull private final ExecutingTest executingTest = new ExecutingTest();
   @NotNull private final MockClasses mockClasses = new MockClasses();

   // Static "getters" for global state ///////////////////////////////////////////////////////////////////////////////

   public static boolean isInsideNoMockingZone() { return noMockingCount.get() > 0; }

   @Nullable public static Class getCurrentTestClass() { return INSTANCE.currentTestClass; }

   @Nullable public static Object getCurrentTestInstance() { return INSTANCE.currentTestInstance; }

   @SuppressWarnings("unused")
   public static int getTestId() { return INSTANCE.testId; }

   @Nullable public static SavePoint getSavePointForTestClass() { return INSTANCE.savePointForTestClass; }

   @Nullable public static SharedFieldTypeRedefinitions getSharedFieldTypeRedefinitions()
   {
      return INSTANCE.sharedFieldTypeRedefinitions;
   }

   @NotNull public static MockFixture mockFixture() { return INSTANCE.mockFixture; }

   @NotNull public static ExecutingTest getExecutingTest() { return INSTANCE.executingTest; }

   @Nullable public static RecordAndReplayExecution getRecordAndReplayForRunningTest()
   {
      return INSTANCE.currentTestInstance == null ? null : INSTANCE.executingTest.getCurrentRecordAndReplay();
   }

   @Nullable public static RecordAndReplayExecution getRecordAndReplayForRunningTest(boolean create)
   {
      if (INSTANCE.currentTestInstance == null) {
         return null;
      }

      return INSTANCE.executingTest.getRecordAndReplay(INSTANCE.savePointForTestMethod != null && create);
   }

   @NotNull public static MockClasses getMockClasses() { return INSTANCE.mockClasses; }

   public static void verifyExpectationsOnAnnotatedMocks()
   {
      getMockClasses().getMockStates().verifyExpectations();
   }

   // Static "mutators" for global state //////////////////////////////////////////////////////////////////////////////

   public static void resetExpectationsOnAnnotatedMocks()
   {
      getMockClasses().getMockStates().resetExpectations();
   }

   public static void setCurrentTestClass(@Nullable Class testClass)
   {
      INSTANCE.currentTestClass = testClass;
   }

   public static void prepareForNextTest()
   {
      INSTANCE.testId++;
      INSTANCE.executingTest.setRecordAndReplay(null);
   }

   public static void setSavePointForTestMethod(@Nullable SavePoint savePointForTestMethod)
   {
      INSTANCE.savePointForTestMethod = savePointForTestMethod;

      if (savePointForTestMethod != null) {
         INSTANCE.executingTest.clearRecordAndReplayForVerifications();
      }
   }

   public static void enterNoMockingZone() { noMockingCount.set(1); }
   public static void exitNoMockingZone() { noMockingCount.set(-1); }

   public static void setRunningIndividualTest(@Nullable Object testInstance)
   {
      INSTANCE.currentTestInstance = testInstance;
   }

   public static void setSavePointForTestClass(@Nullable SavePoint savePoint)
   {
      INSTANCE.savePointForTestClass = savePoint;
   }

   public static void setSharedFieldTypeRedefinitions(@Nullable SharedFieldTypeRedefinitions redefinitions)
   {
      INSTANCE.sharedFieldTypeRedefinitions = redefinitions;
   }

   public static void finishCurrentTestExecution(boolean clearSharedMocks)
   {
      INSTANCE.savePointForTestMethod = null;
      INSTANCE.executingTest.finishExecution(clearSharedMocks);
   }

   // Methods to be called only from generated bytecode or from the MockingBridge /////////////////////////////////////

   @Nullable public static Object getMock(int index)
   {
      return getMockClasses().regularMocks.getMock(index);
   }

   @SuppressWarnings("unused")
   @Nullable public static Object getStartupMock(int index)
   {
      return getMockClasses().startupMocks.getMock(index);
   }

   public static boolean updateMockState(@NotNull String mockClassDesc, int mockStateIndex)
   {
      MockStates mockStates = getMockStates();
      return mockStates.updateMockState(mockClassDesc, mockStateIndex);
   }

   @NotNull private static MockStates getMockStates()
   {
      return getMockClasses().getMockStates();
   }

   public static void exitReentrantMock(@NotNull String mockClassDesc, int mockStateIndex)
   {
      MockStates mockStates = getMockStates();
      mockStates.exitReentrantMock(mockClassDesc, mockStateIndex);
   }

   @Nullable
   public static MockInvocation createMockInvocation(
      @NotNull String mockClassDesc, int mockStateIndex, @Nullable Object mockedInstance, @NotNull Object[] mockArgs)
   {
      MockStates mockStates = getMockStates();
      return mockStates.createMockInvocation(mockClassDesc, mockStateIndex, mockedInstance, mockArgs);
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy