Ejemplo 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;
  }
Ejemplo 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);
 }
Ejemplo 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);
  }
Ejemplo 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);
      }
    }
  }
  @Mock(reentrant = true)
  public boolean shouldRun(Filter filter, Object m) {
    testMethod = null;

    if (!coverageMap.isEmpty()) {
      if (m instanceof JUnit38ClassRunner) {
        boolean noTestsToRun =
            verifyTestMethodsInJUnit38TestClassThatShouldRun((JUnit38ClassRunner) m);

        if (noTestsToRun) {
          return false;
        }
      } else if (m instanceof FrameworkMethod) {
        testMethod = ((FrameworkMethod) m).getMethod();
        Boolean shouldRun = shouldRunTestInCurrentTestRun(Test.class, testMethod);

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

    Boolean shouldRun = MethodReflection.invoke(it, shouldRunMethod, filter, m);

    if (testMethod != null) {
      testMethods.put(testMethod, shouldRun);
    }

    return shouldRun;
  }
  private Boolean shouldRunTestInCurrentTestRun(
      Class<? extends Annotation> testAnnotation, Method testMethod) {
    Boolean shouldRun = testMethods.get(testMethod);

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

    if (isTestNotApplicableInCurrentTestRun(testAnnotation, testMethod)) {
      reportTestAsNotApplicableInCurrentTestRun(testMethod);
      testMethods.put(testMethod, false);
      return false;
    }

    return null;
  }
Ejemplo n.º 7
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);
        }
      }
    }
  }
Ejemplo n.º 8
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);
      }
    }
  }
Ejemplo n.º 9
0
  private void registerReplacementInstanceIfApplicable(
      @Nullable Object mock, @Nonnull ExpectedInvocation invocation) {
    Object replacementInstance = invocation.replacementInstance;

    if (replacementInstance != null && replacementInstance != invocation.instance) {
      replacementMap.put(mock, replacementInstance);
    }
  }
Ejemplo n.º 10
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);
  }
Ejemplo n.º 11
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);
  }
Ejemplo n.º 12
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;
  }
Ejemplo n.º 13
0
 public void addTransformedClass(
     @Nonnull ClassIdentification classId, @Nonnull byte[] pretransformClassfile) {
   transformedClasses.put(classId, pretransformClassfile);
 }
Ejemplo n.º 14
0
 public void registerInstanceFactoryForMockedType(
     @Nonnull Class<?> mockedType, @Nonnull InstanceFactory mockedInstanceFactory) {
   registerMockedClass(mockedType);
   mockedTypesAndInstances.put(mockedType, mockedInstanceFactory);
 }
Ejemplo n.º 15
0
 private void removeMockedClass(@Nonnull Class<?> mockedClass) {
   mockedTypesAndInstances.remove(mockedClass);
   mockedClasses.remove(mockedClass);
 }
Ejemplo n.º 16
0
 @Nullable
 Object getReplacementInstanceForMethodInvocation(
     @Nonnull Object invokedInstance, @Nonnull String methodNameAndDesc) {
   return methodNameAndDesc.charAt(0) == '<' ? null : replacementMap.get(invokedInstance);
 }
Ejemplo n.º 17
0
 @Nonnull
 Set<ClassIdentification> getTransformedClasses() {
   return transformedClasses.isEmpty()
       ? Collections.<ClassIdentification>emptySet()
       : new HashSet<ClassIdentification>(transformedClasses.keySet());
 }