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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  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 (ResolvedType ty : primitives) {
  43. modifiersTest(ty, Modifier.PUBLIC | Modifier.FINAL);
  44. fieldsTest(ty, ResolvedMember.NONE);
  45. methodsTest(ty, ResolvedMember.NONE);
  46. interfacesTest(ty, ResolvedType.NONE);
  47. superclassTest(ty, null);
  48. pointcutsTest(ty, ResolvedMember.NONE);
  49. isInterfaceTest(ty, false);
  50. isClassTest(ty, false);
  51. isAspectTest(ty, false);
  52. for (ResolvedType ty1 : primitives) {
  53. if (ty.equals(ty1)) {
  54. isCoerceableFromTest(ty, ty1, true);
  55. } else if (ty.equals(UnresolvedType.BOOLEAN) || ty1.equals(UnresolvedType.BOOLEAN)
  56. || ty.equals(UnresolvedType.VOID) || ty1.equals(UnresolvedType.VOID)) {
  57. isCoerceableFromTest(ty, ty1, false);
  58. } else {
  59. isCoerceableFromTest(ty, ty1, true);
  60. }
  61. }
  62. // Result of this depends on whether autoboxing is supported
  63. // isCoerceableFromTest(ty, UnresolvedType.OBJECT, getSupportsAutoboxing());
  64. primAssignTest("B", new String[]{});
  65. primAssignTest("S", new String[]{"B"});
  66. primAssignTest("C", new String[]{"B"});
  67. primAssignTest("I", new String[]{"B", "S", "C"});
  68. primAssignTest("J", new String[]{"B", "S", "C", "I"});
  69. primAssignTest("F", new String[]{"B", "S", "C", "I", "J"});
  70. primAssignTest("D", new String[]{"B", "S", "C", "I", "J", "F"});
  71. primAssignTest("Z", new String[]{});
  72. primAssignTest("V", new String[]{});
  73. }
  74. }
  75. private void primAssignTest(String sig, String[] lowers) {
  76. ResolvedType[] primitives = world.resolve(primitiveTypes);
  77. UnresolvedType tx = UnresolvedType.forSignature(sig);
  78. ResolvedType ty = world.resolve(tx, true);
  79. assertTrue("Couldnt find type " + tx, !ty.isMissing());
  80. ResolvedType[] lowerTyArray = world.resolve(UnresolvedType.forSignatures(lowers));
  81. List<ResolvedType> lowerTys = new ArrayList<>(Arrays.asList(lowerTyArray));
  82. lowerTys.add(ty);
  83. Set<ResolvedType> allLowerTys = new HashSet<>(lowerTys);
  84. Set<ResolvedType> allUpperTys = new HashSet<>(Arrays.asList(primitives));
  85. allUpperTys.removeAll(allLowerTys);
  86. for (ResolvedType other : allLowerTys) {
  87. isAssignableFromTest(ty, other, true);
  88. }
  89. for (ResolvedType other : allUpperTys) {
  90. isAssignableFromTest(ty, other, false);
  91. }
  92. }
  93. public void testPrimitiveArrays() {
  94. ResolvedType[] primitives = world.resolve(primitiveTypes);
  95. for (ResolvedType ty : primitives) {
  96. UnresolvedType tx = UnresolvedType.forSignature("[" + ty.getSignature());
  97. // 'void[]' is an illegal type -> skip
  98. if (tx.getSignature().equals("[V"))
  99. continue;
  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 (ResolvedType ty1 : primitives) {
  115. isCoerceableFromTest(aty, ty1, false);
  116. tx = UnresolvedType.forSignature("[" + ty1.getSignature());
  117. // 'void[]' is an illegal type -> skip
  118. if (tx.getSignature().equals("[V"))
  119. continue;
  120. ResolvedType aty1 = getWorld().resolve(tx, true);
  121. assertTrue("Couldnt find type " + tx, !aty1.isMissing());
  122. if (ty.equals(ty1)) {
  123. isCoerceableFromTest(aty, aty1, true);
  124. isAssignableFromTest(aty, aty1, true);
  125. } else {
  126. isCoerceableFromTest(aty, aty1, false);
  127. isAssignableFromTest(aty, aty1, false);
  128. }
  129. }
  130. }
  131. // double dimension arrays
  132. for (ResolvedType ty : primitives) {
  133. UnresolvedType tx = UnresolvedType.forSignature("[[" + ty.getSignature());
  134. // 'void[][]' is an illegal type -> skip
  135. if (tx.getSignature().equals("[[V"))
  136. continue;
  137. ResolvedType aty = world.resolve(tx, true);
  138. assertTrue("Couldnt find type " + tx, !aty.isMissing());
  139. modifiersTest(aty, Modifier.PUBLIC | Modifier.FINAL);
  140. fieldsTest(aty, ResolvedMember.NONE);
  141. methodsTest(aty, ResolvedMember.NONE);
  142. interfaceTest(
  143. aty,
  144. new ResolvedType[]{world.getCoreType(UnresolvedType.CLONEABLE),
  145. world.getCoreType(UnresolvedType.SERIALIZABLE)});
  146. superclassTest(aty, UnresolvedType.OBJECT);
  147. pointcutsTest(aty, ResolvedMember.NONE);
  148. isInterfaceTest(aty, false);
  149. isClassTest(aty, false);
  150. isAspectTest(aty, false);
  151. for (ResolvedType ty1 : primitives) {
  152. isCoerceableFromTest(aty, ty1, false);
  153. tx = UnresolvedType.forSignature("[[" + ty1.getSignature());
  154. // 'void[][]' is an illegal type -> skip
  155. if (tx.getSignature().equals("[[V"))
  156. continue;
  157. ResolvedType aty1 = getWorld().resolve(tx, true);
  158. assertTrue("Couldnt find type " + tx, !aty1.isMissing());
  159. if (ty.equals(ty1)) {
  160. isCoerceableFromTest(aty, aty1, true);
  161. isAssignableFromTest(aty, aty1, true);
  162. } else {
  163. isCoerceableFromTest(aty, aty1, false);
  164. isAssignableFromTest(aty, aty1, false);
  165. }
  166. }
  167. }
  168. }
  169. // ---- tests for parts of ResolvedType objects
  170. protected void modifiersTest(ResolvedType ty, int mods) {
  171. assertEquals(ty + " modifiers:", Modifier.toString(mods), Modifier.toString(ty.getModifiers()));
  172. }
  173. protected void fieldsTest(ResolvedType ty, Member[] x) {
  174. TestUtil.assertSetEquals(ty + " fields:", x, ty.getDeclaredJavaFields());
  175. }
  176. protected void methodsTest(ResolvedType ty, Member[] x) {
  177. TestUtil.assertSetEquals(ty + " methods:", x, ty.getDeclaredJavaMethods());
  178. }
  179. protected void mungersTest(ResolvedType ty, ShadowMunger[] x) {
  180. List<ShadowMunger> l = ty.getDeclaredShadowMungers();
  181. ShadowMunger[] array = (ShadowMunger[]) l.toArray(new ShadowMunger[0]);
  182. TestUtil.assertSetEquals(ty + " mungers:", x, array);
  183. }
  184. protected void interfaceTest(ResolvedType type, ResolvedType[] expectedInterfaces) {
  185. ResolvedType[] interfaces = type.getDeclaredInterfaces();
  186. for (ResolvedType expectedInterface : expectedInterfaces) {
  187. boolean wasMissing = true;
  188. for (ResolvedType anInterface : interfaces) {
  189. if (anInterface.getSignature().equals(expectedInterface.getSignature())) {
  190. wasMissing = false;
  191. }
  192. }
  193. if (wasMissing) {
  194. fail("Expected declared interface " + expectedInterface + " but it wasn't found in "
  195. + Arrays.asList(interfaces));
  196. }
  197. }
  198. }
  199. protected void interfacesTest(ResolvedType ty, ResolvedType[] x) {
  200. TestUtil.assertArrayEquals(ty + " interfaces:", x, ty.getDeclaredInterfaces());
  201. }
  202. protected void superclassTest(ResolvedType ty, UnresolvedType x) {
  203. assertEquals(ty + " superclass:", x, ty.getSuperclass());
  204. }
  205. protected void pointcutsTest(ResolvedType ty, Member[] x) {
  206. TestUtil.assertSetEquals(ty + " pointcuts:", x, ty.getDeclaredPointcuts());
  207. }
  208. protected void isInterfaceTest(ResolvedType ty, boolean x) {
  209. assertEquals(ty + " is interface:", x, ty.isInterface());
  210. }
  211. protected void isAspectTest(ResolvedType ty, boolean x) {
  212. assertEquals(ty + " is aspect:", x, ty.isAspect());
  213. }
  214. protected void isClassTest(ResolvedType ty, boolean x) {
  215. assertEquals(ty + " is class:", x, ty.isClass());
  216. }
  217. protected void isCoerceableFromTest(UnresolvedType ty0, UnresolvedType ty1, boolean x) {
  218. assertEquals(ty0 + " is coerceable from " + ty1, ty0.resolve(world).isCoerceableFrom(ty1.resolve(world)), x);
  219. assertEquals(ty1 + " is coerceable from " + ty0, ty1.resolve(world).isCoerceableFrom(ty0.resolve(world)), x);
  220. }
  221. protected void isAssignableFromTest(UnresolvedType ty0, UnresolvedType ty1, boolean x) {
  222. ResolvedType rty0 = ty0.resolve(world);
  223. ResolvedType rty1 = ty1.resolve(world);
  224. boolean result = rty0.isAssignableFrom(rty1);
  225. assertEquals(ty0 + " is assignable from " + ty1, result, x);
  226. }
  227. // ---- tests for parts of ResolvedMethod objects
  228. protected void modifiersTest(ResolvedMember m, int mods) {
  229. assertEquals(m + " modifiers:", Modifier.toString(mods), Modifier.toString(m.getModifiers()));
  230. }
  231. protected void exceptionsTest(ResolvedMember m, UnresolvedType[] exns) {
  232. TestUtil.assertSetEquals(m + " exceptions:", exns, m.getExceptions());
  233. }
  234. }