Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AjNaiveASTFlattenerTest.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 v 2.0
  5. * which accompanies this distribution and is available at
  6. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  7. *
  8. * Contributors: IBM Corporation - initial API and implementation
  9. * Eduardo Piveta - initial version
  10. * Helen Hawkins - ammended to fit within JUnit framework
  11. *******************************************************************/
  12. package org.aspectj.tools.ajc;
  13. import java.util.HashMap;
  14. import junit.framework.TestCase;
  15. import org.aspectj.org.eclipse.jdt.core.dom.AST;
  16. import org.aspectj.org.eclipse.jdt.core.dom.ASTParser;
  17. import org.aspectj.org.eclipse.jdt.core.dom.AjNaiveASTFlattener;
  18. import org.aspectj.org.eclipse.jdt.core.dom.CompilationUnit;
  19. public class AjNaiveASTFlattenerTest extends TestCase {
  20. public void testNoPointcutArgs() {
  21. check("public aspect A { pointcut y(): call(* *.*(..));}",
  22. "public aspect A {\n pointcut y():call(* *.*(..));\n}\n");
  23. }
  24. public void testOneIntPointcutArg() {
  25. check("public aspect A { pointcut y(int a): call(* *.*(..));}",
  26. "public aspect A {\n pointcut y(int a):call(* *.*(..));\n}\n");
  27. }
  28. public void testOneIntOneDoublePointcutArg() {
  29. check("public aspect A { pointcut y(int a, double b): call(* *.*(..));}",
  30. "public aspect A {\n pointcut y(int a, double b):call(* *.*(..));\n}\n");
  31. }
  32. public void testOneTypedPointcutArg() {
  33. check("public aspect A { pointcut y(X a): call(* *.*(..));}",
  34. "public aspect A {\n pointcut y(X a):call(* *.*(..));\n}\n");
  35. }
  36. public void testTwoTypedPointcutArgs() {
  37. check("public aspect A { pointcut y(X a, X b): call(* *.*(..));}",
  38. "public aspect A {\n pointcut y(X a, X b):call(* *.*(..));\n}\n");
  39. }
  40. public void testOneTypedAndOneIntPointcutArg() {
  41. check("public aspect A { pointcut y(X a, int b): call(* *.*(..));}",
  42. "public aspect A {\n pointcut y(X a, int b):call(* *.*(..));\n}\n");
  43. }
  44. public void testOneIntAndOneTypedPointcutArg() {
  45. check("public aspect A { pointcut y(int a, X b): call(* *.*(..));}",
  46. "public aspect A {\n pointcut y(int a, X b):call(* *.*(..));\n}\n");
  47. }
  48. public void testOneIntOneDoubleAndOneTypedPointcutArg() {
  49. check("public aspect A { pointcut y(int a, double b, Y c): call(* *.*(..));}",
  50. "public aspect A {\n pointcut y(int a, double b, Y c):call(* *.*(..));\n}\n");
  51. }
  52. public void testDeclareParentsDeclaration() throws Exception {
  53. check("public aspect A { declare parents: X extends Y; }",
  54. "public aspect A {\n declare parents: X extends Y;\n}\n");
  55. }
  56. /*
  57. *
  58. *
  59. * START: Test TypePattern nodes introduced in Bugzilla 329268.
  60. *
  61. *
  62. */
  63. public void testDeclareParentsDeclarationAny() throws Exception {
  64. check("public aspect A { declare parents: * extends Y; }",
  65. "public aspect A {\n declare parents: * extends Y;\n}\n");
  66. }
  67. public void testDeclareParentsAndDeclaration() throws Exception {
  68. check("public aspect A { declare parents: W && X && Y extends Z; }",
  69. "public aspect A {\n declare parents: W && X && Y extends Z;\n}\n");
  70. }
  71. public void testDeclareParentsOrDeclaration() throws Exception {
  72. check("public aspect A { declare parents: W || X || Y extends Z; }",
  73. "public aspect A {\n declare parents: W || X || Y extends Z;\n}\n");
  74. }
  75. public void testDeclareParentsNot() throws Exception {
  76. check("public aspect A { declare parents: W && !X extends Z; }",
  77. "public aspect A {\n declare parents: W && !X extends Z;\n}\n");
  78. }
  79. public void testDeclareParentsTypeCategory() throws Exception {
  80. check("public aspect A { declare parents: B && is(AnonymousType) extends Z; }",
  81. "public aspect A {\n declare parents: B && is(AnonymousType) extends Z;\n}\n");
  82. }
  83. public void testDeclareParentsTypeCategoryNot() throws Exception {
  84. check("public aspect A { declare parents: B && !is(InnerType) extends Z; }",
  85. "public aspect A {\n declare parents: B && !is(InnerType) extends Z;\n}\n");
  86. }
  87. // TODO: commented until hasmethod is supported in AspectJ
  88. // public void testDeclareParentsHasMember() {
  89. // check("public aspect A { declare parents : A && hasmethod(void foo*(..)) extends D; }",
  90. // "public aspect A {\n declare parents : A && hasmethod(void foo*(..)) extends D;\n}\n");
  91. // }
  92. /*
  93. *
  94. *
  95. * END: Test TypePattern nodes introduced in Bugzilla 329268.
  96. *
  97. *
  98. */
  99. public void testDeclareWarning() throws Exception {
  100. check("public aspect A { declare warning: call(* *.*(..)) : \"warning!\"; }",
  101. "public aspect A {\n declare warning: call(* *.*(..)) : \"warning!\" ;\n}\n");
  102. }
  103. public void testDeclareErrorDeclaration() throws Exception {
  104. check("public aspect A { declare error: call(* *.*(..)) : \"error!\"; }",
  105. "public aspect A {\n declare error: call(* *.*(..)) : \"error!\" ;\n}\n");
  106. }
  107. public void testDeclareSoftDeclaration() throws Exception {
  108. check("public aspect A { declare soft: X : call(* *.*(..)); }",
  109. "public aspect A {\n declare soft: X : call(* *.*(..)) ;\n}\n");
  110. }
  111. public void testDeclarePrecedenceDeclaration() throws Exception {
  112. check("public aspect A { declare precedence: X, Y, Z; }",
  113. "public aspect A {\n declare precedence: X, Y, Z;\n}\n");
  114. }
  115. private void check(String source, String expectedOutput) {
  116. ASTParser parser = ASTParser.newParser(AST.JLS2);
  117. parser.setCompilerOptions(new HashMap());
  118. parser.setSource(source.toCharArray());
  119. CompilationUnit cu2 = (CompilationUnit) parser.createAST(null);
  120. AjNaiveASTFlattener visitor = new AjNaiveASTFlattener();
  121. cu2.accept(visitor);
  122. String result = visitor.getResult();
  123. System.err.println(result);
  124. assertTrue("Expected:\n"+ expectedOutput + "====Actual:\n" + result,
  125. expectedOutput.equals(result));
  126. }
  127. }