KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > util > test > FortressConfigTestCase


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.util.test;
19
20 import junit.framework.TestCase;
21 import org.apache.avalon.excalibur.logger.DefaultLoggerManager;
22 import org.apache.avalon.excalibur.logger.LoggerManager;
23 import org.apache.avalon.fortress.ContainerManagerConstants;
24 import org.apache.avalon.fortress.RoleManager;
25 import org.apache.avalon.fortress.impl.extensions.test.TestInstrumentManager;
26 import org.apache.avalon.fortress.impl.role.FortressRoleManager;
27 import org.apache.avalon.fortress.util.ContextManagerConstants;
28 import org.apache.avalon.fortress.util.FortressConfig;
29 import org.apache.avalon.fortress.util.LifecycleExtensionManager;
30 import org.apache.avalon.framework.configuration.Configuration;
31 import org.apache.avalon.framework.configuration.DefaultConfiguration;
32 import org.apache.avalon.framework.context.Context;
33 import org.apache.avalon.framework.service.DefaultServiceManager;
34 import org.apache.avalon.framework.service.ServiceManager;
35 import org.apache.excalibur.event.Sink;
36 import org.apache.excalibur.event.impl.DefaultQueue;
37 import org.apache.excalibur.instrument.InstrumentManager;
38 import org.apache.excalibur.mpool.DefaultPoolManager;
39 import org.apache.excalibur.mpool.PoolManager;
40
41 import java.io.File JavaDoc;
42
43 /**
44  * FortressConfigTestCase does XYZ
45  *
46  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
47  * @version CVS $ Revision: 1.1 $
48  */

