KickJava   Java API By Example, From Geeks To Geeks.

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


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

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