1 package org.picocontainer.tck;
2
3 import junit.framework.TestCase;
4 import org.picocontainer.PicoContainer;
5 import org.picocontainer.PicoInitializationException;
6 import org.picocontainer.PicoIntrospectionException;
7 import org.picocontainer.PicoRegistrationException;
8 import org.picocontainer.RegistrationPicoContainer;
9 import org.picocontainer.testmodel.DependsOnTouchable;
10 import org.picocontainer.testmodel.SimpleTouchable;
11 import org.picocontainer.testmodel.Touchable;
12 import org.picocontainer.defaults.DuplicateComponentKeyRegistrationException;
13 import org.picocontainer.defaults.UnsatisfiedDependencyInstantiationException;
14
15 import java.io.ByteArrayInputStream;
16 import java.io.ByteArrayOutputStream;
17 import java.io.IOException;
18 import java.io.ObjectInputStream;
19 import java.io.ObjectOutputStream;
20 import java.util.Map;
21 import java.util.HashMap;
22
23 public abstract class AbstractBasicClassCompatabilityTestCase extends TestCase {
24
25 protected abstract RegistrationPicoContainer createClassRegistrationPicoContainer();
26
27 protected final RegistrationPicoContainer createPicoContainerWithTouchableAndDependency() throws
28 PicoRegistrationException, PicoIntrospectionException {
29 RegistrationPicoContainer pico = createClassRegistrationPicoContainer();
30
31 pico.registerComponent(Touchable.class, SimpleTouchable.class);
32 pico.registerComponentByClass(DependsOnTouchable.class);
33 return pico;
34 }
35
36 protected final RegistrationPicoContainer createPicoContainerWithTouchablesDependancyOnly() throws
37 PicoRegistrationException, PicoIntrospectionException {
38 RegistrationPicoContainer pico = createClassRegistrationPicoContainer();
39 pico.registerComponentByClass(DependsOnTouchable.class);
40 return pico;
41
42 }
43
44 public void testNotNull() throws PicoRegistrationException, PicoIntrospectionException {
45 assertNotNull("Are you calling super.setUp() in your setUp method?", createPicoContainerWithTouchableAndDependency());
46 }
47
48 public void testBasicInstantiationAndContainment() throws PicoInitializationException, PicoRegistrationException {
49 PicoContainer picoContainer = createPicoContainerWithTouchableAndDependency();
50 picoContainer.instantiateComponents();
51 assertTrue("Container should have Touchable component",
52 picoContainer.hasComponent(Touchable.class));
53 assertTrue("Container should have DependsOnTouchable component",
54 picoContainer.hasComponent(DependsOnTouchable.class));
55 assertTrue("Component should be instance of Touchable",
56 picoContainer.getComponent(Touchable.class) instanceof Touchable);
57 assertTrue("Component should be instance of DependsOnTouchable",
58 picoContainer.getComponent(DependsOnTouchable.class) instanceof DependsOnTouchable);
59 assertTrue("should not have non existent component", !picoContainer.hasComponent(Map.class));
60 }
61
62 public void testSerializabilityOfContainer() throws PicoRegistrationException, PicoInitializationException,
63 IOException, ClassNotFoundException {
64
65 PicoContainer picoContainer = createPicoContainerWithTouchableAndDependency();
66 picoContainer.instantiateComponents();
67
68 ByteArrayOutputStream baos = new ByteArrayOutputStream();
69 ObjectOutputStream oos = new ObjectOutputStream(baos);
70
71 oos.writeObject(picoContainer);
72
73 // yeah yeah, is not needed.
74 picoContainer = null;
75
76 ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
77
78 picoContainer = (PicoContainer) ois.readObject();
79
80 SimpleTouchable touchable = (SimpleTouchable) picoContainer.getComponent(Touchable.class);
81
82 assertTrue("hello should have been called in Touchable", touchable.wasTouched);
83 }
84
85 public void testTooFewComponents() throws PicoInitializationException, PicoRegistrationException {
86
87 PicoContainer picoContainer = createPicoContainerWithTouchablesDependancyOnly();
88
89 try {
90 picoContainer.instantiateComponents();
91 fail("should need a Touchable");
92 } catch (UnsatisfiedDependencyInstantiationException e) {
93 // expected
94 assertTrue(e.getClassThatNeedsDeps() == DependsOnTouchable.class);
95 assertTrue(e.getMessage().indexOf(DependsOnTouchable.class.getName()) > 0);
96
97 }
98 }
99
100 public void testDoubleInstantiation() throws PicoRegistrationException, PicoInitializationException {
101 PicoContainer picoContainer = createPicoContainerWithTouchableAndDependency();
102 picoContainer.instantiateComponents();
103 try {
104 picoContainer.instantiateComponents();
105 fail("should have barfed");
106 } catch (IllegalStateException e) {
107 // expected
108 }
109 }
110
111 protected final void addAnotherSimpleTouchable(RegistrationPicoContainer picoContainer) throws PicoRegistrationException, PicoIntrospectionException {
112 picoContainer.registerComponent(Touchable.class, SimpleTouchable.class);
113 }
114
115 public void testDuplicateRegistration() throws Exception {
116 RegistrationPicoContainer picoContainer = createPicoContainerWithTouchableAndDependency();
117 try {
118 addAnotherSimpleTouchable(picoContainer);
119 //picoContainer.instantiateComponents();
120 fail("Should have barfed with dupe registration");
121 } catch (DuplicateComponentKeyRegistrationException e) {
122 // expected
123 assertTrue("Wrong key", e.getDuplicateKey() == Touchable.class);
124 }
125 }
126
127 protected final void addAHashMapByInstance(RegistrationPicoContainer picoContainer) throws PicoRegistrationException, PicoIntrospectionException {
128 picoContainer.registerComponent(Map.class, new HashMap());
129 }
130
131 public void testByInstanceRegistration() throws PicoRegistrationException, PicoInitializationException {
132 RegistrationPicoContainer picoContainer = createPicoContainerWithTouchableAndDependency();
133 addAHashMapByInstance(picoContainer);
134 picoContainer.instantiateComponents();
135 assertEquals("Wrong number of comps in the internals", 3, picoContainer.getComponents().size());
136 assertEquals("Key - Map, Impl - HashMap should be in internals", HashMap.class, picoContainer.getComponent(Map.class).getClass());
137 //TODO - some way to test hashmap was passed in as an instance ?
138 // should unmanaged side of DefaultPicoContainer be more exposed thru interface?
139 }
140
141 }
This page was automatically generated by Maven