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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  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 org.aspectj.ajde.core.AjdeCoreTestCase;
  18. import org.aspectj.ajde.core.TestCompilerConfiguration;
  19. import org.aspectj.asm.AsmManager;
  20. import org.aspectj.asm.HierarchyWalker;
  21. import org.aspectj.asm.IHierarchy;
  22. import org.aspectj.asm.IProgramElement;
  23. public class StructureModelTest extends AjdeCoreTestCase {
  24. private AsmManager manager = null;
  25. private final String[] files = new String[] { "figures" + File.separator + "Debug.java",
  26. "figures" + File.separator + "Figure.java", "figures" + File.separator + "FigureElement.java",
  27. "figures" + File.separator + "Main.java", "figures" + File.separator + "composites" + File.separator + "Line.java",
  28. "figures" + File.separator + "composites" + File.separator + "Square.java",
  29. "figures" + File.separator + "primitives" + File.separator + "planar" + File.separator + "Point.java",
  30. "figures" + File.separator + "primitives" + File.separator + "solid" + File.separator + "SolidPoint.java" };
  31. private TestCompilerConfiguration compilerConfig;
  32. protected void setUp() throws Exception {
  33. super.setUp();
  34. initialiseProject("figures-coverage");
  35. compilerConfig = (TestCompilerConfiguration) getCompiler().getCompilerConfiguration();
  36. compilerConfig.setProjectSourceFiles(getSourceFileList(files));
  37. compilerConfig.setNonStandardOptions("-Xset:minimalModel=false");
  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 = 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 = (node).getChildren().get(3);
  58. assertEquals(IProgramElement.Kind.ASPECT, pNode.getKind());
  59. IProgramElement pointcut = 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 (IProgramElement child : node.getChildren()) {
  102. if (node == child.getParent()) {
  103. testModelIntegrityHelper(child);
  104. } else {
  105. throw new Exception("parent-child check failed for child: " + child.toString());
  106. }
  107. }
  108. }
  109. public void testNoChildIsNull() {
  110. HierarchyWalker walker = new HierarchyWalker() {
  111. public void preProcess(IProgramElement node) {
  112. if (node.getChildren() == null)
  113. return;
  114. for (IProgramElement iProgramElement : node.getChildren()) {
  115. if (iProgramElement == null)
  116. throw new NullPointerException("null child on node: " + node.getName());
  117. }
  118. }
  119. };
  120. manager.getHierarchy().getRoot().walk(walker);
  121. }
  122. }