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 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 testName() {
  22. TypeVariable tv = new TypeVariable("T");
  23. assertEquals("T", tv.getName());
  24. }
  25. public void testDefaultBounds() {
  26. TypeVariable tv = new TypeVariable("T");
  27. assertEquals("Object", UnresolvedType.OBJECT, tv.getFirstBound());
  28. assertNull(tv.getUpperBound());
  29. assertEquals("no additional bounds", 0, tv.getSuperInterfaces().length);
  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.getSuperInterfaces().length);
  38. assertEquals("java.util.List", javaUtilList, tv.getSuperInterfaces()[0]);
  39. tv = new TypeVariable("E", null, new UnresolvedType[] { javaUtilList });
  40. assertEquals("1 additional bound", 1, tv.getSuperInterfaces().length);
  41. assertEquals("java.util.List", javaUtilList, tv.getSuperInterfaces()[0]);
  42. }
  43. public void testResolution() {
  44. TypeVariable tv = new TypeVariable("T", javaLangNumber, new UnresolvedType[] { javaUtilList });
  45. tv.resolve(world);
  46. assertEquals("resolved number", javaLangNumber.resolve(world), tv.getUpperBound());
  47. assertEquals("resolved list", javaUtilList.resolve(world), tv.getSuperInterfaces()[0]);
  48. }
  49. public void testBindWithoutResolve() {
  50. TypeVariable tv = new TypeVariable("X");
  51. try {
  52. tv.canBeBoundTo(null);
  53. fail("Should throw illegal state exception");
  54. } catch (IllegalStateException ex) {
  55. }
  56. }
  57. public void testCanBindToUpperMatch() {
  58. TypeVariable tv = new TypeVariable("X", javaLangNumber);
  59. tv.resolve(world);
  60. assertTrue(tv.canBeBoundTo(javaLangDouble.resolve(world)));
  61. }
  62. public void testCanBindToUpperFail() {
  63. TypeVariable tv = new TypeVariable("X", javaLangNumber);
  64. tv.resolve(world);
  65. assertFalse(tv.canBeBoundTo(UnresolvedType.OBJECT.resolve(world)));
  66. }
  67. public void testCanBindToInterfaceMatch() {
  68. TypeVariable tv = new TypeVariable("T", javaLangNumber, new UnresolvedType[] { javaIoSerializable });
  69. tv.resolve(world);
  70. assertTrue(tv.canBeBoundTo(javaLangDouble.resolve(world)));
  71. }
  72. public void testCanBindToInterfaceFail() {
  73. TypeVariable tv = new TypeVariable("T", javaLangNumber, new UnresolvedType[] { javaUtilList });
  74. tv.resolve(world);
  75. assertFalse(tv.canBeBoundTo(javaLangDouble.resolve(world)));
  76. }
  77. // ---
  78. @Override
  79. protected void setUp() throws Exception {
  80. super.setUp();
  81. javaLangNumber = UnresolvedType.forSignature("Ljava/lang/Number;");
  82. javaLangDouble = UnresolvedType.forSignature("Ljava/lang/Double;");
  83. javaIoSerializable = UnresolvedType.forSignature("Ljava/io/Serializable;");
  84. javaUtilList = UnresolvedType.forSignature("Ljava/util/List;");
  85. world = new BcelWorld();
  86. }
  87. @Override
  88. protected void tearDown() throws Exception {
  89. super.tearDown();
  90. }
  91. }