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

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