1 10 11 package org.picocontainer.alternatives; 12 13 import org.jmock.Mock; 14 import org.jmock.MockObjectTestCase; 15 import org.picocontainer.ComponentAdapter; 16 import org.picocontainer.Disposable; 17 import org.picocontainer.MutablePicoContainer; 18 import org.picocontainer.PicoContainer; 19 import org.picocontainer.PicoVerificationException; 20 import org.picocontainer.PicoVisitor; 21 import org.picocontainer.defaults.DefaultPicoContainer; 22 import org.picocontainer.defaults.VerifyingVisitor; 23 24 import java.util.Collection ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Vector ; 30 31 35 public class ImmutablePicoContainerTestCase extends MockObjectTestCase { 36 37 public void testImmutingofNullBarfs() { 38 try { 39 new ImmutablePicoContainer(null); 40 fail("Should have barfed"); 41 } catch (NullPointerException e) { 42 } 44 } 45 46 public void testDelegationOfGettingComponentInstance() { 47 DefaultPicoContainer mpc = new DefaultPicoContainer(); 48 mpc.registerComponentImplementation(Map .class, HashMap .class); 49 ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc); 50 Map map = (Map ) ipc.getComponentInstance(Map .class); 51 assertNotNull(map); 52 } 53 54 public void testDelegationOfGettingComponentInstanceOfType() { 55 DefaultPicoContainer mpc = new DefaultPicoContainer(); 56 mpc.registerComponentImplementation(Map .class, HashMap .class); 57 ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc); 58 Map map = (Map ) ipc.getComponentInstanceOfType(Map .class); 59 assertNotNull(map); 60 } 61 62 public void testDelegationOfGettingComponentInstancesOfType() { 63 DefaultPicoContainer mpc = new DefaultPicoContainer(); 64 mpc.registerComponentImplementation(Map .class, HashMap .class); 65 mpc.registerComponentImplementation(Collection .class, Vector .class); 66 ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc); 67 List list = ipc.getComponentInstancesOfType(Map .class); 68 assertNotNull(list); 69 assertEquals(1,list.size()); 70 } 71 72 public void testDelegationOfGetComponentInstances() { 73 DefaultPicoContainer mpc = new DefaultPicoContainer(); 74 mpc.registerComponentImplementation(Map .class, HashMap .class); 75 ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc); 76 List comps = ipc.getComponentInstances(); 77 assertNotNull(comps); 78 assertEquals(1, comps.size()); 79 } 80 81 public void testDelegationOfGetComponentAdapter() { 82 DefaultPicoContainer mpc = new DefaultPicoContainer(); 83 mpc.registerComponentImplementation(Map .class, HashMap .class); 84 ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc); 85 ComponentAdapter ca = ipc.getComponentAdapter(Map .class); 86 assertNotNull(ca); 87 88 } 89 90 public void testDelegationOfGetComponentAdapterOfType() { 91 DefaultPicoContainer mpc = new DefaultPicoContainer(); 92 mpc.registerComponentImplementation(Map .class, HashMap .class); 93 ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc); 94 ComponentAdapter ca = ipc.getComponentAdapterOfType(Map .class); 95 assertNotNull(ca); 96 } 97 98 public void testDelegationOfGetComponentAdapters() { 99 DefaultPicoContainer mpc = new DefaultPicoContainer(); 100 mpc.registerComponentImplementation(Map .class, HashMap .class); 101 ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc); 102 Collection comps = ipc.getComponentAdapters(); 103 assertNotNull(comps); 104 assertEquals(1, comps.size()); 105 } 106 107 public void testDelegationOfGetComponentAdaptersOfType() { 108 DefaultPicoContainer mpc = new DefaultPicoContainer(); 109 mpc.registerComponentImplementation(Map .class, HashMap .class); 110 ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc); 111 List comps = ipc.getComponentAdaptersOfType(Map .class); 112 assertNotNull(comps); 113 assertEquals(1, comps.size()); 114 } 115 116 public static class UnsatisfiableIterator implements Iterator { 117 118 public UnsatisfiableIterator(Map map) { 119 } 120 121 public void remove() { 122 } 123 124 public boolean hasNext() { 125 return false; 126 } 127 128 public Object next() { 129 return null; 130 } 131 } 132 133 public void testDelegationOfVerify() { 134 DefaultPicoContainer mpc = new DefaultPicoContainer(); 135 mpc.registerComponentImplementation(Iterator .class, UnsatisfiableIterator.class); 136 ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc); 137 try { 138 new VerifyingVisitor().traverse(ipc); 139 fail("PicoVerificationException expected"); 140 } catch (PicoVerificationException e) { 141 } 143 try { 144 ipc.verify(); 145 fail("PicoVerificationException expected"); 146 } catch (PicoVerificationException e) { 147 } 149 } 150 151 public void testGetParentForMutable() { 152 DefaultPicoContainer par = new DefaultPicoContainer(); 153 DefaultPicoContainer mpc = new DefaultPicoContainer(par); 154 mpc.registerComponentImplementation(Map .class, HashMap .class); 155 ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc); 156 PicoContainer parent = ipc.getParent(); 157 assertNotNull(parent); 158 assertNotSame(par, parent); 159 PicoContainer parent2 = ipc.getParent(); 160 assertNotNull(parent2); 161 assertEquals(parent, parent2); 162 } 163 164 public void testGetParentForNonMutable() { 165 DefaultPicoContainer par = new DefaultPicoContainer(); 166 ImmutablePicoContainer par2 = new ImmutablePicoContainer(par); 167 DefaultPicoContainer mpc = new DefaultPicoContainer(par2); 168 mpc.registerComponentImplementation(Map .class, HashMap .class); 169 ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc); 170 PicoContainer parent = ipc.getParent(); 171 assertNotNull(parent); 172 assertNotSame(par, parent); 173 PicoContainer parent2 = ipc.getParent(); 174 assertNotNull(parent2); 175 assertEquals(parent, parent2); 176 } 177 178 public void testStartBarfs() { 179 DefaultPicoContainer mpc = new DefaultPicoContainer(); 180 mpc.registerComponentImplementation(Map .class, HashMap .class); 181 ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc); 182 try { 183 ipc.start(); 184 fail("should have barfed"); 185 } catch (UnsupportedOperationException e) { 186 } 188 } 189 190 public void testStopBarfs() { 191 DefaultPicoContainer mpc = new DefaultPicoContainer(); 192 mpc.registerComponentImplementation(Map .class, HashMap .class); 193 ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc); 194 try { 195 ipc.stop(); 196 fail("stop have barfed"); 197 } catch (UnsupportedOperationException e) { 198 } 200 } 201 202 public void testDisposeBarfs() { 203 DefaultPicoContainer mpc = new DefaultPicoContainer(); 204 mpc.registerComponentImplementation(Map .class, HashMap .class); 205 ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc); 206 try { 207 ipc.dispose(); 208 fail("should have barfed"); 209 } catch (UnsupportedOperationException e) { 210 } 212 } 213 214 public static class MyDisposable implements Disposable { 215 public boolean disposed; 216 217 public void dispose() { 218 disposed = true; 219 } 220 } 221 222 public void testLifecycleGuardIsEasyToCircumventSoItMightAsWellBeDeleted() { 223 DefaultPicoContainer mpc = new DefaultPicoContainer(); 224 mpc.registerComponentImplementation(MyDisposable.class); 225 ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc); 226 List componentInstances = ipc.getComponentInstances(); 227 for (Iterator iterator = componentInstances.iterator(); iterator.hasNext();) { 228 Object o = iterator.next(); 229 if(o instanceof Disposable) { 230 ((Disposable) o).dispose(); 231 } 232 } 233 MyDisposable disposable = (MyDisposable ) ipc.getComponentInstance(MyDisposable.class); 234 assertTrue(disposable.disposed); 235 } 236 237 public void testFacetiouslyThatLifeCycleGuardPreventsCyclingOfChildContainersAsComponentsAreNotTheOnlyThingsThatAreLifecycleable() { 238 DefaultPicoContainer parent = new DefaultPicoContainer(); 239 MutablePicoContainer child = parent.makeChildContainer(); 240 parent.registerComponentImplementation("foo", MyDisposable.class); 241 child.registerComponentImplementation("bar", MyDisposable.class); 242 ImmutablePicoContainer ipc = new ImmutablePicoContainer(parent); 243 try { 244 ipc.dispose(); 245 fail("Should have barfed"); 246 } catch (UnsupportedOperationException e) { 247 } 249 250 MyDisposable parentDisposable = (MyDisposable) parent.getComponentInstance("foo"); 251 assertFalse(parentDisposable.disposed); 252 253 MyDisposable childDisposable = (MyDisposable) child.getComponentInstance("bar"); 254 assertFalse(childDisposable.disposed); 255 256 ((Disposable) ipc.getComponentInstances().get(0)).dispose(); 258 259 assertTrue(parentDisposable.disposed); 261 262 } 273 274 public void testVisitingOfImmutableContainerWorks() { 275 DefaultPicoContainer pico = new DefaultPicoContainer(); 276 Object foo = new Object (); 277 ComponentAdapter componentAdapter = pico.registerComponentInstance(foo); 278 279 Mock fooVisitor = new Mock(PicoVisitor.class); 280 fooVisitor.expects(once()).method("visitContainer").with(eq(pico)); 281 fooVisitor.expects(once()).method("visitComponentAdapter").with(eq(componentAdapter)); 282 283 ImmutablePicoContainer ipc = new ImmutablePicoContainer(pico); 284 285 ipc.accept((PicoVisitor) fooVisitor.proxy()); 286 287 288 } 289 } 290 | Popular Tags |