KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > defaults > AbstractImplementationHidingPicoContainerTestCase


1 package org.picocontainer.defaults;
2
3 import org.picocontainer.tck.AbstractPicoContainerTestCase;
4 import org.picocontainer.Parameter;
5 import org.picocontainer.PicoException;
6 import org.picocontainer.PicoInitializationException;
7 import org.picocontainer.MutablePicoContainer;
8
9 import java.util.Map JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.awt.event.ActionListener JavaDoc;
13 import java.awt.event.ActionEvent JavaDoc;
14
15 /**
16  * @author Paul Hammant
17  * @version $Revision: 1445 $
18  */

19 public abstract class AbstractImplementationHidingPicoContainerTestCase extends AbstractPicoContainerTestCase {
20
21
22     public void testInstanceIsNotAutomaticallyHidden() {
23         MutablePicoContainer pc = createImplementationHidingPicoContainer();
24         pc.registerComponentInstance(Map JavaDoc.class, new HashMap JavaDoc());
25         Map JavaDoc map = (Map JavaDoc) pc.getComponentInstance(Map JavaDoc.class);
26         assertNotNull(map);
27         assertTrue(map instanceof HashMap JavaDoc);
28     }
29
30     protected abstract MutablePicoContainer createImplementationHidingPicoContainer();
31
32
33     public void testImplementaionIsAutomaticallyHidden() {
34         MutablePicoContainer pc = createImplementationHidingPicoContainer();
35         pc.registerComponentImplementation(Map JavaDoc.class, HashMap JavaDoc.class);
36         Map JavaDoc map = (Map JavaDoc) pc.getComponentInstance(Map JavaDoc.class);
37         assertNotNull(map);
38         assertFalse(map instanceof HashMap JavaDoc);
39     }
40
41     public void testNonInterfaceImplementaionIsAutomaticallyHidden() {
42         MutablePicoContainer pc = createImplementationHidingPicoContainer();
43         pc.registerComponentImplementation(HashMap JavaDoc.class, HashMap JavaDoc.class);
44         Map JavaDoc map = (Map JavaDoc) pc.getComponentInstance(HashMap JavaDoc.class);
45         assertNotNull(map);
46         assertTrue(map instanceof HashMap JavaDoc);
47     }
48
49     public void testNonInterfaceImplementaionWithParametersIsAutomaticallyHidden() {
50         MutablePicoContainer pc = createImplementationHidingPicoContainer();
51         pc.registerComponentImplementation(HashMap JavaDoc.class, HashMap JavaDoc.class, new Parameter[0]);
52         Map JavaDoc map = (Map JavaDoc) pc.getComponentInstance(HashMap JavaDoc.class);
53         assertNotNull(map);
54         assertTrue(map instanceof HashMap JavaDoc);
55     }
56
57
58     public void testImplementaionWithParametersIsAutomaticallyHidden() {
59         MutablePicoContainer pc = createImplementationHidingPicoContainer();
60         pc.registerComponentImplementation(Map JavaDoc.class, HashMap JavaDoc.class, new Parameter[0]);
61         Map JavaDoc map = (Map JavaDoc) pc.getComponentInstance(Map JavaDoc.class);
62         assertNotNull(map);
63         assertFalse(map instanceof HashMap JavaDoc);
64     }
65
66     public void testSerializedContainerCanRetrieveImplementation() throws PicoException, PicoInitializationException,
67             IOException JavaDoc, ClassNotFoundException JavaDoc {
68         try {
69             super.testSerializedContainerCanRetrieveImplementation();
70             fail("The ImplementationHidingPicoContainer should not be able to retrieve the component impl");
71         } catch (ClassCastException JavaDoc cce) {
72             // expected.
73
}
74     }
75
76     public void testExceptionThrowingFromHiddenComponent() {
77         MutablePicoContainer pc = createImplementationHidingPicoContainer();
78         pc.registerComponentImplementation(ActionListener JavaDoc.class, Burp.class);
79         try {
80             ActionListener JavaDoc ac = (ActionListener JavaDoc) pc.getComponentInstance(ActionListener JavaDoc.class);
81             ac.actionPerformed(null);
82             fail("Oh no.");
83         } catch (RuntimeException JavaDoc e) {
84             assertEquals("woohoo", e.getMessage());
85         }
86     }
87
88     public static class Burp implements ActionListener JavaDoc {
89
90         public void actionPerformed(ActionEvent JavaDoc e) {
91             throw new RuntimeException JavaDoc("woohoo");
92         }
93     }
94
95 }
96
Popular Tags