diff options
author | aclement <aclement> | 2004-12-08 09:47:36 +0000 |
---|---|---|
committer | aclement <aclement> | 2004-12-08 09:47:36 +0000 |
commit | 14769b5d6ddcc43da0af272187992abb7ea499a8 (patch) | |
tree | 6bc4b9083395a39db8c6a133b33e64ea73c762db /weaver/testsrc/org | |
parent | 5bb7e432a8e1e94ebf6785a3b449b803df4a4942 (diff) | |
download | aspectj-14769b5d6ddcc43da0af272187992abb7ea499a8.tar.gz aspectj-14769b5d6ddcc43da0af272187992abb7ea499a8.zip |
Annotation matching.
Diffstat (limited to 'weaver/testsrc/org')
3 files changed, 202 insertions, 2 deletions
diff --git a/weaver/testsrc/org/aspectj/weaver/patterns/AnnotationPatternMatchingTestCase.java b/weaver/testsrc/org/aspectj/weaver/patterns/AnnotationPatternMatchingTestCase.java new file mode 100644 index 000000000..8d6462b75 --- /dev/null +++ b/weaver/testsrc/org/aspectj/weaver/patterns/AnnotationPatternMatchingTestCase.java @@ -0,0 +1,190 @@ +/* ******************************************************************* + * Copyright (c) 2004 IBM Corporation. + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Andy Clement initial implementation + * ******************************************************************/ +package org.aspectj.weaver.patterns; + +import java.util.List; +import java.util.ArrayList; + +import junit.framework.TestCase; + +import org.aspectj.bridge.AbortException; +import org.aspectj.bridge.IMessage; +import org.aspectj.bridge.IMessageHandler; +import org.aspectj.bridge.MessageHandler; +import org.aspectj.bridge.IMessage.Kind; +import org.aspectj.weaver.BcweaverTests; +import org.aspectj.weaver.ResolvedMember; +import org.aspectj.weaver.ResolvedTypeX; +import org.aspectj.weaver.bcel.BcelWorld; + +/* + * Sample types that this program uses are: + +import p.SimpleAnnotation; + +@SimpleAnnotation(id=2) +public class AnnotatedClass { + + @SimpleAnnotation(id=3) + public void m1() { } + + @SimpleAnnotation(id=4) + int i; +} + + * with SimpleAnnotation defined as: + +package p; +import java.lang.annotation.*; + +@Retention(RetentionPolicy.RUNTIME) +public @interface SimpleAnnotation { + int id(); + String fruit() default "bananas"; +} + +*NOTE NOTE NOTE NOTE NOTE NOTE NOTE* +If you need to rebuild the test data code, run 'ant -f build-15.xml' in the +testdata directory. + + */ +public class AnnotationPatternMatchingTestCase extends TestCase { + + private BcelWorld world; + private AnnotationTypePattern fooTP,simpleAnnotationTP; + + private ResolvedTypeX loadType(String name) { + if (world == null) { + world = new BcelWorld(BcweaverTests.TESTDATA_PATH + "/testcode.jar"); + } + return world.resolve(name); + } + + private void initAnnotationTypePatterns() { + PatternParser p = new PatternParser("@Foo"); + fooTP = p.parseAnnotationTypePattern(); + fooTP = fooTP.resolveBindings(makeSimpleScope(),new Bindings(3),true); + + p = new PatternParser("@p.SimpleAnnotation"); + simpleAnnotationTP = p.parseAnnotationTypePattern(); + simpleAnnotationTP = simpleAnnotationTP.resolveBindings(makeSimpleScope(),new Bindings(3),true); + } + + + public void testAnnotationPatternMatchingOnTypes() { + + ResolvedTypeX rtx = loadType("AnnotatedClass"); + initAnnotationTypePatterns(); + + // One should match + assertTrue("@Foo should not match on the AnnotatedClass", + fooTP.matches(rtx).alwaysFalse()); + assertTrue("@SimpleAnnotation should match on the AnnotatedClass", + simpleAnnotationTP.matches(rtx).alwaysTrue()); + + } + + static class MyMessageHandler implements IMessageHandler { + public List messages = new ArrayList(); + public boolean handleMessage(IMessage message) throws AbortException { + messages.add(message); + return false; + } + public boolean isIgnoring(Kind kind) {return false;} + } + + public void testReferenceToNonAnnotationType() { + ResolvedTypeX rtx = loadType("AnnotatedClass"); // inits the world + PatternParser p = new PatternParser("@java.lang.String"); + + MyMessageHandler mh = new MyMessageHandler(); + world.setMessageHandler(mh); + AnnotationTypePattern atp = p.parseAnnotationTypePattern(); + atp = atp.resolveBindings(makeSimpleScope(),new Bindings(3),true); + + assertTrue("Expected 1 error message but got "+mh.messages.size(),mh.messages.size()==1); + + String expected = "Type referred to is not an annotation type"; + String msg = ((IMessage)mh.messages.get(0)).toString(); + assertTrue("Expected: "+expected+" but got "+msg,msg.indexOf(expected)!=-1); + } + + public void testReferenceViaFormalToNonAnnotationType() { + ResolvedTypeX rtx = loadType("AnnotatedClass"); // inits the world + PatternParser p = new PatternParser("a"); + + MyMessageHandler mh = new MyMessageHandler(); + world.setMessageHandler(mh); + AnnotationTypePattern atp = p.parseAnnotationNameOrVarTypePattern(); + atp = atp.resolveBindings(makeSimpleScope(),new Bindings(3),true); + + assertTrue("Expected 1 error message but got "+mh.messages.size(),mh.messages.size()==1); + + String expected = "Type referred to is not an annotation type"; + String msg = ((IMessage)mh.messages.get(0)).toString(); + assertTrue("Expected: "+expected+" but got "+msg,msg.indexOf(expected)!=-1); + } + + public TestScope makeSimpleScope() { + return new TestScope(new String[] {"int", "java.lang.String"}, new String[] {"a", "b"}, world); + } + + public void testUnresolvedAnnotationTypes() { + ResolvedTypeX rtx = loadType("AnnotatedClass"); + + PatternParser p = new PatternParser("@Foo"); + AnnotationTypePattern fooTP = p.parseAnnotationTypePattern(); + try { + fooTP.matches(rtx); + fail("Should have failed with illegal state exception, fooTP is not resolved"); + } catch (IllegalStateException ise) { + // Correct! + } + } + + public void testAnnotationPatternMatchingOnMethods() { + + ResolvedTypeX rtx = loadType("AnnotatedClass"); + ResolvedMember aMethod = rtx.getDeclaredMethods()[1]; + + assertTrue("Haven't got the right method, I'm looking for 'm1()': "+aMethod.getName(), + aMethod.getName().equals("m1")); + + initAnnotationTypePatterns(); + + // One should match + assertTrue("@Foo should not match on the AnnotatedClass.m1() method", + fooTP.matches(aMethod).alwaysFalse()); + assertTrue("@SimpleAnnotation should match on the AnnotatedClass.m1() method", + simpleAnnotationTP.matches(aMethod).alwaysTrue()); + + } + + public void testAnnotationPatternMatchingOnFields() { + + ResolvedTypeX rtx = loadType("AnnotatedClass"); + ResolvedMember aField = rtx.getDeclaredFields()[0]; + + assertTrue("Haven't got the right field, I'm looking for 'i'"+aField.getName(), + aField.getName().equals("i")); + + initAnnotationTypePatterns(); + + // One should match + assertTrue("@Foo should not match on the AnnotatedClass.i field", + fooTP.matches(aField).alwaysFalse()); + assertTrue("@SimpleAnnotation should match on the AnnotatedClass.i field", + simpleAnnotationTP.matches(aField).alwaysTrue()); + + } + +} diff --git a/weaver/testsrc/org/aspectj/weaver/patterns/AnnotationPatternTestCase.java b/weaver/testsrc/org/aspectj/weaver/patterns/AnnotationPatternTestCase.java index e11415c80..489d24550 100644 --- a/weaver/testsrc/org/aspectj/weaver/patterns/AnnotationPatternTestCase.java +++ b/weaver/testsrc/org/aspectj/weaver/patterns/AnnotationPatternTestCase.java @@ -10,6 +10,8 @@ package org.aspectj.weaver.patterns; import org.aspectj.weaver.AnnotatedElement; +import org.aspectj.weaver.BcweaverTests; +import org.aspectj.weaver.ResolvedTypeX; import org.aspectj.weaver.TypeX; import org.aspectj.weaver.bcel.BcelWorld; @@ -247,6 +249,7 @@ public class AnnotationPatternTestCase extends TestCase { public void testExactAnnotationPatternMatching() { PatternParser p = new PatternParser("@Foo"); AnnotationTypePattern ap = p.parseAnnotationTypePattern(); + ap = ap.resolveBindings(makeSimpleScope(),new Bindings(3),true); AnnotatedElementImpl ae = new AnnotatedElementImpl(new String[]{"Foo"}); assertTrue("matches element with Foo",ap.matches(ae).alwaysTrue()); AnnotatedElementImpl ae2 = new AnnotatedElementImpl(new String[]{"Boo"}); @@ -266,6 +269,7 @@ public class AnnotationPatternTestCase extends TestCase { public void testAndAnnotationPatternMatching() { PatternParser p = new PatternParser("@Foo && @Boo"); AnnotationTypePattern ap = p.parseAnnotationTypePattern(); + ap = ap.resolveBindings(makeSimpleScope(),new Bindings(3),true); AnnotatedElementImpl ae = new AnnotatedElementImpl(new String[] {"Foo","Boo"}); assertTrue("matches foo and boo",ap.matches(ae).alwaysTrue()); ae = new AnnotatedElementImpl(new String[] {"Foo"}); @@ -279,6 +283,7 @@ public class AnnotationPatternTestCase extends TestCase { public void testOrAnnotationPatternMatching() { PatternParser p = new PatternParser("@Foo || @Boo"); AnnotationTypePattern ap = p.parseAnnotationTypePattern(); + ap = ap.resolveBindings(makeSimpleScope(),new Bindings(3),true); AnnotatedElementImpl ae = new AnnotatedElementImpl(new String[] {"Foo","Boo"}); assertTrue("matches foo and boo",ap.matches(ae).alwaysTrue()); ae = new AnnotatedElementImpl(new String[] {"Foo"}); @@ -292,6 +297,7 @@ public class AnnotationPatternTestCase extends TestCase { public void testNotAnnotationPatternMatching() { PatternParser p = new PatternParser("!@Foo"); AnnotationTypePattern ap = p.parseAnnotationTypePattern(); + ap = ap.resolveBindings(makeSimpleScope(),new Bindings(3),true); AnnotatedElementImpl ae = new AnnotatedElementImpl(new String[] {"Foo","Boo"}); assertTrue("does not match foo and boo",ap.matches(ae).alwaysFalse()); ae = new AnnotatedElementImpl(new String[] {"Boo"}); @@ -307,7 +313,10 @@ public class AnnotationPatternTestCase extends TestCase { public TestScope makeSimpleScope() { - return new TestScope(new String[] {"int", "java.lang.String","Foo"}, new String[] {"a", "b","foo"}, new BcelWorld()); + BcelWorld bWorld = new BcelWorld(BcweaverTests.TESTDATA_PATH + "/testcode.jar"); // testcode contains Foo/Boo/Goo/etc + return new TestScope(new String[] {"int", "java.lang.String","Foo"}, + new String[] {"a", "b","foo"}, + bWorld); } // put test cases for AnnotationPatternList matching in separate test class... @@ -320,7 +329,7 @@ public class AnnotationPatternTestCase extends TestCase { this.annotationTypes = annotationTypes; } - public boolean hasAnnotation(TypeX ofType) { + public boolean hasAnnotation(ResolvedTypeX ofType) { for (int i = 0; i < annotationTypes.length; i++) { if (annotationTypes[i].equals(ofType.getName())) return true; } diff --git a/weaver/testsrc/org/aspectj/weaver/patterns/PatternsTests.java b/weaver/testsrc/org/aspectj/weaver/patterns/PatternsTests.java index 5ed4c21c0..71f35d6df 100644 --- a/weaver/testsrc/org/aspectj/weaver/patterns/PatternsTests.java +++ b/weaver/testsrc/org/aspectj/weaver/patterns/PatternsTests.java @@ -38,6 +38,7 @@ public class PatternsTests extends TestCase { suite.addTestSuite(KindedTestCase.class); suite.addTestSuite(WithinCodeTestCase.class); suite.addTestSuite(AnnotationPatternTestCase.class); + suite.addTestSuite(AnnotationPatternMatchingTestCase.class); //$JUnit-END$ return suite; } |