…lines 1–16 unchanged (16 lines)
17
18 public NameBasedCandidateFilter(MockCandidateFilter next) {
19 this.next = next;
20 }
21
22 public OngoingInjecter filterCandidate(Collection<Object> mocks,
23 Field field, List<Field> fields, Object fieldInstance) {
24 List<Object> mockNameMatches = new ArrayList<Object>();
25 if (mocks.size() > 1) {
26 for (Object mock : mocks) {
27 if (field.getName().equals(mockUtil.getMockName(mock).toString())) {
28 mockNameMatches.add(mock);
29 }
30 }
31 return next.filterCandidate(mockNameMatches, field, fields,
32 fieldInstance);
33 /*
34 * In this case we have to check whether we have conflicting naming
35 * fields. E.g. 2 fields of the same type, but we have to make sure
36 * we match on the correct name.
37 *
38 * Therefore we have to go through all other fields and make sure
39 * whenever we find a field that does match its name with the mock
40 * name, we should take that field instead.
41 */
42 } else if (mocks.size() == 1) {
43 String mockName = mockUtil.getMockName(mocks.iterator().next())
44 .toString();
45
46 for (Field otherField : fields) {
47 if (!otherField.equals(field)
48 && otherField.getType().equals(field.getType())
49 && otherField.getName().equals(mockName)) {
50
51 return new OngoingInjecter() {
52 public Object thenInject() {
53 return null;
54 }
55 };
56 }
57 }
58 }
59 return next.filterCandidate(mocks, field, fields, fieldInstance);
60 }
61}
62