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