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.

AsmRelationshipsTests.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 java.util.Iterator;
  13. import java.util.List;
  14. import org.aspectj.ajde.core.AjdeCoreTestCase;
  15. import org.aspectj.ajde.core.TestCompilerConfiguration;
  16. import org.aspectj.asm.AsmManager;
  17. import org.aspectj.asm.IProgramElement;
  18. import org.aspectj.asm.IRelationship;
  19. public class AsmRelationshipsTests extends AjdeCoreTestCase {
  20. private AsmManager manager = null;
  21. private final String[] files = new String[] { "ModelCoverage.java", "pkg" + File.separator + "InPackage.java" };
  22. private TestCompilerConfiguration compilerConfig;
  23. protected void setUp() throws Exception {
  24. super.setUp();
  25. initialiseProject("coverage");
  26. compilerConfig = (TestCompilerConfiguration) getCompiler().getCompilerConfiguration();
  27. compilerConfig.setProjectSourceFiles(getSourceFileList(files));
  28. doBuild();
  29. manager = AsmManager.lastActiveStructureModel;
  30. }
  31. protected void tearDown() throws Exception {
  32. super.tearDown();
  33. compilerConfig = null;
  34. manager = null;
  35. }
  36. // // see pr148027
  37. // public void testUsesPointcut() {
  38. // if (!AsmHierarchyBuilder.shouldAddUsesPointcut) return;
  39. //
  40. // IProgramElement ptUsage = AsmManager.getDefault().getHierarchy().findElementForType(null, "PointcutUsage");
  41. // assertNotNull(ptUsage);
  42. // IProgramElement pts = AsmManager.getDefault().getHierarchy().findElementForType(null, "Pointcuts");
  43. // assertNotNull(pts);
  44. //
  45. // IProgramElement pUsesA = manager.getHierarchy().findElementForLabel(
  46. // ptUsage,
  47. // IProgramElement.Kind.POINTCUT,
  48. // "usesA()"/*Point"*/);
  49. // assertNotNull(pUsesA);
  50. //
  51. // IProgramElement ptsA = manager.getHierarchy().findElementForLabel(
  52. // pts,
  53. // IProgramElement.Kind.POINTCUT,
  54. // "a()"/*Point"*/);
  55. // assertNotNull(ptsA);
  56. //
  57. // assertTrue(AsmManager.getDefault().getRelationshipMap().get(pUsesA).size()>0);
  58. // assertTrue(AsmManager.getDefault().getRelationshipMap().get(ptsA).size()>0);
  59. // }
  60. public void testDeclareParents() {
  61. IProgramElement aspect = manager.getHierarchy().findElementForType(null, "DeclareCoverage");
  62. IProgramElement dp = manager.getHierarchy().findElementForLabel(aspect, IProgramElement.Kind.DECLARE_PARENTS,
  63. "declare parents: implements Serializable"/* Point" */);
  64. assertNotNull(dp);
  65. /* List relations = */manager.getRelationshipMap().get(dp);
  66. List rels = manager.getRelationshipMap().get(dp);
  67. assertTrue(rels.size() > 0);
  68. // assertTrue(rel.getTargets().size() > 0);
  69. //
  70. // checkDeclareMapping("DeclareCoverage", "Point", ,
  71. // "Point", "matched by", "matches declare",
  72. // IProgramElement.Kind.DECLARE_PARENTS);
  73. }
  74. public void testDeclareWarningAndError() {
  75. checkDeclareMapping("DeclareCoverage", "Point", "declare warning: \"Illegal call.\"", "method-call(void Point.setX(int))",
  76. "matched by", "matches declare", IProgramElement.Kind.DECLARE_WARNING);
  77. }
  78. public void testInterTypeDeclarations() {
  79. checkInterTypeMapping("InterTypeDecCoverage", "Point", "Point.xxx", "Point", "declared on", "aspect declarations",
  80. IProgramElement.Kind.INTER_TYPE_FIELD);
  81. checkInterTypeMapping("InterTypeDecCoverage", "Point", "Point.check(int,Line)", "Point", "declared on",
  82. "aspect declarations", IProgramElement.Kind.INTER_TYPE_METHOD);
  83. }
  84. public void testAdvice() {
  85. checkMapping("AdvisesRelationshipCoverage", "Point", "before(): methodExecutionP..", "setX(int)", "advises", "advised by");
  86. checkUniDirectionalMapping("AdvisesRelationshipCoverage", "Point", "before(): getP..", "field-get(int Point.x)", "advises");
  87. checkUniDirectionalMapping("AdvisesRelationshipCoverage", "Point", "before(): setP..", "field-set(int Point.x)", "advises");
  88. }
  89. private void checkDeclareMapping(String fromType, String toType, String from, String to, String forwardRelName,
  90. String backRelName, IProgramElement.Kind kind) {
  91. IProgramElement aspect = manager.getHierarchy().findElementForType(null, fromType);
  92. assertNotNull(aspect);
  93. String beforeExec = from;
  94. IProgramElement beforeExecNode = manager.getHierarchy().findElementForLabel(aspect, kind, beforeExec);
  95. assertNotNull(beforeExecNode);
  96. IRelationship rel = manager.getRelationshipMap().get(beforeExecNode, IRelationship.Kind.DECLARE, forwardRelName);
  97. assertTrue(rel.getTargets().size() > 0);
  98. String handle = (String) rel.getTargets().get(0);
  99. assertEquals(manager.getHierarchy().findElementForHandle(handle).toString(), to);
  100. IProgramElement clazz = manager.getHierarchy().findElementForType(null, toType);
  101. assertNotNull(clazz);
  102. String set = to;
  103. IProgramElement setNode = manager.getHierarchy().findElementForLabel(clazz, IProgramElement.Kind.CODE, set);
  104. assertNotNull(setNode);
  105. IRelationship rel2 = manager.getRelationshipMap().get(setNode, IRelationship.Kind.DECLARE, backRelName);
  106. String handle2 = (String) rel2.getTargets().get(0);
  107. assertEquals(manager.getHierarchy().findElementForHandle(handle2).toString(), from);
  108. }
  109. private void checkUniDirectionalMapping(String fromType, String toType, String from, String to, String relName) {
  110. IProgramElement aspect = manager.getHierarchy().findElementForType(null, fromType);
  111. assertNotNull(aspect);
  112. String beforeExec = from;
  113. IProgramElement beforeExecNode = manager.getHierarchy()
  114. .findElementForLabel(aspect, IProgramElement.Kind.ADVICE, beforeExec);
  115. assertNotNull(beforeExecNode);
  116. IRelationship rel = manager.getRelationshipMap().get(beforeExecNode, IRelationship.Kind.ADVICE, relName);
  117. for (Iterator<String> it = rel.getTargets().iterator(); it.hasNext();) {
  118. String currHandle = (String) it.next();
  119. if (manager.getHierarchy().findElementForHandle(currHandle).toLabelString().equals(to))
  120. return;
  121. }
  122. fail(); // didn't find it
  123. }
  124. private void checkMapping(String fromType, String toType, String from, String to, String forwardRelName, String backRelName) {
  125. IProgramElement aspect = manager.getHierarchy().findElementForType(null, fromType);
  126. assertNotNull(aspect);
  127. String beforeExec = from;
  128. IProgramElement beforeExecNode = manager.getHierarchy()
  129. .findElementForLabel(aspect, IProgramElement.Kind.ADVICE, beforeExec);
  130. assertNotNull(beforeExecNode);
  131. IRelationship rel = manager.getRelationshipMap().get(beforeExecNode, IRelationship.Kind.ADVICE, forwardRelName);
  132. String handle = (String) rel.getTargets().get(0);
  133. assertEquals(manager.getHierarchy().findElementForHandle(handle).toString(), to);
  134. IProgramElement clazz = manager.getHierarchy().findElementForType(null, toType);
  135. assertNotNull(clazz);
  136. String set = to;
  137. IProgramElement setNode = manager.getHierarchy().findElementForLabel(clazz, IProgramElement.Kind.METHOD, set);
  138. assertNotNull(setNode);
  139. IRelationship rel2 = manager.getRelationshipMap().get(setNode, IRelationship.Kind.ADVICE, backRelName);
  140. String handle2 = (String) rel2.getTargets().get(0);
  141. assertEquals(manager.getHierarchy().findElementForHandle(handle2).toString(), from);
  142. }
  143. private void checkInterTypeMapping(String fromType, String toType, String from, String to, String forwardRelName,
  144. String backRelName, IProgramElement.Kind declareKind) {
  145. IProgramElement aspect = manager.getHierarchy().findElementForType(null, fromType);
  146. assertNotNull(aspect);
  147. String beforeExec = from;
  148. IProgramElement fromNode = manager.getHierarchy().findElementForLabel(aspect, declareKind, beforeExec);
  149. assertNotNull(fromNode);
  150. IRelationship rel = manager.getRelationshipMap().get(fromNode, IRelationship.Kind.DECLARE_INTER_TYPE, forwardRelName);
  151. String handle = (String) rel.getTargets().get(0);
  152. assertEquals(manager.getHierarchy().findElementForHandle(handle).toString(), to);
  153. IProgramElement clazz = manager.getHierarchy().findElementForType(null, toType);
  154. assertNotNull(clazz);
  155. // String set = to;
  156. IRelationship rel2 = manager.getRelationshipMap().get(clazz, IRelationship.Kind.DECLARE_INTER_TYPE, backRelName);
  157. // String handle2 = (String)rel2.getTargets().get(0);
  158. for (Iterator<String> it = rel2.getTargets().iterator(); it.hasNext();) {
  159. String currHandle = (String) it.next();
  160. if (manager.getHierarchy().findElementForHandle(currHandle).toLabelString().equals(from))
  161. return;
  162. }
  163. fail(); // didn't find it
  164. }
  165. }