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

mockit.internal.mockups.MockClassSetup 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.lang.reflect.*;
import java.lang.reflect.Type;
import java.util.*;
import javax.annotation.*;

import mockit.*;
import mockit.external.asm.*;
import mockit.internal.*;
import mockit.internal.startup.*;
import mockit.internal.state.*;
import mockit.internal.util.*;
import static mockit.external.asm.ClassReader.*;

public final class MockClassSetup
{
   @Nonnull final Class realClass;
   @Nullable private ClassReader rcReader;
   @Nonnull private final MockMethods mockMethods;
   @Nonnull final MockUp mockUp;
   private final boolean forStartupMock;

   public MockClassSetup(
      @Nonnull Class realClass, @Nonnull Class classToMock, @Nullable Type mockedType, @Nonnull MockUp mockUp,
      boolean targetIsInternal)
   {
      this(realClass, classToMock, mockedType, mockUp, targetIsInternal, null);
   }

   public MockClassSetup(
      @Nonnull Class realClass, @Nullable Type mockedType, @Nonnull MockUp mockUp, @Nullable byte[] realClassCode)
   {
      this(realClass, realClass, mockedType, mockUp, true, realClassCode);
   }

   private MockClassSetup(
      @Nonnull Class realClass, @Nonnull Class classToMock, @Nullable Type mockedType, @Nonnull MockUp mockUp,
      boolean targetIsInternal, @Nullable byte[] realClassCode)
   {
      this.realClass = classToMock;
      mockMethods = new MockMethods(realClass, mockedType, targetIsInternal);
      this.mockUp = mockUp;
      forStartupMock = Startup.initializing;
      rcReader = realClassCode == null ? null : new ClassReader(realClassCode);

      Class mockUpClass = mockUp.getClass();
      new MockMethodCollector(mockMethods).collectMockMethods(mockUpClass);

      mockMethods.registerMockStates(mockUp, forStartupMock);

      MockClasses mockClasses = TestRun.getMockClasses();

      if (forStartupMock) {
         mockClasses.addMock(mockMethods.getMockClassInternalName(), mockUp);
      }
      else {
         mockClasses.addMock(mockUp);
      }
   }

   public void redefineMethodsInGeneratedClass()
   {
      byte[] modifiedClassFile = modifyRealClass(realClass);
      validateThatAllMockMethodsWereApplied();

      if (modifiedClassFile != null) {
         applyClassModifications(realClass, modifiedClassFile);
      }
   }

   @Nonnull
   public Set> redefineMethods()
   {
      Set> redefinedClasses = redefineMethodsInClassHierarchy();
      validateThatAllMockMethodsWereApplied();
      return redefinedClasses;
   }

   @Nonnull
   private Set> redefineMethodsInClassHierarchy()
   {
      Set> redefinedClasses = new HashSet>();
      @Nullable Class classToModify = realClass;

      while (classToModify != null && mockMethods.hasUnusedMocks()) {
         byte[] modifiedClassFile = modifyRealClass(classToModify);

         if (modifiedClassFile != null) {
            applyClassModifications(classToModify, modifiedClassFile);
            redefinedClasses.add(classToModify);
         }

         Class superClass = classToModify.getSuperclass();
         classToModify = superClass == Object.class || superClass == Proxy.class ? null : superClass;
         rcReader = null;
      }

      return redefinedClasses;
   }

   @Nullable
   private byte[] modifyRealClass(@Nonnull Class classToModify)
   {
      if (rcReader == null) {
         rcReader = createClassReaderForRealClass(classToModify);
      }

      MockupsModifier modifier = new MockupsModifier(rcReader, classToModify, mockUp, mockMethods);
      rcReader.accept(modifier, SKIP_FRAMES);

      return modifier.wasModified() ? modifier.toByteArray() : null;
   }

   @Nonnull
   BaseClassModifier createClassModifier(@Nonnull ClassReader cr)
   {
      return new MockupsModifier(cr, realClass, mockUp, mockMethods);
   }

   @Nonnull
   private static ClassReader createClassReaderForRealClass(@Nonnull Class classToModify)
   {
      if (classToModify.isInterface() || classToModify.isArray()) {
         throw new IllegalArgumentException("Not a modifiable class: " + classToModify.getName());
      }

      return ClassFile.createReaderFromLastRedefinitionIfAny(classToModify);
   }

   void applyClassModifications(@Nonnull Class classToModify, @Nonnull byte[] modifiedClassFile)
   {
      Startup.redefineMethods(classToModify, modifiedClassFile);

      if (forStartupMock) {
         CachedClassfiles.addClassfile(classToModify, modifiedClassFile);
      }
      else {
         String mockClassDesc = mockMethods.getMockClassInternalName();
         TestRun.mockFixture().addRedefinedClass(mockClassDesc, classToModify, modifiedClassFile);
      }
   }

   void validateThatAllMockMethodsWereApplied()
   {
      List remainingMocks = mockMethods.getUnusedMockSignatures();

      if (!remainingMocks.isEmpty()) {
         String classDesc = mockMethods.getMockClassInternalName();
         String mockSignatures = new MethodFormatter(classDesc).friendlyMethodSignatures(remainingMocks);

         throw new IllegalArgumentException(
            "Matching real methods not found for the following mocks:\n" + mockSignatures);
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy