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.

IfPointcutTest.java 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*******************************************************************************
  2. * Copyright (c) 2005 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * initial implementation Alexandre Vasseur
  11. *******************************************************************************/
  12. package ataspectj;
  13. import org.aspectj.lang.annotation.Aspect;
  14. import org.aspectj.lang.annotation.Pointcut;
  15. import org.aspectj.lang.annotation.Before;
  16. import org.aspectj.lang.annotation.Aspect;
  17. import org.aspectj.lang.annotation.Pointcut;
  18. import junit.framework.TestCase;
  19. import org.aspectj.lang.annotation.Aspect;
  20. /**
  21. * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
  22. */
  23. public class IfPointcutTest extends TestCase {
  24. static StringBuffer s_log = new StringBuffer();
  25. static void log(String s) {
  26. s_log.append(s).append(" ");
  27. }
  28. public void hello(int i) {
  29. log("hello-" + i);
  30. }
  31. public void testIf() {
  32. s_log = new StringBuffer();
  33. IfPointcutTest me = new IfPointcutTest();
  34. me.hello(1);
  35. assertEquals("aop hello-1 ", s_log.toString());
  36. me.hello(-1);
  37. assertEquals("aop hello-1 hello--1 ", s_log.toString());//unchanged
  38. }
  39. public static void main(String[] args) {
  40. TestHelper.runAndThrowOnFailure(suite());
  41. }
  42. public static junit.framework.Test suite() {
  43. return new junit.framework.TestSuite(IfPointcutTest.class);
  44. }
  45. @Aspect
  46. public static class TestAspect {
  47. @Pointcut("args(i) && if() && if(true)")
  48. public static boolean positive(int i) {
  49. return i>=0;
  50. }
  51. @Before("execution(* ataspectj.IfPointcutTest.hello(int)) && positive(i)")
  52. public void before(int i) {
  53. log("aop");
  54. }
  55. }
  56. }