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 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 StructureModelTest 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. compilerConfig.setNonStandardOptions("-Xset:minimalModel=false");
  39. doBuild();
  40. manager = AsmManager.lastActiveStructureModel;
  41. }
  42. protected void tearDown() throws Exception {
  43. super.tearDown();
  44. compilerConfig = null;
  45. manager = null;
  46. }
  47. public void testRootForSourceFile() throws IOException {
  48. File testFile = openFile("figures" + File.separator + "Figure.java");
  49. IProgramElement node = manager.getHierarchy().findElementForSourceFile(testFile.getAbsolutePath());
  50. assertTrue("find result", node != null);
  51. String child = ((IProgramElement) node.getChildren().get(2)).getName();
  52. assertTrue("expected Figure got child " + child, child.equals("Figure"));
  53. }
  54. public void testPointcutName() throws IOException {
  55. File testFile = openFile("figures" + File.separator + "Main.java");
  56. IProgramElement node = manager.getHierarchy().findElementForSourceFile(testFile.getAbsolutePath());
  57. assertTrue("find result", node != null);
  58. IProgramElement pNode = (IProgramElement) (node).getChildren().get(3);
  59. assertEquals(IProgramElement.Kind.ASPECT, pNode.getKind());
  60. IProgramElement pointcut = (IProgramElement) pNode.getChildren().get(0);
  61. assertTrue("kind", pointcut.getKind().equals(IProgramElement.Kind.POINTCUT));
  62. assertTrue("found node: " + pointcut.getName(), pointcut.toLabelString().equals("testptct()"));
  63. }
  64. public void testFileNodeFind() throws IOException {
  65. File testFile = openFile("figures" + File.separator + "Main.java");
  66. // System.err.println(((IProgramElement)((IProgramElement)Ajde.getDefault().getStructureModelManager().getHierarchy().getRoot().getChildren().get(0)).getChildren().get(3)).getSourceLocation().getSourceFile().getAbsolutePath());
  67. // System.err.println(testFile.getAbsolutePath());
  68. IProgramElement node = manager.getHierarchy().findElementForSourceLine(testFile.getAbsolutePath(), 1);
  69. assertTrue("find result", node != null);
  70. assertEquals("find result has children", 4, node.getChildren().size()); // package, import and 2 types
  71. assertTrue("found node: " + node.getName(), node.getKind().equals(IProgramElement.Kind.FILE_JAVA));
  72. }
  73. /**
  74. * @todo add negative test to make sure things that aren't runnable aren't annotated
  75. */
  76. public void testMainClassNodeInfo() throws IOException {
  77. IHierarchy model = manager.getHierarchy();
  78. assertTrue("model exists", model != null);
  79. assertTrue("root exists", model.getRoot() != null);
  80. File testFile = openFile("figures" + File.separator + "Main.java");
  81. IProgramElement node = model.findElementForSourceLine(testFile.getAbsolutePath(), 11);
  82. assertTrue("find result", node != null);
  83. IProgramElement pNode = node.getParent();
  84. if (null == pNode) {
  85. assertTrue("null parent of " + node, false);
  86. }
  87. assertTrue("found node: " + pNode.getName(), pNode.isRunnable());
  88. }
  89. /**
  90. * Integrity could be checked somewhere in the API.
  91. */
  92. public void testModelIntegrity() {
  93. IProgramElement modelRoot = manager.getHierarchy().getRoot();
  94. assertTrue("root exists", modelRoot != null);
  95. try {
  96. testModelIntegrityHelper(modelRoot);
  97. } catch (Exception e) {
  98. assertTrue(e.toString(), false);
  99. }
  100. }
  101. private void testModelIntegrityHelper(IProgramElement node) throws Exception {
  102. for (IProgramElement child : node.getChildren()) {
  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 (IProgramElement iProgramElement : node.getChildren()) {
  116. if (iProgramElement == null)
  117. throw new NullPointerException("null child on node: " + node.getName());
  118. }
  119. }
  120. };
  121. manager.getHierarchy().getRoot().walk(walker);
  122. }
  123. }