1 package org.nanocontainer.reflection; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.IOException ; 6 import java.io.ObjectInputStream ; 7 import java.io.ObjectOutputStream ; 8 import java.io.Reader ; 9 import java.io.StringReader ; 10 import java.lang.reflect.InvocationTargetException ; 11 12 import junit.framework.TestCase; 13 14 import org.nanocontainer.integrationkit.ContainerPopulator; 15 import org.nanocontainer.integrationkit.ContainerRecorder; 16 import org.nanocontainer.script.xml.XMLContainerBuilder; 17 import org.nanocontainer.testmodel.FredImpl; 18 import org.nanocontainer.testmodel.ThingThatTakesParamsInConstructor; 19 import org.nanocontainer.testmodel.Wilma; 20 import org.nanocontainer.testmodel.WilmaImpl; 21 import org.picocontainer.MutablePicoContainer; 22 import org.picocontainer.Parameter; 23 import org.picocontainer.defaults.ComponentParameter; 24 import org.picocontainer.defaults.DefaultPicoContainer; 25 26 30 public class DefaultContainerRecorderTestCase extends TestCase { 31 public void testInvocationsCanBeRecordedAndReplayedOnADifferentContainerInstance() throws Exception { 32 ContainerRecorder recorder = new DefaultContainerRecorder(new DefaultNanoPicoContainer()); 33 MutablePicoContainer recorded = recorder.getContainerProxy(); 34 35 recorded.registerComponentInstance("fruit", "apple"); 36 recorded.registerComponentInstance("int", new Integer (239)); 37 recorded.registerComponentImplementation("thing", 38 ThingThatTakesParamsInConstructor.class, 39 new Parameter[]{ 40 ComponentParameter.DEFAULT, 41 ComponentParameter.DEFAULT, 42 }); 43 44 MutablePicoContainer slave = new DefaultPicoContainer(); 45 recorder.replay(slave); 46 assertEquals("apple", slave.getComponentInstance("fruit")); 47 assertEquals("apple239", ((ThingThatTakesParamsInConstructor) slave.getComponentInstance("thing")).getValue()); 48 49 MutablePicoContainer anotherSlave = new DefaultPicoContainer(); 51 recorder.replay(anotherSlave); 52 assertEquals("apple", anotherSlave.getComponentInstance("fruit")); 53 assertEquals("apple239", ((ThingThatTakesParamsInConstructor) anotherSlave.getComponentInstance("thing")).getValue()); 54 } 55 56 public void testRecorderWorksAfterSerialization() throws IOException , ClassNotFoundException , IllegalAccessException , InvocationTargetException { 57 ContainerRecorder recorder = new DefaultContainerRecorder(new DefaultPicoContainer()); 58 MutablePicoContainer recorded = recorder.getContainerProxy(); 59 recorded.registerComponentInstance("fruit", "apple"); 60 61 ContainerRecorder serializedRecorder = (ContainerRecorder) serializeAndDeserialize(recorder); 62 MutablePicoContainer slave = new DefaultPicoContainer(); 63 serializedRecorder.replay(slave); 64 assertEquals("apple", slave.getComponentInstance("fruit")); 65 } 66 67 private Object serializeAndDeserialize(Object o) throws IOException , ClassNotFoundException { 68 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 69 ObjectOutputStream oos = new ObjectOutputStream (baos); 70 71 oos.writeObject(o); 72 ObjectInputStream ois = new ObjectInputStream (new ByteArrayInputStream (baos.toByteArray())); 73 74 return ois.readObject(); 75 } 76 77 78 public void testXMLRecorderHierarchy() throws ClassNotFoundException { 79 MutablePicoContainer parentPrototype = new DefaultPicoContainer(); 80 DefaultContainerRecorder parentRecorder = new DefaultContainerRecorder(parentPrototype); 81 StringReader parentResource = new StringReader ("" 82 + "<container>" 83 + " <component-implementation key='wilma' class='"+WilmaImpl.class.getName()+"'/>" 84 + "</container>" 85 ); 86 87 populateXMLContainer(parentRecorder, parentResource); 88 MutablePicoContainer parentContainer = parentRecorder.getContainerProxy(); 89 assertNull(parentContainer.getComponentInstance("fred")); 90 assertNotNull(parentContainer.getComponentInstance("wilma")); 91 92 MutablePicoContainer childPrototype = new DefaultPicoContainer(parentPrototype); 93 DefaultContainerRecorder childRecorder = new DefaultContainerRecorder(childPrototype); 94 StringReader childResource = new StringReader ("" 95 + "<container>" 96 + " <component-implementation key='fred' class='"+FredImpl.class.getName()+"'>" 97 + " <parameter key='wilma'/>" 98 + " </component-implementation>" 99 + "</container>" 100 ); 101 populateXMLContainer(childRecorder, childResource); 102 MutablePicoContainer childContainer = childRecorder.getContainerProxy(); 103 assertNotNull(childContainer.getComponentInstance("fred")); 104 assertNotNull(childContainer.getComponentInstance("wilma")); 105 FredImpl fred = (FredImpl)childContainer.getComponentInstance("fred"); 106 Wilma wilma = (Wilma)childContainer.getComponentInstance("wilma"); 107 assertSame(wilma, fred.wilma()); 108 } 109 110 private void populateXMLContainer(ContainerRecorder recorder, Reader resource) { 111 ContainerPopulator populator = new XMLContainerBuilder(resource, Thread.currentThread().getContextClassLoader()); 112 populator.populateContainer(recorder.getContainerProxy()); 113 } 114 } 115 | Popular Tags |