You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TypeVariableTestCase.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* *******************************************************************
  2. * Copyright (c) 2005 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Adrian Colyer Initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver;
  13. import org.aspectj.weaver.bcel.BcelWorld;
  14. import junit.framework.TestCase;
  15. public class TypeVariableTestCase extends TestCase {
  16. private UnresolvedType javaLangNumber;
  17. private UnresolvedType javaLangDouble;
  18. private UnresolvedType javaUtilList;
  19. private UnresolvedType javaIoSerializable;
  20. private World world;
  21. public void testDefaultBounds() {
  22. TypeVariable tv = new TypeVariable("T");
  23. assertEquals("Object",UnresolvedType.OBJECT,tv.getUpperBound());
  24. assertEquals("no additional bounds",0,tv.getAdditionalInterfaceBounds().length);
  25. assertNull("no lower bound",tv.getLowerBound());
  26. }
  27. public void testName() {
  28. TypeVariable tv = new TypeVariable("T");
  29. assertEquals("T",tv.getName());
  30. }
  31. public void testUpperBound() {
  32. TypeVariable tv = new TypeVariable("N",javaLangNumber);
  33. assertEquals("java.lang.Number",javaLangNumber,tv.getUpperBound());
  34. }
  35. public void testAdditionalUpperBounds() {
  36. TypeVariable tv = new TypeVariable("E",UnresolvedType.OBJECT,new UnresolvedType[] {javaUtilList});
  37. assertEquals("1 additional bound",1,tv.getAdditionalInterfaceBounds().length);
  38. assertEquals("java.util.List",javaUtilList,tv.getAdditionalInterfaceBounds()[0]);
  39. }
  40. public void testLowerBound() {
  41. TypeVariable tv = new TypeVariable("X",UnresolvedType.OBJECT,new UnresolvedType[0],javaLangDouble);
  42. assertEquals("java.lang.Double",javaLangDouble,tv.getLowerBound());
  43. }
  44. public void testResolution() {
  45. TypeVariable tv = new TypeVariable(
  46. "T",
  47. javaLangNumber,
  48. new UnresolvedType[] {javaUtilList},
  49. javaLangDouble
  50. );
  51. tv.resolve(world);
  52. assertEquals("resolved number",javaLangNumber.resolve(world),tv.getUpperBound());
  53. assertEquals("resolved list",javaUtilList.resolve(world),
  54. tv.getAdditionalInterfaceBounds()[0]);
  55. assertEquals("resolved double",javaLangDouble.resolve(world),tv.getLowerBound());
  56. }
  57. public void testBindWithoutResolve() {
  58. TypeVariable tv = new TypeVariable("X");
  59. try {
  60. tv.canBeBoundTo(null);
  61. fail ("Should throw illegal state exception");
  62. } catch (IllegalStateException ex) {}
  63. }
  64. public void testCanBindToUpperMatch() {
  65. TypeVariable tv = new TypeVariable("X",javaLangNumber);
  66. tv.resolve(world);
  67. assertTrue(tv.canBeBoundTo(javaLangDouble.resolve(world)));
  68. }
  69. public void testCanBindToUpperFail() {
  70. TypeVariable tv = new TypeVariable("X",javaLangNumber);
  71. tv.resolve(world);
  72. assertFalse(tv.canBeBoundTo(UnresolvedType.OBJECT.resolve(world)));
  73. }
  74. public void testCanBindToInterfaceMatch() {
  75. TypeVariable tv = new TypeVariable("T",javaLangNumber,new UnresolvedType[] {javaIoSerializable});
  76. tv.resolve(world);
  77. assertTrue(tv.canBeBoundTo(javaLangDouble.resolve(world)));
  78. }
  79. public void testCanBindToInterfaceFail() {
  80. TypeVariable tv = new TypeVariable("T",javaLangNumber,new UnresolvedType[] {javaUtilList});
  81. tv.resolve(world);
  82. assertFalse(tv.canBeBoundTo(javaLangDouble.resolve(world)));
  83. }
  84. public void testCanBindToLowerMatch() {
  85. TypeVariable tv = new TypeVariable("T",javaLangNumber,new UnresolvedType[0],javaLangDouble);
  86. tv.resolve(world);
  87. assertTrue(tv.canBeBoundTo(javaLangNumber.resolve(world)));
  88. }
  89. public void testCanBindToLowerFail() {
  90. TypeVariable tv = new TypeVariable("T",javaLangNumber,new UnresolvedType[0],javaLangNumber);
  91. tv.resolve(world);
  92. assertFalse(tv.canBeBoundTo(javaLangDouble.resolve(world)));
  93. }
  94. protected void setUp() throws Exception {
  95. super.setUp();
  96. javaLangNumber = UnresolvedType.forSignature("Ljava/lang/Number;");
  97. javaLangDouble = UnresolvedType.forSignature("Ljava/lang/Double;");
  98. javaIoSerializable = UnresolvedType.forSignature("Ljava/io/Serializable;");
  99. javaUtilList = UnresolvedType.forSignature("Ljava/util/List;");
  100. world = new BcelWorld();
  101. }
  102. protected void tearDown() throws Exception {
  103. super.tearDown();
  104. }
  105. }