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.

ExtensionTests.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*******************************************************************************
  2. * Copyright (c) 2004 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * Andy Clement - initial implementation
  10. *******************************************************************************/
  11. package org.aspectj.ajde;
  12. import java.io.File;
  13. import java.util.Iterator;
  14. import java.util.List;
  15. import org.aspectj.asm.AsmManager;
  16. import org.aspectj.asm.IProgramElement;
  17. import org.aspectj.bridge.IMessage;
  18. import org.aspectj.org.eclipse.jdt.core.compiler.IProblem;
  19. import org.aspectj.tools.ajc.AjcTestCase;
  20. import org.aspectj.tools.ajc.CompilationResult;
  21. /**
  22. * Tests the 'extensions' to AJDE: 1) ID is now available on messages to allow you to see what 'kind' of message it is - this
  23. * activates quick fixes/etc in Eclipse.
  24. */
  25. public class ExtensionTests extends AjcTestCase {
  26. public static final String PROJECT_DIR = "extensions";
  27. private static final boolean debugTests = false;
  28. private File baseDir;
  29. protected void setUp() throws Exception {
  30. super.setUp();
  31. // TODO-path
  32. baseDir = new File("../ajde/testdata", PROJECT_DIR);
  33. }
  34. /**
  35. * Aim: Check that the ID of certain message kinds are correct
  36. *
  37. * ajc -warn:unusedImport UnusedImport.java
  38. *
  39. * Expected result is that id matches IProblem.UnusedImport
  40. */
  41. public void testMessageID() {
  42. String[] args = new String[] { "UnusedImport.java", "-warn:unusedImport" };
  43. CompilationResult result = ajc(baseDir, args);
  44. List l = result.getWarningMessages();
  45. IMessage m = ((IMessage) l.get(0));
  46. assertTrue("Expected ID of message to be " + IProblem.UnusedImport + " (UnusedImport) but found an ID of " + m.getID(),
  47. m.getID() == IProblem.UnusedImport);
  48. }
  49. public void testInnerClassesInASM() {
  50. String[] args = new String[] { "InnerClasses.java", "-emacssym", "-Xset:minimalModel=false" };
  51. CompilationResult result = ajc(baseDir, args);
  52. /* List l = */result.getWarningMessages();
  53. /* Properties p = */AsmManager.lastActiveStructureModel.summarizeModel().getProperties();
  54. if (debugTests)
  55. System.out.println("Structure Model for InnerClasses.java:");
  56. walkit(AsmManager.lastActiveStructureModel.getHierarchy().getRoot(), 0);
  57. foundNode = null;
  58. findChild("main", AsmManager.lastActiveStructureModel.getHierarchy().getRoot());
  59. assertTrue("Should have found node 'main' in the model", foundNode != null);
  60. IProgramElement runnableChild = getChild(foundNode, "new Runnable() {..}");
  61. assertTrue("'main' should have a child 'new Runnable() {..}'", runnableChild != null);
  62. assertTrue("'new Runnable() {..}' should have a 'run' child", getChild(runnableChild, "run") != null);
  63. /*
  64. * Left hand side is before the fix, right hand side is after: <root> InnerClasses.java import declarations InnerClasses A A
  65. * method method 1 new Runnable() {..} run run main main 2 new Runnable() {..} run run 3 new Object() {..} toString toString
  66. * 4 new Runnable run run
  67. */
  68. }
  69. private IProgramElement getChild(IProgramElement parent, String s) {
  70. List<IProgramElement> kids = parent.getChildren();
  71. for (Iterator<IProgramElement> iter = kids.iterator(); iter.hasNext();) {
  72. IProgramElement element = (IProgramElement) iter.next();
  73. if (element.getName().indexOf(s) != -1)
  74. return element;
  75. }
  76. return null;
  77. }
  78. private IProgramElement foundNode = null;
  79. private void findChild(String s, IProgramElement ipe) {
  80. if (ipe == null)
  81. return;
  82. if (ipe.getName().indexOf(s) != -1) {
  83. foundNode = ipe;
  84. return;
  85. }
  86. if (ipe.getChildren() != null) {
  87. List kids = ipe.getChildren();
  88. for (Iterator iter = kids.iterator(); iter.hasNext();) {
  89. IProgramElement element = (IProgramElement) iter.next();
  90. findChild(s, element);
  91. }
  92. }
  93. }
  94. public void walkit(IProgramElement ipe, int indent) {
  95. if (ipe != null) {
  96. if (debugTests)
  97. for (int i = 0; i < indent; i++)
  98. System.out.print(" ");
  99. if (debugTests)
  100. System.out.println(ipe.toLabelString());// getName());
  101. if (ipe.getChildren() != null) {
  102. List kids = ipe.getChildren();
  103. for (Iterator iter = kids.iterator(); iter.hasNext();) {
  104. IProgramElement element = (IProgramElement) iter.next();
  105. walkit(element, indent + 2);
  106. }
  107. }
  108. }
  109. }
  110. /**
  111. * Aim: Check that the start/end of certain warnings are correct
  112. *
  113. * ajc -warn:unusedImport UnusedImport.java
  114. *
  115. * Expected result is first warning message has start=7 end=20
  116. */
  117. public void testMessageSourceStartEnd() {
  118. String[] args = new String[] { "UnusedImport.java", "-warn:unusedImport" };
  119. CompilationResult result = ajc(baseDir, args);
  120. List l = result.getWarningMessages();
  121. IMessage m = ((IMessage) l.get(0));
  122. assertTrue("Expected source start to be 7 but was " + m.getSourceStart(), m.getSourceStart() == 7);
  123. assertTrue("Expected source end to be 20 but was " + m.getSourceEnd(), m.getSourceEnd() == 20);
  124. }
  125. }