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.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 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(1)).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(2);
  58. IProgramElement pointcut = (IProgramElement) pNode.getChildren().get(0);
  59. assertTrue("kind", pointcut.getKind().equals(IProgramElement.Kind.POINTCUT));
  60. assertTrue("found node: " + pointcut.getName(), pointcut.toLabelString().equals("testptct()"));
  61. }
  62. public void testFileNodeFind() throws IOException {
  63. File testFile = openFile("figures" + File.separator + "Main.java");
  64. // System.err.println(((IProgramElement)((IProgramElement)Ajde.getDefault().getStructureModelManager().getHierarchy().getRoot().getChildren().get(0)).getChildren().get(3)).getSourceLocation().getSourceFile().getAbsolutePath());
  65. // System.err.println(testFile.getAbsolutePath());
  66. IProgramElement node = manager.getHierarchy().findElementForSourceLine(testFile.getAbsolutePath(), 1);
  67. assertTrue("find result", node != null);
  68. assertEquals("find result has children", 3, node.getChildren().size());
  69. assertTrue("found node: " + node.getName(), node.getKind().equals(IProgramElement.Kind.FILE_JAVA));
  70. }
  71. /**
  72. * @todo add negative test to make sure things that aren't runnable aren't annotated
  73. */
  74. public void testMainClassNodeInfo() throws IOException {
  75. IHierarchy model = manager.getHierarchy();
  76. assertTrue("model exists", model != null);
  77. assertTrue("root exists", model.getRoot() != null);
  78. File testFile = openFile("figures" + File.separator + "Main.java");
  79. IProgramElement node = model.findElementForSourceLine(testFile.getAbsolutePath(), 11);
  80. assertTrue("find result", node != null);
  81. IProgramElement pNode = node.getParent();
  82. if (null == pNode) {
  83. assertTrue("null parent of " + node, false);
  84. }
  85. assertTrue("found node: " + pNode.getName(), pNode.isRunnable());
  86. }
  87. /**
  88. * Integrity could be checked somewhere in the API.
  89. */
  90. public void testModelIntegrity() {
  91. IProgramElement modelRoot = manager.getHierarchy().getRoot();
  92. assertTrue("root exists", modelRoot != null);
  93. try {
  94. testModelIntegrityHelper(modelRoot);
  95. } catch (Exception e) {
  96. assertTrue(e.toString(), false);
  97. }
  98. }
  99. private void testModelIntegrityHelper(IProgramElement node) throws Exception {
  100. for (Iterator it = node.getChildren().iterator(); it.hasNext();) {
  101. IProgramElement child = (IProgramElement) it.next();
  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 (Iterator it = node.getChildren().iterator(); it.hasNext();) {
  115. if (it.next() == null)
  116. throw new NullPointerException("null child on node: " + node.getName());
  117. }
  118. }
  119. };
  120. manager.getHierarchy().getRoot().walk(walker);
  121. }
  122. }