KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > framework > service > test > DefaultServiceManagerTestCase


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.service.test;
52
53 import junit.framework.TestCase;
54 import org.apache.avalon.framework.service.ServiceException;
55 import org.apache.avalon.framework.service.DefaultServiceManager;
56
57 /**
58  * Test the basic public methods of DefaultComponentManager.
59  *
60  * @author <a HREF="mailto:rantene@hotmail.com">Ran Tene</a>
61  */

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

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