1 8 package org.codehaus.loom.info; 9 10 import com.thoughtworks.qdox.model.DefaultDocletTag; 11 import com.thoughtworks.qdox.model.DefaultDocletTagFactory; 12 import com.thoughtworks.qdox.model.DocletTagFactory; 13 import com.thoughtworks.qdox.model.JavaClass; 14 import com.thoughtworks.qdox.model.JavaMethod; 15 import com.thoughtworks.qdox.model.JavaSource; 16 import java.util.ArrayList ; 17 import java.util.Properties ; 18 import junit.framework.TestCase; 19 20 import org.codehaus.loom.info.PhoenixAttributeInterceptor; 21 import org.codehaus.metaclass.model.Attribute; 22 23 27 public class PhoenixAttributeInterceptorTestCase 28 extends TestCase 29 { 30 public void testProcessClassAttributeWithoutTransformations() 31 throws Exception 32 { 33 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 34 final Attribute attribute = new Attribute( "ignored" ); 35 final Attribute result = 36 interceptor.processClassAttribute( 37 new JavaClass( new JavaSource() ), 38 attribute ); 39 assertNotNull( "attribute", result ); 40 assertEquals( "attribute.name", "ignored", result.getName() ); 41 assertEquals( "attribute.value", null, result.getValue() ); 42 assertEquals( "attribute.parameterCount", 43 0, 44 result.getParameterCount() ); 45 } 46 47 public void testProcessClassAttributeWithPhoenixBlock() 48 throws Exception 49 { 50 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 51 final Attribute attribute = new Attribute( "phoenix:block" ); 52 final Attribute result = 53 interceptor.processClassAttribute( 54 new JavaClass( new JavaSource() ), 55 attribute ); 56 assertNotNull( "attribute", result ); 57 assertEquals( "attribute.name", "dna.component", result.getName() ); 58 assertEquals( "attribute.value", null, result.getValue() ); 59 assertEquals( "attribute.parameterCount", 60 0, 61 result.getParameterCount() ); 62 } 63 64 public void testProcessClassAttributeWithPhoenixMxTopic() 65 throws Exception 66 { 67 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 68 final Properties parameters = new Properties (); 69 parameters.setProperty( "name", "MyTopic" ); 70 final Attribute attribute = new Attribute( "phoenix:mx-topic", 71 parameters ); 72 final Attribute result = 73 interceptor.processClassAttribute( 74 new JavaClass( new JavaSource() ), 75 attribute ); 76 assertNotNull( "attribute", result ); 77 assertEquals( "attribute.name", "mx.component", result.getName() ); 78 assertEquals( "attribute.value", null, result.getValue() ); 79 assertEquals( "attribute.parameterCount", 80 1, 81 result.getParameterCount() ); 82 assertEquals( "attribute.parameter(description)", 83 "MyTopic", 84 result.getParameter( "description" ) ); 85 } 86 87 public void testProcessClassAttributeWithPhoenixService() 88 throws Exception 89 { 90 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 91 final Properties parameters = new Properties (); 92 parameters.setProperty( "name", "X" ); 93 final Attribute attribute = new Attribute( "phoenix:service", 94 parameters ); 95 final Attribute result = 96 interceptor.processClassAttribute( 97 new JavaClass( new JavaSource() ), 98 attribute ); 99 assertNotNull( "attribute", result ); 100 assertEquals( "attribute.name", "dna.service", result.getName() ); 101 assertEquals( "attribute.value", null, result.getValue() ); 102 assertEquals( "attribute.parameterCount", 103 1, 104 result.getParameterCount() ); 105 assertEquals( "attribute.parameter(type)", 106 "X", 107 result.getParameter( "type" ) ); 108 } 109 110 public void testProcessClassAttributeWithPhoenixMXProxy() 111 throws Exception 112 { 113 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 114 final Properties parameters = new Properties (); 115 parameters.setProperty( "class", "X" ); 116 final Attribute attribute = new Attribute( "phoenix:mx-proxy", 117 parameters ); 118 final Attribute result = 119 interceptor.processClassAttribute( 120 new JavaClass( new JavaSource() ), 121 attribute ); 122 assertNotNull( "attribute", result ); 123 assertEquals( "attribute.name", "mx.proxy", result.getName() ); 124 assertEquals( "attribute.value", null, result.getValue() ); 125 assertEquals( "attribute.parameterCount", 126 1, 127 result.getParameterCount() ); 128 assertEquals( "attribute.parameter(type)", 129 "X", 130 result.getParameter( "type" ) ); 131 } 132 133 public void testProcessMethodAttributeWithoutTransformation() 134 throws Exception 135 { 136 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 137 final Attribute attribute = new Attribute( "ignore-me" ); 138 final Attribute result = 139 interceptor.processMethodAttribute( new JavaMethod(), attribute ); 140 assertNotNull( "attribute", result ); 141 assertEquals( "attribute.name", "ignore-me", result.getName() ); 142 assertEquals( "attribute.value", null, result.getValue() ); 143 assertEquals( "attribute.parameterCount", 144 0, 145 result.getParameterCount() ); 146 } 147 148 public void testProcessMethodAttributeWithMXDescription() 149 throws Exception 150 { 151 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 152 final Attribute attribute = new Attribute( "phoenix:mx-description" ); 153 final Attribute result = 154 interceptor.processMethodAttribute( new JavaMethod(), attribute ); 155 assertNull( "attribute", result ); 156 } 157 158 public void testProcessMethodAttributeWithMXAttributeAndNoDescriptionOrComment() 159 throws Exception 160 { 161 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 162 final Attribute attribute = new Attribute( "phoenix:mx-attribute" ); 163 final Attribute result = 164 interceptor.processMethodAttribute( new JavaMethod(), attribute ); 165 assertNotNull( "attribute", result ); 166 assertEquals( "attribute.name", "mx.attribute", result.getName() ); 167 assertEquals( "attribute.value", null, result.getValue() ); 168 assertEquals( "attribute.parameterCount", 169 0, 170 result.getParameterCount() ); 171 } 172 173 public void testProcessMethodAttributeWithMXAttributeAndDescription() 174 throws Exception 175 { 176 final String description = "A Random Thought"; 177 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 178 final Attribute attribute = new Attribute( "phoenix:mx-attribute" ); 179 final JavaMethod method = new JavaMethod(); 180 final ArrayList tags = new ArrayList (); 181 final DocletTagFactory factory = new DefaultDocletTagFactory(); 182 tags.add( 183 factory.createDocletTag( "phoenix:mx-description", description ) ); 184 method.setTags( tags ); 185 final Attribute result = 186 interceptor.processMethodAttribute( method, attribute ); 187 assertNotNull( "attribute", result ); 188 assertEquals( "attribute.name", "mx.attribute", result.getName() ); 189 assertEquals( "attribute.value", null, result.getValue() ); 190 assertEquals( "attribute.parameterCount", 191 1, 192 result.getParameterCount() ); 193 assertEquals( "attribute.parameter(description)", 194 description, result.getParameter( "description" ) ); 195 } 196 197 public void testProcessMethodAttributeWithMXAttributeAndComment() 198 throws Exception 199 { 200 final String description = "A Random Thought"; 201 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 202 final Attribute attribute = new Attribute( "phoenix:mx-attribute" ); 203 final JavaMethod method = new JavaMethod(); 204 method.setComment( description ); 205 final Attribute result = 206 interceptor.processMethodAttribute( method, attribute ); 207 assertNotNull( "attribute", result ); 208 assertEquals( "attribute.name", "mx.attribute", result.getName() ); 209 assertEquals( "attribute.value", null, result.getValue() ); 210 assertEquals( "attribute.parameterCount", 211 1, 212 result.getParameterCount() ); 213 assertEquals( "attribute.parameter(description)", 214 description, result.getParameter( "description" ) ); 215 } 216 217 public void testProcessMethodAttributeWithMXOperationAndNoDescriptionOrComment() 218 throws Exception 219 { 220 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 221 final Attribute attribute = new Attribute( "phoenix:mx-operation" ); 222 final Attribute result = 223 interceptor.processMethodAttribute( new JavaMethod(), attribute ); 224 assertNotNull( "attribute", result ); 225 assertEquals( "attribute.name", "mx.operation", result.getName() ); 226 assertEquals( "attribute.value", null, result.getValue() ); 227 assertEquals( "attribute.parameterCount", 228 0, 229 result.getParameterCount() ); 230 } 231 232 public void testProcessMethodAttributeWithMXOperationAndDescription() 233 throws Exception 234 { 235 final String description = "A Random Thought"; 236 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 237 final Attribute attribute = new Attribute( "phoenix:mx-operation" ); 238 final JavaMethod field = new JavaMethod(); 239 final ArrayList tags = new ArrayList (); 240 tags.add( 241 new DefaultDocletTag( "phoenix:mx-description", description ) ); 242 field.setTags( tags ); 243 final Attribute result = 244 interceptor.processMethodAttribute( field, attribute ); 245 assertNotNull( "attribute", result ); 246 assertEquals( "attribute.name", "mx.operation", result.getName() ); 247 assertEquals( "attribute.value", null, result.getValue() ); 248 assertEquals( "attribute.parameterCount", 249 1, 250 result.getParameterCount() ); 251 assertEquals( "attribute.parameter(description)", 252 description, result.getParameter( "description" ) ); 253 } 254 255 public void testProcessMethodAttributeWithMXOperationAndComment() 256 throws Exception 257 { 258 final String description = "A Random Thought"; 259 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 260 final Attribute attribute = new Attribute( "phoenix:mx-operation" ); 261 final JavaMethod field = new JavaMethod(); 262 field.setComment( description ); 263 final Attribute result = 264 interceptor.processMethodAttribute( field, attribute ); 265 assertNotNull( "attribute", result ); 266 assertEquals( "attribute.name", "mx.operation", result.getName() ); 267 assertEquals( "attribute.value", null, result.getValue() ); 268 assertEquals( "attribute.parameterCount", 269 1, 270 result.getParameterCount() ); 271 assertEquals( "attribute.parameter(description)", 272 description, result.getParameter( "description" ) ); 273 } 274 275 public void testProcessMethodAttributeWithPhoenixConfigurationSchema() 276 throws Exception 277 { 278 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 279 final Attribute attribute = new Attribute( 280 "phoenix:configuration-schema" ); 281 final JavaClass javaClass = new JavaClass( new JavaSource() ); 282 javaClass.setName( "com.biz.MyClass" ); 283 final JavaMethod method = new JavaMethod(); 284 method.setParentClass( javaClass ); 285 final Attribute result = 286 interceptor.processMethodAttribute( method, attribute ); 287 assertNotNull( "attribute", result ); 288 assertEquals( "attribute.name", 289 "dna.configuration", 290 result.getName() ); 291 assertEquals( "attribute.value", null, result.getValue() ); 292 assertEquals( "attribute.parameterCount", 293 0, 294 result.getParameterCount() ); 295 } 296 297 public void testProcessMethodAttributeWithPhoenixConfigurationSchemaSpecifyingType() 298 throws Exception 299 { 300 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 301 final Properties parameters = new Properties (); 302 final String type = "BobbaFett"; 303 parameters.setProperty( "type", type ); 304 final Attribute attribute = new Attribute( 305 "phoenix:configuration-schema", parameters ); 306 final JavaClass javaClass = new JavaClass( new JavaSource() ); 307 javaClass.setName( "com.biz.MyClass" ); 308 final JavaMethod method = new JavaMethod(); 309 method.setParentClass( javaClass ); 310 final Attribute result = 311 interceptor.processMethodAttribute( method, attribute ); 312 assertNotNull( "attribute", result ); 313 assertEquals( "attribute.name", 314 "dna.configuration", 315 result.getName() ); 316 assertEquals( "attribute.value", null, result.getValue() ); 317 assertEquals( "attribute.parameterCount", 318 1, 319 result.getParameterCount() ); 320 assertEquals( "attribute.parameter(type)", 321 type, result.getParameter( "type" ) ); 322 } 323 324 public void testProcessMethodAttributeWithPhoenixConfigurationSchemaSpecifyingRelaxType() 325 throws Exception 326 { 327 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 328 final Properties parameters = new Properties (); 329 parameters.setProperty( "type", "relax-ng" ); 330 final Attribute attribute = new Attribute( 331 "phoenix:configuration-schema", parameters ); 332 final JavaClass javaClass = new JavaClass( new JavaSource() ); 333 javaClass.setName( "com.biz.MyClass" ); 334 final JavaMethod method = new JavaMethod(); 335 method.setParentClass( javaClass ); 336 final Attribute result = 337 interceptor.processMethodAttribute( method, attribute ); 338 assertNotNull( "attribute", result ); 339 assertEquals( "attribute.name", 340 "dna.configuration", 341 result.getName() ); 342 assertEquals( "attribute.value", null, result.getValue() ); 343 assertEquals( "attribute.parameterCount", 344 1, 345 result.getParameterCount() ); 346 assertEquals( "attribute.parameter(type)", 347 "http://relaxng.org/ns/structure/1.0", 348 result.getParameter( "type" ) ); 349 } 350 351 public void testProcessMethodAttributeWithPhoenixDependency() 352 throws Exception 353 { 354 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 355 final Properties parameters = new Properties (); 356 final String type = "BobbaFett"; 357 parameters.setProperty( "name", type ); 358 final Attribute attribute = new Attribute( "phoenix:dependency", 359 parameters ); 360 final Attribute result = 361 interceptor.processMethodAttribute( new JavaMethod(), attribute ); 362 assertNotNull( "attribute", result ); 363 assertEquals( "attribute.name", "dna.dependency", result.getName() ); 364 assertEquals( "attribute.value", null, result.getValue() ); 365 assertEquals( "attribute.parameterCount", 366 1, 367 result.getParameterCount() ); 368 assertEquals( "attribute.parameter(type)", 369 type, result.getParameter( "type" ) ); 370 } 371 372 public void testProcessMethodAttributeWithPhoenixDependencyWithRoleSpecified() 373 throws Exception 374 { 375 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 376 final Properties parameters = new Properties (); 377 final String type = "BobbaFett"; 378 final String key = "myKey"; 379 parameters.setProperty( "name", type ); 380 parameters.setProperty( "role", key ); 381 final Attribute attribute = new Attribute( "phoenix:dependency", 382 parameters ); 383 final Attribute result = 384 interceptor.processMethodAttribute( new JavaMethod(), attribute ); 385 assertNotNull( "attribute", result ); 386 assertEquals( "attribute.name", "dna.dependency", result.getName() ); 387 assertEquals( "attribute.value", null, result.getValue() ); 388 assertEquals( "attribute.parameterCount", 389 2, 390 result.getParameterCount() ); 391 assertEquals( "attribute.parameter(type)", 392 type, result.getParameter( "type" ) ); 393 assertEquals( "attribute.parameter(key)", 394 key, result.getParameter( "key" ) ); 395 } 396 397 public void testProcessClassAttributesWithoutMXService() 398 throws Exception 399 { 400 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 401 final Attribute[] input = new Attribute[]{ 402 new Attribute( "RandomJAttribute" )}; 403 final Attribute[] attributes = 404 interceptor.processClassAttributes( 405 new JavaClass( new JavaSource() ), input ); 406 assertEquals( "attributes.length", 1, attributes.length ); 407 assertEquals( "attributes[0].name", 408 "RandomJAttribute", 409 attributes[ 0 ].getName() ); 410 assertEquals( "attributes[0].value", 411 null, 412 attributes[ 0 ].getValue() ); 413 assertEquals( "attributes[0].parameterCount", 414 0, 415 attributes[ 0 ].getParameterCount() ); 416 } 417 418 public void testProcessClassAttributesWithMXService() 419 throws Exception 420 { 421 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 422 final Properties parameters = new Properties (); 423 final String type = "PerformMagicService"; 424 parameters.setProperty( "name", type ); 425 final Attribute attribute = new Attribute( "phoenix:mx", parameters ); 426 final Attribute[] input = new Attribute[]{attribute}; 427 final Attribute[] attributes = 428 interceptor.processClassAttributes( 429 new JavaClass( new JavaSource() ), input ); 430 assertEquals( "attributes.length", 2, attributes.length ); 431 assertEquals( "attributes[0].name", 432 "dna.service", 433 attributes[ 0 ].getName() ); 434 assertEquals( "attributes[0].value", 435 null, 436 attributes[ 0 ].getValue() ); 437 assertEquals( "attributes[0].parameterCount", 438 1, 439 attributes[ 0 ].getParameterCount() ); 440 assertEquals( "attributes[0].parameter(type)", 441 type, attributes[ 0 ].getParameter( "type" ) ); 442 assertEquals( "attributes[1].name", 443 "mx.interface", 444 attributes[ 1 ].getName() ); 445 assertEquals( "attributes[1].value", 446 null, 447 attributes[ 1 ].getValue() ); 448 assertEquals( "attributes[1].parameterCount", 449 2, 450 attributes[ 1 ].getParameterCount() ); 451 assertEquals( "attributes[1].parameter(type)", 452 type, attributes[ 1 ].getParameter( "type" ) ); 453 assertEquals( "attributes[1].parameter(topic)", 454 "PerformMagicService", 455 attributes[ 1 ].getParameter( "type" ) ); 456 } 457 458 public void testProcessMethodAttributesWithoutMXOperation() 459 throws Exception 460 { 461 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 462 final Attribute[] input = new Attribute[]{ 463 new Attribute( "RandomJAttribute" )}; 464 final Attribute[] attributes = 465 interceptor.processMethodAttributes( new JavaMethod(), input ); 466 assertEquals( "attributes.length", 1, attributes.length ); 467 assertEquals( "attributes[0].name", 468 "RandomJAttribute", 469 attributes[ 0 ].getName() ); 470 assertEquals( "attributes[0].value", 471 null, 472 attributes[ 0 ].getValue() ); 473 assertEquals( "attributes[0].parameterCount", 474 0, 475 attributes[ 0 ].getParameterCount() ); 476 477 } 478 479 public void testProcessMethodAttributesWithMXOperationWithoutParameters() 480 throws Exception 481 { 482 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 483 final Attribute[] input = new Attribute[]{ 484 new Attribute( "mx.operation" )}; 485 final Attribute[] attributes = 486 interceptor.processMethodAttributes( new JavaMethod(), input ); 487 assertEquals( "attributes.length", 1, attributes.length ); 488 assertEquals( "attributes[0].name", 489 "mx.operation", 490 attributes[ 0 ].getName() ); 491 assertEquals( "attributes[0].value", 492 null, 493 attributes[ 0 ].getValue() ); 494 assertEquals( "attributes[0].parameterCount", 495 0, 496 attributes[ 0 ].getParameterCount() ); 497 } 498 499 public void testProcessMethodAttributesWithMXOperationWithMalformedParameter() 500 throws Exception 501 { 502 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 503 final Attribute[] input = 504 new Attribute[]{new Attribute( "mx.operation" ), 505 new Attribute( "param", "var" )}; 506 final Attribute[] attributes = 507 interceptor.processMethodAttributes( new JavaMethod(), input ); 508 assertEquals( "attributes.length", 2, attributes.length ); 509 assertEquals( "attributes[0].name", 510 "mx.operation", 511 attributes[ 0 ].getName() ); 512 assertEquals( "attributes[0].value", 513 null, 514 attributes[ 0 ].getValue() ); 515 assertEquals( "attributes[0].parameterCount", 516 0, 517 attributes[ 0 ].getParameterCount() ); 518 assertEquals( "attributes[1].name", 519 "param", 520 attributes[ 1 ].getName() ); 521 assertEquals( "attributes[1].value", 522 "var", 523 attributes[ 1 ].getValue() ); 524 assertEquals( "attributes[1].parameterCount", 525 0, 526 attributes[ 1 ].getParameterCount() ); 527 } 528 529 public void testProcessMethodAttributesWithMXOperationWithParameter() 530 throws Exception 531 { 532 final PhoenixAttributeInterceptor interceptor = new PhoenixAttributeInterceptor(); 533 final Attribute[] input = 534 new Attribute[]{new Attribute( "mx.operation" ), 535 new Attribute( "param", 536 "var This is the description" )}; 537 final Attribute[] attributes = 538 interceptor.processMethodAttributes( new JavaMethod(), input ); 539 assertEquals( "attributes.length", 3, attributes.length ); 540 assertEquals( "attributes[0].name", 541 "mx.operation", 542 attributes[ 0 ].getName() ); 543 assertEquals( "attributes[0].value", 544 null, 545 attributes[ 0 ].getValue() ); 546 assertEquals( "attributes[0].parameterCount", 547 0, 548 attributes[ 0 ].getParameterCount() ); 549 assertEquals( "attributes[1].name", 550 "param", 551 attributes[ 1 ].getName() ); 552 assertEquals( "attributes[1].value", 553 "var This is the description", 554 attributes[ 1 ].getValue() ); 555 assertEquals( "attributes[1].parameterCount", 556 0, 557 attributes[ 1 ].getParameterCount() ); 558 assertEquals( "attributes[2].name", 559 "mx.parameter", 560 attributes[ 2 ].getName() ); 561 assertEquals( "attributes[2].value", 562 null, 563 attributes[ 2 ].getValue() ); 564 assertEquals( "attributes[2].parameterCount", 565 2, 566 attributes[ 2 ].getParameterCount() ); 567 assertEquals( "attributes[2].parameter(name)", 568 "var", 569 attributes[ 2 ].getParameter( "name" ) ); 570 assertEquals( "attributes[2].parameter(description)", 571 "This is the description", 572 attributes[ 2 ].getParameter( "description" ) ); 573 } 574 } 575 | Popular Tags |