001    package org.nanocontainer.script.xml.issues;
002    
003    import java.io.Reader;
004    import java.io.StringReader;
005    
006    import org.nanocontainer.script.AbstractScriptedContainerBuilderTestCase;
007    import org.nanocontainer.script.xml.XMLContainerBuilder;
008    import org.picocontainer.PicoContainer;
009    import org.picocontainer.defaults.SetterInjectionComponentAdapterFactory;
010    
011    public class ServiceDependencyTestCase extends AbstractScriptedContainerBuilderTestCase {
012    
013        public void testCanInstantiateProcessWithSDIDependencies() {
014            Reader script = new StringReader("" +
015                    "<container component-adapter-factory='"+SetterInjectionComponentAdapterFactory.class.getName()+"'>"+
016                    " <component-implementation class='"+Service1Impl.class.getName()+"'/>"+
017                    " <component-implementation class='"+ServiceAImpl.class.getName()+"'/>"+
018                    " <component-implementation class='"+Service2Impl.class.getName()+"'/>"+
019                    " <component-implementation class='"+ServiceBImpl.class.getName()+"'/>"+
020                    " <component-implementation class='"+Process.class.getName()+"'/>"+
021                    "</container>");
022            assertProcessWithDependencies(script);
023        }
024    
025        private void assertProcessWithDependencies(Reader script) {
026            PicoContainer pico = buildContainer(script);
027            assertNotNull(pico);
028            Process process = (Process)pico.getComponentInstanceOfType(Process.class);
029            assertNotNull(process);
030            assertNotNull(process.getServiceA());
031            assertNotNull(process.getServiceA().getService1());
032            assertNotNull(process.getServiceB());
033            assertNotNull(process.getServiceB().getService2());
034        }
035    
036        private PicoContainer buildContainer(Reader script) {
037            return buildContainer(new XMLContainerBuilder(script, getClass().getClassLoader()), null, "SOME_SCOPE");
038        }
039        
040        public static class Process {
041            private ServiceA serviceA;
042    
043            private ServiceB serviceB;
044    
045            // use with SDI
046            public Process() {
047            }
048    
049            public ServiceA getServiceA() {
050                return serviceA;
051            }
052    
053            public void setServiceA(ServiceA serviceA) {
054                this.serviceA = serviceA;
055            }
056    
057            public ServiceB getServiceB() {
058                return serviceB;
059            }
060    
061            public void setServiceB(ServiceB serviceB) {
062                this.serviceB = serviceB;
063            }
064        }
065    
066        public static interface Service1 {
067        }
068    
069        public static interface Service2 {
070        }
071    
072        public static class Service1Impl implements Service1 {
073            public Service1Impl() {
074            }
075        }
076    
077        public static class Service2Impl implements Service2 {
078            public Service2Impl() {
079            }
080        }
081    
082        public static interface ServiceA {
083            public Service1 getService1();
084        }
085    
086        public static interface ServiceB {
087            public Service2 getService2();
088        }
089    
090        public static class ServiceAImpl implements ServiceA {
091            private Service1 service1;
092            public ServiceAImpl() {
093            }
094            public Service1 getService1() {
095                return service1;
096            }
097            public void setService1(Service1 service1) {
098                this.service1 = service1;
099            }        
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