1 8 package org.picocontainer.defaults; 9 10 import org.jmock.Mock; 11 import org.jmock.MockObjectTestCase; 12 import org.picocontainer.ComponentAdapter; 13 import org.picocontainer.PicoContainer; 14 import org.picocontainer.MutablePicoContainer; 15 import org.picocontainer.testmodel.SimpleTouchable; 16 import org.picocontainer.testmodel.Touchable; 17 18 19 23 public class CachingComponentAdapterTestCase extends MockObjectTestCase { 24 25 public void testComponentIsNotStartedWhenCachedAndCanBeStarted() { 26 CachingComponentAdapter adapter = new CachingComponentAdapter( 27 mockComponentAdapterSupportingLifecycleStrategy(true, false, false)); 28 PicoContainer pico = new DefaultPicoContainer(); 29 adapter.getComponentInstance(pico); 30 adapter.start(pico); 31 } 32 33 public void testComponentCanBeStartedAgainAfterBeingStopped() { 34 CachingComponentAdapter adapter = new CachingComponentAdapter( 35 mockComponentAdapterSupportingLifecycleStrategy(true, true, false)); 36 PicoContainer pico = new DefaultPicoContainer(); 37 adapter.start(pico); 38 Object instanceAfterFirstStart = adapter.getComponentInstance(pico); 39 adapter.stop(pico); 40 adapter.start(pico); 41 Object instanceAfterSecondStart = adapter.getComponentInstance(pico); 42 assertSame(instanceAfterFirstStart, instanceAfterSecondStart); 43 } 44 45 public void testComponentCannotBeStartedIfDisposed() { 46 CachingComponentAdapter adapter = new CachingComponentAdapter( 47 mockComponentAdapterSupportingLifecycleStrategy(false, false, true)); 48 PicoContainer pico = new DefaultPicoContainer(); 49 adapter.dispose(pico); 50 try { 51 adapter.start(pico); 52 fail("IllegalStateException expected"); 53 } catch (Exception e) { 54 assertEquals("Already disposed", e.getMessage()); 55 } 56 } 57 58 public void testComponentCannotBeStartedIfAlreadyStarted() { 59 CachingComponentAdapter adapter = new CachingComponentAdapter( 60 mockComponentAdapterSupportingLifecycleStrategy(true, false, false)); 61 PicoContainer pico = new DefaultPicoContainer(); 62 adapter.start(pico); 63 try { 64 adapter.start(pico); 65 fail("IllegalStateException expected"); 66 } catch (Exception e) { 67 assertEquals("Already started", e.getMessage()); 68 } 69 } 70 71 public void testComponentCannotBeStoppeddIfDisposed() { 72 CachingComponentAdapter adapter = new CachingComponentAdapter( 73 mockComponentAdapterSupportingLifecycleStrategy(false, false, true)); 74 PicoContainer pico = new DefaultPicoContainer(); 75 adapter.dispose(pico); 76 try { 77 adapter.stop(pico); 78 fail("IllegalStateException expected"); 79 } catch (Exception e) { 80 assertEquals("Already disposed", e.getMessage()); 81 } 82 } 83 84 public void testComponentCannotBeStoppedIfNotStarted() { 85 CachingComponentAdapter adapter = new CachingComponentAdapter( 86 mockComponentAdapterSupportingLifecycleStrategy(true, true, false)); 87 PicoContainer pico = new DefaultPicoContainer(); 88 adapter.start(pico); 89 adapter.stop(pico); 90 try { 91 adapter.stop(pico); 92 fail("IllegalStateException expected"); 93 } catch (Exception e) { 94 assertEquals("Not started", e.getMessage()); 95 } 96 } 97 98 public void testComponentCannotBeDisposedIfAlreadyDisposed() { 99 CachingComponentAdapter adapter = new CachingComponentAdapter( 100 mockComponentAdapterSupportingLifecycleStrategy(true, true, true)); 101 PicoContainer pico = new DefaultPicoContainer(); 102 adapter.start(pico); 103 adapter.stop(pico); 104 adapter.dispose(pico); 105 try { 106 adapter.dispose(pico); 107 fail("IllegalStateException expected"); 108 } catch (Exception e) { 109 assertEquals("Already disposed", e.getMessage()); 110 } 111 } 112 113 public void testComponentIsStoppedAndDisposedIfStartedWhenFlushed() { 114 CachingComponentAdapter adapter = new CachingComponentAdapter( 115 mockComponentAdapterSupportingLifecycleStrategy(true, true, true)); 116 PicoContainer pico = new DefaultPicoContainer(); 117 adapter.start(pico); 118 adapter.flush(); 119 } 120 121 public void testComponentIsNotStoppedAndDisposedWhenFlushedIfNotStarted() { 122 CachingComponentAdapter adapter = new CachingComponentAdapter( 123 mockComponentAdapterSupportingLifecycleStrategy(false, false, false)); 124 adapter.flush(); 125 } 126 127 public void testComponentIsNotStoppedAndDisposedWhenFlushedIfDelegateDoesNotSupportLifecycle() { 128 CachingComponentAdapter adapter = new CachingComponentAdapter( 129 mockComponentAdapterNotSupportingLifecycleStrategy()); 130 adapter.flush(); 131 } 132 133 public void testLifecycleIsIgnoredIfDelegateDoesNotSupportIt() { 134 CachingComponentAdapter adapter = new CachingComponentAdapter( 135 mockComponentAdapterNotSupportingLifecycleStrategy()); 136 PicoContainer pico = new DefaultPicoContainer(); 137 adapter.start(pico); 138 adapter.stop(pico); 139 adapter.dispose(pico); 140 } 141 142 public void testCanStopAComponentThatWasNeverStartedBecauseItHasNoLifecycle() { 143 MutablePicoContainer pico = new DefaultPicoContainer(); 144 145 pico.registerComponentImplementation(StringBuffer .class); 146 147 pico.start(); 148 149 assertNotNull(pico.getComponentInstance(StringBuffer .class)); 150 151 pico.stop(); 152 pico.dispose(); 153 } 154 155 private ComponentAdapter mockComponentAdapterNotSupportingLifecycleStrategy() { 156 Mock mock = mock(ComponentAdapter.class); 157 return (ComponentAdapter)mock.proxy(); 158 } 159 160 private ComponentAdapter mockComponentAdapterSupportingLifecycleStrategy( 161 boolean start, boolean stop, boolean dispose) { 162 boolean hasLifecycle = start || stop || dispose; 163 Mock mock = mock(ComponentAdapterSupportingLifecycleStrategy.class); 164 if (start) { 165 mock.expects(atLeastOnce()).method("start").with(isA(Touchable.class)); 166 } 167 if (stop) { 168 mock.expects(once()).method("stop").with(isA(Touchable.class)); 169 } 170 if (dispose) { 171 mock.expects(once()).method("dispose").with(isA(Touchable.class)); 172 } 173 if (hasLifecycle) { 174 mock.stubs().method("getComponentInstance").with(isA(PicoContainer.class)).will( 175 returnValue(new SimpleTouchable())); 176 } 177 mock.expects(once()).method("getComponentImplementation").will( 178 returnValue(SimpleTouchable.class)); 179 mock.expects(once()).method("hasLifecycle").with(same(SimpleTouchable.class)).will( 180 returnValue(hasLifecycle)); 181 return (ComponentAdapter)mock.proxy(); 182 } 183 184 static interface ComponentAdapterSupportingLifecycleStrategy extends ComponentAdapter, 185 LifecycleStrategy { 186 } 187 } | Popular Tags |