DefaultMockSelectionStrategy implements MockSelectionStrategy { @SuppressWarnings("unchecked") public <C extends Collection<E>, E> CollectionOfMocksField<C, E> getCollectionOfMocksField( CollectionOfMocksFieldSet collectionOfMocksFieldSet, Class<C> typeOfCollection, Class<E> typeOfElements) { if (collectionOfMocksFieldSet == null) { throw new IllegalArgumentException("collectionOfMocksFieldSet must not be null"); } if (typeOfCollection == null) { throw new IllegalArgumentException("typeOfCollection must not be null"); } if (typeOfElements == null) { throw new IllegalArgumentException("typeOfElements must not be null"); } CollectionOfMocksField<C, E> collectionOfMocksField = null; for (CollectionOfMocksField<?, ?> candidate : collectionOfMocksFieldSet) { if (candidate.getTypeOfCollection().equals(typeOfCollection) && candidate.getTypeOfElements().equals(typeOfElements)) { if (collectionOfMocksField != null) { throw new IllegalArgumentException( "There is more than one CollectionOfMocksField of type of collection " + typeOfCollection + " and type of element " + typeOfElements + ". There may be other offending duplicates."); } collectionOfMocksField = (CollectionOfMocksField<C, E>) candidate; } } return collectionOfMocksField; }  @SuppressWarnings("unchecked") OrderedSet<T> selectMocks(OrderedSet<Object> mocks, Class<T> mockClass); @SuppressWarnings("unchecked") CollectionOfMocksField<C, E> getCollectionOfMocksField(
			CollectionOfMocksFieldSet collectionOfMocksFieldSet, Class<C> typeOfCollection, Class<E> typeOfElements);  }
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void shouldGetMockCollectionGivenTwoTypesOfCollection() { CollectionOfMocksField mockCollection1 = new CollectionOfMocksField(Collections.emptySet(), Set.class, InputStream.class); CollectionOfMocksField mockCollection2 = new CollectionOfMocksField(mock(TreeSet.class), TreeSet.class, InputStream.class); Set<CollectionOfMocksField<Collection<Object>, Object>> mockCollections = new HashSet<CollectionOfMocksField<Collection<Object>, Object>>( Arrays.<CollectionOfMocksField<Collection<Object>, Object>> asList(mockCollection1, mockCollection2)); CollectionOfMocksFieldSet set = mock(CollectionOfMocksFieldSet.class); given(set.iterator()).willReturn(mockCollections.iterator()); Class<Set> typeOfCollection = Set.class; Class<InputStream> typeOfElements = InputStream.class; CollectionOfMocksField actualMockCollection = strategy.getCollectionOfMocksField(set, typeOfCollection, typeOfElements); assertSame(mockCollection1, actualMockCollection); }
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void shouldGetMockCollectionGivenTwoTypesOfElement() { CollectionOfMocksField mockCollection1 = new CollectionOfMocksField(Collections.emptyList(), List.class, FileOutputStream.class); CollectionOfMocksField mockCollection2 = new CollectionOfMocksField(Collections.emptyList(), List.class, OutputStream.class); Set<CollectionOfMocksField<Collection<Object>, Object>> mockCollections = new HashSet<CollectionOfMocksField<Collection<Object>, Object>>( Arrays.<CollectionOfMocksField<Collection<Object>, Object>> asList(mockCollection1, mockCollection2)); CollectionOfMocksFieldSet set = mock(CollectionOfMocksFieldSet.class); given(set.iterator()).willReturn(mockCollections.iterator()); Class<List> typeOfCollection = List.class; Class<OutputStream> typeOfElements = OutputStream.class; CollectionOfMocksField actualMockCollection = strategy.getCollectionOfMocksField(set, typeOfCollection, typeOfElements); assertSame(mockCollection2, actualMockCollection); }
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void shouldGetMockCollectionGivenNoMatchingMockCollection() { CollectionOfMocksField collectionOfMocksField = new CollectionOfMocksField(Collections.emptyList(), List.class, FileOutputStream.class); Set<CollectionOfMocksField<Collection<Object>, Object>> mockCollections = Collections .<CollectionOfMocksField<Collection<Object>, Object>> singleton(collectionOfMocksField); CollectionOfMocksFieldSet set = mock(CollectionOfMocksFieldSet.class); given(set.iterator()).willReturn(mockCollections.iterator()); Class<Collection> typeOfCollection = Collection.class; Class<EventListener> typeOfElements = EventListener.class; CollectionOfMocksField actualMockCollection = strategy.getCollectionOfMocksField(set, typeOfCollection, typeOfElements); assertNull(actualMockCollection); }