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.

CommonWorldTests.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* *******************************************************************
  2. * Copyright (c) 2002-2008 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://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * Andy Clement
  12. * ******************************************************************/
  13. package org.aspectj.weaver;
  14. import java.lang.reflect.Modifier;
  15. import java.util.ArrayList;
  16. import java.util.Arrays;
  17. import java.util.HashSet;
  18. import java.util.List;
  19. import java.util.Set;
  20. import junit.framework.TestCase;
  21. import org.aspectj.testing.util.TestUtil;
  22. /**
  23. * An abstract set of tests that any World implementation should be able to pass. To run it against your World, subclass it and
  24. * implement getWorld().
  25. *
  26. * @author Andy Clement
  27. */
  28. public abstract class CommonWorldTests extends TestCase {
  29. /**
  30. * @return an instance of the World to be tested
  31. */
  32. protected abstract World getWorld();
  33. private World world;
  34. @Override
  35. public void setUp() {
  36. world = getWorld();
  37. }
  38. private final UnresolvedType[] primitiveTypes = UnresolvedType.forSignatures(new String[] { "B", "S", "C", "I", "J", "F", "D",
  39. "V" });
  40. public void testPrimitiveTypes() {
  41. ResolvedType[] primitives = world.resolve(primitiveTypes);
  42. for (int i = 0, len = primitives.length; i < len; i++) {
  43. ResolvedType ty = primitives[i];
  44. modifiersTest(ty, Modifier.PUBLIC | Modifier.FINAL);
  45. fieldsTest(ty, ResolvedMember.NONE);
  46. methodsTest(ty, ResolvedMember.NONE);
  47. interfacesTest(ty, ResolvedType.NONE);
  48. superclassTest(ty, null);
  49. pointcutsTest(ty, ResolvedMember.NONE);
  50. isInterfaceTest(ty, false);
  51. isClassTest(ty, false);
  52. isAspectTest(ty, false);
  53. for (int j = 0; j < len; j++) {
  54. ResolvedType ty1 = primitives[j];
  55. if (ty.equals(ty1)) {
  56. isCoerceableFromTest(ty, ty1, true);
  57. } else if (ty.equals(UnresolvedType.BOOLEAN) || ty1.equals(UnresolvedType.BOOLEAN)
  58. || ty.equals(UnresolvedType.VOID) || ty1.equals(UnresolvedType.VOID)) {
  59. isCoerceableFromTest(ty, ty1, false);
  60. } else {
  61. isCoerceableFromTest(ty, ty1, true);
  62. }
  63. }
  64. // Result of this depends on whether autoboxing is supported
  65. // isCoerceableFromTest(ty, UnresolvedType.OBJECT, getSupportsAutoboxing());
  66. primAssignTest("B", new String[] {});
  67. primAssignTest("S", new String[] { "B" });
  68. primAssignTest("C", new String[] { "B" });
  69. primAssignTest("I", new String[] { "B", "S", "C" });
  70. primAssignTest("J", new String[] { "B", "S", "C", "I" });
  71. primAssignTest("F", new String[] { "B", "S", "C", "I", "J" });
  72. primAssignTest("D", new String[] { "B", "S", "C", "I", "J", "F" });
  73. primAssignTest("Z", new String[] {});
  74. primAssignTest("V", new String[] {});
  75. }
  76. }
  77. private void primAssignTest(String sig, String[] lowers) {
  78. ResolvedType[] primitives = world.resolve(primitiveTypes);
  79. UnresolvedType tx = UnresolvedType.forSignature(sig);
  80. ResolvedType ty = world.resolve(tx, true);
  81. assertTrue("Couldnt find type " + tx, !ty.isMissing());
  82. ResolvedType[] lowerTyArray = world.resolve(UnresolvedType.forSignatures(lowers));
  83. List<ResolvedType> lowerTys = new ArrayList<ResolvedType>(Arrays.asList(lowerTyArray));
  84. lowerTys.add(ty);
  85. Set<ResolvedType> allLowerTys = new HashSet<ResolvedType>(lowerTys);
  86. Set<ResolvedType> allUpperTys = new HashSet<ResolvedType>(Arrays.asList(primitives));
  87. allUpperTys.removeAll(allLowerTys);
  88. for (ResolvedType other : allLowerTys) {
  89. isAssignableFromTest(ty, other, true);
  90. }
  91. for (ResolvedType other : allUpperTys) {
  92. isAssignableFromTest(ty, other, false);
  93. }
  94. }
  95. public void testPrimitiveArrays() {
  96. ResolvedType[] primitives = world.resolve(primitiveTypes);
  97. for (int i = 0, len = primitives.length; i < len; i++) {
  98. ResolvedType ty = primitives[i];
  99. UnresolvedType tx = UnresolvedType.forSignature("[" + ty.getSignature());
  100. ResolvedType aty = world.resolve(tx, true);
  101. assertTrue("Couldnt find type " + tx, !aty.isMissing());
  102. modifiersTest(aty, Modifier.PUBLIC | Modifier.FINAL);
  103. fieldsTest(aty, ResolvedMember.NONE);
  104. methodsTest(aty, ResolvedMember.NONE);
  105. interfaceTest(
  106. aty,
  107. new ResolvedType[] { world.getCoreType(UnresolvedType.CLONEABLE),
  108. world.getCoreType(UnresolvedType.SERIALIZABLE) });
  109. superclassTest(aty, UnresolvedType.OBJECT);
  110. pointcutsTest(aty, ResolvedMember.NONE);
  111. isInterfaceTest(aty, false);
  112. isClassTest(aty, false);
  113. isAspectTest(aty, false);
  114. for (int j = 0; j < len; j++) {
  115. ResolvedType ty1 = primitives[j];
  116. isCoerceableFromTest(aty, ty1, false);
  117. tx = UnresolvedType.forSignature("[" + ty1.getSignature());
  118. ResolvedType aty1 = getWorld().resolve(tx, true);
  119. assertTrue("Couldnt find type " + tx, !aty1.isMissing());
  120. if (ty.equals(ty1)) {
  121. isCoerceableFromTest(aty, aty1, true);
  122. isAssignableFromTest(aty, aty1, true);
  123. } else {
  124. isCoerceableFromTest(aty, aty1, false);
  125. isAssignableFromTest(aty, aty1, false);
  126. }
  127. }
  128. }
  129. // double dimension arrays
  130. for (int i = 0, len = primitives.length; i < len; i++) {
  131. ResolvedType ty = primitives[i];
  132. UnresolvedType tx = UnresolvedType.forSignature("[[" + ty.getSignature());
  133. ResolvedType aty = world.resolve(tx, true);
  134. assertTrue("Couldnt find type " + tx, !aty.isMissing());
  135. modifiersTest(aty, Modifier.PUBLIC | Modifier.FINAL);
  136. fieldsTest(aty, ResolvedMember.NONE);
  137. methodsTest(aty, ResolvedMember.NONE);
  138. interfaceTest(
  139. aty,
  140. new ResolvedType[] { world.getCoreType(UnresolvedType.CLONEABLE),
  141. world.getCoreType(UnresolvedType.SERIALIZABLE) });
  142. superclassTest(aty, UnresolvedType.OBJECT);
  143. pointcutsTest(aty, ResolvedMember.NONE);
  144. isInterfaceTest(aty, false);
  145. isClassTest(aty, false);
  146. isAspectTest(aty, false);
  147. for (int j = 0; j < len; j++) {
  148. ResolvedType ty1 = primitives[j];
  149. isCoerceableFromTest(aty, ty1, false);
  150. tx = UnresolvedType.forSignature("[[" + ty1.getSignature());
  151. ResolvedType aty1 = getWorld().resolve(tx, true);
  152. assertTrue("Couldnt find type " + tx, !aty1.isMissing());
  153. if (ty.equals(ty1)) {
  154. isCoerceableFromTest(aty, aty1, true);
  155. isAssignableFromTest(aty, aty1, true);
  156. } else {
  157. isCoerceableFromTest(aty, aty1, false);
  158. isAssignableFromTest(aty, aty1, false);
  159. }
  160. }
  161. }
  162. }
  163. // ---- tests for parts of ResolvedType objects
  164. protected void modifiersTest(ResolvedType ty, int mods) {
  165. assertEquals(ty + " modifiers:", Modifier.toString(mods), Modifier.toString(ty.getModifiers()));
  166. }
  167. protected void fieldsTest(ResolvedType ty, Member[] x) {
  168. TestUtil.assertSetEquals(ty + " fields:", x, ty.getDeclaredJavaFields());
  169. }
  170. protected void methodsTest(ResolvedType ty, Member[] x) {
  171. TestUtil.assertSetEquals(ty + " methods:", x, ty.getDeclaredJavaMethods());
  172. }
  173. protected void mungersTest(ResolvedType ty, ShadowMunger[] x) {
  174. List<ShadowMunger> l = ty.getDeclaredShadowMungers();
  175. ShadowMunger[] array = (ShadowMunger[]) l.toArray(new ShadowMunger[l.size()]);
  176. TestUtil.assertSetEquals(ty + " mungers:", x, array);
  177. }
  178. protected void interfaceTest(ResolvedType type, ResolvedType[] expectedInterfaces) {
  179. ResolvedType[] interfaces = type.getDeclaredInterfaces();
  180. for (int i = 0; i < expectedInterfaces.length; i++) {
  181. boolean wasMissing = true;
  182. for (int j = 0; j < interfaces.length; j++) {
  183. if (interfaces[j].getSignature().equals(expectedInterfaces[i].getSignature())) {
  184. wasMissing = false;
  185. }
  186. }
  187. if (wasMissing) {
  188. fail("Expected declared interface " + expectedInterfaces[i] + " but it wasn't found in "
  189. + Arrays.asList(interfaces));
  190. }
  191. }
  192. }
  193. protected void interfacesTest(ResolvedType ty, ResolvedType[] x) {
  194. TestUtil.assertArrayEquals(ty + " interfaces:", x, ty.getDeclaredInterfaces());
  195. }
  196. protected void superclassTest(ResolvedType ty, UnresolvedType x) {
  197. assertEquals(ty + " superclass:", x, ty.getSuperclass());
  198. }
  199. protected void pointcutsTest(ResolvedType ty, Member[] x) {
  200. TestUtil.assertSetEquals(ty + " pointcuts:", x, ty.getDeclaredPointcuts());
  201. }
  202. protected void isInterfaceTest(ResolvedType ty, boolean x) {
  203. assertEquals(ty + " is interface:", x, ty.isInterface());
  204. }
  205. protected void isAspectTest(ResolvedType ty, boolean x) {
  206. assertEquals(ty + " is aspect:", x, ty.isAspect());
  207. }
  208. protected void isClassTest(ResolvedType ty, boolean x) {
  209. assertEquals(ty + " is class:", x, ty.isClass());
  210. }
  211. protected void isCoerceableFromTest(UnresolvedType ty0, UnresolvedType ty1, boolean x) {
  212. assertEquals(ty0 + " is coerceable from " + ty1, ty0.resolve(world).isCoerceableFrom(ty1.resolve(world)), x);
  213. assertEquals(ty1 + " is coerceable from " + ty0, ty1.resolve(world).isCoerceableFrom(ty0.resolve(world)), x);
  214. }
  215. protected void isAssignableFromTest(UnresolvedType ty0, UnresolvedType ty1, boolean x) {
  216. ResolvedType rty0 = ty0.resolve(world);
  217. ResolvedType rty1 = ty1.resolve(world);
  218. boolean result = rty0.isAssignableFrom(rty1);
  219. assertEquals(ty0 + " is assignable from " + ty1, result, x);
  220. }
  221. // ---- tests for parts of ResolvedMethod objects
  222. protected void modifiersTest(ResolvedMember m, int mods) {
  223. assertEquals(m + " modifiers:", Modifier.toString(mods), Modifier.toString(m.getModifiers()));
  224. }
  225. protected void exceptionsTest(ResolvedMember m, UnresolvedType[] exns) {
  226. TestUtil.assertSetEquals(m + " exceptions:", exns, m.getExceptions());
  227. }
  228. }