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.

AsmDeclarationsTests.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  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. * ******************************************************************/
  10. package org.aspectj.ajde.core.tests.model;
  11. import java.io.File;
  12. import org.aspectj.ajde.core.AjdeCoreTestCase;
  13. import org.aspectj.ajde.core.TestCompilerConfiguration;
  14. import org.aspectj.asm.AsmManager;
  15. import org.aspectj.asm.IHierarchy;
  16. import org.aspectj.asm.IProgramElement;
  17. public class AsmDeclarationsTests extends AjdeCoreTestCase {
  18. private AsmManager manager = null;
  19. private IHierarchy model = null;
  20. private final String[] files = new String[] { "ModelCoverage.java", "pkg" + File.separator + "InPackage.java" };
  21. private TestCompilerConfiguration compilerConfig;
  22. protected void setUp() throws Exception {
  23. super.setUp();
  24. initialiseProject("coverage");
  25. compilerConfig = (TestCompilerConfiguration) getCompiler().getCompilerConfiguration();
  26. compilerConfig.setProjectSourceFiles(getSourceFileList(files));
  27. doBuild();
  28. manager = AsmManager.lastActiveStructureModel;
  29. model = AsmManager.lastActiveStructureModel.getHierarchy();
  30. }
  31. protected void tearDown() throws Exception {
  32. super.tearDown();
  33. compilerConfig = null;
  34. model = null;
  35. }
  36. public void testRoot() {
  37. IProgramElement root = model.getRoot();
  38. assertNotNull(root);
  39. assertEquals("Expected root to be named 'coverage' but found " + root.toLabelString(), root.toLabelString(), "coverage");
  40. }
  41. public void testAspectAccessibility() {
  42. IProgramElement packageAspect = model.findElementForType(null, "AdviceNamingCoverage");
  43. assertNotNull(packageAspect);
  44. assertEquals(IProgramElement.Accessibility.PACKAGE, packageAspect.getAccessibility());
  45. assertEquals("aspect should not have public in it's signature", "aspect AdviceNamingCoverage", packageAspect
  46. .getSourceSignature());
  47. }
  48. public void testStaticModifiers() {
  49. IProgramElement aspect = model.findElementForType(null, "ModifiersCoverage");
  50. assertNotNull(aspect);
  51. IProgramElement staticA = model.findElementForSignature(aspect, IProgramElement.Kind.FIELD, "staticA");
  52. assertTrue(staticA.getModifiers().contains(IProgramElement.Modifiers.STATIC));
  53. IProgramElement finalA = model.findElementForSignature(aspect, IProgramElement.Kind.FIELD, "finalA");
  54. assertTrue(!finalA.getModifiers().contains(IProgramElement.Modifiers.STATIC));
  55. assertTrue(finalA.getModifiers().contains(IProgramElement.Modifiers.FINAL));
  56. }
  57. public void testFileInPackageAndDefaultPackage() {
  58. IProgramElement root = model.getRoot();
  59. assertEquals(root.toLabelString(), "coverage");
  60. IProgramElement pkg = (IProgramElement) root.getChildren().get(1);
  61. assertEquals(pkg.toLabelString(), "pkg");
  62. assertEquals(((IProgramElement) pkg.getChildren().get(0)).toLabelString(), "InPackage.java");
  63. assertEquals(((IProgramElement) ((IProgramElement) root.getChildren().get(0)).getChildren().get(0)).toLabelString(),
  64. "ModelCoverage.java");
  65. }
  66. public void testDeclares() {
  67. IProgramElement node = model.getRoot();
  68. assertNotNull(node);
  69. IProgramElement aspect = model.findElementForType(null, "DeclareCoverage");
  70. assertNotNull(aspect);
  71. String label = "declare error: \"Illegal construct..\"";
  72. IProgramElement decErrNode = model.findElementForSignature(aspect, IProgramElement.Kind.DECLARE_ERROR, "declare error");
  73. assertNotNull(decErrNode);
  74. assertEquals(decErrNode.toLabelString(), label);
  75. String decWarnMessage = "declare warning: \"Illegal call.\"";
  76. IProgramElement decWarnNode = model
  77. .findElementForSignature(aspect, IProgramElement.Kind.DECLARE_WARNING, "declare warning");
  78. assertNotNull(decWarnNode);
  79. assertEquals(decWarnNode.toLabelString(), decWarnMessage);
  80. String decParentsMessage = "declare parents: implements Serializable";
  81. IProgramElement decParentsNode = model.findElementForSignature(aspect, IProgramElement.Kind.DECLARE_PARENTS,
  82. "declare parents");
  83. assertNotNull(decParentsNode);
  84. assertEquals(decParentsNode.toLabelString(), decParentsMessage);
  85. // check the next two relative to this one
  86. int declareIndex = decParentsNode.getParent().getChildren().indexOf(decParentsNode);
  87. String decParentsPtnMessage = "declare parents: extends Observable";
  88. assertEquals(decParentsPtnMessage, ((IProgramElement) aspect.getChildren().get(declareIndex + 1)).toLabelString());
  89. String decParentsTPMessage = "declare parents: extends Observable";
  90. assertEquals(decParentsTPMessage, ((IProgramElement) aspect.getChildren().get(declareIndex + 2)).toLabelString());
  91. String decSoftMessage = "declare soft: SizeException";
  92. IProgramElement decSoftNode = model.findElementForSignature(aspect, IProgramElement.Kind.DECLARE_SOFT, "declare soft");
  93. assertNotNull(decSoftNode);
  94. assertEquals(decSoftNode.toLabelString(), decSoftMessage);
  95. String decPrecMessage = "declare precedence: AdviceCoverage, InterTypeDecCoverage, <type pattern>";
  96. IProgramElement decPrecNode = model.findElementForSignature(aspect, IProgramElement.Kind.DECLARE_PRECEDENCE,
  97. "declare precedence");
  98. assertNotNull(decPrecNode);
  99. assertEquals(decPrecNode.toLabelString(), decPrecMessage);
  100. }
  101. public void testInterTypeMemberDeclares() {
  102. IProgramElement node = model.getRoot();
  103. assertNotNull(node);
  104. IProgramElement aspect = model.findElementForType(null, "InterTypeDecCoverage");
  105. assertNotNull(aspect);
  106. String fieldMsg = "Point.xxx";
  107. IProgramElement fieldNode = model.findElementForLabel(aspect, IProgramElement.Kind.INTER_TYPE_FIELD, fieldMsg);
  108. assertNotNull(fieldNode);
  109. assertEquals(fieldNode.toLabelString(), fieldMsg);
  110. String methodMsg = "Point.check(int,Line)";
  111. IProgramElement methodNode = model.findElementForLabel(aspect, IProgramElement.Kind.INTER_TYPE_METHOD, methodMsg);
  112. assertNotNull(methodNode);
  113. assertEquals(methodNode.toLabelString(), methodMsg);
  114. // TODO: enable
  115. // String constructorMsg = "Point.new(int, int, int)";
  116. // ProgramElementNode constructorNode = model.findNode(aspect, ProgramElementNode.Kind.INTER_TYPE_CONSTRUCTOR,
  117. // constructorMsg);
  118. // assertNotNull(constructorNode);
  119. // assertEquals(constructorNode.toLabelString(), constructorMsg);
  120. }
  121. public void testPointcuts() {
  122. IProgramElement node = model.getRoot();
  123. assertNotNull(node);
  124. IProgramElement aspect = model.findElementForType(null, "AdviceNamingCoverage");
  125. assertNotNull(aspect);
  126. String ptct = "named()";
  127. IProgramElement ptctNode = model.findElementForSignature(aspect, IProgramElement.Kind.POINTCUT, ptct);
  128. assertNotNull(ptctNode);
  129. assertEquals(ptctNode.toLabelString(), ptct);
  130. String params = "namedWithArgs(int,int)";
  131. IProgramElement paramsNode = model.findElementForSignature(aspect, IProgramElement.Kind.POINTCUT, params);
  132. assertNotNull(paramsNode);
  133. assertEquals(paramsNode.toLabelString(), params);
  134. }
  135. public void testAbstract() {
  136. IProgramElement node = model.getRoot();
  137. assertNotNull(node);
  138. IProgramElement aspect = model.findElementForType(null, "AbstractAspect");
  139. assertNotNull(aspect);
  140. String abst = "abPtct()";
  141. IProgramElement abstNode = model.findElementForSignature(aspect, IProgramElement.Kind.POINTCUT, abst);
  142. assertNotNull(abstNode);
  143. assertEquals(abstNode.toLabelString(), abst);
  144. }
  145. public void testAdvice() {
  146. IProgramElement node = model.getRoot();
  147. assertNotNull(node);
  148. IProgramElement aspect = model.findElementForType(null, "AdviceNamingCoverage");
  149. assertNotNull(aspect);
  150. String anon = "before(): <anonymous pointcut>";
  151. IProgramElement anonNode = model.findElementForLabel(aspect, IProgramElement.Kind.ADVICE, anon);
  152. assertNotNull(anonNode);
  153. assertEquals(anonNode.toLabelString(), anon);
  154. String named = "before(): named..";
  155. IProgramElement namedNode = model.findElementForLabel(aspect, IProgramElement.Kind.ADVICE, named);
  156. assertNotNull(namedNode);
  157. assertEquals(namedNode.toLabelString(), named);
  158. String namedWithOneArg = "around(int): namedWithOneArg..";
  159. IProgramElement namedWithOneArgNode = model.findElementForLabel(aspect, IProgramElement.Kind.ADVICE, namedWithOneArg);
  160. assertNotNull(namedWithOneArgNode);
  161. assertEquals(namedWithOneArgNode.toLabelString(), namedWithOneArg);
  162. String afterReturning = "afterReturning(int,int): namedWithArgs..";
  163. IProgramElement afterReturningNode = model.findElementForLabel(aspect, IProgramElement.Kind.ADVICE, afterReturning);
  164. assertNotNull(afterReturningNode);
  165. assertEquals(afterReturningNode.toLabelString(), afterReturning);
  166. String around = "around(int): namedWithOneArg..";
  167. IProgramElement aroundNode = model.findElementForLabel(aspect, IProgramElement.Kind.ADVICE, around);
  168. assertNotNull(aroundNode);
  169. assertEquals(aroundNode.toLabelString(), around);
  170. String compAnon = "before(int): <anonymous pointcut>..";
  171. IProgramElement compAnonNode = model.findElementForLabel(aspect, IProgramElement.Kind.ADVICE, compAnon);
  172. assertNotNull(compAnonNode);
  173. assertEquals(compAnonNode.toLabelString(), compAnon);
  174. String compNamed = "before(int): named()..";
  175. IProgramElement compNamedNode = model.findElementForLabel(aspect, IProgramElement.Kind.ADVICE, compNamed);
  176. assertNotNull(compNamedNode);
  177. assertEquals(compNamedNode.toLabelString(), compNamed);
  178. }
  179. }