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.

CreatingModelForInjarTests.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /********************************************************************
  2. * Copyright (c) 2006 Contributors. All rights reserved.
  3. * This program and the accompanying materials are made available
  4. * under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution and is available at
  6. * http://eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: IBM Corporation - initial API and implementation
  9. * Helen Hawkins - initial version
  10. *******************************************************************/
  11. package org.aspectj.systemtest.ajc152;
  12. import java.io.File;
  13. import java.util.Iterator;
  14. import java.util.List;
  15. import junit.framework.Test;
  16. import org.aspectj.asm.AsmManager;
  17. import org.aspectj.asm.IHierarchy;
  18. import org.aspectj.asm.IProgramElement;
  19. import org.aspectj.asm.IRelationshipMap;
  20. import org.aspectj.testing.XMLBasedAjcTestCase;
  21. //import org.aspectj.weaver.World;
  22. public class CreatingModelForInjarTests extends org.aspectj.testing.XMLBasedAjcTestCase {
  23. public void testAdviceAndNamedPCD() {
  24. runTest("advice and deow");
  25. // expect:
  26. // - pkg {package}
  27. // - A.aj (binary) {java source file}
  28. // - import declarations {import reference}
  29. // - A {aspect}
  30. // - p {pointcut}
  31. // - before {advice}
  32. IProgramElement pkgNode = getPkgNode();
  33. IProgramElement srcFile = checkChild(pkgNode,IProgramElement.Kind.FILE_JAVA,"A.aj (binary)",1);
  34. checkChild(srcFile,IProgramElement.Kind.IMPORT_REFERENCE,"import declarations",-1);
  35. IProgramElement aspectNode = checkChild(srcFile,IProgramElement.Kind.ASPECT,"A",-1);
  36. checkChild(aspectNode,IProgramElement.Kind.POINTCUT,"p",5);
  37. checkChild(aspectNode,IProgramElement.Kind.ADVICE,"before",7);
  38. }
  39. public void testDeclareWarning() {
  40. runTest("advice and deow");
  41. // expect:
  42. // - pkg {package}
  43. // - Deow.aj (binary) {java source file}
  44. // - import declarations {import reference}
  45. // - Deow {aspect}
  46. // - declare warning {declare warning}
  47. IHierarchy top = AsmManager.getDefault().getHierarchy();
  48. IProgramElement dwNode = top.findElementForLabel(top.getRoot(),
  49. IProgramElement.Kind.DECLARE_WARNING,
  50. "declare warning: \"There should be n..\"");
  51. assertNotNull("Couldn't find 'declare warning: \"There should be n..\"' " +
  52. "element in the tree",dwNode);
  53. assertEquals("expected 'declare warning: \"There should be n..\"'" +
  54. " to be on line 5 but was on " + dwNode.getSourceLocation().getLine(),
  55. 5, dwNode.getSourceLocation().getLine());
  56. }
  57. public void testNumberOfPackageNodes() {
  58. runTest("advice and deow");
  59. // check that the 'pkg' package node has not been added twice
  60. IProgramElement root = AsmManager.getDefault().getHierarchy().getRoot();
  61. List l = root.getChildren();
  62. int numberOfPkgs = 0;
  63. for (Iterator iter = l.iterator(); iter.hasNext();) {
  64. IProgramElement element = (IProgramElement) iter.next();
  65. if (element.getKind().equals(IProgramElement.Kind.PACKAGE)
  66. && element.getName().equals("pkg")) {
  67. numberOfPkgs++;
  68. }
  69. }
  70. assertEquals("expected one package called 'pkg' but found " + numberOfPkgs,1,numberOfPkgs);
  71. }
  72. public void testAdviceInRelMap() {
  73. runTest("advice and deow");
  74. IHierarchy top = AsmManager.getDefault().getHierarchy();
  75. IProgramElement adviceNode = top.findElementForLabel(top.getRoot(), IProgramElement.Kind.ADVICE,"before(): p..");
  76. IRelationshipMap relMap = AsmManager.getDefault().getRelationshipMap();
  77. List adviceRels = relMap.get(adviceNode);
  78. assertFalse("expected before advice to have relationships but did not",adviceRels.isEmpty());
  79. }
  80. public void testDeclareWarningInRelMap() {
  81. runTest("advice and deow");
  82. IHierarchy top = AsmManager.getDefault().getHierarchy();
  83. IProgramElement dwNode = top.findElementForLabel(top.getRoot(),
  84. IProgramElement.Kind.DECLARE_WARNING,
  85. "declare warning: \"There should be n..\"");
  86. IRelationshipMap relMap = AsmManager.getDefault().getRelationshipMap();
  87. List dwRels = relMap.get(dwNode);
  88. assertFalse("expected declare warning to have relationships but did not",dwRels.isEmpty());
  89. }
  90. public void testAdviceLabelsCorrect() {
  91. runTest("ensure advice label is correct");
  92. IHierarchy top = AsmManager.getDefault().getHierarchy();
  93. IProgramElement node = top.findElementForLabel(top.getRoot(),
  94. IProgramElement.Kind.ADVICE, "before(): execM1()..");
  95. assertNotNull("expected to find ipe with label 'before(): execM1()..'" +
  96. " but didn't", node);
  97. node = top.findElementForLabel(top.getRoot(),
  98. IProgramElement.Kind.ADVICE, "before(): execM2()..");
  99. assertNotNull("expected to find ipe with label 'before(): execM2()..'" +
  100. " but didn't", node);
  101. node = top.findElementForLabel(top.getRoot(),
  102. IProgramElement.Kind.ADVICE, "before(): <anonymous pointcut>");
  103. assertNotNull("expected to find ipe with label 'before(): <anonymous pointcut>'" +
  104. " but didn't", node);
  105. }
  106. // ensure that filled in hierarchy only has one entry for
  107. // aspect
  108. public void testOnlyOneAspectEntry() {
  109. runTest("ensure advice label is correct");
  110. IProgramElement pkgNode = getPkgNode();
  111. assertEquals("expected one child node but found " +
  112. pkgNode.getChildren().size(), 1, pkgNode.getChildren().size());
  113. }
  114. public void testOnlyOneAspectEntry_inDefaultPackage() {
  115. runTest("aspect in default package");
  116. // expect there to be two children - 'pack' and
  117. // 'AspectInDefaultPackage.aj (binary)'
  118. IProgramElement defaultPkg = AsmManager.getDefault().getHierarchy().getRoot();
  119. assertEquals("expected two child node but found " +
  120. defaultPkg.getChildren().size(), 2, defaultPkg.getChildren().size());
  121. }
  122. // --------------------- Helper methods ---------------------
  123. private IProgramElement getPkgNode() {
  124. IHierarchy top = AsmManager.getDefault().getHierarchy();
  125. IProgramElement pkgNode = top.findElementForLabel(top.getRoot(),
  126. IProgramElement.Kind.PACKAGE,"pkg");
  127. assertNotNull("Couldn't find 'pkg' element in the tree",pkgNode);
  128. return pkgNode;
  129. }
  130. private IProgramElement checkChild(IProgramElement parent,
  131. IProgramElement.Kind childKind,
  132. String childName,
  133. int childLineNumbr) {
  134. List children = parent.getChildren();
  135. boolean foundChild = false;
  136. for (Iterator iter = children.iterator(); iter.hasNext();) {
  137. IProgramElement element = (IProgramElement) iter.next();
  138. if (element.getKind().equals(childKind)
  139. && element.getName().equals(childName) ) {
  140. foundChild = true;
  141. if (childLineNumbr != -1) {
  142. assertEquals("expected " + childKind.toString() + childName +
  143. " to be on line " + childLineNumbr + " but was on " +
  144. element.getSourceLocation().getLine(),
  145. childLineNumbr, element.getSourceLocation().getLine());
  146. }
  147. return element;
  148. }
  149. }
  150. assertTrue("expected " + parent.getName() + " to have child " + childName
  151. + " but it did not", foundChild);
  152. return null;
  153. }
  154. protected void setUp() throws Exception {
  155. super.setUp();
  156. // World.createInjarHierarchy = true;
  157. }
  158. protected void tearDown() throws Exception {
  159. super.tearDown();
  160. // World.createInjarHierarchy = false;
  161. }
  162. // ///////////////////////////////////////
  163. public static Test suite() {
  164. return XMLBasedAjcTestCase.loadSuite(CreatingModelForInjarTests.class);
  165. }
  166. protected File getSpecFile() {
  167. return new File("../tests/src/org/aspectj/systemtest/ajc152/injar.xml");
  168. }
  169. }