1 17 18 package org.apache.avalon.fortress.test; 19 20 import junit.framework.TestCase; 21 import org.apache.avalon.fortress.MetaInfoEntry; 22 import org.apache.avalon.fortress.RoleEntry; 23 import org.apache.avalon.fortress.util.Service; 24 import org.apache.avalon.fortress.impl.handler.FactoryComponentHandler; 25 import org.apache.avalon.fortress.impl.handler.PerThreadComponentHandler; 26 import org.apache.avalon.fortress.impl.handler.PoolableComponentHandler; 27 import org.apache.avalon.fortress.impl.handler.ThreadSafeComponentHandler; 28 import org.apache.avalon.fortress.test.data.BaseRole; 29 import org.apache.avalon.fortress.test.data.Component1; 30 import org.apache.avalon.fortress.test.data.Role1; 31 import org.apache.avalon.fortress.test.data.Role2; 32 33 import java.util.*; 34 35 41 public class MetaInfoEntryTestCase extends TestCase 42 { 43 private Class m_componentClass; 44 private Properties m_properties; 45 private List m_dependencies; 46 private Map m_lifecycleMap; 47 48 public MetaInfoEntryTestCase( String name ) 49 { 50 super( name ); 51 } 52 53 public void setUp() 54 { 55 m_componentClass = Component1.class; 56 m_properties = new Properties(); 57 m_properties.setProperty( "x-avalon.name", "component1" ); 58 m_properties.setProperty( "x-avalon.lifestyle", "singleton" ); 59 60 m_dependencies = new ArrayList(); 61 62 Map lifecycleMap = new HashMap(); 63 lifecycleMap.put( "singleton", ThreadSafeComponentHandler.class ); 64 lifecycleMap.put( "thread", PerThreadComponentHandler.class ); 65 lifecycleMap.put( "pooled", PoolableComponentHandler.class ); 66 lifecycleMap.put( "transient", FactoryComponentHandler.class ); 67 68 m_lifecycleMap = Collections.unmodifiableMap( lifecycleMap ); 69 } 70 71 public void testFullySpecified() throws Exception 72 { 73 MetaInfoEntry entry = new MetaInfoEntry( m_componentClass, m_properties, m_dependencies ); 74 checkMetaInfoEntry( entry, ThreadSafeComponentHandler.class, "component1", false ); 75 } 76 77 public void testAutoDiscovery() throws Exception 78 { 79 m_properties.remove( "x-avalon.lifestyle" ); 80 m_properties.remove( "x-avalon.name" ); 81 m_properties.setProperty( "fortress.handler", ThreadSafeComponentHandler.class.getName() ); 82 m_componentClass = MetaInfoEntry.class; 83 84 MetaInfoEntry entry = new MetaInfoEntry( m_componentClass, m_properties, m_dependencies ); 85 86 checkMetaInfoEntry( entry, ThreadSafeComponentHandler.class, "meta-info-entry", false ); 87 } 88 89 public void testLifestyleMarkers() throws Exception 90 { 91 String name = "component1"; 92 93 Iterator it = m_lifecycleMap.keySet().iterator(); 94 while ( it.hasNext() ) 95 { 96 String type = (String ) it.next(); 97 m_properties.setProperty( "x-avalon.lifestyle", type ); 98 MetaInfoEntry entry = new MetaInfoEntry( m_componentClass, m_properties, m_dependencies ); 99 checkMetaInfoEntry( entry, (Class ) m_lifecycleMap.get( type ), name, false ); 100 } 101 } 102 103 public void testRoleEntryParent() throws Exception 104 { 105 RoleEntry roleEntry = new RoleEntry( Role1.class.getName(), "component1", 106 m_componentClass, ThreadSafeComponentHandler.class ); 107 108 MetaInfoEntry entry = new MetaInfoEntry( roleEntry ); 109 110 checkMetaInfoEntry( entry, ThreadSafeComponentHandler.class, "component1", true ); 111 } 112 113 public void testNullPointerException() throws Exception 114 { 115 try 116 { 117 new MetaInfoEntry( null ); 118 fail( "Did not throw an exception" ); 119 } 120 catch ( NullPointerException npe ) 121 { 122 } 124 catch ( Exception e ) 125 { 126 fail( "Threw wrong exception type: " + e.getClass().getName() ); 127 } 128 129 try 130 { 131 new MetaInfoEntry( null, m_properties, m_dependencies ); 132 fail( "Did not throw an exception" ); 133 } 134 catch ( NullPointerException npe ) 135 { 136 } 138 catch ( Exception e ) 139 { 140 fail( "Threw wrong exception type: " + e.getClass().getName() ); 141 } 142 143 try 144 { 145 new MetaInfoEntry( m_componentClass, null, m_dependencies ); 146 fail( "Did not throw an exception" ); 147 } 148 catch ( NullPointerException npe ) 149 { 150 } 152 catch ( Exception e ) 153 { 154 fail( "Threw wrong exception type: " + e.getClass().getName() ); 155 } 156 157 try 158 { 159 new MetaInfoEntry( m_componentClass, m_properties, null ); 160 fail( "Did not throw an exception" ); 161 } 162 catch ( NullPointerException npe ) 163 { 164 } 166 catch ( Exception e ) 167 { 168 fail( "Threw wrong exception type: " + e.getClass().getName() ); 169 } 170 171 try 172 { 173 MetaInfoEntry entry = new MetaInfoEntry( m_componentClass, m_properties, m_dependencies ); 174 entry.addRole( null ); 175 fail( "Did not throw an exception" ); 176 } 177 catch ( NullPointerException npe ) 178 { 179 } 181 catch ( Exception e ) 182 { 183 fail( "Threw wrong exception type: " + e.getClass().getName() ); 184 } 185 186 try 187 { 188 MetaInfoEntry entry = new MetaInfoEntry( m_componentClass, m_properties, m_dependencies ); 189 entry.addRole( Role1.class.getName() ); 190 entry.containsRole( null ); 191 fail( "Did not throw an exception" ); 192 } 193 catch ( NullPointerException npe ) 194 { 195 } 197 catch ( Exception e ) 198 { 199 fail( "Threw wrong exception type: " + e.getClass().getName() ); 200 } 201 } 202 203 public void testCreateShortName() 204 { 205 String start = "Regular"; 206 String end = "regular"; 207 208 assertEquals( end, MetaInfoEntry.createShortName(start)); 209 210 start = "TwoWords"; 211 end = "two-words"; 212 213 assertEquals( end, MetaInfoEntry.createShortName(start)); 214 215 start = "MANYcaps"; 216 end = "manycaps"; 217 218 assertEquals( end, MetaInfoEntry.createShortName( start ) ); 219 220 start = "MANYcapsAndWords"; 221 end = "manycaps-and-words"; 222 223 assertEquals( end, MetaInfoEntry.createShortName( start ) ); 224 } 225 226 private void checkMetaInfoEntry( MetaInfoEntry entry, Class handler, String name, boolean oneRole ) 227 { 228 assertEquals( m_componentClass, entry.getComponentClass() ); 229 assertEquals( name, entry.getConfigurationName() ); 230 assertEquals( handler, entry.getHandlerClass() ); 231 232 if ( oneRole ) 233 { 234 checkSize( 1, entry.getRoles() ); 235 } 237 else 238 { 239 checkSize( 0, entry.getRoles() ); 240 entry.addRole( Role1.class.getName() ); 241 checkSize( 1, entry.getRoles() ); 242 entry.addRole( BaseRole.class.getName() ); 243 checkSize( 2, entry.getRoles() ); 244 entry.makeReadOnly(); 245 246 assertTrue( entry.containsRole( BaseRole.class.getName() ) ); 247 } 248 249 assertTrue( entry.containsRole( Role1.class.getName() ) ); 250 251 try 252 { 253 entry.addRole( Role2.class.getName() ); 254 fail( "Should not allow Role2 to be added" ); 255 } 256 catch ( SecurityException se ) 257 { 258 } 260 catch ( Exception e ) 261 { 262 fail( "Threw the wrong exception: " + e.getMessage() ); 263 } 264 265 assertTrue( !entry.containsRole( Role2.class.getName() ) ); 266 } 267 268 private void checkSize( int numRoles, Iterator roles ) 269 { 270 int i = 0; 271 while ( roles.hasNext() ) 272 { 273 i++; 274 roles.next(); 275 } 276 277 assertEquals( numRoles, i ); 278 } 279 } 280 | Popular Tags |