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.

AjASTTestCase.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /********************************************************************
  2. * Copyright (c) 2006, 2010 Contributors. All rights reserved.
  3. * This program and the accompanying materials are made available
  4. * under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution and is available at
  6. * http://eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Helen Hawkins - initial implementation
  9. * Matthew Webster - initial implementation
  10. *******************************************************************/
  11. package org.aspectj.tools.ajc;
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import junit.framework.TestCase;
  16. import org.aspectj.org.eclipse.jdt.core.dom.AST;
  17. import org.aspectj.org.eclipse.jdt.core.dom.ASTParser;
  18. import org.aspectj.org.eclipse.jdt.core.dom.AjAST;
  19. import org.aspectj.org.eclipse.jdt.core.dom.AjASTVisitor;
  20. import org.aspectj.org.eclipse.jdt.core.dom.CompilationUnit;
  21. import org.aspectj.org.eclipse.jdt.core.SourceRange;
  22. public abstract class AjASTTestCase extends TestCase {
  23. protected AjAST createAjAST() {
  24. return createAjAST(AST.JLS3);
  25. }
  26. protected AjAST createAjAST(int astlevel) {
  27. if (astlevel != AST.JLS2 && astlevel != AST.JLS3) {
  28. fail("need to pass AST.JLS2 or AST.JLS3 as an argument");
  29. }
  30. String source = "";
  31. ASTParser parser = ASTParser.newParser(astlevel);
  32. parser.setSource(source.toCharArray());
  33. parser.setCompilerOptions(new HashMap());
  34. CompilationUnit cu = (CompilationUnit) parser.createAST(null);
  35. AST ast = cu.getAST();
  36. assertTrue("the ast should be an instance of AjAST",
  37. ast instanceof AjAST);
  38. return (AjAST) ast;
  39. }
  40. protected void checkJLS3(String source, ITypePatternTester tester) {
  41. ASTParser parser = ASTParser.newParser(AST.JLS3);
  42. parser.setCompilerOptions(new HashMap());
  43. parser.setSource(source.toCharArray());
  44. CompilationUnit cu2 = (CompilationUnit) parser.createAST(null);
  45. AjASTVisitor visitor = tester.createVisitor();
  46. cu2.accept(visitor);
  47. tester.testCondition(visitor);
  48. }
  49. protected void checkJLS3(String source, int start, int length) {
  50. checkJLS3(source, new SourceRangeTester(start, length));
  51. }
  52. /**
  53. *
  54. * @param source
  55. * to parse and visit
  56. * @param expectedSourceRanges
  57. * of TypePattern nodes encountered while visiting the AST
  58. */
  59. protected void checkTypePatternSourceRangesJLS3(String source,
  60. int[][] expectedSourceRanges) {
  61. checkJLS3(source,
  62. new TypePatternSourceRangeTester(expectedSourceRanges));
  63. }
  64. /**
  65. *
  66. * @param source
  67. * to parse and visit
  68. * @param expectedCategory
  69. * expected category of a TypeCategoryTypePattern node
  70. * encountered in the AST
  71. */
  72. protected void checkCategoryTypePatternJLS3(String source,
  73. int expectedCategory, String expectedExpression) {
  74. checkJLS3(source, new TypeCategoryTester(expectedCategory, expectedExpression));
  75. }
  76. protected List<SourceRange> getSourceRanges(int[][] sourceRanges) {
  77. List<SourceRange> convertedRanges = new ArrayList<SourceRange>();
  78. for (int i = 0; i < sourceRanges.length; i++) {
  79. convertedRanges.add(new SourceRange(sourceRanges[i][0],
  80. sourceRanges[i][1]));
  81. }
  82. return convertedRanges;
  83. }
  84. /*
  85. *
  86. *
  87. * Testing Classes and Interfaces
  88. */
  89. /**
  90. * Tests the results of a visitor when walking the AST
  91. *
  92. */
  93. interface ITypePatternTester {
  94. /**
  95. *
  96. * @return visitor to walk the AST. Must not be null.
  97. */
  98. public AjASTVisitor createVisitor();
  99. /**
  100. * Tests a condition after the visitor has visited the AST. This means
  101. * the visitor should contain the results of the visitation.
  102. *
  103. * @return true if test condition passed. False otherwise
  104. */
  105. public void testCondition(AjASTVisitor visitor);
  106. }
  107. /**
  108. * Tests whether a particular type category type pattern (InnerType,
  109. * InterfaceType, ClassType, etc..) is encountered when visiting nodes in an
  110. * AST.
  111. *
  112. */
  113. class TypeCategoryTester implements ITypePatternTester {
  114. private int expectedCategory;
  115. private String expectedExpression;
  116. public TypeCategoryTester(int expectedCategory,
  117. String expectedExpression) {
  118. this.expectedCategory = expectedCategory;
  119. this.expectedExpression = expectedExpression;
  120. }
  121. public AjASTVisitor createVisitor() {
  122. return new TypeCategoryTypeVisitor();
  123. }
  124. public void testCondition(AjASTVisitor visitor) {
  125. TypeCategoryTypeVisitor tcVisitor = (TypeCategoryTypeVisitor) visitor;
  126. assertTrue("Expected type category: " + expectedCategory
  127. + ". Actual type category: "
  128. + tcVisitor.getTypeCategoryNode().getTypeCategory(),
  129. expectedCategory == tcVisitor.getTypeCategoryNode()
  130. .getTypeCategory());
  131. assertTrue("Expected type category expression: "
  132. + expectedExpression
  133. + ". Actual type category expression: "
  134. + tcVisitor.getTypeCategoryNode()
  135. .getTypePatternExpression(),
  136. expectedExpression.equals(tcVisitor.getTypeCategoryNode()
  137. .getTypePatternExpression()));
  138. }
  139. }
  140. /**
  141. * Tests the starting location and source length of each TypePattern node
  142. * encountered while walking the AST.
  143. *
  144. */
  145. class TypePatternSourceRangeTester implements ITypePatternTester {
  146. private int[][] expectedRawSourceRanges;
  147. public TypePatternSourceRangeTester(int[][] expectedRawSourceRanges) {
  148. this.expectedRawSourceRanges = expectedRawSourceRanges;
  149. }
  150. public AjASTVisitor createVisitor() {
  151. return new TypePatternSourceRangeVisitor();
  152. }
  153. public void testCondition(AjASTVisitor visitor) {
  154. TypePatternSourceRangeVisitor sourceRangeVisitor = (TypePatternSourceRangeVisitor) visitor;
  155. List<SourceRange> actualRanges = sourceRangeVisitor
  156. .getVisitedSourceRanges();
  157. List<SourceRange> expectedRanges = getSourceRanges(expectedRawSourceRanges);
  158. assertTrue("Expected " + expectedRanges.size()
  159. + " number of source range entries. Actual: "
  160. + actualRanges.size(),
  161. expectedRanges.size() == actualRanges.size());
  162. for (int i = 0; i < actualRanges.size(); i++) {
  163. SourceRange expected = expectedRanges.get(i);
  164. SourceRange actual = actualRanges.get(i);
  165. assertTrue(
  166. "Expected source range: " + expected.toString()
  167. + " does not match actual source range: "
  168. + actual.toString(), expected.equals(actual));
  169. }
  170. }
  171. }
  172. /**
  173. * Tests whether a particular AST node starts at a given expected location
  174. * and has an expected length
  175. *
  176. */
  177. class SourceRangeTester implements ITypePatternTester {
  178. private int expectedStart;
  179. private int expectedLength;
  180. public SourceRangeTester(int expectedStart, int expectedLength) {
  181. this.expectedLength = expectedLength;
  182. this.expectedStart = expectedStart;
  183. }
  184. public AjASTVisitor createVisitor() {
  185. return new SourceRangeVisitor();
  186. }
  187. public void testCondition(AjASTVisitor visitor) {
  188. int s = ((SourceRangeVisitor) visitor).getStart();
  189. int l = ((SourceRangeVisitor) visitor).getLength();
  190. assertTrue("Expected start position: " + expectedStart
  191. + ", Actual:" + s, expectedStart == s);
  192. assertTrue("Expected length: " + expectedLength + ", Actual:" + l,
  193. expectedLength == l);
  194. }
  195. }
  196. }