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

mockit.internal.mockups.MockStates Maven / Gradle / Ivy

Go to download

JMockit is a Java toolkit for automated developer testing. It contains mocking/faking APIs and a code coverage tool, 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.49
Show newest version
/*
 * Copyright (c) 2006 Rogério Liesenfeld
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.internal.mockups;

import java.util.*;
import java.util.Map.*;
import java.util.regex.*;
import javax.annotation.*;

import mockit.internal.util.*;

/**
 * Holds state associated with mock class containing {@linkplain mockit.Mock annotated mocks}.
 */
public final class MockStates
{
   private static final Pattern SPACE = Pattern.compile(" ");

   /**
    * For each mockup instance and each {@code @Mock} method containing the {@code Invocation} parameter or an
    * invocation count constraint, a runtime state will be kept here.
    */
   @Nonnull private final Map> mockUpsToMockStates;
   @Nonnull private final Map> startupMockUpsToMockStates;

   /**
    * For each annotated mock method with at least one invocation expectation, its mock state will
    * also be kept here, as an optimization.
    */
   @Nonnull private final Set mockStatesWithExpectations;

   public MockStates()
   {
      startupMockUpsToMockStates = new IdentityHashMap>(2);
      mockUpsToMockStates = new IdentityHashMap>(8);
      mockStatesWithExpectations = new LinkedHashSet(10);
   }

   void addStartupMockUpAndItsMockStates(@Nonnull Object mockUp, @Nonnull List mockStates)
   {
      startupMockUpsToMockStates.put(mockUp, mockStates);
   }

   void addMockStates(@Nonnull Iterable mockStates)
   {
      for (MockState mockState : mockStates) {
         if (mockState.isWithExpectations()) {
            mockStatesWithExpectations.add(mockState);
         }
      }
   }

   void addMockUpAndItsMockStates(@Nonnull Object mockUp, @Nonnull List mockStates)
   {
      mockUpsToMockStates.put(mockUp, mockStates);
   }

   public void copyMockStates(@Nonnull Object previousMockUp, @Nonnull Object newMockUp)
   {
      List mockStates = mockUpsToMockStates.get(previousMockUp);

      if (mockStates != null) {
         List copiedMockStates = new ArrayList(mockStates.size());

         for (MockState mockState : mockStates) {
            copiedMockStates.add(new MockState(mockState));
         }

         mockUpsToMockStates.put(newMockUp, copiedMockStates);
      }
   }

   public void removeClassState(@Nonnull Class redefinedClass, @Nullable String internalNameForOneOrMoreMockClasses)
   {
      removeMockStates(redefinedClass);

      if (internalNameForOneOrMoreMockClasses != null) {
         if (internalNameForOneOrMoreMockClasses.indexOf(' ') < 0) {
            removeMockStates(internalNameForOneOrMoreMockClasses);
         }
         else {
            String[] mockClassesInternalNames = SPACE.split(internalNameForOneOrMoreMockClasses);

            for (String mockClassInternalName : mockClassesInternalNames) {
               removeMockStates(mockClassInternalName);
            }
         }
      }
   }

   private void removeMockStates(@Nonnull Class redefinedClass)
   {
      for (Iterator> itr = mockUpsToMockStates.values().iterator(); itr.hasNext(); ) {
         List mockStates = itr.next();
         MockState mockState = mockStates.get(0);

         if (mockState.getRealClass() == redefinedClass) {
            mockStatesWithExpectations.removeAll(mockStates);
            mockStates.clear();
            itr.remove();
         }
      }
   }

   private void removeMockStates(@Nonnull String mockClassInternalName)
   {
      Class mockUpClass = ClassLoad.loadClass(mockClassInternalName.replace('/', '.'));

      for (Iterator>> itr = mockUpsToMockStates.entrySet().iterator(); itr.hasNext(); ) {
         Entry> mockUpAndMockStates = itr.next();
         Object mockUp = mockUpAndMockStates.getKey();

         if (mockUp.getClass() == mockUpClass) {
            List mockStates = mockUpAndMockStates.getValue();

            if (mockStates != null) {
               mockStatesWithExpectations.removeAll(mockStates);
            }

            itr.remove();
         }
      }
   }

   public boolean updateMockState(@Nonnull Object mockUp, int mockStateIndex)
   {
      MockState mockState = getMockState(mockUp, mockStateIndex);
      return mockState.update();
   }

   @Nonnull
   MockState getMockState(@Nonnull Object mockUp, int mockStateIndex)
   {
      List mockStates = startupMockUpsToMockStates.get(mockUp);

      if (mockStates == null) {
         mockStates = mockUpsToMockStates.get(mockUp);
      }

      MockState mockState = mockStates.get(mockStateIndex);
      assert mockState != null;
      return mockState;
   }

   public void verifyMissingInvocations()
   {
      for (MockState mockState : mockStatesWithExpectations) {
         mockState.verifyMissingInvocations();
      }
   }

   public void resetExpectations()
   {
      for (MockState mockState : mockStatesWithExpectations) {
         mockState.reset();
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy