CollectionInjector { public void inject(InjectionDetails injectionDetails) { for (Object injectCollections : injectionDetails.getInjectCollections()) { inject(injectCollections, injectionDetails.getMocks(), injectionDetails.getInjectableCollectionSet(), injectCollections.getClass()); } } CollectionInjector(CollectionFactory collectionFactory, MockSelectionStrategy strategy,
			GenericCollectionTypeResolver genericCollectionTypeResolver); void inject(InjectionDetails injectionDetails);  }
@Test public void shouldInjectGivenNullType() throws Exception { ClassWithPublicEventListenerCollection injectCollections = new ClassWithPublicEventListenerCollection(); InjectionDetails injectionDetails = new InjectionDetails(Collections.<Object> singleton(injectCollections), new OrderedSet<Object>(), new CollectionOfMocksFieldSet()); Field field = getField("listeners", injectCollections); given(genericCollectionTypeResolver.getCollectionFieldType(field)).willReturn(null); injector.inject(injectionDetails); assertNull(injectCollections.listeners); assertNull(injectCollections.eventListener); assertNull(injectCollections.iterator); }
@Test public void shouldInjectGivenNoMatchingInjectables() throws Exception { ClassWithPublicEventListenerCollection injectCollections = new ClassWithPublicEventListenerCollection(); InjectionDetails injectionDetails = new InjectionDetails(Collections.<Object> singleton(injectCollections), new OrderedSet<Object>(), new CollectionOfMocksFieldSet()); Field field = getField("listeners", injectCollections); given(genericCollectionTypeResolver.getCollectionFieldType(field)).willReturn((Class) EventListener.class); given(strategy.selectMocks(any(OrderedSet.class), any(Class.class))).willReturn(new OrderedSet()); injector.inject(injectionDetails); assertNull(injectCollections.listeners); assertNull(injectCollections.eventListener); assertNull(injectCollections.iterator); }
@Test public void shouldInjectIntoPublicField() throws Exception { ClassWithPublicEventListenerCollection injectCollections = new ClassWithPublicEventListenerCollection(); OrderedSet<Object> mocks = new OrderedSet<Object>(); InjectionDetails injectionDetails = new InjectionDetails(Collections.<Object> singleton(injectCollections), mocks, new CollectionOfMocksFieldSet()); OrderedSet<EventListener> stragtegyInjectables = mock(OrderedSet.class); Class<EventListener> clazz = EventListener.class; given(strategy.selectMocks(mocks, clazz)).willReturn(stragtegyInjectables); Collection<Object> set = mock(Collection.class); given(collectionFactory.createCollection(Collection.class, stragtegyInjectables)).willReturn(set); Field field = getField("listeners", injectCollections); given(genericCollectionTypeResolver.getCollectionFieldType(field)).willReturn((Class) clazz); injector.inject(injectionDetails); assertSame(set, injectCollections.listeners); assertNull(injectCollections.eventListener); assertNull(injectCollections.iterator); }
@Test public void shouldInjectIntoPrivateField() throws Exception { ClassWithPrivateEventListenerCollection injectCollections = new ClassWithPrivateEventListenerCollection(); OrderedSet<Object> mocks = new OrderedSet(); InjectionDetails injectionDetails = new InjectionDetails(Collections.<Object> singleton(injectCollections), mocks, new CollectionOfMocksFieldSet()); OrderedSet<EventListener> stragtegyInjectables = mock(OrderedSet.class); Class<EventListener> clazz = EventListener.class; given(strategy.selectMocks(mocks, clazz)).willReturn(stragtegyInjectables); Collection<Object> collection = mock(Collection.class); given(collectionFactory.createCollection(Collection.class, stragtegyInjectables)).willReturn(collection); Field field = getField("listeners", injectCollections); given(genericCollectionTypeResolver.getCollectionFieldType(field)).willReturn((Class) clazz); injector.inject(injectionDetails); assertSame(collection, injectCollections.listeners); }
@Test public void shouldInjectIntoPrivateSetField() throws Exception { ClassWithPrivateEventListenerSet injectCollections = new ClassWithPrivateEventListenerSet(); OrderedSet<Object> mocks = new OrderedSet(); InjectionDetails injectionDetails = new InjectionDetails(Collections.<Object> singleton(injectCollections), mocks, new CollectionOfMocksFieldSet()); OrderedSet<EventListener> stragtegyInjectables = mock(OrderedSet.class); Class<EventListener> clazz = EventListener.class; given(strategy.selectMocks(mocks, clazz)).willReturn(stragtegyInjectables); Set<Object> set = mock(Set.class); given(collectionFactory.createCollection(Set.class, stragtegyInjectables)).willReturn(set); Field field = getField("listeners", injectCollections); given(genericCollectionTypeResolver.getCollectionFieldType(field)).willReturn((Class) clazz); injector.inject(injectionDetails); assertSame(set, injectCollections.listeners); }
@Test public void shouldInjectIntoPrivateVectorField() throws Exception { ClassWithPrivateEventListenerVector injectCollections = new ClassWithPrivateEventListenerVector(); OrderedSet<Object> mocks = new OrderedSet(); InjectionDetails injectionDetails = new InjectionDetails(Collections.<Object> singleton(injectCollections), mocks, new CollectionOfMocksFieldSet()); OrderedSet<EventListener> stragtegyInjectables = mock(OrderedSet.class); Class<EventListener> clazz = EventListener.class; given(strategy.selectMocks(mocks, clazz)).willReturn(stragtegyInjectables); Vector<Object> vector = mock(Vector.class); given(collectionFactory.createCollection(Vector.class, stragtegyInjectables)).willReturn(vector); Field field = getField("listeners", injectCollections); given(genericCollectionTypeResolver.getCollectionFieldType(field)).willReturn((Class) clazz); injector.inject(injectionDetails); assertSame(vector, injectCollections.listeners); }
@Test public void shouldInjectIntoPrivateArrayField() throws Exception { ClassWithPrivateEventListenerArray injectCollections = new ClassWithPrivateEventListenerArray(); OrderedSet<Object> mocks = new OrderedSet(); InjectionDetails injectionDetails = new InjectionDetails(Collections.<Object> singleton(injectCollections), mocks, new CollectionOfMocksFieldSet()); OrderedSet<EventListener> stragtegyInjectables = mock(OrderedSet.class); EventListener[] eventListeners = { mock(EventListener.class) }; given(strategy.selectMocks(mocks, EventListener.class)).willReturn(stragtegyInjectables); given(stragtegyInjectables.toArray()).willReturn(eventListeners); injector.inject(injectionDetails); assertSame(eventListeners, injectCollections.listeners); }
@Test public void shouldInjectGivenCollectionOfMocksWithInstanceSame() throws NoSuchFieldException { ClassWithPrivateEventListenerSet injectCollections = new ClassWithPrivateEventListenerSet(); OrderedSet<Object> mocks = new OrderedSet<Object>(); CollectionOfMocksFieldSet collectionOfMocksFieldSet = mock(CollectionOfMocksFieldSet.class); InjectionDetails injectionDetails = new InjectionDetails(Collections.<Object> singleton(injectCollections), mocks, collectionOfMocksFieldSet); Class<EventListener> clazz = EventListener.class; CollectionOfMocksField collectionOfMocksField = mock(CollectionOfMocksField.class, RETURNS_DEEP_STUBS); Set<EventListener> eventListeners = Collections.singleton(mock(EventListener.class)); given(collectionOfMocksField.getValue()).willReturn(eventListeners); given(strategy.getCollectionOfMocksField(collectionOfMocksFieldSet, Set.class, clazz)).willReturn( collectionOfMocksField); Set injectableEventListeners = new HashSet<EventListener>(); given(collectionFactory.createCollection(any(Class.class), any(OrderedSet.class))).willReturn( injectableEventListeners); Field field = getField("listeners", injectCollections); given(genericCollectionTypeResolver.getCollectionFieldType(field)).willReturn((Class) clazz); injector.inject(injectionDetails); assertSame(injectableEventListeners, injectCollections.listeners); assertFalse(eventListeners == injectCollections.listeners); }
@Test public void shouldInjectIntoExtendedClassWithInheritedCollection() throws Exception { ExtendedClassWithInheritedCollection injectCollections = new ExtendedClassWithInheritedCollection(); OrderedSet<Object> mocks = new OrderedSet<Object>(); InjectionDetails injectionDetails = new InjectionDetails(Collections.<Object> singleton(injectCollections), mocks, new CollectionOfMocksFieldSet()); OrderedSet<EventListener> stragtegyInjectables = mock(OrderedSet.class); Class<EventListener> clazz = EventListener.class; given(strategy.selectMocks(mocks, clazz)).willReturn(stragtegyInjectables); Collection<Object> collection = mock(Collection.class); given(collectionFactory.createCollection(Collection.class, stragtegyInjectables)).willReturn(collection); Field field = getField("listeners", ClassWithPrivateEventListenerCollection.class); given(genericCollectionTypeResolver.getCollectionFieldType(field)).willReturn((Class) clazz); OrderedSet<EventListenerProxy> eventListenerProxyStragtegyInjectables = mock(OrderedSet.class); Class<EventListenerProxy> childClazz = EventListenerProxy.class; given(strategy.selectMocks(mocks, childClazz)).willReturn(eventListenerProxyStragtegyInjectables); Collection<Object> childCollection = mock(Collection.class); given(collectionFactory.createCollection(Collection.class, eventListenerProxyStragtegyInjectables)).willReturn( childCollection); Field childField = getField("childListeners", ExtendedClassWithInheritedCollection.class); given(genericCollectionTypeResolver.getCollectionFieldType(childField)).willReturn((Class) childClazz); injector.inject(injectionDetails); assertSame(childCollection, injectCollections.childListeners); assertSame(collection, injectCollections.getListeners()); }
@Test public void shouldInjectIntoExtendedClassWithInheritedCollectionOfSameName() throws Exception { ExtendedClassWithInheritedCollectionOfSameName injectCollections = new ExtendedClassWithInheritedCollectionOfSameName(); OrderedSet<Object> mocks = new OrderedSet(); InjectionDetails injectionDetails = new InjectionDetails(Collections.<Object> singleton(injectCollections), mocks, new CollectionOfMocksFieldSet()); OrderedSet<EventListener> eventListenerStragtegyInjectables = mock(OrderedSet.class); Class<EventListener> clazz = EventListener.class; given(strategy.selectMocks(mocks, clazz)).willReturn(eventListenerStragtegyInjectables); Collection<Object> collection = mock(Collection.class); given(collectionFactory.createCollection(Collection.class, eventListenerStragtegyInjectables)).willReturn( collection); Field field = getField("listeners", ClassWithPrivateEventListenerCollection.class); given(genericCollectionTypeResolver.getCollectionFieldType(field)).willReturn((Class) clazz); OrderedSet<EventListenerProxy> eventListenerProxyStragtegyInjectables = mock(OrderedSet.class); Class<EventListenerProxy> childClazz = EventListenerProxy.class; given(strategy.selectMocks(mocks, childClazz)).willReturn(eventListenerProxyStragtegyInjectables); Collection<Object> childCollection = mock(Collection.class); given(collectionFactory.createCollection(Collection.class, eventListenerProxyStragtegyInjectables)).willReturn( childCollection); Field childField = getField("listeners", ExtendedClassWithInheritedCollectionOfSameName.class); given(genericCollectionTypeResolver.getCollectionFieldType(childField)).willReturn((Class) childClazz); injector.inject(injectionDetails); assertSame(childCollection, injectCollections.listeners); assertSame(collection, injectCollections.getListeners()); }