Skip to content

Commit

Permalink
Refactorings associated to issue #393.
Browse files Browse the repository at this point in the history
  • Loading branch information
rliesenfeld committed Mar 18, 2017
1 parent 032c0b4 commit 95c3b15
Showing 1 changed file with 47 additions and 26 deletions.
73 changes: 47 additions & 26 deletions main/src/mockit/internal/expectations/PhasedExecutionState.java
Expand Up @@ -80,7 +80,10 @@ void addExpectation(@Nonnull Expectation expectation, boolean strict)

private void forceMatchingOnMockInstanceIfRequired(@Nonnull ExpectedInvocation invocation)
{
if (isToBeMatchedOnInstance(invocation.instance, invocation.getMethodNameAndDescription())) {
if (
!invocation.matchInstance &&
isToBeMatchedOnInstance(invocation.instance, invocation.getMethodNameAndDescription())
) {
invocation.matchInstance = true;
}
}
Expand Down Expand Up @@ -126,33 +129,39 @@ private Expectation findPreviousNotStrictExpectation(@Nonnull ExpectedInvocation
}

Object mock = newInvocation.instance;
@Nonnull Boolean matchInstance = newInvocation.matchInstance;
String mockClassDesc = newInvocation.getClassDesc();
String mockNameAndDesc = newInvocation.getMethodNameAndDescription();
boolean constructorInvocation = newInvocation.isConstructor();
boolean isConstructor = newInvocation.isConstructor();

for (int i = 0; i < n; i++) {
Expectation previousExpectation = notStrictExpectations.get(i);
Expectation previous = notStrictExpectations.get(i);

if (
isMatchingInvocation(mock, mockClassDesc, mockNameAndDesc, constructorInvocation, previousExpectation) &&
isWithMatchingArguments(newInvocation, previousExpectation.invocation)
isMatchingInvocation(mock, matchInstance, mockClassDesc, mockNameAndDesc, isConstructor, previous) &&
isWithMatchingArguments(newInvocation, previous.invocation)
) {
return previousExpectation;
return previous;
}
}

return null;
}

private boolean isMatchingInvocation(
@Nullable Object mock, @Nonnull String mockClassDesc, @Nonnull String mockNameAndDesc,
boolean constructorInvocation, @Nonnull Expectation expectation)
@Nullable Object mock, @Nullable Boolean matchInstance, @Nonnull String mockClassDesc,
@Nonnull String mockNameAndDesc, boolean constructorInvocation, @Nonnull Expectation expectation)
{
ExpectedInvocation invocation = expectation.invocation;
//
// if (matchInstance == null && instanceMap.containsKey(mock)) {
// matchInstance = true;
// }

return
invocation.isMatch(mock, mockClassDesc, mockNameAndDesc) &&
isSameMockedClass(mock, invocation.instance) &&
(constructorInvocation || mock == null || isMatchingInstance(mock, expectation));
(constructorInvocation || mock == null || isMatchingInstance(mock, matchInstance, expectation));
}

private boolean isSameMockedClass(@Nullable Object mock1, @Nullable Object mock2)
Expand Down Expand Up @@ -191,7 +200,7 @@ private boolean isWithMatchingArguments(
Expectation findNotStrictExpectation(
@Nullable Object mock, @Nonnull String mockClassDesc, @Nonnull String mockNameAndDesc, @Nonnull Object[] args)
{
boolean constructorInvocation = mockNameAndDesc.charAt(0) == '<';
boolean isConstructor = mockNameAndDesc.charAt(0) == '<';
Expectation replayExpectationFound = null;

// Note: new expectations might get added to the list, so a regular loop would cause a CME:
Expand All @@ -203,15 +212,15 @@ Expectation findNotStrictExpectation(
}

if (
isMatchingInvocation(mock, mockClassDesc, mockNameAndDesc, constructorInvocation, expectation) &&
isMatchingInvocation(mock, null, mockClassDesc, mockNameAndDesc, isConstructor, expectation) &&
expectation.invocation.arguments.isMatch(args, instanceMap)
) {
if (expectation.recordPhase == null) {
replayExpectationFound = expectation;
continue;
}

if (constructorInvocation) {
if (isConstructor) {
registerReplacementInstanceIfApplicable(mock, expectation.invocation);
}

Expand All @@ -231,7 +240,8 @@ private void registerReplacementInstanceIfApplicable(@Nullable Object mock, @Non
}
}

private boolean isMatchingInstance(@Nonnull Object invokedInstance, @Nonnull Expectation expectation)
private boolean isMatchingInstance(
@Nonnull Object invokedInstance, @Nullable Boolean matchInstance, @Nonnull Expectation expectation)
{
ExpectedInvocation invocation = expectation.invocation;
Object invocationInstance = invocation.instance;
Expand All @@ -245,22 +255,13 @@ private boolean isMatchingInstance(@Nonnull Object invokedInstance, @Nonnull Exp
return false;
}

if (dynamicMockInstancesToMatch != null) {
if (containsReference(dynamicMockInstancesToMatch, invokedInstance)) {
return false;
}

Class<?> invokedClass = invocationInstance.getClass();

for (Object dynamicMock : dynamicMockInstancesToMatch) {
if (dynamicMock.getClass() == invokedClass) {
return false;
}
}
//noinspection SimplifiableIfStatement
if (isDynamicMockInstanceOrClass(invokedInstance, invocationInstance)) {
return false;
}

return
!invocation.matchInstance && expectation.recordPhase != null &&
(matchInstance == null || !matchInstance) && !invocation.matchInstance && expectation.recordPhase != null &&
!replacementMap.containsValue(invocationInstance);
}

Expand All @@ -270,10 +271,30 @@ boolean isEquivalentInstance(@Nonnull Object invocationInstance, @Nonnull Object
invocationInstance == invokedInstance ||
invocationInstance == replacementMap.get(invokedInstance) ||
invocationInstance == instanceMap.get(invokedInstance) ||
// replacementMap.containsKey(instanceMap.get(invokedInstance)) ||
invokedInstance == instanceMap.get(invocationInstance) ||
TestRun.getExecutingTest().isInvokedInstanceEquivalentToCapturedInstance(invocationInstance, invokedInstance);
}

private boolean isDynamicMockInstanceOrClass(@Nonnull Object invokedInstance, @Nonnull Object invocationInstance)
{
if (dynamicMockInstancesToMatch != null) {
if (containsReference(dynamicMockInstancesToMatch, invokedInstance)) {
return true;
}

Class<?> invokedClass = invocationInstance.getClass();

for (Object dynamicMock : dynamicMockInstancesToMatch) {
if (dynamicMock.getClass() == invokedClass) {
return true;
}
}
}

return false;
}

boolean areInDifferentEquivalenceSets(@Nonnull Object mock1, @Nonnull Object mock2)
{
if (mock1 == mock2 || instanceMap.isEmpty()) {
Expand Down

0 comments on commit 95c3b15

Please sign in to comment.