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

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