Esempio n. 1
0
  @Nullable
  public InstanceFactory findInstanceFactory(@Nonnull Type mockedType) {
    InstanceFactory instanceFactory = mockedTypesAndInstances.get(mockedType);

    if (instanceFactory != null) {
      return instanceFactory;
    }

    Class<?> mockedClass = getClassType(mockedType);
    //noinspection ReuseOfLocalVariable
    instanceFactory = mockedTypesAndInstances.get(mockedClass);

    if (instanceFactory != null) {
      return instanceFactory;
    }

    boolean abstractType = mockedClass.isInterface() || isAbstract(mockedClass.getModifiers());

    for (Entry<Type, InstanceFactory> entry : mockedTypesAndInstances.entrySet()) {
      Type registeredMockedType = entry.getKey();
      Class<?> registeredMockedClass = getClassType(registeredMockedType);

      if (abstractType) {
        registeredMockedClass = getMockedClassOrInterfaceType(registeredMockedClass);
      }

      if (mockedClass.isAssignableFrom(registeredMockedClass)) {
        instanceFactory = entry.getValue();
        break;
      }
    }

    return instanceFactory;
  }
Esempio n. 2
0
 boolean isEquivalentInstance(
     @Nonnull Object invocationInstance, @Nonnull Object invokedInstance) {
   return invocationInstance == invokedInstance
       || invocationInstance == replacementMap.get(invokedInstance)
       || invocationInstance == instanceMap.get(invokedInstance)
       || invokedInstance == instanceMap.get(invocationInstance)
       || TestRun.getExecutingTest()
           .isInvokedInstanceEquivalentToCapturedInstance(invocationInstance, invokedInstance);
 }
Esempio n. 3
0
  private void restoreAndRemoveTransformedClasses(
      @Nonnull Set<ClassIdentification> classesToRestore) {
    RedefinitionEngine redefinitionEngine = new RedefinitionEngine();

    for (ClassIdentification transformedClassId : classesToRestore) {
      byte[] definitionToRestore = transformedClasses.get(transformedClassId);
      redefinitionEngine.restoreToDefinition(
          transformedClassId.getLoadedClass(), definitionToRestore);
    }

    transformedClasses.keySet().removeAll(classesToRestore);
  }
Esempio n. 4
0
  void removeMockedClasses(@Nonnull List<Class<?>> previousMockedClasses) {
    int currentMockedClassCount = mockedClasses.size();

    if (currentMockedClassCount > 0) {
      int previousMockedClassCount = previousMockedClasses.size();

      if (previousMockedClassCount == 0) {
        mockedClasses.clear();
        mockedTypesAndInstances.clear();
      } else if (previousMockedClassCount < currentMockedClassCount) {
        mockedClasses.retainAll(previousMockedClasses);
        mockedTypesAndInstances.keySet().retainAll(previousMockedClasses);
      }
    }
  }
Esempio n. 5
0
  void restoreRedefinedClasses(@Nonnull Map<?, byte[]> previousDefinitions) {
    if (redefinedClasses.isEmpty()) {
      return;
    }

    RedefinitionEngine redefinitionEngine = new RedefinitionEngine();
    Iterator<Entry<Class<?>, byte[]>> itr = redefinedClasses.entrySet().iterator();

    while (itr.hasNext()) {
      Entry<Class<?>, byte[]> entry = itr.next();
      Class<?> redefinedClass = entry.getKey();
      byte[] currentDefinition = entry.getValue();
      byte[] previousDefinition = previousDefinitions.get(redefinedClass);

      //noinspection ArrayEquality
      if (currentDefinition != previousDefinition) {
        redefinitionEngine.restoreDefinition(redefinedClass, previousDefinition);

        if (previousDefinition == null) {
          restoreDefinition(redefinedClass);
          discardStateForCorrespondingMockClassIfAny(redefinedClass);
          itr.remove();
        } else {
          entry.setValue(previousDefinition);
        }
      }
    }
  }
Esempio n. 6
0
  void restoreTransformedClasses(@Nonnull Set<ClassIdentification> previousTransformedClasses) {
    if (!transformedClasses.isEmpty()) {
      Set<ClassIdentification> classesToRestore;

      if (previousTransformedClasses.isEmpty()) {
        classesToRestore = transformedClasses.keySet();
      } else {
        classesToRestore = getTransformedClasses();
        classesToRestore.removeAll(previousTransformedClasses);
      }

      if (!classesToRestore.isEmpty()) {
        restoreAndRemoveTransformedClasses(classesToRestore);
      }
    }
  }
