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.

StructureModelTests.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. * Contributors:
  10. * Xerox/PARC initial implementation
  11. * AMC 21.01.2003 fixed for new source location in eclipse.org
  12. * Helen Hawkins Converted to new interface (bug 148190)
  13. * ******************************************************************/
  14. package org.aspectj.ajde.core.tests.model;
  15. import java.io.File;
  16. import java.io.IOException;
  17. import java.util.Iterator;
  18. import org.aspectj.ajde.core.AjdeCoreTestCase;
  19. import org.aspectj.ajde.core.TestCompilerConfiguration;
  20. import org.aspectj.asm.AsmManager;
  21. import org.aspectj.asm.HierarchyWalker;
  22. import org.aspectj.asm.IHierarchy;
  23. import org.aspectj.asm.IProgramElement;
  24. public class StructureModelTests extends AjdeCoreTestCase {
  25. private AsmManager manager = null;
  26. private final String[] files = new String[] { "figures" + File.separator + "Debug.java",
  27. "figures" + File.separator + "Figure.java", "figures" + File.separator + "FigureElement.java",
  28. "figures" + File.separator + "Main.java", "figures" + File.separator + "composites" + File.separator + "Line.java",
  29. "figures" + File.separator + "composites" + File.separator + "Square.java",
  30. "figures" + File.separator + "primitives" + File.separator + "planar" + File.separator + "Point.java",
  31. "figures" + File.separator + "primitives" + File.separator + "solid" + File.separator + "SolidPoint.java" };
  32. private TestCompilerConfiguration compilerConfig;
  33. protected void setUp() throws Exception {
  34. super.setUp();
  35. initialiseProject("figures-coverage");
  36. compilerConfig = (TestCompilerConfiguration) getCompiler().getCompilerConfiguration();
  37. compilerConfig.setProjectSourceFiles(getSourceFileList(files));
  38. doBuild();
  39. manager = AsmManager.lastActiveStructureModel;
  40. }
  41. protected void tearDown() throws Exception {
  42. super.tearDown();
  43. compilerConfig = null;
  44. manager = null;
  45. }
  46. public void testRootForSourceFile() throws IOException {
  47. File testFile = openFile("figures" + File.separator + "Figure.java");
  48. IProgramElement node = manager.getHierarchy().findElementForSourceFile(testFile.getAbsolutePath());
  49. assertTrue("find result", node != null);
  50. String child = ((IProgramElement) node.getChildren().get(2)).getName();
  51. assertTrue("expected Figure got child " + child, child.equals("Figure"));
  52. }
  53. public void testPointcutName() throws IOException {
  54. File testFile = openFile("figures" + File.separator + "Main.java");
  55. IProgramElement node = manager.getHierarchy().findElementForSourceFile(testFile.getAbsolutePath());
  56. assertTrue("find result", node != null);
  57. IProgramElement pNode = (IProgramElement) (node).getChildren().get(3);
  58. assertEquals(IProgramElement.Kind.ASPECT,pNode.getKind());
  59. IProgramElement pointcut = (IProgramElement) pNode.getChildren().get(0);
  60. assertTrue("kind", pointcut.getKind().equals(IProgramElement.Kind.POINTCUT));
  61. assertTrue("found node: " + pointcut.getName(), pointcut.toLabelString().equals("testptct()"));
  62. }
  63. public void testFileNodeFind() throws IOException {
  64. File testFile = openFile("figures" + File.separator + "Main.java");
  65. // System.err.println(((IProgramElement)((IProgramElement)Ajde.getDefault().getStructureModelManager().getHierarchy().getRoot().getChildren().get(0)).getChildren().get(3)).getSourceLocation().getSourceFile().getAbsolutePath());
  66. // System.err.println(testFile.getAbsolutePath());
  67. IProgramElement node = manager.getHierarchy().findElementForSourceLine(testFile.getAbsolutePath(), 1);
  68. assertTrue("find result", node != null);
  69. assertEquals("find result has children", 4, node.getChildren().size()); // package, import and 2 types
  70. assertTrue("found node: " + node.getName(), node.getKind().equals(IProgramElement.Kind.FILE_JAVA));
  71. }
  72. /**
  73. * @todo add negative test to make sure things that aren't runnable aren't annotated
  74. */
  75. public void testMainClassNodeInfo() throws IOException {
  76. IHierarchy model = manager.getHierarchy();
  77. assertTrue("model exists", model != null);
  78. assertTrue("root exists", model.getRoot() != null);
  79. File testFile = openFile("figures" + File.separator + "Main.java");
  80. IProgramElement node = model.findElementForSourceLine(testFile.getAbsolutePath(), 11);
  81. assertTrue("find result", node != null);
  82. IProgramElement pNode = node.getParent();
  83. if (null == pNode) {
  84. assertTrue("null parent of " + node, false);
  85. }
  86. assertTrue("found node: " + pNode.getName(), pNode.isRunnable());
  87. }
  88. /**
  89. * Integrity could be checked somewhere in the API.
  90. */
  91. public void testModelIntegrity() {
  92. IProgramElement modelRoot = manager.getHierarchy().getRoot();
  93. assertTrue("root exists", modelRoot != null);
  94. try {
  95. testModelIntegrityHelper(modelRoot);
  96. } catch (Exception e) {
  97. assertTrue(e.toString(), false);
  98. }
  99. }
  100. private void testModelIntegrityHelper(IProgramElement node) throws Exception {
  101. for (Iterator it = node.getChildren().iterator(); it.hasNext();) {
  102. IProgramElement child = (IProgramElement) it.next();
  103. if (node == child.getParent()) {
  104. testModelIntegrityHelper(child);
  105. } else {
  106. throw new Exception("parent-child check failed for child: " + child.toString());
  107. }
  108. }
  109. }
  110. public void testNoChildIsNull() {
  111. HierarchyWalker walker = new HierarchyWalker() {
  112. public void preProcess(IProgramElement node) {
  113. if (node.getChildren() == null)
  114. return;
  115. for (Iterator it = node.getChildren().iterator(); it.hasNext();) {
  116. if (it.next() == null)
  117. throw new NullPointerException("null child on node: " + node.getName());
  118. }
  119. }
  120. };
  121. manager.getHierarchy().getRoot().walk(walker);
  122. }
  123. }