1 package org.nanocontainer.script.xml.issues; 2 3 import java.io.Reader ; 4 import java.io.StringReader ; 5 6 import org.nanocontainer.script.AbstractScriptedContainerBuilderTestCase; 7 import org.nanocontainer.script.xml.XMLContainerBuilder; 8 import org.picocontainer.PicoContainer; 9 import org.picocontainer.defaults.SetterInjectionComponentAdapterFactory; 10 11 public class ServiceDependencyTestCase extends AbstractScriptedContainerBuilderTestCase { 12 13 public void testCanInstantiateProcessWithSDIDependencies() { 14 Reader script = new StringReader ("" + 15 "<container component-adapter-factory='"+SetterInjectionComponentAdapterFactory.class.getName()+"'>"+ 16 " <component-implementation class='"+Service1Impl.class.getName()+"'/>"+ 17 " <component-implementation class='"+ServiceAImpl.class.getName()+"'/>"+ 18 " <component-implementation class='"+Service2Impl.class.getName()+"'/>"+ 19 " <component-implementation class='"+ServiceBImpl.class.getName()+"'/>"+ 20 " <component-implementation class='"+Process .class.getName()+"'/>"+ 21 "</container>"); 22 assertProcessWithDependencies(script); 23 } 24 25 private void assertProcessWithDependencies(Reader script) { 26 PicoContainer pico = buildContainer(script); 27 assertNotNull(pico); 28 Process process = (Process )pico.getComponentInstanceOfType(Process .class); 29 assertNotNull(process); 30 assertNotNull(process.getServiceA()); 31 assertNotNull(process.getServiceA().getService1()); 32 assertNotNull(process.getServiceB()); 33 assertNotNull(process.getServiceB().getService2()); 34 } 35 36 private PicoContainer buildContainer(Reader script) { 37 return buildContainer(new XMLContainerBuilder(script, getClass().getClassLoader()), null, "SOME_SCOPE"); 38 } 39 40 public static class Process { 41 private ServiceA serviceA; 42 43 private ServiceB serviceB; 44 45 public Process() { 47 } 48 49 public ServiceA getServiceA() { 50 return serviceA; 51 } 52 53 public void setServiceA(ServiceA serviceA) { 54 this.serviceA = serviceA; 55 } 56 57 public ServiceB getServiceB() { 58 return serviceB; 59 } 60 61 public void setServiceB(ServiceB serviceB) { 62 this.serviceB = serviceB; 63 } 64 } 65 66 public static interface Service1 { 67 } 68 69 public static interface Service2 { 70 } 71 72 public static class Service1Impl implements Service1 { 73 public Service1Impl() { 74 } 75 } 76 77 public static class Service2Impl implements Service2 { 78 public Service2Impl() { 79 } 80 } 81 82 public static interface ServiceA { 83 public Service1 getService1(); 84 } 85 86 public static interface ServiceB { 87 public Service2 getService2(); 88 } 89 90 public static class ServiceAImpl implements ServiceA { 91 private Service1 service1; 92 public ServiceAImpl() { 93 } 94 public Service1 getService1() { 95 return service1; 96 } 97 public void setService1(Service1 service1) { 98 this.service1 = service1; 99 } 100 } 101 102 public static class ServiceBImpl implements ServiceB { 103 private Service2 service2; 104 public ServiceBImpl() { 105 } 106 public Service2 getService2() { 107 return service2; 108 } 109 public void setService2(Service2 service2) { 110 this.service2 = service2; 111 } 112 } 113 } 114 115 | Popular Tags |