KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > test > HandlersTestCase


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.fortress.test;
19
20 import junit.framework.TestCase;
21 import org.apache.avalon.fortress.ContainerManager;
22 import org.apache.avalon.fortress.impl.DefaultContainer;
23 import org.apache.avalon.fortress.impl.DefaultContainerManager;
24 import org.apache.avalon.fortress.test.data.*;
25 import org.apache.avalon.fortress.util.FortressConfig;
26 import org.apache.avalon.framework.container.ContainerUtil;
27 import org.apache.avalon.framework.service.ServiceManager;
28
29 /**
30  * A testcase for the different handlers.
31  *
32  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
33  * @version $Revision: 1.17 $ $Date: 2004/02/28 15:16:26 $
34  */

35 public class HandlersTestCase extends TestCase
36 {
37     protected Exception JavaDoc m_exception;
38
39     public HandlersTestCase( final String JavaDoc name )
40     {
41         super( name );
42     }
43
44     public void testThreadsafe()
45         throws Exception JavaDoc
46     {
47         final ServiceManager serviceManager = getServiceManager();
48         final String JavaDoc key = Role1.ROLE;
49         final BaseRole object1 = (BaseRole) serviceManager.lookup( key );
50         final BaseRole object2 = (BaseRole) serviceManager.lookup( key );
51
52         assertSame( "Threadsafe objects (1 vs 2)", object1, object2 );
53         assertEquals( "Threadsafe object IDs (1 vs 2)", object1.getID(), object2.getID() );
54
55         final Thread JavaDoc thread = new Thread JavaDoc()
56         {
57             public void run()
58             {
59                 try
60                 {
61                     final BaseRole object3 = (BaseRole) serviceManager.lookup( key );
62                     final BaseRole object4 = (BaseRole) serviceManager.lookup( key );
63
64                     assertSame( "Threadsafe objects (1 vs 3)", object1, object3 );
65                     assertEquals( "Threadsafe object IDs (1 vs 3)", object1.getID(), object3.getID() );
66                     assertSame( "Threadsafe objects (2 vs 4)", object2, object4 );
67                     assertEquals( "Threadsafe object IDs (2 vs 4)", object2.getID(), object4.getID() );
68                     assertSame( "Threadsafe objects (3 vs 4)", object3, object4 );
69                     assertEquals( "Threadsafe object IDs (3 vs 4)", object3.getID(), object4.getID() );
70                 }
71                 catch ( final Exception JavaDoc e )
72                 {
73                     m_exception = e;
74                 }
75             }
76         };
77         thread.start();
78         thread.join();
79
80         checkException();
81     }
82
83     public void testPerThread()
84         throws Exception JavaDoc
85     {
86         final String JavaDoc key = Role3.ROLE;
87         final String JavaDoc type = "PerThread";
88
89         final ServiceManager serviceManager = getServiceManager();
90         final BaseRole object1 = (BaseRole) serviceManager.lookup( key );
91         final BaseRole object2 = (BaseRole) serviceManager.lookup( key );
92
93         assertEquals( type + " object IDs (1 vs 2)", object1.getID(), object2.getID() );
94
95         final Thread JavaDoc thread = new Thread JavaDoc()
96         {
97             public void run()
98             {
99                 try
100                 {
101                     final BaseRole object3 = (BaseRole) serviceManager.lookup( key );
102                     final BaseRole object4 = (BaseRole) serviceManager.lookup( key );
103
104                     assertTrue( type + " object IDs (1 vs 3)", object1.getID() != object3.getID() );
105                     assertTrue( type + " object IDs (2 vs 4)", object2.getID() != object4.getID() );
106                     assertEquals( type + " object IDs (3 vs 4)", object3.getID(), object4.getID() );
107                 }
108                 catch ( final Exception JavaDoc e )
109                 {
110                     m_exception = e;
111                 }
112             }
113         };
114         thread.start();
115         thread.join();
116
117         checkException();
118     }
119
120     public void testFactory()
121         throws Exception JavaDoc
122     {
123         final String JavaDoc key = Role4.ROLE;
124         final String JavaDoc type = "Factory";
125
126         final ServiceManager serviceManager = getServiceManager();
127         final BaseRole object1 = (BaseRole) serviceManager.lookup( key );
128         final BaseRole object2 = (BaseRole) serviceManager.lookup( key );
129
130         assertTrue( type + " object IDs (1 vs 2)", object1.getID() != object2.getID() );
131
132         final Thread JavaDoc thread = new Thread JavaDoc()
133         {
134             public void run()
135             {
136                 try
137                 {
138                     final BaseRole object3 = (BaseRole) serviceManager.lookup( key );
139                     final BaseRole object4 = (BaseRole) serviceManager.lookup( key );
140
141                     assertTrue( type + " object IDs (1 vs 3)", object1.getID() != object3.getID() );
142                     assertTrue( type + " object IDs (2 vs 4)", object2.getID() != object4.getID() );
143                     assertTrue( type + " object IDs (3 vs 4)", object3.getID() != object4.getID() );
144                 }
145                 catch ( final Exception JavaDoc e )
146                 {
147                     m_exception = e;
148                 }
149             }
150         };
151         thread.start();
152         thread.join();
153
154         checkException();
155     }
156
157     private void checkException() throws Exception JavaDoc
158     {
159         if ( null != m_exception )
160         {
161             final Exception JavaDoc exception = m_exception;
162             m_exception = null;
163             throw exception;
164         }
165     }
166
167     public void testPoolable()
168         throws Exception JavaDoc
169     {
170         final ServiceManager serviceManager = getServiceManager();
171         final String JavaDoc key = Role2.ROLE;
172         final BaseRole object1 = (BaseRole) serviceManager.lookup( key );
173         final BaseRole object2 = (BaseRole) serviceManager.lookup( key );
174         final BaseRole object3 = (BaseRole) serviceManager.lookup( key );
175
176         serviceManager.release( object1 );
177         serviceManager.release( object2 );
178         serviceManager.release( object3 );
179     }
180
181     private ServiceManager getServiceManager() throws Exception JavaDoc
182     {
183         final FortressConfig config = new FortressConfig();
184         config.setContextDirectory( "./" );
185         config.setWorkDirectory( "./" );
186         final String JavaDoc BASE = "resource://org/apache/avalon/fortress/test/data/";
187         config.setContainerConfiguration( BASE + "test1.xconf" );
188         config.setLoggerManagerConfiguration( BASE + "test1.xlog" );
189
190         final ContainerManager cm = new DefaultContainerManager( config.getContext() );
191         ContainerUtil.initialize( cm );
192
193         final DefaultContainer container = (DefaultContainer) cm.getContainer();
194         final ServiceManager serviceManager = container.getServiceManager();
195         return serviceManager;
196     }
197 }
198
Popular Tags