49 public class FortressConfigTestCase extends TestCase
50 {
51     private FortressConfig m_config;
52
53     public FortressConfigTestCase( String JavaDoc name )
54     {
55         super( name );
56     }
57
58     public void setUp() throws Exception JavaDoc
59     {
60         m_config = new FortressConfig( FortressConfig.createDefaultConfig() );
61         m_config.setCommandSink( new DefaultQueue() );
62         m_config.setContextClassLoader( FortressConfigTestCase.class.getClassLoader() );
63         m_config.setInstrumentManager( new TestInstrumentManager() );
64         m_config.setLifecycleExtensionManager( new LifecycleExtensionManager() );
65         m_config.setLoggerCategory( "test" );
66         m_config.setLoggerManager( new DefaultLoggerManager() );
67         m_config.setNumberOfThreadsPerCPU( 10 );
68         m_config.setPoolManager( new DefaultPoolManager() );
69         m_config.setRoleManager( new FortressRoleManager() );
70         m_config.setServiceManager( new DefaultServiceManager() );
71         m_config.setThreadTimeout( 50 );
72     }
73
74     public void testFortressConfigUsingURI() throws Exception JavaDoc
75     {
76         m_config.setContainerClass( FullLifecycleComponent.class.getName() );
77         m_config.setContainerConfiguration( "resource://config.xml" );
78         m_config.setContextDirectory( "/" );
79         m_config.setWorkDirectory( "/" );
80         m_config.setInstrumentManagerConfiguration( "resource://config.xml" );
81         m_config.setLoggerManagerConfiguration( "resource://config.xml" );
82         m_config.setRoleManagerConfiguration( "resource://config.xml" );
83
84         checkContext( m_config.getContext(), true );
85     }
86
87     public void testFortressConfigUsingObject() throws Exception JavaDoc
88     {
89         m_config.setContainerClass( FullLifecycleComponent.class );
90         m_config.setContainerConfiguration( new DefaultConfiguration( "test" ) );
91         m_config.setContextDirectory( new File JavaDoc( "/" ) );
92         m_config.setWorkDirectory( new File JavaDoc( "/" ) );
93         m_config.setInstrumentManagerConfiguration( new DefaultConfiguration( "test" ) );
94         m_config.setLoggerManagerConfiguration( new DefaultConfiguration( "test" ) );
95         m_config.setRoleManagerConfiguration( new DefaultConfiguration( "test" ) );
96
97         checkContext( m_config.getContext(), false );
98     }
99
100     private void checkContext( Context context, boolean useURI ) throws Exception JavaDoc
101     {
102         assertNotNull( context.get( Sink.ROLE ) );
103         assertInstanceof( context.get( Sink.ROLE ), Sink.class );
104
105         assertNotNull( context.get( ContainerManagerConstants.CONTAINER_CLASS ) );
106         assertInstanceof( context.get( ContainerManagerConstants.CONTAINER_CLASS ), Class JavaDoc.class );
107         assertEquals( FullLifecycleComponent.class, context.get( ContainerManagerConstants.CONTAINER_CLASS ) );
108
109         assertNotNull( context.get( ClassLoader JavaDoc.class.getName() ) );
110         assertInstanceof( context.get( ClassLoader JavaDoc.class.getName() ), ClassLoader JavaDoc.class );
111
112         assertNotNull( context.get( ContextManagerConstants.CONTEXT_DIRECTORY ) );
113         assertInstanceof( context.get( ContextManagerConstants.CONTEXT_DIRECTORY ), File JavaDoc.class );
114         assertEquals( new File JavaDoc( "/" ), context.get( ContextManagerConstants.CONTEXT_DIRECTORY ) );
115
116         assertNotNull( context.get( ContextManagerConstants.WORK_DIRECTORY ) );
117         assertInstanceof( context.get( ContextManagerConstants.WORK_DIRECTORY ), File JavaDoc.class );
118         assertEquals( new File JavaDoc( "/" ), context.get( ContextManagerConstants.WORK_DIRECTORY ) );
119
120         assertNotNull( context.get( InstrumentManager.ROLE ) );
121         assertInstanceof( context.get( InstrumentManager.ROLE ), InstrumentManager.class );
122
123         assertNotNull( context.get( LifecycleExtensionManager.ROLE ) );
124         assertInstanceof( context.get( LifecycleExtensionManager.ROLE ), LifecycleExtensionManager.class );
125
126         assertNotNull( context.get( ContextManagerConstants.LOG_CATEGORY ) );
127         assertInstanceof( context.get( ContextManagerConstants.LOG_CATEGORY ), String JavaDoc.class );
128         assertEquals( "test", context.get( ContextManagerConstants.LOG_CATEGORY ) );
129
130         assertNotNull( context.get( LoggerManager.ROLE ) );
131         assertInstanceof( context.get( LoggerManager.ROLE ), LoggerManager.class );
132
133         assertNotNull( context.get( ContextManagerConstants.THREADS_CPU ) );
134         assertInstanceof( context.get( ContextManagerConstants.THREADS_CPU ), Integer JavaDoc.class );
135         assertEquals( new Integer JavaDoc( 10 ), context.get( ContextManagerConstants.THREADS_CPU ) );
136
137         assertNotNull( context.get( PoolManager.ROLE ) );
138         assertInstanceof( context.get( PoolManager.ROLE ), PoolManager.class );
139
140         assertNotNull( context.get( RoleManager.ROLE ) );
141         assertInstanceof( context.get( RoleManager.ROLE ), RoleManager.class );
142
143         assertNotNull( context.get( ContextManagerConstants.SERVICE_MANAGER ) );
144         assertInstanceof( context.get( ContextManagerConstants.SERVICE_MANAGER ), ServiceManager.class );
145
146
147         assertNotNull( context.get( ContextManagerConstants.THREAD_TIMEOUT ) );
148         assertInstanceof( context.get( ContextManagerConstants.THREAD_TIMEOUT ), Long JavaDoc.class );
149         assertEquals( new Long JavaDoc( 50 ), context.get( ContextManagerConstants.THREAD_TIMEOUT ) );
150
151         if ( useURI )
152         {
153             assertNotNull( context.get( ContextManagerConstants.CONFIGURATION_URI ) );
154             assertInstanceof( context.get( ContextManagerConstants.CONFIGURATION_URI ), String JavaDoc.class );
155             assertEquals( "resource://config.xml", context.get( ContextManagerConstants.CONFIGURATION_URI ) );
156
157             assertNotNull( context.get( ContextManagerConstants.INSTRUMENT_MANAGER_CONFIGURATION_URI ) );
158             assertInstanceof( context.get( ContextManagerConstants.INSTRUMENT_MANAGER_CONFIGURATION_URI ), String JavaDoc.class );
159             assertEquals( "resource://config.xml", context.get( ContextManagerConstants.INSTRUMENT_MANAGER_CONFIGURATION_URI ) );
160
161             assertNotNull( context.get( ContextManagerConstants.LOGGER_MANAGER_CONFIGURATION_URI ) );
162             assertInstanceof( context.get( ContextManagerConstants.LOGGER_MANAGER_CONFIGURATION_URI ), String JavaDoc.class );
163             assertEquals( "resource://config.xml", context.get( ContextManagerConstants.LOGGER_MANAGER_CONFIGURATION_URI ) );
164
165             assertNotNull( context.get( ContextManagerConstants.ROLE_MANAGER_CONFIGURATION_URI ) );
166             assertInstanceof( context.get( ContextManagerConstants.ROLE_MANAGER_CONFIGURATION_URI ), String JavaDoc.class );
167             assertEquals( "resource://config.xml", context.get( ContextManagerConstants.ROLE_MANAGER_CONFIGURATION_URI ) );
168         }
169         else
170         {
171             assertNotNull( context.get( ContextManagerConstants.CONFIGURATION ) );
172             assertInstanceof( context.get( ContextManagerConstants.CONFIGURATION ), Configuration.class );
173             assertEquals( "test", ( (Configuration) context.get( ContextManagerConstants.CONFIGURATION ) ).getName() );
174
175             assertNotNull( context.get( ContextManagerConstants.INSTRUMENT_MANAGER_CONFIGURATION ) );
176             assertInstanceof( context.get( ContextManagerConstants.INSTRUMENT_MANAGER_CONFIGURATION ), Configuration.class );
177             assertEquals( "test", ( (Configuration) context.get( ContextManagerConstants.INSTRUMENT_MANAGER_CONFIGURATION ) ).getName() );
178
179             assertNotNull( context.get( ContextManagerConstants.LOGGER_MANAGER_CONFIGURATION ) );
180             assertInstanceof( context.get( ContextManagerConstants.LOGGER_MANAGER_CONFIGURATION ), Configuration.class );
181             assertEquals( "test", ( (Configuration) context.get( ContextManagerConstants.LOGGER_MANAGER_CONFIGURATION ) ).getName() );
182
183             assertNotNull( context.get( ContextManagerConstants.ROLE_MANAGER_CONFIGURATION ) );
184             assertInstanceof( context.get( ContextManagerConstants.ROLE_MANAGER_CONFIGURATION ), Configuration.class );
185             assertEquals( "test", ( (Configuration) context.get( ContextManagerConstants.ROLE_MANAGER_CONFIGURATION ) ).getName() );
186         }
187     }
188
189     protected void assertInstanceof( Object JavaDoc obj, Class JavaDoc klass )
190     {
191         assertTrue( obj.getClass().getName() + " is not an instance of " + klass.getName(),
192             klass.isAssignableFrom( obj.getClass() ) );
193     }
194 }
195
Popular Tags