1 /*
2  * Copyright (c) 2016 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockito.internal.junit;
6 
7 import org.mockito.internal.util.MockitoLogger;
8 import org.mockito.invocation.Invocation;
9 
10 import java.util.LinkedHashMap;
11 import java.util.LinkedHashSet;
12 import java.util.Map;
13 import java.util.Set;
14 
15 /**
16  * Contains stubbing arg mismatches, knows how to format them
17  */
18 class StubbingArgMismatches {
19 
20     final Map<Invocation, Set<Invocation>> mismatches = new LinkedHashMap<Invocation, Set<Invocation>>();
21 
add(Invocation invocation, Invocation stubbing)22     public void add(Invocation invocation, Invocation stubbing) {
23         Set<Invocation> matchingInvocations = mismatches.get(stubbing);
24         if (matchingInvocations == null) {
25             matchingInvocations = new LinkedHashSet<Invocation>();
26             mismatches.put(stubbing, matchingInvocations);
27         }
28         matchingInvocations.add(invocation);
29     }
30 
format(String testName, MockitoLogger logger)31     public void format(String testName, MockitoLogger logger) {
32         if (mismatches.isEmpty()) {
33             return;
34         }
35 
36         StubbingHint hint = new StubbingHint(testName);
37         int x = 1;
38         for (Map.Entry<Invocation, Set<Invocation>> m : mismatches.entrySet()) {
39             hint.appendLine(x++, ". Unused... ", m.getKey().getLocation());
40             for (Invocation invocation : m.getValue()) {
41                 hint.appendLine(" ...args ok? ", invocation.getLocation());
42             }
43         }
44 
45         logger.log(hint.toString());
46     }
47 
size()48     public int size() {
49         return mismatches.size();
50     }
51 
toString()52     public String toString() {
53         return "" + mismatches;
54     }
55 }
56