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.

ExtensionTest.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 ExtensionTest 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 (IProgramElement element : kids) {
  72. if (element.getName().contains(s))
  73. return element;
  74. }
  75. return null;
  76. }
  77. private IProgramElement foundNode = null;
  78. private void findChild(String s, IProgramElement ipe) {
  79. if (ipe == null)
  80. return;
  81. if (ipe.getName().contains(s)) {
  82. foundNode = ipe;
  83. return;
  84. }
  85. if (ipe.getChildren() != null) {
  86. List kids = ipe.getChildren();
  87. for (Object kid : kids) {
  88. IProgramElement element = (IProgramElement) kid;
  89. findChild(s, element);
  90. }
  91. }
  92. }
  93. public void walkit(IProgramElement ipe, int indent) {
  94. if (ipe != null) {
  95. if (debugTests)
  96. for (int i = 0; i < indent; i++)
  97. System.out.print(" ");
  98. if (debugTests)
  99. System.out.println(ipe.toLabelString());// getName());
  100. if (ipe.getChildren() != null) {
  101. List kids = ipe.getChildren();
  102. for (Object kid : kids) {
  103. IProgramElement element = (IProgramElement) kid;
  104. walkit(element, indent + 2);
  105. }
  106. }
  107. }
  108. }
  109. /**
  110. * Aim: Check that the start/end of certain warnings are correct
  111. *
  112. * ajc -warn:unusedImport UnusedImport.java
  113. *
  114. * Expected result is first warning message has start=7 end=20
  115. */
  116. public void testMessageSourceStartEnd() {
  117. String[] args = new String[] { "UnusedImport.java", "-warn:unusedImport" };
  118. CompilationResult result = ajc(baseDir, args);
  119. List l = result.getWarningMessages();
  120. IMessage m = ((IMessage) l.get(0));
  121. assertTrue("Expected source start to be 7 but was " + m.getSourceStart(), m.getSourceStart() == 7);
  122. assertTrue("Expected source end to be 20 but was " + m.getSourceEnd(), m.getSourceEnd() == 20);
  123. }
  124. }