KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > reflection > DefaultContainerRecorderTestCase


1 package org.nanocontainer.reflection;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.ObjectInputStream JavaDoc;
7 import java.io.ObjectOutputStream JavaDoc;
8 import java.io.Reader JavaDoc;
9 import java.io.StringReader JavaDoc;
10 import java.lang.reflect.InvocationTargetException JavaDoc;
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 /**
27  * @author Konstantin Pribluda ( konstantin.pribluda(at)infodesire.com )
28  * @author Aslak Hellesøy
29  */

30 public class DefaultContainerRecorderTestCase extends TestCase {
31     public void testInvocationsCanBeRecordedAndReplayedOnADifferentContainerInstance() throws Exception JavaDoc {
32         ContainerRecorder recorder = new DefaultContainerRecorder(new DefaultNanoPicoContainer());
33         MutablePicoContainer recorded = recorder.getContainerProxy();
34
35         recorded.registerComponentInstance("fruit", "apple");
36         recorded.registerComponentInstance("int", new Integer JavaDoc(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         // test that we can replay once more
50
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 JavaDoc, ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
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 JavaDoc serializeAndDeserialize(Object JavaDoc o) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
68         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
69         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
70
71         oos.writeObject(o);
72         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(baos.toByteArray()));
73
74         return ois.readObject();
75     }
76
77
78     public void testXMLRecorderHierarchy() throws ClassNotFoundException JavaDoc {
79         MutablePicoContainer parentPrototype = new DefaultPicoContainer();
80         DefaultContainerRecorder parentRecorder = new DefaultContainerRecorder(parentPrototype);
81         StringReader JavaDoc parentResource = new StringReader JavaDoc(""
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 JavaDoc childResource = new StringReader JavaDoc(""
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 JavaDoc resource) {
111         ContainerPopulator populator = new XMLContainerBuilder(resource, Thread.currentThread().getContextClassLoader());
112         populator.populateContainer(recorder.getContainerProxy());
113     }
114 }
115
Popular Tags