KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*****************************************************************************
2  * Copyright (c) PicoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Idea by Rachel Davies, Original code by Stacy Curl *
9  *****************************************************************************/

10
11 package org.picocontainer.defaults;
12
13 import junit.framework.TestCase;
14 import org.picocontainer.ComponentAdapter;
15 import org.picocontainer.PicoInitializationException;
16 import org.picocontainer.PicoRegistrationException;
17 import org.picocontainer.testmodel.AlternativeTouchable;
18 import org.picocontainer.testmodel.SimpleTouchable;
19 import org.picocontainer.testmodel.Touchable;
20
21 public class DefaultComponentRegistryTestCase extends TestCase {
22     private DefaultPicoContainer picoContainer;
23
24     protected void setUp() throws Exception JavaDoc {
25         picoContainer = new DefaultPicoContainer();
26     }
27
28     public void testRegisterComponent() throws PicoRegistrationException {
29         ComponentAdapter componentAdapter = createComponentAdapter();
30         picoContainer.registerComponent(componentAdapter);
31         assertTrue(picoContainer.getComponentAdapters().contains(componentAdapter));
32     }
33
34     public void testUnregisterComponent() throws PicoRegistrationException {
35         ComponentAdapter componentAdapter = createComponentAdapter();
36         picoContainer.registerComponent(componentAdapter);
37         picoContainer.unregisterComponent(Touchable.class);
38         assertFalse(picoContainer.getComponentAdapters().contains(componentAdapter));
39     }
40
41     public void testCannotInstantiateAnUnregisteredComponent() throws PicoRegistrationException, AssignabilityRegistrationException, NotConcreteRegistrationException, PicoInvocationTargetInitializationException, PicoInitializationException {
42         ComponentAdapter componentAdapter = createComponentAdapter();
43         picoContainer.registerComponent(componentAdapter);
44         picoContainer.getComponentInstances();
45         picoContainer.unregisterComponent(Touchable.class);
46
47         assertTrue(picoContainer.getComponentInstances().isEmpty());
48     }
49
50     public void testCanInstantiateReplacedComponent() throws PicoRegistrationException, AssignabilityRegistrationException, NotConcreteRegistrationException, PicoInvocationTargetInitializationException, PicoInitializationException {
51         ComponentAdapter componentAdapter = createComponentAdapter();
52         picoContainer.registerComponent(componentAdapter);
53         picoContainer.getComponentInstances();
54         picoContainer.unregisterComponent(Touchable.class);
55
56         picoContainer.registerComponentImplementation(Touchable.class, AlternativeTouchable.class);
57
58         assertEquals("Container should container 1 component",
59                 1, picoContainer.getComponentInstances().size());
60     }
61
62     public void testUnregisterAfterInstantiateComponents() throws PicoRegistrationException, AssignabilityRegistrationException, PicoInitializationException, DuplicateComponentKeyRegistrationException, PicoInvocationTargetInitializationException, AmbiguousComponentResolutionException {
63         ComponentAdapter componentAdapter = createComponentAdapter();
64         picoContainer.registerComponent(componentAdapter);
65         picoContainer.getComponentInstances();
66         picoContainer.unregisterComponent(Touchable.class);
67         assertNull(picoContainer.getComponentInstance(Touchable.class));
68     }
69
70     public void testReplacedInstantiatedComponentHasCorrectClass() throws PicoRegistrationException, AssignabilityRegistrationException, NotConcreteRegistrationException, PicoInvocationTargetInitializationException, PicoInitializationException {
71         ComponentAdapter componentAdapter = createComponentAdapter();
72         picoContainer.registerComponent(componentAdapter);
73         picoContainer.getComponentInstances();
74         picoContainer.unregisterComponent(Touchable.class);
75
76         picoContainer.registerComponentImplementation(Touchable.class, AlternativeTouchable.class);
77         Object JavaDoc component = picoContainer.getComponentInstances().iterator().next();
78
79         assertEquals(AlternativeTouchable.class, component.getClass());
80     }
81
82     private ComponentAdapter createComponentAdapter() throws AssignabilityRegistrationException, NotConcreteRegistrationException {
83         return new ConstructorInjectionComponentAdapter(Touchable.class, SimpleTouchable.class);
84     }
85 }
86
Popular Tags