1 8 package org.codehaus.loom.components.application; 9 10 import java.net.URL ; 11 import java.util.HashMap ; 12 import java.util.Map ; 13 import junit.framework.TestCase; 14 import org.codehaus.dna.Configuration; 15 import org.codehaus.dna.impl.ConsoleLogger; 16 import org.codehaus.dna.impl.DefaultConfiguration; 17 import org.codehaus.loom.components.application.DefaultApplication; 18 import org.codehaus.loom.components.assembler.data.Component1; 19 import org.codehaus.loom.components.assembler.data.Component2; 20 import org.codehaus.loom.components.assembler.data.Component3; 21 import org.codehaus.loom.components.assembler.data.Component4; 22 import org.codehaus.loom.components.assembler.data.Service1; 23 import org.codehaus.loom.components.deployer.PhoenixProfileBuilder; 24 import org.codehaus.loom.components.util.ConfigurationBuilder; 25 import org.codehaus.loom.components.util.info.ComponentInfo; 26 import org.codehaus.loom.components.util.info.DependencyDescriptor; 27 import org.codehaus.loom.components.util.info.ServiceDescriptor; 28 import org.codehaus.loom.components.util.metadata.ComponentTemplate; 29 import org.codehaus.loom.components.util.metadata.DependencyDirective; 30 import org.codehaus.loom.components.util.metadata.PartitionTemplate; 31 import org.codehaus.loom.components.util.profile.ComponentProfile; 32 import org.codehaus.loom.components.util.profile.PartitionProfile; 33 import org.codehaus.loom.interfaces.ContainerConstants; 34 import org.xml.sax.InputSource ; 35 36 42 public class ApplicationTestCase 43 extends TestCase 44 { 45 private static final Class S1_TYPE = Service1.class; 46 private static final Class C1_TYPE = Component1.class; 47 private static final Class C2_TYPE = Component2.class; 48 private static final Class C3_TYPE = Component3.class; 49 private static final Class C4_TYPE = Component4.class; 50 51 private static final String S1_NAME = S1_TYPE.getName(); 52 private static final String C1_NAME = C1_TYPE.getName(); 53 private static final String C2_NAME = C2_TYPE.getName(); 54 private static final String C3_NAME = C3_TYPE.getName(); 55 private static final String C4_NAME = C4_TYPE.getName(); 56 57 private static final String S1_ARRAY_NAME = S1_NAME + 58 DependencyDescriptor.ARRAY_POSTFIX; 59 private static final String S1_MAP_NAME = S1_NAME + 60 DependencyDescriptor.MAP_POSTFIX; 61 62 private static final ComponentInfo C1 = 63 new ComponentInfo( C1_TYPE, 64 ServiceDescriptor.EMPTY_SET, 65 new DependencyDescriptor[]{ 66 new DependencyDescriptor( S1_NAME, 67 S1_NAME, 68 false )}, 69 null ); 70 71 private static final ComponentInfo C2 = 72 new ComponentInfo( C2_TYPE, 73 new ServiceDescriptor[]{ 74 new ServiceDescriptor( S1_NAME )}, 75 DependencyDescriptor.EMPTY_SET, 76 null ); 77 78 private static final ComponentInfo C3 = 79 new ComponentInfo( C3_TYPE, 80 ServiceDescriptor.EMPTY_SET, 81 new DependencyDescriptor[]{ 82 new DependencyDescriptor( S1_ARRAY_NAME, 83 S1_ARRAY_NAME, 84 false )}, 85 null ); 86 87 private static final ComponentInfo C4 = 88 new ComponentInfo( C4_TYPE, 89 ServiceDescriptor.EMPTY_SET, 90 new DependencyDescriptor[]{ 91 new DependencyDescriptor( S1_MAP_NAME, 92 S1_MAP_NAME, 93 false )}, 94 null ); 95 96 public void testBasic() 97 throws Exception 98 { 99 final ComponentTemplate md1 = 100 new ComponentTemplate( "c1", 101 C1_NAME, 102 new DependencyDirective[]{ 103 new DependencyDirective( S1_NAME, 104 "c2", 105 "c2" )}, 106 null, 107 null, 108 false ); 109 final ComponentProfile cp1 = new ComponentProfile( C1, md1 ); 110 final ComponentTemplate md2 = 111 new ComponentTemplate( "c2", 112 C2_NAME, 113 DependencyDirective.EMPTY_SET, 114 null, 115 null, 116 true ); 117 final ComponentProfile cp2 = new ComponentProfile( C2, md2 ); 118 final PartitionTemplate blockPartitionMD = 119 new PartitionTemplate( ContainerConstants.BLOCK_PARTITION, 120 new String []{ 121 ContainerConstants.LISTENER_PARTITION}, 122 PartitionTemplate.EMPTY_SET, 123 new ComponentTemplate[]{md1, md2} ); 124 final PartitionTemplate listenerPartitionMD = 125 new PartitionTemplate( ContainerConstants.LISTENER_PARTITION, 126 new String [ 0 ], 127 PartitionTemplate.EMPTY_SET, 128 ComponentTemplate.EMPTY_SET ); 129 final PartitionTemplate partitionMD = 130 new PartitionTemplate( "test", 131 new String [ 0 ], 132 new PartitionTemplate[]{blockPartitionMD, 133 listenerPartitionMD}, 134 ComponentTemplate.EMPTY_SET ); 135 136 final PartitionProfile blockPartitionProfile = 137 new PartitionProfile( blockPartitionMD, 138 PartitionProfile.EMPTY_SET, 139 new ComponentProfile[]{cp1, cp2} ); 140 final PartitionProfile listenerPartitionProfile = 141 new PartitionProfile( listenerPartitionMD, 142 PartitionProfile.EMPTY_SET, 143 ComponentProfile.EMPTY_SET ); 144 final PartitionProfile partitionProfile = 145 new PartitionProfile( partitionMD, 146 new PartitionProfile[]{blockPartitionProfile, 147 listenerPartitionProfile}, 148 ComponentProfile.EMPTY_SET ); 149 runApplicationTest( partitionProfile ); 150 } 151 152 public void testArrayAssembly() 153 throws Exception 154 { 155 final DependencyDirective[] c3Deps = 156 new DependencyDirective[]{new DependencyDirective( S1_ARRAY_NAME, 157 "c2a", 158 "c2a" ), 159 new DependencyDirective( S1_ARRAY_NAME, 160 "c2b", 161 "c2b" ), 162 new DependencyDirective( S1_ARRAY_NAME, 163 "c2c", 164 "c2c" )}; 165 final ComponentTemplate md3 = 166 new ComponentTemplate( "c3", 167 C3_NAME, 168 c3Deps, 169 null, 170 null, 171 false ); 172 final ComponentProfile cp3 = new ComponentProfile( C3, md3 ); 173 final ComponentTemplate md2a = 174 new ComponentTemplate( "c2a", 175 C2_NAME, 176 DependencyDirective.EMPTY_SET, 177 null, 178 null, 179 false ); 180 final ComponentProfile cp2a = new ComponentProfile( C2, md2a ); 181 final ComponentTemplate md2b = 182 new ComponentTemplate( "c2b", 183 C2_NAME, 184 DependencyDirective.EMPTY_SET, 185 null, 186 null, 187 false ); 188 final ComponentProfile cp2b = new ComponentProfile( C2, md2b ); 189 final ComponentTemplate md2c = 190 new ComponentTemplate( "c2c", 191 C2_NAME, 192 DependencyDirective.EMPTY_SET, 193 null, 194 null, 195 false ); 196 final ComponentProfile cp2c = new ComponentProfile( C2, md2c ); 197 198 final PartitionTemplate blockPartitionMD = 199 new PartitionTemplate( ContainerConstants.BLOCK_PARTITION, 200 new String []{ 201 ContainerConstants.LISTENER_PARTITION}, 202 PartitionTemplate.EMPTY_SET, 203 new ComponentTemplate[]{md3, 204 md2a, 205 md2b, 206 md2c} ); 207 final PartitionTemplate listenerPartitionMD = 208 new PartitionTemplate( ContainerConstants.LISTENER_PARTITION, 209 new String [ 0 ], 210 PartitionTemplate.EMPTY_SET, 211 ComponentTemplate.EMPTY_SET ); 212 final PartitionTemplate partitionMD = 213 new PartitionTemplate( "test", 214 new String [ 0 ], 215 new PartitionTemplate[]{blockPartitionMD, 216 listenerPartitionMD}, 217 ComponentTemplate.EMPTY_SET ); 218 219 final PartitionProfile blockPartitionProfile = 220 new PartitionProfile( blockPartitionMD, 221 PartitionProfile.EMPTY_SET, 222 new ComponentProfile[]{cp3, 223 cp2a, 224 cp2b, 225 cp2c} ); 226 final PartitionProfile listenerPartitionProfile = 227 new PartitionProfile( listenerPartitionMD, 228 PartitionProfile.EMPTY_SET, 229 ComponentProfile.EMPTY_SET ); 230 final PartitionProfile partitionProfile = 231 new PartitionProfile( partitionMD, 232 new PartitionProfile[]{blockPartitionProfile, 233 listenerPartitionProfile}, 234 ComponentProfile.EMPTY_SET ); 235 runApplicationTest( partitionProfile ); 236 } 237 238 public void testMapAssembly() 239 throws Exception 240 { 241 final DependencyDirective[] c4Deps = 242 new DependencyDirective[]{new DependencyDirective( S1_MAP_NAME, 243 "c2a", 244 "c2a" ), 245 new DependencyDirective( S1_MAP_NAME, 246 "c2b", 247 "c2b" ), 248 new DependencyDirective( S1_MAP_NAME, 249 "c2c", 250 "anAlias" )}; 251 final ComponentTemplate md4 = 252 new ComponentTemplate( "c4", 253 C4_NAME, 254 c4Deps, 255 null, 256 null, 257 false ); 258 final ComponentProfile cp4 = new ComponentProfile( C4, md4 ); 259 final ComponentTemplate md2a = 260 new ComponentTemplate( "c2a", 261 C2_NAME, 262 DependencyDirective.EMPTY_SET, 263 null, 264 null, 265 false ); 266 final ComponentProfile cp2a = new ComponentProfile( C2, md2a ); 267 final ComponentTemplate md2b = 268 new ComponentTemplate( "c2b", 269 C2_NAME, 270 DependencyDirective.EMPTY_SET, 271 null, 272 null, 273 false ); 274 final ComponentProfile cp2b = new ComponentProfile( C2, md2b ); 275 final ComponentTemplate md2c = 276 new ComponentTemplate( "c2c", 277 C2_NAME, 278 DependencyDirective.EMPTY_SET, 279 null, 280 null, 281 false ); 282 final ComponentProfile cp2c = new ComponentProfile( C2, md2c ); 283 284 final PartitionTemplate blockPartitionMD = 285 new PartitionTemplate( ContainerConstants.BLOCK_PARTITION, 286 new String []{ 287 ContainerConstants.LISTENER_PARTITION}, 288 PartitionTemplate.EMPTY_SET, 289 new ComponentTemplate[]{md4, 290 md2a, 291 md2b, 292 md2c} ); 293 final PartitionTemplate listenerPartitionMD = 294 new PartitionTemplate( ContainerConstants.LISTENER_PARTITION, 295 new String [ 0 ], 296 PartitionTemplate.EMPTY_SET, 297 ComponentTemplate.EMPTY_SET ); 298 final PartitionTemplate partitionMD = 299 new PartitionTemplate( "test", 300 new String [ 0 ], 301 new PartitionTemplate[]{blockPartitionMD, 302 listenerPartitionMD}, 303 ComponentTemplate.EMPTY_SET ); 304 305 final PartitionProfile blockPartitionProfile = 306 new PartitionProfile( blockPartitionMD, 307 PartitionProfile.EMPTY_SET, 308 new ComponentProfile[]{cp4, 309 cp2a, 310 cp2b, 311 cp2c} ); 312 final PartitionProfile listenerPartitionProfile = 313 new PartitionProfile( listenerPartitionMD, 314 PartitionProfile.EMPTY_SET, 315 ComponentProfile.EMPTY_SET ); 316 final PartitionProfile partitionProfile = 317 new PartitionProfile( partitionMD, 318 new PartitionProfile[]{blockPartitionProfile, 319 listenerPartitionProfile}, 320 ComponentProfile.EMPTY_SET ); 321 runApplicationTest( partitionProfile ); 322 } 323 324 private void runApplicationTest( final PartitionProfile sarMetaData ) 325 throws Exception 326 { 327 final DefaultApplication application = new DefaultApplication(); 328 application.enableLogging( new ConsoleLogger() ); 329 final MockApplicationContext context = 330 new MockApplicationContext( sarMetaData, new ConsoleLogger() ); 331 application.setApplicationContext( context ); 332 application.initialize(); 333 application.dispose(); 334 } 335 336 protected PartitionProfile assembleSar( final String config ) 337 throws Exception 338 { 339 final PhoenixProfileBuilder assembler = new PhoenixProfileBuilder(); 340 final URL resource = getClass().getResource( config ); 341 assertNotNull( "Config resource: " + config, resource ); 342 final Configuration assembly = 343 ConfigurationBuilder.build( 344 new InputSource ( resource.toExternalForm() ), 345 ConfigurationBuilder.ASSEMBLY_SCHEMA, 346 new ConsoleLogger() ); 347 final Map parameters = new HashMap (); 348 parameters.put( ContainerConstants.ASSEMBLY_NAME, "test" ); 349 parameters.put( ContainerConstants.ASSEMBLY_DESCRIPTOR, assembly ); 350 parameters.put( ContainerConstants.CONFIG_DESCRIPTOR, 351 new DefaultConfiguration( "config", "", "" ) ); 352 parameters.put( ContainerConstants.ASSEMBLY_CLASSLOADER, 353 getClass().getClassLoader() ); 354 return assembler.buildProfile( parameters ); 355 } 356 } 357 | Popular Tags |