1e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson/*
2e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson * Copyright (c) 2007 Mockito contributors
3e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson * This program is made available under the terms of the MIT License.
4e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson */
5e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson
6e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinsonpackage org.mockito.internal.verification.checkers;
7e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson
82637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport static org.mockito.internal.exceptions.Reporter.argumentsAreDifferent;
92637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport static org.mockito.internal.exceptions.Reporter.wantedButNotInvoked;
102637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport static org.mockito.internal.exceptions.Reporter.wantedButNotInvokedInOrder;
112637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport static org.mockito.internal.invocation.InvocationsFinder.findAllMatchingUnverifiedChunks;
122637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport static org.mockito.internal.invocation.InvocationsFinder.findInvocations;
132637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport static org.mockito.internal.invocation.InvocationsFinder.findPreviousVerifiedInOrder;
142637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport static org.mockito.internal.invocation.InvocationsFinder.findSimilarInvocation;
152637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport static org.mockito.internal.verification.argumentmatching.ArgumentMatchingTool.getSuspiciouslyNotMatchingArgsIndexes;
16e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson
172637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport java.util.List;
1808bd32ce48b12ae751dd5c4829ff09a6fb9894f0Philip P. Moltmann
19e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinsonimport org.mockito.internal.reporting.SmartPrinter;
202637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport org.mockito.internal.verification.api.InOrderContext;
21e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinsonimport org.mockito.invocation.Invocation;
222637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport org.mockito.invocation.MatchableInvocation;
23e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson
24e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinsonpublic class MissingInvocationChecker {
252637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
262637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin    private MissingInvocationChecker() {
27e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson    }
282637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
292637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin    public static void checkMissingInvocation(List<Invocation> invocations, MatchableInvocation wanted) {
302637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin        List<Invocation> actualInvocations = findInvocations(invocations, wanted);
312637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
322637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin        if (!actualInvocations.isEmpty()){
332637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin            return;
342637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin        }
352637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
362637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin        Invocation similar = findSimilarInvocation(invocations, wanted);
372637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin        if (similar == null) {
382637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin            throw wantedButNotInvoked(wanted, invocations);
392637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin        }
402637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
412637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin        Integer[] indexesOfSuspiciousArgs = getSuspiciouslyNotMatchingArgsIndexes(wanted.getMatchers(), similar.getArguments());
422637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin        SmartPrinter smartPrinter = new SmartPrinter(wanted, similar, indexesOfSuspiciousArgs);
432637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin        throw argumentsAreDifferent(smartPrinter.getWanted(), smartPrinter.getActual(), similar.getLocation());
442637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
45e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson    }
462637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
472637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin    public static void checkMissingInvocation(List<Invocation> invocations, MatchableInvocation wanted, InOrderContext context) {
482637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin        List<Invocation> chunk = findAllMatchingUnverifiedChunks(invocations, wanted, context);
492637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
502637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin        if (!chunk.isEmpty()) {
512637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin            return;
52e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson        }
532637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
542637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin        Invocation previousInOrder = findPreviousVerifiedInOrder(invocations, context);
552637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin        if (previousInOrder != null) {
562637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin            throw wantedButNotInvokedInOrder(wanted, previousInOrder);
572637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin        }
582637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
592637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin        checkMissingInvocation(invocations, wanted);
60e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson    }
612637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin}
62