1 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 35 public class HandlersTestCase extends TestCase 36 { 37 protected Exception m_exception; 38 39 public HandlersTestCase( final String name ) 40 { 41 super( name ); 42 } 43 44 public void testThreadsafe() 45 throws Exception 46 { 47 final ServiceManager serviceManager = getServiceManager(); 48 final String 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 thread = new Thread () 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 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 85 { 86 final String key = Role3.ROLE; 87 final String 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 thread = new Thread () 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 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 122 { 123 final String key = Role4.ROLE; 124 final String 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 thread = new Thread () 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 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 158 { 159 if ( null != m_exception ) 160 { 161 final Exception exception = m_exception; 162 m_exception = null; 163 throw exception; 164 } 165 } 166 167 public void testPoolable() 168 throws Exception 169 { 170 final ServiceManager serviceManager = getServiceManager(); 171 final String 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 182 { 183 final FortressConfig config = new FortressConfig(); 184 config.setContextDirectory( "./" ); 185 config.setWorkDirectory( "./" ); 186 final String 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 |