1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 6 package org.mockito.internal.invocation.finder; 7 8 import org.mockito.internal.invocation.InvocationComparator; 9 import org.mockito.stubbing.Stubbing; 10 import org.mockito.internal.stubbing.StubbingComparator; 11 import org.mockito.internal.util.DefaultMockingDetails; 12 import org.mockito.invocation.Invocation; 13 14 import java.util.*; 15 16 public class AllInvocationsFinder { 17 AllInvocationsFinder()18 private AllInvocationsFinder() {} 19 20 /** 21 * gets all invocations from mocks. Invocations are ordered earlier first. 22 * 23 * @param mocks mocks 24 * @return invocations 25 */ find(Iterable<?> mocks)26 public static List<Invocation> find(Iterable<?> mocks) { 27 Set<Invocation> invocationsInOrder = new TreeSet<Invocation>(new InvocationComparator()); 28 for (Object mock : mocks) { 29 Collection<Invocation> fromSingleMock = new DefaultMockingDetails(mock).getInvocations(); 30 invocationsInOrder.addAll(fromSingleMock); 31 } 32 33 return new LinkedList<Invocation>(invocationsInOrder); 34 } 35 36 /** 37 * Gets all stubbings from mocks. Invocations are ordered earlier first. 38 * 39 * @param mocks mocks 40 * @return stubbings 41 */ findStubbings(Iterable<?> mocks)42 public static Set<Stubbing> findStubbings(Iterable<?> mocks) { 43 Set<Stubbing> stubbings = new TreeSet<Stubbing>(new StubbingComparator()); 44 for (Object mock : mocks) { 45 Collection<? extends Stubbing> fromSingleMock = new DefaultMockingDetails(mock).getStubbings(); 46 stubbings.addAll(fromSingleMock); 47 } 48 49 return stubbings; 50 } 51 } 52