KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > framework > component > test > DefaultComponentManagerTestCase


1 /*
2
3  ============================================================================
4                    The Apache Software License, Version 1.1
5  ============================================================================
6  
7  Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
8  
9  Redistribution and use in source and binary forms, with or without modifica-
10  tion, are permitted provided that the following conditions are met:
11  
12  1. Redistributions of source code must retain the above copyright notice,
13     this list of conditions and the following disclaimer.
14  
15  2. Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18  
19  3. The end-user documentation included with the redistribution, if any, must
20     include the following acknowledgment: "This product includes software
21     developed by the Apache Software Foundation (http://www.apache.org/)."
22     Alternately, this acknowledgment may appear in the software itself, if
23     and wherever such third-party acknowledgments normally appear.
24  
25  4. The names "Jakarta", "Apache Avalon", "Avalon Excalibur", "Avalon
26     Framework" and "Apache Software Foundation" must not be used to endorse
27     or promote products derived from this software without prior written
28     permission. For written permission, please contact apache@apache.org.
29  
30  5. Products derived from this software may not be called "Apache", nor may
31     "Apache" appear in their name, without prior written permission of the
32     Apache Software Foundation.
33  
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  
45  This software consists of voluntary contributions made by many individuals
46  on behalf of the Apache Software Foundation and was originally created by
47  Stefano Mazzocchi <stefano@apache.org>. For more information on the Apache
48  Software Foundation, please see <http://www.apache.org/>.
49  
50 */

51 package org.apache.avalon.framework.component.test;
52
53 import junit.framework.TestCase;
54 import org.apache.avalon.framework.component.Component;
55 import org.apache.avalon.framework.component.ComponentException;
56 import org.apache.avalon.framework.component.DefaultComponentManager;
57
58 /**
59  * Test the basic public methods of DefaultComponentManager.
60  *
61  * @author <a HREF="mailto:rantene@hotmail.com">Ran Tene</a>
62  */

63 public final class DefaultComponentManagerTestCase
64     extends TestCase
65 {
66
67     class DefaultRoleA
68         implements Component,RoleA
69     {
70         public DefaultRoleA()
71         {
72         }
73     }
74
75     class DefaultRoleB
76         implements Component,RoleB
77     {
78         public DefaultRoleB()
79         {
80         }
81     }
82
83
84     private DefaultComponentManager m_componentManager;
85
86     protected boolean m_exceptionThrown;
87
88
89     public DefaultComponentManagerTestCase()
90     {
91         this("DefaultComponentManager Test Case");
92     }
93
94     public DefaultComponentManagerTestCase( final String JavaDoc name )
95     {
96         super( name );
97     }
98
99     protected void setUp()
100         throws Exception JavaDoc
101     {
102         m_componentManager = new DefaultComponentManager();
103         m_exceptionThrown = false;
104     }
105
106     protected void tearDown()
107         throws Exception JavaDoc
108     {
109         m_componentManager = null;
110     }
111
112     /**
113      * lookup contract:
114      * return first component found for role
115      * search in hirarchy from current componentManager up.
116      * if no compnent exist for role a in hierarchy
117      * throw ComponentException
118      */

119
120
121     public void testlookup1()
122         throws Exception JavaDoc
123     {
124         DefaultRoleB roleBinBase = new DefaultRoleB();
125         DefaultRoleB roleBinParent = new DefaultRoleB();
126         DefaultRoleA roleAinParent = new DefaultRoleA();
127
128         m_componentManager.put(RoleA.ROLE,roleAinParent);
129         m_componentManager.put(RoleB.ROLE,roleBinParent);
130         DefaultComponentManager baseComponentManager = new DefaultComponentManager(m_componentManager);
131         baseComponentManager.put(RoleB.ROLE,roleBinBase);
132         Object JavaDoc lookupAinBase = baseComponentManager.lookup(RoleA.ROLE);
133         Object JavaDoc lookupBinBase = baseComponentManager.lookup(RoleB.ROLE);
134         Object JavaDoc lookupBinParent = m_componentManager.lookup(RoleB.ROLE);
135         assertTrue( lookupAinBase instanceof RoleA);
136         assertEquals( lookupBinBase, roleBinBase );
137         assertEquals( lookupBinParent, roleBinParent );
138         assertEquals( lookupAinBase,roleAinParent);
139     }
140
141     public void testlookup2()
142         throws Exception JavaDoc
143     {
144         m_componentManager.put(RoleA.ROLE,new DefaultRoleA());
145         Object JavaDoc o = null;
146         try
147         {
148             o = m_componentManager.lookup(RoleB.ROLE);
149         }
150         catch (ComponentException ce)
151         {
152             m_exceptionThrown = true;
153         }
154         if (o == null)
155             assertTrue("ComponentException was not thrown when component was not found by lookup." ,m_exceptionThrown );
156         else
157             assertTrue("component was found by lookup ,when there was no component.",false);
158
159     }
160
161     public void testhasComponent()
162         throws Exception JavaDoc
163     {
164         m_componentManager.put(RoleA.ROLE,new DefaultRoleA());
165         assertTrue(m_componentManager.hasComponent(RoleA.ROLE));
166         assertTrue(!m_componentManager.hasComponent(RoleB.ROLE));
167     }
168
169     public void testmakeReadOnly()
170         throws Exception JavaDoc
171     {
172         //before read only
173
m_componentManager.put(RoleA.ROLE,new DefaultRoleA());
174         Object JavaDoc a = m_componentManager.lookup(RoleA.ROLE);
175         assertTrue( a instanceof RoleA);
176         m_componentManager.makeReadOnly();
177         //after read only
178
try
179         {
180             m_componentManager.put(RoleB.ROLE,new DefaultRoleB());
181         }
182         catch (IllegalStateException JavaDoc se)
183         {
184             m_exceptionThrown = true;
185         }
186         assertTrue("IllegalStateException was not thrown in put after makeReadOnly." , m_exceptionThrown );
187     }
188 }
189
190
191
192
193
194
195
196
Popular Tags