Esempio n. 7
0
  private void registerReplacementInstanceIfApplicable(
      @Nullable Object mock, @Nonnull ExpectedInvocation invocation) {
    Object replacementInstance = invocation.replacementInstance;

    if (replacementInstance != null && replacementInstance != invocation.instance) {
      replacementMap.put(mock, replacementInstance);
    }
  }
Esempio n. 8
0
  boolean areInDifferentEquivalenceSets(@Nonnull Object mock1, @Nonnull Object mock2) {
    if (mock1 == mock2 || instanceMap.isEmpty()) {
      return false;
    }

    Object mock1Equivalent = instanceMap.get(mock1);
    Object mock2Equivalent = instanceMap.get(mock2);

    if (mock1Equivalent == mock2 || mock2Equivalent == mock1) {
      return false;
    }

    if (mock1Equivalent != null && mock2Equivalent != null) {
      return true;
    }

    return instanceMapHasMocksInSeparateEntries(mock1, mock2);
  }
Esempio n. 9
0
 @Override
 void markAsCoveredIfNoUnreadValuesAreLeft() {
   for (Boolean withUnreadValue : testIdsToAssignments.values()) {
     if (withUnreadValue == null) {
       covered = true;
       break;
     }
   }
 }
Esempio n. 10
0
  public boolean isStillMocked(@Nullable Object instance, @Nonnull String classDesc) {
    Class<?> targetClass;

    if (instance == null) {
      targetClass = ClassLoad.loadByInternalName(classDesc);
      return isClassAssignableTo(mockedClasses, targetClass);
    }

    targetClass = instance.getClass();
    return mockedTypesAndInstances.containsKey(targetClass) || isInstanceOfMockedClass(instance);
  }
Esempio n. 11
0
  private boolean instanceMapHasMocksInSeparateEntries(
      @Nonnull Object mock1, @Nonnull Object mock2) {
    boolean found1 = false;
    boolean found2 = false;

    for (Entry<Object, Object> entry : instanceMap.entrySet()) {
      if (!found1 && isInMapEntry(entry, mock1)) {
        found1 = true;
      }

      if (!found2 && isInMapEntry(entry, mock2)) {
        found2 = true;
      }

      if (found1 && found2) {
        return true;
      }
    }

    return false;
  }
Esempio n. 12
0
 void registerRead() {
   int testId = TestRun.getTestId();
   testIdsToAssignments.put(testId, null);
   readCount++;
 }
Esempio n. 13
0
 void registerAssignment() {
   int testId = TestRun.getTestId();
   testIdsToAssignments.put(testId, Boolean.TRUE);
   writeCount++;
 }
Esempio n. 14
0
 public void registerInstanceFactoryForMockedType(
     @Nonnull Class<?> mockedType, @Nonnull InstanceFactory mockedInstanceFactory) {
   registerMockedClass(mockedType);
   mockedTypesAndInstances.put(mockedType, mockedInstanceFactory);
 }
Esempio n. 15
0
 @Nonnull
 Set<ClassIdentification> getTransformedClasses() {
   return transformedClasses.isEmpty()
       ? Collections.<ClassIdentification>emptySet()
       : new HashSet<ClassIdentification>(transformedClasses.keySet());
 }
Esempio n. 16
0
 private void removeMockedClass(@Nonnull Class<?> mockedClass) {
   mockedTypesAndInstances.remove(mockedClass);
   mockedClasses.remove(mockedClass);
 }
Esempio n. 17
0
 @Nullable
 Object getReplacementInstanceForMethodInvocation(
     @Nonnull Object invokedInstance, @Nonnull String methodNameAndDesc) {
   return methodNameAndDesc.charAt(0) == '<' ? null : replacementMap.get(invokedInstance);
 }
Esempio n. 18
0
 public void addTransformedClass(
     @Nonnull ClassIdentification classId, @Nonnull byte[] pretransformClassfile) {
   transformedClasses.put(classId, pretransformClassfile);
 }