From d5bc7f7caac0cde68010baa5c2f73de8b7ccc410 Mon Sep 17 00:00:00 2001 From: acolyer Date: Wed, 8 Dec 2004 16:14:20 +0000 Subject: [PATCH] lots of pointcut parsing test cases for @xxx pcds --- .../patterns/ArgsAnnotationPointcut.java | 8 + .../patterns/ExactAnnotationTypePattern.java | 5 +- .../weaver/patterns/PatternParser.java | 4 +- .../ThisOrTargetAnnotationPointcut.java | 11 ++ .../weaver/patterns/WildTypePattern.java | 1 + .../patterns/WithinAnnotationPointcut.java | 10 ++ .../WithinCodeAnnotationPointcut.java | 10 ++ .../patterns/AnnotationPatternTestCase.java | 2 +- .../weaver/patterns/ParserTestCase.java | 167 +++++++++++++++++- 9 files changed, 209 insertions(+), 9 deletions(-) diff --git a/weaver/src/org/aspectj/weaver/patterns/ArgsAnnotationPointcut.java b/weaver/src/org/aspectj/weaver/patterns/ArgsAnnotationPointcut.java index 9d37412a3..cbf21bec8 100644 --- a/weaver/src/org/aspectj/weaver/patterns/ArgsAnnotationPointcut.java +++ b/weaver/src/org/aspectj/weaver/patterns/ArgsAnnotationPointcut.java @@ -91,4 +91,12 @@ public class ArgsAnnotationPointcut extends NameBindingPointcut { } + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + public String toString() { + StringBuffer buf = new StringBuffer("@args"); + buf.append(arguments.toString()); + return buf.toString(); + } } diff --git a/weaver/src/org/aspectj/weaver/patterns/ExactAnnotationTypePattern.java b/weaver/src/org/aspectj/weaver/patterns/ExactAnnotationTypePattern.java index d1ff327d1..b44c93e2d 100644 --- a/weaver/src/org/aspectj/weaver/patterns/ExactAnnotationTypePattern.java +++ b/weaver/src/org/aspectj/weaver/patterns/ExactAnnotationTypePattern.java @@ -153,6 +153,9 @@ public class ExactAnnotationTypePattern extends AnnotationTypePattern { } public String toString() { - return "@" + annotationType.toString(); + if (!resolved && formalName != null) return formalName; + String ret = "@" + annotationType.toString(); + if (formalName != null) ret = ret + " " + formalName; + return ret; } } diff --git a/weaver/src/org/aspectj/weaver/patterns/PatternParser.java b/weaver/src/org/aspectj/weaver/patterns/PatternParser.java index fd1ab22e3..ae6f0cbc2 100644 --- a/weaver/src/org/aspectj/weaver/patterns/PatternParser.java +++ b/weaver/src/org/aspectj/weaver/patterns/PatternParser.java @@ -726,7 +726,7 @@ public class PatternParser { } int endPos = tokenSource.peek(-1).getEnd(); if (buf.length() == 0 && names.isEmpty()) { - throw new ParserException("expected name pattern", tok); + throw new ParserException("name pattern", tok); } if (buf.length() == 0 && justProcessedEllipsis) { @@ -788,7 +788,7 @@ public class PatternParser { } int endPos = tokenSource.peek(-1).getEnd(); if (buf.length() == 0) { - throw new ParserException("expected name pattern", tok); + throw new ParserException("name pattern", tok); } checkLegalName(buf.toString(), previous); diff --git a/weaver/src/org/aspectj/weaver/patterns/ThisOrTargetAnnotationPointcut.java b/weaver/src/org/aspectj/weaver/patterns/ThisOrTargetAnnotationPointcut.java index c2b10b065..de488dc17 100644 --- a/weaver/src/org/aspectj/weaver/patterns/ThisOrTargetAnnotationPointcut.java +++ b/weaver/src/org/aspectj/weaver/patterns/ThisOrTargetAnnotationPointcut.java @@ -54,6 +54,8 @@ public class ThisOrTargetAnnotationPointcut extends NameBindingPointcut { return null; } + public boolean isThis() { return isThis; } + /* (non-Javadoc) * @see org.aspectj.weaver.patterns.Pointcut#resolveBindings(org.aspectj.weaver.patterns.IScope, org.aspectj.weaver.patterns.Bindings) */ @@ -94,4 +96,13 @@ public class ThisOrTargetAnnotationPointcut extends NameBindingPointcut { } + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + public String toString() { + StringBuffer buf = new StringBuffer(); + buf.append(isThis ? "@this(" : "@target("); + buf.append(type.toString()); + buf.append(")"); + return buf.toString(); } } diff --git a/weaver/src/org/aspectj/weaver/patterns/WildTypePattern.java b/weaver/src/org/aspectj/weaver/patterns/WildTypePattern.java index 2be4c38b1..4e7dc4b79 100644 --- a/weaver/src/org/aspectj/weaver/patterns/WildTypePattern.java +++ b/weaver/src/org/aspectj/weaver/patterns/WildTypePattern.java @@ -520,6 +520,7 @@ public class WildTypePattern extends TypePattern { buf.append(name.toString()); } } + if (includeSubtypes) buf.append('+'); if (annotationPattern != AnnotationTypePattern.ANY) { buf.append(')'); } diff --git a/weaver/src/org/aspectj/weaver/patterns/WithinAnnotationPointcut.java b/weaver/src/org/aspectj/weaver/patterns/WithinAnnotationPointcut.java index c9194f2b5..71937aadf 100644 --- a/weaver/src/org/aspectj/weaver/patterns/WithinAnnotationPointcut.java +++ b/weaver/src/org/aspectj/weaver/patterns/WithinAnnotationPointcut.java @@ -91,4 +91,14 @@ public class WithinAnnotationPointcut extends NameBindingPointcut { } + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + public String toString() { + StringBuffer buf = new StringBuffer(); + buf.append("@within("); + buf.append(type.toString()); + buf.append(")"); + return buf.toString(); + } } diff --git a/weaver/src/org/aspectj/weaver/patterns/WithinCodeAnnotationPointcut.java b/weaver/src/org/aspectj/weaver/patterns/WithinCodeAnnotationPointcut.java index ad36ea073..2ebff9be9 100644 --- a/weaver/src/org/aspectj/weaver/patterns/WithinCodeAnnotationPointcut.java +++ b/weaver/src/org/aspectj/weaver/patterns/WithinCodeAnnotationPointcut.java @@ -91,4 +91,14 @@ public class WithinCodeAnnotationPointcut extends NameBindingPointcut { } + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + public String toString() { + StringBuffer buf = new StringBuffer(); + buf.append("@withincode("); + buf.append(type.toString()); + buf.append(")"); + return buf.toString(); + } } diff --git a/weaver/testsrc/org/aspectj/weaver/patterns/AnnotationPatternTestCase.java b/weaver/testsrc/org/aspectj/weaver/patterns/AnnotationPatternTestCase.java index c3ea08e4d..2a4ce76fd 100644 --- a/weaver/testsrc/org/aspectj/weaver/patterns/AnnotationPatternTestCase.java +++ b/weaver/testsrc/org/aspectj/weaver/patterns/AnnotationPatternTestCase.java @@ -98,7 +98,7 @@ public class AnnotationPatternTestCase extends TestCase { AnnotationTypePattern bad = p.maybeParseAnnotationPattern(); fail("ParserException expected"); } catch(ParserException pEx) { - assertEquals("expected name pattern",pEx.getMessage()); + assertEquals("name pattern",pEx.getMessage()); } } diff --git a/weaver/testsrc/org/aspectj/weaver/patterns/ParserTestCase.java b/weaver/testsrc/org/aspectj/weaver/patterns/ParserTestCase.java index 1be581690..f50ebbaea 100644 --- a/weaver/testsrc/org/aspectj/weaver/patterns/ParserTestCase.java +++ b/weaver/testsrc/org/aspectj/weaver/patterns/ParserTestCase.java @@ -17,6 +17,7 @@ import junit.framework.TestCase; import org.aspectj.weaver.BcweaverTests; import org.aspectj.weaver.ResolvedTypeX; +import org.aspectj.weaver.Shadow; import org.aspectj.weaver.World; import org.aspectj.weaver.bcel.BcelShadow; import org.aspectj.weaver.bcel.BcelWorld; @@ -62,15 +63,171 @@ public class ParserTestCase extends TestCase { } } - public void testParseWithAnnotation() { + public void testParseExecutionWithAnnotation() { PatternParser parser = new PatternParser("execution(@SimpleAnnotation void Hello.*(..))"); KindedPointcut p = (KindedPointcut) parser.parsePointcut(); // XXX - needs finishing... p.resolveBindings(makeSimpleScope(),new Bindings(3)); - System.err.println(p); -// assertEquals(p.kind, BcelShadow.MethodExecution); -// assertTrue(p.signature.getName().matches("foobar")); -// p.signature.resolveBindings(makeSimpleScope(),new Bindings(3)); + assertEquals("execution(@p.SimpleAnnotation void Hello.*(..))",p.toString()); + assertEquals(p.kind, Shadow.MethodExecution); + assertTrue(p.signature.getName().matches("foobar")); + } + + // note... toString on a pointcut is a very quick and easy way to test a successful parse + public void testParseExecutionWithMultipleAnnotations() { + PatternParser parser = new PatternParser("execution(@SimpleAnnotation (@Foo Integer) (@Goo Hello).*(..))"); + KindedPointcut p = (KindedPointcut) parser.parsePointcut(); + assertEquals("execution(@(SimpleAnnotation) (@(Foo) Integer) (@(Goo) Hello).*(..))",p.toString());; + } + + public void testParseCallWithMultipleAnnotations() { + PatternParser parser = new PatternParser("call(@SimpleAnnotation (@Foo Integer) (@Goo Hello).*(..))"); + KindedPointcut p = (KindedPointcut) parser.parsePointcut(); + assertEquals("call(@(SimpleAnnotation) (@(Foo) Integer) (@(Goo) Hello).*(..))",p.toString());; + } + + public void testParseGetWithAnnotations() { + PatternParser parser = new PatternParser("get(@Foo (@SimpleAnnotation ReturnType) (@Foo @Goo Hello).*)"); + KindedPointcut p = (KindedPointcut) parser.parsePointcut(); + assertEquals("get(@(Foo) (@(SimpleAnnotation) ReturnType) (@(Foo) @(Goo) Hello).*)",p.toString());; + } + + public void testParseBadGetWithAnnotations() { + PatternParser parser = new PatternParser("get(@Foo (@Foo @Goo Hello).*)"); + try { + KindedPointcut p = (KindedPointcut) parser.parsePointcut(); + fail("Expected parser exception"); + } catch (ParserException pEx) { + assertEquals("name pattern",pEx.getMessage()); + } + } + + public void testParseGetWithAndAggregationAnnotations() { + PatternParser parser = new PatternParser("get(@Foo @SimpleAnnotation ReturnType (@Foo @Goo Hello).*)"); + KindedPointcut p = (KindedPointcut) parser.parsePointcut(); + assertEquals("get(@(Foo) @(SimpleAnnotation) ReturnType (@(Foo) @(Goo) Hello).*)",p.toString());; + } + + + public void testParseSetWithAnnotations() { + PatternParser parser = new PatternParser("set(@Foo (@SimpleAnnotation ReturnType) (@Foo @Goo Hello).*)"); + KindedPointcut p = (KindedPointcut) parser.parsePointcut(); + assertEquals("set(@(Foo) (@(SimpleAnnotation) ReturnType) (@(Foo) @(Goo) Hello).*)",p.toString());; + } + + public void testParseHandlerWithAnnotations() { + PatternParser parser = new PatternParser("handler(@Critical Exception+)"); + Pointcut p = parser.parsePointcut(); + assertEquals("handler((@(Critical) Exception+))",p.toString());; + } + + public void testParseInitializationWithAnnotations() { + PatternParser parser = new PatternParser("initialization(@Foo (@Goo Hello).new(@Foo Integer))"); + Pointcut p = parser.parsePointcut(); + assertEquals("initialization(@(Foo) (@(Goo) Hello).new((@(Foo) Integer)))",p.toString()); + + } + + public void testParsePreInitializationWithAnnotations() { + PatternParser parser = new PatternParser("preinitialization(@Foo (@Goo Hello).new(@Foo Integer))"); + Pointcut p = parser.parsePointcut(); + assertEquals("preinitialization(@(Foo) (@(Goo) Hello).new((@(Foo) Integer)))",p.toString()); + } + + public void testStaticInitializationWithAnnotations() { + PatternParser parser = new PatternParser("staticinitialization(@Foo @Boo @Goo Moo)"); + Pointcut p = parser.parsePointcut(); + assertEquals("staticinitialization((@(Foo) @(Boo) @(Goo) Moo).())",p.toString()); + } + + public void testWithinWithAnnotations() { + PatternParser parser = new PatternParser("within(@Foo *)"); + Pointcut p = parser.parsePointcut(); + assertEquals("within((@(Foo) *))",p.toString()); + } + + public void testWithinCodeWithAnnotations() { + PatternParser parser = new PatternParser("withincode(@Foo * *.*(..))"); + Pointcut p = parser.parsePointcut(); + assertEquals("withincode(@(Foo) * *.*(..))",p.toString()); + } + + public void testAtAnnotation() { + PatternParser parser = new PatternParser("@annotation(@Foo)"); + AnnotationPointcut p = (AnnotationPointcut) parser.parsePointcut(); + assertEquals("@annotation(@Foo)",p.toString()); + } + + public void testBadAtAnnotation() { + PatternParser parser = new PatternParser("@annotation(!@Foo)"); + try { + Pointcut p = parser.parsePointcut(); + fail("Expected parser exception"); + } catch (ParserException pEx) { + assertEquals("identifier",pEx.getMessage()); + } + } + + public void testAtAnnotationWithBinding() { + PatternParser parser = new PatternParser("@annotation(foo)"); + AnnotationPointcut p = (AnnotationPointcut) parser.parsePointcut(); + assertEquals("@annotation(foo)",p.toString()); + } + + public void testDoubleAtAnnotation() { + PatternParser parser = new PatternParser("@annotation(@Foo @Goo)"); + try { + Pointcut p = parser.parsePointcut(); + fail("Expected parser exception"); + } catch (ParserException pEx) { + assertEquals(")",pEx.getMessage()); + } + } + + public void testAtWithin() { + PatternParser parser = new PatternParser("@within(foo)"); + WithinAnnotationPointcut p = (WithinAnnotationPointcut) parser.parsePointcut(); + assertEquals("@within(foo)",p.toString()); + parser = new PatternParser("@within(@Foo))"); + p = (WithinAnnotationPointcut) parser.parsePointcut(); + assertEquals("@within(@Foo)",p.toString()); + } + + public void testAtWithinCode() { + PatternParser parser = new PatternParser("@withincode(foo)"); + WithinCodeAnnotationPointcut p = (WithinCodeAnnotationPointcut) parser.parsePointcut(); + assertEquals("@withincode(foo)",p.toString()); + parser = new PatternParser("@withincode(@Foo))"); + p = (WithinCodeAnnotationPointcut) parser.parsePointcut(); + assertEquals("@withincode(@Foo)",p.toString()); + } + + public void testAtThis() { + PatternParser parser = new PatternParser("@this(foo)"); + ThisOrTargetAnnotationPointcut p = (ThisOrTargetAnnotationPointcut) parser.parsePointcut(); + assertEquals("@this(foo)",p.toString()); + assertTrue("isThis",p.isThis()); + parser = new PatternParser("@this(@Foo))"); + p = (ThisOrTargetAnnotationPointcut) parser.parsePointcut(); + assertTrue("isThis",p.isThis()); + assertEquals("@this(@Foo)",p.toString()); + } + + public void testAtTarget() { + PatternParser parser = new PatternParser("@target(foo)"); + ThisOrTargetAnnotationPointcut p = (ThisOrTargetAnnotationPointcut) parser.parsePointcut(); + assertEquals("@target(foo)",p.toString()); + assertTrue("isTarget",!p.isThis()); + parser = new PatternParser("@target(@Foo))"); + p = (ThisOrTargetAnnotationPointcut) parser.parsePointcut(); + assertTrue("isTarget",!p.isThis()); + assertEquals("@target(@Foo)",p.toString()); + } + + public void testAtArgs() { + PatternParser parser = new PatternParser("@args(@Foo,@Goo,*,..,@Moo)"); + Pointcut p = parser.parsePointcut(); + assertEquals("@args(@Foo, @Goo, @ANY, .., @Moo)",p.toString()); } public TestScope makeSimpleScope() { -- 2.39.5