1 5 package com.tc.util; 6 7 import com.tc.asm.ClassAdapter; 8 import com.tc.asm.ClassVisitor; 9 import com.tc.asm.ClassWriter; 10 import com.tc.aspectwerkz.reflect.ClassInfo; 11 import com.tc.aspectwerkz.reflect.MemberInfo; 12 import com.tc.config.schema.NewCommonL1Config; 13 import com.tc.config.schema.builder.DSOApplicationConfigBuilder; 14 import com.tc.exception.ImplementMe; 15 import com.tc.object.Portability; 16 import com.tc.object.PortabilityImpl; 17 import com.tc.object.bytecode.ClassAdapterFactory; 18 import com.tc.object.bytecode.TransparencyClassAdapter; 19 import com.tc.object.bytecode.TransparentAccess; 20 import com.tc.object.config.ConfigLockLevel; 21 import com.tc.object.config.DSOClientConfigHelper; 22 import com.tc.object.config.DSOSpringConfigHelper; 23 import com.tc.object.config.DistributedMethodSpec; 24 import com.tc.object.config.Lock; 25 import com.tc.object.config.LockDefinition; 26 import com.tc.object.config.ModuleSpec; 27 import com.tc.object.config.TransparencyClassSpec; 28 import com.tc.object.config.schema.DSOInstrumentationLoggingOptions; 29 import com.tc.object.config.schema.DSORuntimeLoggingOptions; 30 import com.tc.object.config.schema.DSORuntimeOutputOptions; 31 import com.tc.object.config.schema.InstrumentedClass; 32 import com.tc.object.logging.InstrumentationLogger; 33 import com.tc.util.ClassUtils.ClassSpec; 34 import com.terracottatech.config.Modules; 35 36 import java.io.ByteArrayOutputStream ; 37 import java.io.OutputStream ; 38 import java.net.Socket ; 39 import java.text.ParseException ; 40 import java.util.Arrays ; 41 import java.util.Collection ; 42 import java.util.Collections ; 43 import java.util.HashMap ; 44 import java.util.HashSet ; 45 import java.util.Iterator ; 46 import java.util.Map ; 47 import java.util.Set ; 48 49 import junit.framework.TestCase; 50 51 public class ClassUtilsTest extends TestCase { 52 53 57 public void setUp() { 58 } 62 63 public void testArrayMethods() { 64 assertEquals(int.class, ClassUtils.baseComponetType(int[][][][][].class)); 65 assertEquals(Object .class, ClassUtils.baseComponetType(Object [].class)); 66 67 try { 68 ClassUtils.baseComponetType(null); 69 fail(); 70 } catch (NullPointerException e) { 71 } 73 74 try { 75 ClassUtils.baseComponetType(int.class); 76 fail(); 77 } catch (IllegalArgumentException e) { 78 } 80 81 assertEquals(5, ClassUtils.arrayDimensions(int[][][][][].class)); 82 assertEquals(1, ClassUtils.arrayDimensions(Object [].class)); 83 84 try { 85 ClassUtils.arrayDimensions(null); 86 fail(); 87 } catch (NullPointerException e) { 88 } 90 91 try { 92 ClassUtils.arrayDimensions(int.class); 93 fail(); 94 } catch (IllegalArgumentException e) { 95 } 97 98 } 99 100 public void testNonPortableReason() { 101 NonPortableReason reason = null; 102 Config config; 103 Portability p; 104 105 config = new Config(); 107 p = new PortabilityImpl(config); 108 config.isNeverAdaptable.add(NotAdaptable.class.getName()); 109 reason = p.getNonPortableReason(NotAdaptable.class); 110 assertEquals(NonPortableReason.CLASS_NOT_ADAPTABLE, reason.getReason()); 111 assertEquals(NotAdaptable.class.getName(), reason.getClassName()); 112 assertEquals(Collections.EMPTY_LIST, reason.getErroneousBootJarSuperClasses()); 113 assertEquals(Collections.EMPTY_LIST, reason.getErroneousSuperClasses()); 114 117 config = new Config(); 119 p = new PortabilityImpl(config); 120 config.isNeverAdaptable.add(NotAdaptable.class.getName()); 121 reason = p.getNonPortableReason(ExtendsNotAdaptable.class); 122 assertEquals(NonPortableReason.SUPER_CLASS_NOT_ADAPTABLE, reason.getReason()); 123 assertEquals(ExtendsNotAdaptable.class.getName(), reason.getClassName()); 124 assertEquals(Collections.EMPTY_LIST, reason.getErroneousBootJarSuperClasses()); 125 assertEquals(Arrays.asList(new Object [] { NotAdaptable.class.getName() }), reason.getErroneousSuperClasses()); 126 128 config = new Config(); 130 p = new PortabilityImpl(config); 131 reason = p.getNonPortableReason(LogicalSubclass.class); 132 assertEquals(NonPortableReason.SUBCLASS_OF_LOGICALLY_MANAGED_CLASS, reason.getReason()); 133 assertEquals(LogicalSubclass.class.getName(), reason.getClassName()); 134 assertEquals(Arrays.asList(new Object [] { HashMap .class.getName() }), reason.getErroneousBootJarSuperClasses()); 135 assertEquals(Collections.EMPTY_LIST, reason.getErroneousSuperClasses()); 136 138 config = new Config(); 141 p = new PortabilityImpl(config); 142 reason = p.getNonPortableReason(OutputStream .class); 143 assertEquals(NonPortableReason.CLASS_NOT_IN_BOOT_JAR, reason.getReason()); 144 assertEquals(OutputStream .class.getName(), reason.getClassName()); 145 assertEquals(Collections.EMPTY_LIST, reason.getErroneousBootJarSuperClasses()); 146 assertEquals(Collections.EMPTY_LIST, reason.getErroneousSuperClasses()); 147 149 config = new Config(); 152 p = new PortabilityImpl(config); 153 reason = p.getNonPortableReason(ByteArrayOutputStream .class); 154 assertEquals(NonPortableReason.CLASS_NOT_IN_BOOT_JAR, reason.getReason()); 155 assertEquals(ByteArrayOutputStream .class.getName(), reason.getClassName()); 156 assertEquals(Arrays.asList(new Object [] { OutputStream .class.getName() }), reason.getErroneousBootJarSuperClasses()); 157 assertEquals(Collections.EMPTY_LIST, reason.getErroneousSuperClasses()); 158 160 163 config = new Config(); 166 p = new PortabilityImpl(config); 167 reason = p.getNonPortableReason(Pojo.class); 168 assertEquals(NonPortableReason.CLASS_NOT_INCLUDED_IN_CONFIG, reason.getReason()); 169 assertEquals(Pojo.class.getName(), reason.getClassName()); 170 assertEquals(Collections.EMPTY_LIST, reason.getErroneousBootJarSuperClasses()); 171 assertEquals(Collections.EMPTY_LIST, reason.getErroneousSuperClasses()); 172 174 config = new Config(); 177 p = new PortabilityImpl(config); 178 reason = p.getNonPortableReason(SubPojo.class); 179 assertEquals(NonPortableReason.CLASS_NOT_INCLUDED_IN_CONFIG, reason.getReason()); 180 assertEquals(SubPojo.class.getName(), reason.getClassName()); 181 assertEquals(Collections.EMPTY_LIST, reason.getErroneousBootJarSuperClasses()); 182 assertEquals(Arrays.asList(new Object [] { Pojo.class.getName() }), reason.getErroneousSuperClasses()); 183 185 config = new Config(); 188 p = new PortabilityImpl(config); 189 reason = p.getNonPortableReason(SubClassOfBootJarClass.class); 190 assertEquals(NonPortableReason.CLASS_NOT_INCLUDED_IN_CONFIG, reason.getReason()); 191 assertEquals(SubClassOfBootJarClass.class.getName(), reason.getClassName()); 192 assertEquals(Arrays.asList(new Object [] { ByteArrayOutputStream .class.getName(), OutputStream .class.getName() }), 193 reason.getErroneousBootJarSuperClasses()); 194 assertEquals(Collections.EMPTY_LIST, reason.getErroneousSuperClasses()); 195 197 config = new Config(); 200 p = new PortabilityImpl(config); 201 reason = p.getNonPortableReason(PojoExtendsBootJar.class); 202 assertEquals(NonPortableReason.CLASS_NOT_INCLUDED_IN_CONFIG, reason.getReason()); 203 assertEquals(PojoExtendsBootJar.class.getName(), reason.getClassName()); 204 assertEquals(Arrays.asList(new Object [] { ByteArrayOutputStream .class.getName(), OutputStream .class.getName() }), 205 reason.getErroneousBootJarSuperClasses()); 206 assertEquals(Arrays.asList(new Object [] { SubClassOfBootJarClass.class.getName() }), reason 207 .getErroneousSuperClasses()); 208 210 config = new Config(); 213 p = new PortabilityImpl(config); 214 reason = p.getNonPortableReason(InstrumentedExtendsRegularNotInstrumented.class); 215 assertEquals(NonPortableReason.SUPER_CLASS_NOT_INSTRUMENTED, reason.getReason()); 216 assertEquals(InstrumentedExtendsRegularNotInstrumented.class.getName(), reason.getClassName()); 217 assertEquals(Collections.EMPTY_LIST, reason.getErroneousBootJarSuperClasses()); 218 assertEquals(Arrays.asList(new Object [] { Pojo.class.getName() }), reason.getErroneousSuperClasses()); 219 221 config = new Config(); 224 p = new PortabilityImpl(config); 225 reason = p.getNonPortableReason(InstrumentedExtendsBootJarNotInstrumented.class); 226 assertEquals(NonPortableReason.SUPER_CLASS_NOT_INSTRUMENTED, reason.getReason()); 227 assertEquals(InstrumentedExtendsBootJarNotInstrumented.class.getName(), reason.getClassName()); 228 assertEquals(Arrays.asList(new Object [] { OutputStream .class.getName() }), reason.getErroneousBootJarSuperClasses()); 229 assertEquals(Collections.EMPTY_LIST, reason.getErroneousSuperClasses()); 230 232 config = new Config(); 235 p = new PortabilityImpl(config); 236 reason = p.getNonPortableReason(InstrumentedExtendsBothNotInstrumented.class); 237 assertEquals(NonPortableReason.SUPER_CLASS_NOT_INSTRUMENTED, reason.getReason()); 238 assertEquals(InstrumentedExtendsBothNotInstrumented.class.getName(), reason.getClassName()); 239 assertEquals(Arrays.asList(new Object [] { ByteArrayOutputStream .class.getName(), OutputStream .class.getName() }), 240 reason.getErroneousBootJarSuperClasses()); 241 assertEquals(Arrays.asList(new Object [] { SubClassOfBootJarClass.class.getName() }), reason 242 .getErroneousSuperClasses()); 243 245 } 246 247 260 267 public void testIsPrimitiveArray() { 268 269 assertTrue(ClassUtils.isPrimitiveArray(new byte[0])); 270 assertTrue(ClassUtils.isPrimitiveArray(new boolean[1])); 271 assertTrue(ClassUtils.isPrimitiveArray(new char[2])); 272 assertTrue(ClassUtils.isPrimitiveArray(new double[3])); 273 assertTrue(ClassUtils.isPrimitiveArray(new float[4])); 274 assertTrue(ClassUtils.isPrimitiveArray(new int[5])); 275 assertTrue(ClassUtils.isPrimitiveArray(new long[6])); 276 assertTrue(ClassUtils.isPrimitiveArray(new short[7])); 277 278 assertFalse(ClassUtils.isPrimitiveArray(new Object [0])); 279 assertFalse(ClassUtils.isPrimitiveArray(new TestCase[42])); 280 assertFalse(ClassUtils.isPrimitiveArray(new Socket [][] { {} })); 281 assertFalse(ClassUtils.isPrimitiveArray(new byte[][] { {} })); 282 283 assertFalse(ClassUtils.isPrimitiveArray(null)); 284 assertFalse(ClassUtils.isPrimitiveArray(new Object ())); 285 } 286 287 public void testParseFullyQualifiedFieldName() throws Exception { 288 String shortFieldName = "baz"; 289 String fullyQualifiedClassname = "foo.Bar"; 290 String fullyQualifiedFieldname = fullyQualifiedClassname + "." + shortFieldName; 291 ClassUtils.parseFullyQualifiedFieldName(fullyQualifiedFieldname); 292 293 ClassSpec spec = ClassUtils.parseFullyQualifiedFieldName(fullyQualifiedFieldname); 294 assertEquals(shortFieldName, spec.getShortFieldName()); 295 assertEquals(fullyQualifiedClassname, spec.getFullyQualifiedClassName()); 296 297 try { 298 spec = ClassUtils.parseFullyQualifiedFieldName(shortFieldName); 299 fail("Expected a ParseException"); 300 } catch (ParseException e) { 301 } 303 304 try { 305 spec = ClassUtils.parseFullyQualifiedFieldName("foo."); 306 fail("Excpected a ParseException"); 307 } catch (ParseException e) { 308 } 310 311 try { 312 spec = ClassUtils.parseFullyQualifiedFieldName(".foo"); 313 fail("Excpected a ParseException"); 314 } catch (ParseException e) { 315 } 317 318 } 319 320 private static class NotAdaptable { 321 } 323 324 private static class ExtendsNotAdaptable extends NotAdaptable { 325 } 327 328 private static class LogicalSubclass extends java.util.HashMap { 329 } 331 332 private static class SubClassOfBootJarClass extends ByteArrayOutputStream { 333 } 335 336 private static class Pojo extends Object { 337 } 339 340 private static class SubPojo extends Pojo { 341 } 343 344 private static class PojoExtendsBootJar extends SubClassOfBootJarClass { 345 } 347 348 private static class InstrumentedExtendsBothNotInstrumented extends SubClassOfBootJarClass implements 349 TransparentAccess { 350 public void __tc_getallfields(Map map) { 351 throw new ImplementMe(); 352 } 353 354 public void __tc_setfield(String name, Object value) { 355 throw new ImplementMe(); 356 } 357 358 public Object __tc_getmanagedfield(String name) { 359 throw new ImplementMe(); 360 } 361 362 public void __tc_setmanagedfield(String name, Object value) { 363 throw new ImplementMe(); 364 } 365 } 366 367 private static class InstrumentedExtendsBootJarNotInstrumented extends OutputStream implements TransparentAccess { 368 369 public void write(int b) { 370 throw new ImplementMe(); 371 } 372 373 public void __tc_getallfields(Map map) { 374 throw new ImplementMe(); 375 } 376 377 public void __tc_setfield(String name, Object value) { 378 throw new ImplementMe(); 379 } 380 381 public Object __tc_getmanagedfield(String name) { 382 throw new ImplementMe(); 383 } 384 385 public void __tc_setmanagedfield(String name, Object value) { 386 throw new ImplementMe(); 387 } 388 } 389 390 private static class InstrumentedExtendsRegularNotInstrumented extends Pojo implements TransparentAccess { 391 public void __tc_getallfields(Map map) { 392 throw new ImplementMe(); 393 } 394 395 public void __tc_setfield(String name, Object value) { 396 throw new ImplementMe(); 397 } 398 399 public Object __tc_getmanagedfield(String name) { 400 throw new ImplementMe(); 401 } 402 403 public void __tc_setmanagedfield(String name, Object value) { 404 throw new ImplementMe(); 405 } 406 } 407 408 private static class Config implements DSOClientConfigHelper { 409 410 final Set isNeverAdaptable = new HashSet (); 411 412 public NewCommonL1Config getNewCommonL1Config() { 413 return null; 414 } 415 416 public boolean shouldBeAdapted(ClassInfo classInfo) { 417 return true; 418 } 419 420 public boolean isNeverAdaptable(ClassInfo classInfo) { 421 return isNeverAdaptable.contains(classInfo.getName()); 422 } 423 424 public boolean isLogical(String theClass) { 425 return theClass.startsWith("java.util."); 426 } 427 428 public boolean isPhysical(String className) { 429 throw new ImplementMe(); 430 } 431 432 public DSOInstrumentationLoggingOptions getInstrumentationLoggingOptions() { 433 throw new ImplementMe(); 434 } 435 436 public TransparencyClassSpec[] getAllSpecs() { 437 throw new ImplementMe(); 438 } 439 440 public void verifyBootJarContents() { 441 throw new ImplementMe(); 442 } 443 444 public Iterator getAllUserDefinedBootSpecs() { 445 throw new ImplementMe(); 446 } 447 448 public ClassAdapter createClassAdapterFor(ClassWriter writer, ClassInfo classInfo, InstrumentationLogger lgr, 449 ClassLoader caller) { 450 throw new ImplementMe(); 451 } 452 453 public ClassAdapter createClassAdapterFor(ClassWriter writer, ClassInfo classInfo, InstrumentationLogger lgr, 454 ClassLoader caller, boolean disableSuperClassTypeChecking) { 455 throw new ImplementMe(); 456 } 457 458 public boolean isCallConstructorOnLoad(String className) { 459 throw new ImplementMe(); 460 } 461 462 public String getChangeApplicatorClassNameFor(String className) { 463 throw new ImplementMe(); 464 } 465 466 public TransparencyClassSpec getOrCreateSpec(String className) { 467 throw new ImplementMe(); 468 } 469 470 public TransparencyClassSpec getOrCreateSpec(String className, String applicator) { 471 throw new ImplementMe(); 472 } 473 474 public LockDefinition[] lockDefinitionsFor(int access, String className, String methodName, String description, 475 String [] exceptions) { 476 throw new ImplementMe(); 477 } 478 479 public boolean isRoot(String className, String fieldName) { 480 throw new ImplementMe(); 481 } 482 483 public boolean isTransient(int modifiers, String classname, String field) { 484 throw new ImplementMe(); 485 } 486 487 public String rootNameFor(String className, String fieldName) { 488 throw new ImplementMe(); 489 } 490 491 public boolean isLockMethod(int access, String className, String methodName, String description, String [] exceptions) { 492 throw new ImplementMe(); 493 } 494 495 public DistributedMethodSpec getDmiSpec(int modifiers, String className, String methodName, String description, 496 String [] exceptions) { 497 throw new ImplementMe(); 498 } 499 500 public TransparencyClassSpec getSpec(String className) { 501 throw new ImplementMe(); 502 } 503 504 public boolean isDSOSessions(String name) { 505 throw new ImplementMe(); 506 } 507 508 public DSORuntimeLoggingOptions runtimeLoggingOptions() { 509 throw new ImplementMe(); 510 } 511 512 public DSORuntimeOutputOptions runtimeOutputOptions() { 513 throw new ImplementMe(); 514 } 515 516 public DSOInstrumentationLoggingOptions instrumentationLoggingOptions() { 517 throw new ImplementMe(); 518 } 519 520 public int getClientInMemoryObjectCount() { 521 throw new ImplementMe(); 522 } 523 524 public int getFaultCount() { 525 throw new ImplementMe(); 526 } 527 528 public void addWriteAutolock(String methodPattern) { 529 throw new ImplementMe(); 530 } 531 532 public void addLock(String methodPattern, LockDefinition lockDefinition) { 533 throw new ImplementMe(); 534 } 535 536 public void addReadAutolock(String methodPattern) { 537 throw new ImplementMe(); 538 } 539 540 public void addAutolock(String methodPattern, ConfigLockLevel type) { 541 throw new ImplementMe(); 542 } 543 544 public void setFaultCount(int count) { 545 throw new ImplementMe(); 546 } 547 548 public void addRoot(String className, String fieldName, String rootName, boolean addSpecForClass) { 549 throw new ImplementMe(); 550 } 551 552 public boolean matches(Lock lock, MemberInfo methodInfo) { 553 throw new ImplementMe(); 554 } 555 556 public boolean matches(String expression, MemberInfo methodInfo) { 557 throw new ImplementMe(); 558 } 559 560 public void addTransient(String className, String fieldName) { 561 throw new ImplementMe(); 562 } 563 564 public void addTransientType(String className, String fieldName) { 565 throw new ImplementMe(); 566 } 567 568 public String getOnLoadScriptIfDefined(String className) { 569 throw new ImplementMe(); 570 } 571 572 public String getOnLoadMethodIfDefined(String className) { 573 throw new ImplementMe(); 574 } 575 576 public boolean isUseNonDefaultConstructor(Class clazz) { 577 throw new ImplementMe(); 578 } 579 580 public void addIncludePattern(String expression) { 581 throw new ImplementMe(); 582 } 583 584 public void addIncludePattern(String expression, boolean honorTransient) { 585 throw new ImplementMe(); 586 } 587 588 public void addExcludePattern(String expression) { 589 throw new ImplementMe(); 590 } 591 592 public boolean hasIncludeExcludePatterns() { 593 throw new ImplementMe(); 594 } 595 596 public boolean hasIncludeExcludePattern(ClassInfo classInfo) { 597 throw new ImplementMe(); 598 } 599 600 public void addAspectModule(String pattern, String moduleName) { 601 throw new ImplementMe(); 602 } 603 604 public Map getAspectModules() { 605 throw new ImplementMe(); 606 } 607 608 public void addDSOSpringConfig(DSOSpringConfigHelper config) { 609 throw new ImplementMe(); 610 } 611 612 public Collection getDSOSpringConfigs() { 613 throw new ImplementMe(); 614 } 615 616 public void addIncludePattern(String expression, boolean honorTransient, boolean oldStyleCallConstructorOnLoad) { 617 throw new ImplementMe(); 618 } 619 620 public void addIncludePattern(String expression, boolean honorTransient, boolean oldStyleCallConstructorOnLoad, 621 boolean honorVolatile) { 622 throw new ImplementMe(); 623 } 624 625 public void addIncludeAndLockIfRequired(String expression, boolean honorTransient, 626 boolean oldStyleCallConstructorOnLoad, boolean honorVolatile, 627 String lockExpression) { 628 throw new ImplementMe(); 629 } 630 631 public boolean isVolatile(int modifiers, String classname, String field) { 632 throw new ImplementMe(); 633 } 634 635 public void addRoot(String rootName, String rootFieldName) { 636 throw new ImplementMe(); 637 638 } 639 640 public void writeTo(DSOApplicationConfigBuilder appConfigBuilder) { 641 throw new ImplementMe(); 642 643 } 644 645 public boolean isRootDSOFinal(String className, String fieldName, boolean isPrimitive) { 646 throw new ImplementMe(); 647 } 648 649 public void addRoot(String className, String fieldName, String rootName, boolean dsoFinal, boolean addSpecForClass) { 650 throw new ImplementMe(); 651 } 652 653 public String getPostCreateMethodIfDefined(String className) { 654 throw new ImplementMe(); 655 } 656 657 public Portability getPortability() { 658 throw new ImplementMe(); 659 } 660 661 public void removeSpec(String className) { 662 throw new ImplementMe(); 663 664 } 665 666 public String getLogicalExtendingClassName(String className) { 667 return null; 668 } 669 670 public TransparencyClassAdapter createDsoClassAdapterFor(ClassVisitor writer, ClassInfo classInfo, 671 InstrumentationLogger lgr, ClassLoader caller, 672 boolean forcePortable) { 673 return null; 674 } 675 676 public void addSynchronousWriteAutolock(String methodPattern) { 677 throw new ImplementMe(); 678 } 679 680 public void addApplicationName(String name) { 681 throw new ImplementMe(); 682 } 683 684 public void addInstrumentationDescriptor(InstrumentedClass classDesc) { 685 throw new ImplementMe(); 686 } 687 688 public void addUserDefinedBootSpec(String className, TransparencyClassSpec spec) { 689 throw new ImplementMe(); 690 } 691 692 public boolean hasCustomAdapter(String fullName) { 693 throw new ImplementMe(); 694 } 695 696 public Class getChangeApplicator(Class clazz) { 697 throw new ImplementMe(); 698 } 699 700 public void setModuleSpecs(ModuleSpec[] pluginSpecs) { 701 throw new ImplementMe(); 702 } 703 704 public void addNewModule(String name, String version) { 705 throw new ImplementMe(); 706 } 707 708 public Modules getModulesForInitialization() { 709 return null; 710 } 711 712 public boolean isPortableModuleClass(Class clazz) { 713 return false; 714 } 715 716 public void addCustomAdapter(String name, ClassAdapterFactory factory) { 717 throw new ImplementMe(); 718 } 719 720 public int getSessionLockType(String appName) { 721 throw new ImplementMe(); 722 } 723 724 public void addSynchronousWriteApplication(String name) { 725 throw new ImplementMe(); 726 } 727 728 public void addDistributedMethodCall(DistributedMethodSpec dms) { 729 throw new ImplementMe(); 730 } 731 732 public Class getTCPeerClass(Class clazz) { 733 throw new ImplementMe(); 734 } 735 } 736 737 } 738 | Popular Tags |