### Question:
AssociationInjector implements AssociationListener<T> { public void discovered(Association<T> association, T service) { inject(association, service); } AssociationInjector(Object target); void setBackend(Object target); void setCallerClassLoader(ClassLoader callerCL); void terminate(); Map<AssociationDescriptor, Long> getInvocationMap(); void injectEmpty(Association<T> association); void discovered(Association<T> association, T service); void changed(Association<T> association, T service); void broken(Association<T> association, T service);  }### Answer:
@Test public void testInjectProxy() { Association<Dummy> a = new DefaultAssociation<Dummy>(createAssociationDescriptor()); Target1 t = new Target1(); AssociationInjector<Dummy> ai = new AssociationInjector<Dummy>(t); for(int i=0; i<10; i++) { Dummy dummy = new DummyImpl(i); a.addServiceItem(AssociationUtils.makeServiceItem(dummy)); ai.discovered(a, dummy); } Assert.assertEquals(1, t.injectedCount); }
@Test public void testInjectProxyWithListener() { Association<Dummy> a = new DefaultAssociation<Dummy>(createAssociationDescriptor()); Target1 t = new Target1(); AssociationInjector<Dummy> ai = new AssociationInjector<Dummy>(t); AL aL = new AL(); a.registerAssociationServiceListener(aL); for(int i=0; i<10; i++) { Dummy dummy = new DummyImpl(i); a.addServiceItem(AssociationUtils.makeServiceItem(dummy)); ai.discovered(a, dummy); } Assert.assertEquals(1, t.injectedCount); Assert.assertEquals(10, aL.addedServiceCount.get()); Assert.assertEquals(0, aL.removedServiceCount.get()); Collection<Dummy> dummies = a.getServices(); for(Dummy d: dummies) a.removeService(d); Assert.assertEquals(10, aL.removedServiceCount.get()); Assert.assertEquals(0, a.getServiceCount()); }
@Test public void testInjectIterable() { Association<Dummy> a = new DefaultAssociation<Dummy>(createAssociationDescriptor()); Target2 t = new Target2(); AssociationInjector<Dummy> ai = new AssociationInjector<Dummy>(t); for(int i=0; i<10; i++) { Dummy dummy = new DummyImpl(i); a.addServiceItem(AssociationUtils.makeServiceItem(dummy)); ai.discovered(a, dummy); } Assert.assertEquals(1, t.injectedCount); int j = 0; for(Dummy dummy : t.dummies) j++; Assert.assertEquals(10, j); }
@Test public void testInjectAssociation() { Association<Dummy> a = new DefaultAssociation<Dummy>(createAssociationDescriptor()); Target3 t = new Target3(); AssociationInjector<Dummy> ai = new AssociationInjector<Dummy>(t); for(int i=0; i<10; i++) { Dummy dummy = new DummyImpl(i); ServiceItem item = AssociationUtils.makeServiceItem(dummy); a.addServiceItem(item); ai.discovered(a, dummy); } Assert.assertEquals(1, t.injectedCount); }