1 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 ; 42 43 49 public class FortressConfigTestCase extends TestCase 50 { 51 private FortressConfig m_config; 52 53 public FortressConfigTestCase( String name ) 54 { 55 super( name ); 56 } 57 58 public void setUp() throws Exception 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 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 88 { 89 m_config.setContainerClass( FullLifecycleComponent.class ); 90 m_config.setContainerConfiguration( new DefaultConfiguration( "test" ) ); 91 m_config.setContextDirectory( new File ( "/" ) ); 92 m_config.setWorkDirectory( new File ( "/" ) ); 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 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 .class ); 107 assertEquals( FullLifecycleComponent.class, context.get( ContainerManagerConstants.CONTAINER_CLASS ) ); 108 109 assertNotNull( context.get( ClassLoader .class.getName() ) ); 110 assertInstanceof( context.get( ClassLoader .class.getName() ), ClassLoader .class ); 111 112 assertNotNull( context.get( ContextManagerConstants.CONTEXT_DIRECTORY ) ); 113 assertInstanceof( context.get( ContextManagerConstants.CONTEXT_DIRECTORY ), File .class ); 114 assertEquals( new File ( "/" ), context.get( ContextManagerConstants.CONTEXT_DIRECTORY ) ); 115 116 assertNotNull( context.get( ContextManagerConstants.WORK_DIRECTORY ) ); 117 assertInstanceof( context.get( ContextManagerConstants.WORK_DIRECTORY ), File .class ); 118 assertEquals( new File ( "/" ), 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 .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 .class ); 135 assertEquals( new Integer ( 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 .class ); 149 assertEquals( new Long ( 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 .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 .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 .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 .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 obj, Class klass ) 190 { 191 assertTrue( obj.getClass().getName() + " is not an instance of " + klass.getName(), 192 klass.isAssignableFrom( obj.getClass() ) ); 193 } 194 } 195 | Popular Tags |