KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.picocontainer.defaults;
9
10 import junit.framework.TestCase;
11 import org.picocontainer.ComponentAdapter;
12 import org.picocontainer.ComponentMonitor;
13 import org.picocontainer.Parameter;
14 import org.picocontainer.PicoContainer;
15 import org.picocontainer.PicoInitializationException;
16 import org.picocontainer.PicoIntrospectionException;
17 import org.picocontainer.PicoVerificationException;
18 import org.picocontainer.PicoVisitor;
19
20 import java.lang.reflect.Constructor JavaDoc;
21
22 /**
23  * Test AbstractComponentAdapter behaviour
24  * @author Jörg Schaible
25  */

26 public class ComponentAdapterTestCase
27         extends TestCase {
28
29     private static class TestComponentAdapter extends AbstractComponentAdapter {
30         TestComponentAdapter(Object JavaDoc componentKey, Class JavaDoc componentImplementation, ComponentMonitor componentMonitor) throws AssignabilityRegistrationException {
31             super(componentKey, componentImplementation, componentMonitor);
32         }
33         TestComponentAdapter(Object JavaDoc componentKey, Class JavaDoc componentImplementation) throws AssignabilityRegistrationException {
34             super(componentKey, componentImplementation);
35         }
36         public Object JavaDoc getComponentInstance(PicoContainer container) throws PicoInitializationException, PicoIntrospectionException {
37             return null;
38         }
39         public void verify(PicoContainer container) throws PicoVerificationException {
40         }
41         
42     }
43
44     private static class TestMonitoringComponentAdapter extends MonitoringComponentAdapter {
45         TestMonitoringComponentAdapter(ComponentMonitor componentMonitor) throws AssignabilityRegistrationException {
46             super(componentMonitor);
47         }
48         public Object JavaDoc getComponentInstance(PicoContainer container) throws PicoInitializationException, PicoIntrospectionException {
49             return null;
50         }
51         public void verify(PicoContainer container) throws PicoVerificationException {
52         }
53         public Object JavaDoc getComponentKey() {
54             return null;
55         }
56         public Class JavaDoc getComponentImplementation() {
57             return null;
58         }
59         public void accept(PicoVisitor visitor) {
60         }
61     }
62     
63     private static class TestInstantiatingComponentAdapter extends InstantiatingComponentAdapter {
64         TestInstantiatingComponentAdapter(Object JavaDoc componentKey, Class JavaDoc componentImplementation, Parameter[] parameters, boolean allowNonPublicClasses) {
65             super(componentKey, componentImplementation, parameters, allowNonPublicClasses);
66         }
67         protected Constructor JavaDoc getGreediestSatisfiableConstructor(PicoContainer container) throws PicoIntrospectionException, UnsatisfiableDependenciesException, AmbiguousComponentResolutionException, AssignabilityRegistrationException, NotConcreteRegistrationException {
68             return null;
69         }
70         public Object JavaDoc getComponentInstance(PicoContainer container) throws PicoInitializationException, PicoIntrospectionException {
71             return null;
72         }
73     }
74     
75     public void testComponentImplementationMayNotBeNull() {
76         try {
77             new TestComponentAdapter("Key", null);
78             fail("NullPointerException expected");
79         } catch (NullPointerException JavaDoc e) {
80             assertEquals("componentImplementation", e.getMessage());
81         }
82     }
83
84     public void testComponentKeyCanBeNullButNotRequested() {
85         ComponentAdapter componentAdapter = new TestComponentAdapter(null, String JavaDoc.class);
86         try {
87             componentAdapter.getComponentKey();
88             fail("NullPointerException expected");
89         } catch (NullPointerException JavaDoc e) {
90             assertEquals("componentKey", e.getMessage());
91         }
92     }
93
94     public void testComponentMonitorMayNotBeNull() {
95         try {
96             new TestComponentAdapter("Key", String JavaDoc.class, null);
97             fail("NullPointerException expected");
98         } catch (NullPointerException JavaDoc e) {
99             assertEquals("monitor", e.getMessage());
100         }
101         try {
102             new TestMonitoringComponentAdapter(null);
103             fail("NullPointerException expected");
104         } catch (NullPointerException JavaDoc e) {
105             assertEquals("monitor", e.getMessage());
106         }
107     }
108
109     public void testParameterMayNotBeNull() throws Exception JavaDoc {
110         try {
111             new TestInstantiatingComponentAdapter("Key", String JavaDoc.class, new Parameter[]{new ConstantParameter("Value"), null}, false);
112             fail("Thrown " + NullPointerException JavaDoc.class.getName() + " expected");
113         } catch (final NullPointerException JavaDoc e) {
114             assertTrue(e.getMessage().endsWith("1 is null"));
115         }
116     }
117     
118     public void testStringRepresentation() {
119         ComponentAdapter componentAdapter = new TestComponentAdapter("Key", Integer JavaDoc.class);
120         assertEquals(TestComponentAdapter.class.getName() + "[Key]", componentAdapter.toString());
121     }
122 }
123
Popular Tags