1 10 package org.picocontainer.defaults; 11 12 import org.picocontainer.ComponentAdapter; 13 import org.picocontainer.Disposable; 14 import org.picocontainer.MutablePicoContainer; 15 import org.picocontainer.PicoContainer; 16 import org.picocontainer.Startable; 17 import org.picocontainer.tck.AbstractComponentAdapterTestCase; 18 import org.picocontainer.testmodel.NullLifecycle; 19 import org.picocontainer.testmodel.SimpleTouchable; 20 import org.picocontainer.testmodel.Touchable; 21 22 import java.util.Map ; 23 24 30 public class InstanceComponentAdapterTestCase 31 extends AbstractComponentAdapterTestCase { 32 33 public void testComponentAdapterReturnsSame() { 34 final Touchable touchable = new SimpleTouchable(); 35 final ComponentAdapter componentAdapter = new InstanceComponentAdapter(Touchable.class, touchable); 36 assertSame(touchable, componentAdapter.getComponentInstance(null)); 37 } 38 39 public void testDefaultLifecycleStrategy() { 40 LifecycleComponent component = new LifecycleComponent(); 41 InstanceComponentAdapter componentAdapter = 42 new InstanceComponentAdapter(LifecycleComponent.class, component); 43 PicoContainer pico = new DefaultPicoContainer(); 44 componentAdapter.start(pico); 45 componentAdapter.stop(pico); 46 componentAdapter.dispose(pico); 47 assertEquals("start>stop>dispose>", component.buffer.toString()); 48 componentAdapter.start(component); 49 componentAdapter.stop(component); 50 componentAdapter.dispose(component); 51 assertEquals("start>stop>dispose>start>stop>dispose>", component.buffer.toString()); 52 } 53 54 private static class LifecycleComponent implements Startable, Disposable { 55 StringBuffer buffer = new StringBuffer (); 56 57 public void start() { 58 buffer.append("start>"); 59 } 60 61 public void stop() { 62 buffer.append("stop>"); 63 } 64 65 public void dispose() { 66 buffer.append("dispose>"); 67 } 68 } 69 70 public void testCustomLifecycleCanBeInjected() { 71 NullLifecycle component = new NullLifecycle(); 72 RecordingLifecycleStrategy strategy = new RecordingLifecycleStrategy(new StringBuffer ()); 73 InstanceComponentAdapter componentAdapter = new InstanceComponentAdapter(NullLifecycle.class, component, strategy); 74 PicoContainer pico = new DefaultPicoContainer(); 75 componentAdapter.start(pico); 76 componentAdapter.stop(pico); 77 componentAdapter.dispose(pico); 78 assertEquals("<start<stop<dispose", strategy.recording()); 79 componentAdapter.start(component); 80 componentAdapter.stop(component); 81 componentAdapter.dispose(component); 82 assertEquals("<start<stop<dispose<start<stop<dispose", strategy.recording()); 83 } 84 85 public void testComponentAdapterCanIgnoreLifecycle() { 86 final Touchable touchable = new SimpleTouchable(); 87 InstanceComponentAdapter componentAdapter = new InstanceComponentAdapter(Touchable.class, touchable); 88 PicoContainer pico = new DefaultPicoContainer(); 89 componentAdapter.start(pico); 90 componentAdapter.stop(pico); 91 componentAdapter.dispose(pico); 92 componentAdapter.start(touchable); 93 componentAdapter.stop(touchable); 94 componentAdapter.dispose(touchable); 95 } 96 97 public void testGuardAgainstNullInstance() { 98 try { 99 new InstanceComponentAdapter(Map .class, null); 100 fail("should have barfed"); 101 } catch (NullPointerException e) { 102 assertEquals("componentInstance cannot be null", e.getMessage()); 103 } 104 } 105 106 107 111 protected Class getComponentAdapterType() { 112 return InstanceComponentAdapter.class; 113 } 114 115 119 protected int getComponentAdapterNature() { 120 return super.getComponentAdapterNature() & ~(RESOLVING | VERIFYING | INSTANTIATING ); 121 } 122 123 127 protected ComponentAdapter prepDEF_verifyWithoutDependencyWorks(MutablePicoContainer picoContainer) { 128 return new InstanceComponentAdapter("foo", "bar"); 129 } 130 131 135 protected ComponentAdapter prepDEF_verifyDoesNotInstantiate( 136 MutablePicoContainer picoContainer) { 137 return new InstanceComponentAdapter("Key", new Integer (4711)); 138 } 139 140 144 protected ComponentAdapter prepDEF_visitable() { 145 return new InstanceComponentAdapter("Key", new Integer (4711)); 146 } 147 148 152 protected ComponentAdapter prepSER_isSerializable(MutablePicoContainer picoContainer) { 153 return new InstanceComponentAdapter("Key", new Integer (4711)); 154 } 155 156 160 protected ComponentAdapter prepSER_isXStreamSerializable(MutablePicoContainer picoContainer) { 161 return new InstanceComponentAdapter("Key", new Integer (4711)); 162 } 163 164 } 165 | Popular Tags |