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.

StructureModelTest.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-v10.html
  8. *
  9. * Contributors:
  10. * Xerox/PARC initial implementation
  11. * AMC 21.01.2003 fixed for new source location in eclipse.org
  12. * ******************************************************************/
  13. package org.aspectj.ajde;
  14. import java.io.*;
  15. import java.util.Iterator;
  16. import junit.framework.TestSuite;
  17. import org.aspectj.asm.*;
  18. /**
  19. * @author Mik Kersten
  20. */
  21. public class StructureModelTest extends AjdeTestCase {
  22. private final String CONFIG_FILE_PATH = "../examples/figures-coverage/all.lst";
  23. public StructureModelTest(String name) {
  24. super(name);
  25. }
  26. public static void main(String[] args) {
  27. junit.swingui.TestRunner.run(StructureModelTest.class);
  28. }
  29. public static TestSuite suite() {
  30. TestSuite result = new TestSuite();
  31. result.addTestSuite(StructureModelTest.class);
  32. return result;
  33. }
  34. // XXX this should work
  35. // public void testFieldInitializerCorrespondence() throws IOException {
  36. // File testFile = createFile("testdata/examples/figures-coverage/figures/Figure.java");
  37. // IProgramElement node = Ajde.getDefault().getStructureModelManager().getStructureModel().findNodeForSourceLine(
  38. // testFile.getCanonicalPath(), 28);
  39. // assertTrue("find result", node != null) ;
  40. // ProgramElementNode pNode = (ProgramElementNode)node;
  41. // ProgramElementNode foundNode = null;
  42. // final List list = pNode.getRelations();
  43. // assertNotNull("pNode.getRelations()", list);
  44. // for (Iterator it = list.iterator(); it.hasNext(); ) {
  45. // RelationNode relation = (RelationNode)it.next();
  46. //
  47. // if (relation.getRelation().equals(AdviceAssociation.FIELD_ACCESS_RELATION)) {
  48. // for (Iterator it2 = relation.getChildren().iterator(); it2.hasNext(); ) {
  49. // LinkNode linkNode = (LinkNode)it2.next();
  50. // if (linkNode.getProgramElementNode().getName().equals("this.currVal = 0")) {
  51. // foundNode = linkNode.getProgramElementNode();
  52. // }
  53. // }
  54. // }
  55. // }
  56. //
  57. // assertTrue("find associated node", foundNode != null) ;
  58. //
  59. // File pointFile = createFile("testdata/examples/figures-coverage/figures/primitives/planar/Point.java");
  60. // IProgramElement fieldNode = Ajde.getDefault().getStructureModelManager().getStructureModel().findNodeForSourceLine(
  61. // pointFile.getCanonicalPath(), 12);
  62. // assertTrue("find result", fieldNode != null);
  63. //
  64. // assertTrue("matches", foundNode.getParent() == fieldNode.getParent());
  65. // }
  66. public void testRootForSourceFile() throws IOException {
  67. File testFile = openFile("figures-coverage/figures/Figure.java");
  68. IProgramElement node = Ajde.getDefault().getStructureModelManager().getHierarchy().findElementForSourceFile(
  69. testFile.getAbsolutePath());
  70. assertTrue("find result", node != null) ;
  71. IProgramElement pNode = (IProgramElement)node;
  72. String child = ((IProgramElement)pNode.getChildren().get(1)).getName();
  73. assertTrue("expected Figure got child " + child, child.equals("Figure"));
  74. }
  75. public void testPointcutName() throws IOException {
  76. File testFile = openFile("figures-coverage/figures/Main.java");
  77. IProgramElement node = Ajde.getDefault().getStructureModelManager().getHierarchy().findElementForSourceFile(
  78. testFile.getAbsolutePath());
  79. assertTrue("find result", node != null) ;
  80. IProgramElement pNode = (IProgramElement)((IProgramElement)node).getChildren().get(2);
  81. IProgramElement pointcut = (IProgramElement)pNode.getChildren().get(0);
  82. assertTrue("kind", pointcut.getKind().equals(IProgramElement.Kind.POINTCUT));
  83. assertTrue("found node: " + pointcut.getName(), pointcut.toLabelString().equals("testptct()"));
  84. }
  85. public void testFileNodeFind() throws IOException {
  86. File testFile = openFile("figures-coverage/figures/Main.java");
  87. // System.err.println(((IProgramElement)((IProgramElement)Ajde.getDefault().getStructureModelManager().getHierarchy().getRoot().getChildren().get(0)).getChildren().get(3)).getSourceLocation().getSourceFile().getAbsolutePath());
  88. // System.err.println(testFile.getAbsolutePath());
  89. IProgramElement node = Ajde.getDefault().getStructureModelManager().getHierarchy().findElementForSourceLine(
  90. testFile.getAbsolutePath(), 1);
  91. assertTrue("find result", node != null) ;
  92. assertEquals("find result has children", 3, node.getChildren().size()) ;
  93. IProgramElement pNode = (IProgramElement)node;
  94. assertTrue("found node: " + pNode.getName(), pNode.getKind().equals(IProgramElement.Kind.FILE_JAVA));
  95. }
  96. /**
  97. * @todo add negative test to make sure things that aren't runnable aren't annotated
  98. */
  99. public void testMainClassNodeInfo() throws IOException {
  100. IHierarchy model = Ajde.getDefault().getStructureModelManager().getHierarchy();
  101. assertTrue("model exists", model != null);
  102. assertTrue("root exists", model.getRoot() != null);
  103. File testFile = openFile("figures-coverage/figures/Main.java");
  104. IProgramElement node = model.findElementForSourceLine(testFile.getAbsolutePath(), 11);
  105. assertTrue("find result", node != null);
  106. IProgramElement pNode = (IProgramElement)((IProgramElement)node).getParent();
  107. if (null == pNode) {
  108. assertTrue("null parent of " + node, false);
  109. }
  110. assertTrue("found node: " + pNode.getName(), pNode.isRunnable());
  111. }
  112. /**
  113. * Integrity could be checked somewhere in the API.
  114. */
  115. public void testModelIntegrity() {
  116. IProgramElement modelRoot = Ajde.getDefault().getStructureModelManager().getHierarchy().getRoot();
  117. assertTrue("root exists", modelRoot != null);
  118. try {
  119. testModelIntegrityHelper(modelRoot);
  120. } catch (Exception e) {
  121. assertTrue(e.toString(), false);
  122. }
  123. }
  124. private void testModelIntegrityHelper(IProgramElement node) throws Exception {
  125. for (Iterator it = node.getChildren().iterator(); it.hasNext(); ) {
  126. IProgramElement child = (IProgramElement)it.next();
  127. if (node == child.getParent()) {
  128. testModelIntegrityHelper(child);
  129. } else {
  130. throw new Exception("parent-child check failed for child: " + child.toString());
  131. }
  132. }
  133. }
  134. public void testNoChildIsNull() {
  135. HierarchyWalker walker = new HierarchyWalker() {
  136. public void preProcess(IProgramElement node) {
  137. if (node.getChildren() == null) return;
  138. for (Iterator it = node.getChildren().iterator(); it.hasNext(); ) {
  139. if (it.next() == null) throw new NullPointerException("null child on node: " + node.getName());
  140. }
  141. }
  142. };
  143. Ajde.getDefault().getStructureModelManager().getHierarchy().getRoot().walk(walker);
  144. }
  145. protected void setUp() throws Exception {
  146. super.setUp("examples");
  147. doSynchronousBuild(CONFIG_FILE_PATH);
  148. }
  149. protected void tearDown() throws Exception {
  150. super.tearDown();
  151. }
  152. }