1 10 package org.picocontainer.defaults; 11 12 import org.picocontainer.Parameter; 13 import org.picocontainer.PicoInitializationException; 14 import org.picocontainer.tck.AbstractComponentAdapterFactoryTestCase; 15 import org.picocontainer.tck.AbstractComponentAdapterTestCase.RecordingLifecycleStrategy; 16 import org.picocontainer.testmodel.NullLifecycle; 17 import org.picocontainer.testmodel.RecordingLifecycle; 18 import org.picocontainer.testmodel.RecordingLifecycle.One; 19 20 24 public class SetterInjectionComponentAdapterFactoryTestCase extends AbstractComponentAdapterFactoryTestCase { 25 protected void setUp() throws Exception { 26 picoContainer = new DefaultPicoContainer(createComponentAdapterFactory()); 27 } 28 29 protected ComponentAdapterFactory createComponentAdapterFactory() { 30 return new SetterInjectionComponentAdapterFactory(); 31 } 32 33 public static interface Bean { 34 } 35 36 public static class NamedBean implements Bean { 37 private String name; 38 39 public String getName() { 40 return name; 41 } 42 43 public void setName(String name) { 44 this.name = name; 45 } 46 } 47 48 public static class NamedBeanWithPossibleDefault extends NamedBean { 49 private boolean byDefault; 50 51 public NamedBeanWithPossibleDefault() { 52 } 53 54 public NamedBeanWithPossibleDefault(String name) { 55 setName(name); 56 byDefault = true; 57 } 58 59 public boolean getByDefault() { 60 return byDefault; 61 } 62 } 63 64 public static class NoBean extends NamedBean { 65 public NoBean(String name) { 66 setName(name); 67 } 68 } 69 70 public void testContainerUsesStandardConstructor() { 71 picoContainer.registerComponentImplementation(Bean.class, NamedBeanWithPossibleDefault.class); 72 picoContainer.registerComponentInstance("Tom"); 73 NamedBeanWithPossibleDefault bean = (NamedBeanWithPossibleDefault) picoContainer.getComponentInstance(Bean.class); 74 assertFalse(bean.getByDefault()); 75 } 76 77 public void testContainerUsesOnlyStandardConstructor() { 78 picoContainer.registerComponentImplementation(Bean.class, NoBean.class); 79 picoContainer.registerComponentInstance("Tom"); 80 try { 81 picoContainer.getComponentInstance(Bean.class); 82 fail("Instantiation should have failed."); 83 } catch (PicoInitializationException e) { 84 } 85 } 86 87 public void testCustomLifecycleCanBeInjected() throws NoSuchMethodException { 88 RecordingLifecycleStrategy strategy = new RecordingLifecycleStrategy(new StringBuffer ()); 89 SetterInjectionComponentAdapterFactory caf = new SetterInjectionComponentAdapterFactory(false, strategy); 90 SetterInjectionComponentAdapter sica = (SetterInjectionComponentAdapter)caf.createComponentAdapter(NullLifecycle.class, NullLifecycle.class, new Parameter[0]); 91 One one = new RecordingLifecycle.One(new StringBuffer ()); 92 sica.start(one); 93 sica.stop(one); 94 sica.dispose(one); 95 assertEquals("<start<stop<dispose", strategy.recording()); 96 } 97 } | Popular Tags |