]> source.dussan.org Git - aspectj.git/commitdiff
lots of pointcut parsing test cases for @xxx pcds
authoracolyer <acolyer>
Wed, 8 Dec 2004 16:14:20 +0000 (16:14 +0000)
committeracolyer <acolyer>
Wed, 8 Dec 2004 16:14:20 +0000 (16:14 +0000)
weaver/src/org/aspectj/weaver/patterns/ArgsAnnotationPointcut.java
weaver/src/org/aspectj/weaver/patterns/ExactAnnotationTypePattern.java
weaver/src/org/aspectj/weaver/patterns/PatternParser.java
weaver/src/org/aspectj/weaver/patterns/ThisOrTargetAnnotationPointcut.java
weaver/src/org/aspectj/weaver/patterns/WildTypePattern.java
weaver/src/org/aspectj/weaver/patterns/WithinAnnotationPointcut.java
weaver/src/org/aspectj/weaver/patterns/WithinCodeAnnotationPointcut.java
weaver/testsrc/org/aspectj/weaver/patterns/AnnotationPatternTestCase.java
weaver/testsrc/org/aspectj/weaver/patterns/ParserTestCase.java

index 9d37412a341b3e5285abf519db3b390608a1e988..cbf21bec81c75892e45761a525a2322c66e3f99f 100644 (file)
@@ -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();
+    }
 }
index d1ff327d1ccc609abafb45011d12db71a7e0d30a..b44c93e2db6db372c540a132642bbc2c6972a4e5 100644 (file)
@@ -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;
        }
 }
index fd1ab22e32a29e07d143ee79147f55a42fdf2aae..ae6f0cbc206c0ffff74420f35f8079c7851e4647 100644 (file)
@@ -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);
index c2b10b0659b2cd184871379b1f058a992c23f0c6..de488dc17bbec22e138c96202182426705f2ffc8 100644 (file)
@@ -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();    }
 }
index 2be4c38b1118218bc8a19f81aa02d7d11dd14e00..4e7dc4b797cb6745f1796904c8b1cf4062aa0c4f 100644 (file)
@@ -520,6 +520,7 @@ public class WildTypePattern extends TypePattern {
                        buf.append(name.toString());
                }
        }
+       if (includeSubtypes) buf.append('+');
        if (annotationPattern != AnnotationTypePattern.ANY) {
                buf.append(')');
        }
index c9194f2b507d6d0a39a598e2b7694f1d48fe7d6a..71937aadff4644df7a2aa0f988dbcf93d9b6b9b0 100644 (file)
@@ -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();
+    }
 }
index ad36ea07327c238ace0af83eaad18a5b521e3f28..2ebff9be924f25e65c41808b8971c966fa8b04d4 100644 (file)
@@ -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();
+    }
 }
index c3ea08e4d5c982ac07432f02a5f82db033d643a8..2a4ce76fda76ebd5cffe5dc0ec0875130a5d335d 100644 (file)
@@ -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());
                }
        }
        
index 1be5816904de252a2f4aa96e6fa94fd228976f84..f50ebbaea2049d7615d81bdf5eb35d4346d65a44 100644 (file)
@@ -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).<clinit>())",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() {