From: mwebster Date: Mon, 7 Aug 2006 16:41:05 +0000 (+0000) Subject: Bug 152982 "org.aspectj Restructure - Phase 2: Move tests" (move Java 5 dependedent... X-Git-Tag: pre_pr_153572~3 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=6dca4cc41d0af83c9baeb1aa1734b48cec11b1b0;p=aspectj.git Bug 152982 "org.aspectj Restructure - Phase 2: Move tests" (move Java 5 dependedent tests to weaver5, reduce use of reflection and conditional execution, remove duplication) --- diff --git a/run-all-junit-tests/testsrc/AllTests.java b/run-all-junit-tests/testsrc/AllTests.java index bac614eb2..6ef81a518 100644 --- a/run-all-junit-tests/testsrc/AllTests.java +++ b/run-all-junit-tests/testsrc/AllTests.java @@ -11,10 +11,9 @@ * ******************************************************************/ // default package -import org.aspectj.util.LangUtil; -import org.aspectj.util.UtilModuleTests; -import org.aspectj.weaver.BcweaverModuleTests; -import org.aspectj.weaver.BcweaverModuleTests15; +import junit.framework.TestCase; +import junit.framework.TestSuite; + import org.aspectj.ajde.AjdeModuleTests; import org.aspectj.ajdt.EajcModuleTests; import org.aspectj.asm.AsmModuleTests; @@ -30,9 +29,10 @@ import org.aspectj.testingutil.TestingUtilModuleTests; import org.aspectj.tools.ajbrowser.AjbrowserModuleTests; import org.aspectj.tools.ajdoc.AjdocModuleTests; import org.aspectj.tools.ant.TaskdefsModuleTests; - -import junit.framework.TestCase; -import junit.framework.TestSuite; +import org.aspectj.util.LangUtil; +import org.aspectj.util.UtilModuleTests; +import org.aspectj.weaver.BcweaverModuleTests; +import org.aspectj.weaver.Weaver5ModuleTests; public class AllTests extends TestCase { public static final boolean skipSupportModules = false; @@ -65,8 +65,7 @@ public class AllTests extends TestCase { TestUtil.loadTestsReflectively(suite, "org.aspectj.runtime.Aspectj5rtModuleTests", false); TestUtil.loadTestsReflectively(suite, "org.aspectj.loadtime.Loadtime5ModuleTests", false); // this next one is built normally, but needs 1.5 rt.jar to pass - suite.addTest(BcweaverModuleTests15.suite()); - TestUtil.loadTestsReflectively(suite, "org.aspectj.weaver.Weaver5ModuleTests",false); + suite.addTest(Weaver5ModuleTests.suite()); } else { suite.addTest(TestUtil.skipTest("for 1.5")); } diff --git a/weaver/testsrc/org/aspectj/weaver/BcweaverModuleTests15.java b/weaver/testsrc/org/aspectj/weaver/BcweaverModuleTests15.java deleted file mode 100644 index 7220ea50a..000000000 --- a/weaver/testsrc/org/aspectj/weaver/BcweaverModuleTests15.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.aspectj.weaver; -/* ******************************************************************* - * Copyright (c) 2005 Contributors. - * All rights reserved. - * This program and the accompanying materials are made available - * under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution and is available at - * http://eclipse.org/legal/epl-v10.html - * - * Contributors: - * Adrian Colyer Initial implementation - * ******************************************************************/ -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -import org.aspectj.testing.util.TestUtil; -import org.aspectj.util.LangUtil; -import org.aspectj.weaver.bcel.BcelGenericSignatureToTypeXTestCase; -import org.aspectj.weaver.patterns.WildTypePatternResolutionTestCase; - -public class BcweaverModuleTests15 extends TestCase { - public static Test suite() { - TestSuite suite = new TestSuite(BcweaverModuleTests15.class.getName()); - suite.addTestSuite(TypeVariableTestCase.class); - suite.addTestSuite(ReferenceTypeTestCase.class); - suite.addTestSuite(BoundedReferenceTypeTestCase.class); - suite.addTestSuite(TypeVariableReferenceTypeTestCase.class); - suite.addTestSuite(MemberTestCase15.class); - suite.addTestSuite(BcelGenericSignatureToTypeXTestCase.class); - suite.addTestSuite(WildTypePatternResolutionTestCase.class); - if (LangUtil.is15VMOrGreater()) { - TestUtil.loadTestsReflectively(suite, "org.aspectj.weaver.tools.Java15PointcutExpressionTest", false); - TestUtil.loadTestsReflectively(suite, "org.aspectj.weaver.TestJava5ReflectionBasedReferenceTypeDelegate", false); - } - return suite; - } - - public BcweaverModuleTests15(String name) { super(name); } -} diff --git a/weaver/testsrc/org/aspectj/weaver/bcel/AsmDelegateTests.java b/weaver/testsrc/org/aspectj/weaver/bcel/AsmDelegateTests.java index 146a5ba2b..58918b778 100644 --- a/weaver/testsrc/org/aspectj/weaver/bcel/AsmDelegateTests.java +++ b/weaver/testsrc/org/aspectj/weaver/bcel/AsmDelegateTests.java @@ -31,7 +31,6 @@ import org.aspectj.weaver.ResolvedMemberImpl; import org.aspectj.weaver.ResolvedType; import org.aspectj.weaver.ShadowMunger; import org.aspectj.weaver.TypeVariable; -import org.aspectj.weaver.UnresolvedType; import org.aspectj.weaver.World; import org.aspectj.weaver.AjAttribute.EffectiveSignatureAttribute; import org.aspectj.weaver.asm.AsmDelegate; @@ -153,105 +152,6 @@ public class AsmDelegateTests extends AbstractWorldTestCase { checkEquivalent("",(AbstractReferenceTypeDelegate)aComplex.getDelegate(),(AbstractReferenceTypeDelegate)bComplex.getDelegate()); } - /** - * Methods are transformed according to generic signatures - this checks - * that some of the generic methods in java.lang.Class appear the same - * whether viewed through an ASM or a BCEL delegate. - */ - public void testCompareGenericMethods() { - BcelWorld slowWorld = new BcelWorld(); - slowWorld.setFastDelegateSupport(false); - slowWorld.setBehaveInJava5Way(true); - - BcelWorld fastWorld = new BcelWorld(); - fastWorld.setBehaveInJava5Way(true); - - ResolvedType bcelJavaLangClass = slowWorld.resolve(UnresolvedType.forName("java.lang.Class")); - ResolvedType asmJavaLangClass = fastWorld.resolve(UnresolvedType.forName("java.lang.Class")); - - bcelJavaLangClass = bcelJavaLangClass.getGenericType(); - asmJavaLangClass = asmJavaLangClass.getGenericType(); - - //if (bcelJavaLangClass == null) return; // for < 1.5 - - ResolvedMember[] bcelMethods = bcelJavaLangClass.getDeclaredMethods(); - ResolvedMember[] asmMethods = asmJavaLangClass.getDeclaredMethods(); - - for (int i = 0; i < bcelMethods.length; i++) { - bcelMethods[i].setParameterNames(null); // forget them, asm delegates dont currently know them - String one = bcelMethods[i].toDebugString(); - String two = asmMethods[i].toDebugString(); - if (!one.equals(two)) { - fail("These methods look different when viewed through ASM or BCEL\nBCEL='"+bcelMethods[i].toDebugString()+ - "'\n ASM='"+asmMethods[i].toDebugString()+"'"); - } - // If one is parameterized, check the other is... - if (bcelMethods[i].canBeParameterized()) { - assertTrue("ASM method '"+asmMethods[i].toDebugString()+"' can't be parameterized whereas its' BCEL variant could", - asmMethods[i].canBeParameterized()); - } - - } - - // Let's take a special look at: - // public Class asSubclass(Class clazz) - ResolvedMember bcelSubclassMethod = null; - for (int i = 0; i < bcelMethods.length; i++) { - if (bcelMethods[i].getName().equals("asSubclass")) { bcelSubclassMethod = bcelMethods[i]; break; } - } - ResolvedMember asmSubclassMethod = null; - for (int i = 0; i < asmMethods.length; i++) { - if (asmMethods[i].getName().equals("asSubclass")) { asmSubclassMethod = asmMethods[i];break; } - } - - TypeVariable[] tvs = bcelSubclassMethod.getTypeVariables(); - assertTrue("should have one type variable on the bcel version but found: "+format(tvs),tvs!=null && tvs.length==1); - tvs = asmSubclassMethod.getTypeVariables(); - assertTrue("should have one type variable on the asm version but found: "+format(tvs),tvs!=null && tvs.length==1); - - } - - private String format(TypeVariable[] tvs) { - if (tvs==null) return "null"; - StringBuffer s = new StringBuffer(); - s.append("["); - for (int i = 0; i < tvs.length; i++) { - s.append(tvs[i]); - if ((i+1) Class asSubclass(Class clazz) + ResolvedMember bcelSubclassMethod = null; + for (int i = 0; i < bcelMethods.length; i++) { + if (bcelMethods[i].getName().equals("asSubclass")) { bcelSubclassMethod = bcelMethods[i]; break; } + } + ResolvedMember asmSubclassMethod = null; + for (int i = 0; i < asmMethods.length; i++) { + if (asmMethods[i].getName().equals("asSubclass")) { asmSubclassMethod = asmMethods[i];break; } + } + + TypeVariable[] tvs = bcelSubclassMethod.getTypeVariables(); + assertTrue("should have one type variable on the bcel version but found: "+format(tvs),tvs!=null && tvs.length==1); + tvs = asmSubclassMethod.getTypeVariables(); + assertTrue("should have one type variable on the asm version but found: "+format(tvs),tvs!=null && tvs.length==1); + + } + + private String format(TypeVariable[] tvs) { + if (tvs==null) return "null"; + StringBuffer s = new StringBuffer(); + s.append("["); + for (int i = 0; i < tvs.length; i++) { + s.append(tvs[i]); + if ((i+1);",comparable.getSignature()); + UnresolvedType serializable = + BcelGenericSignatureToTypeXConverter.classTypeSignature2TypeX( + cSig.superInterfaceSignatures[1], + cSig.formalTypeParameters, + world + ); + assertEquals("Ljava/io/Serializable;",serializable.getSignature()); + } + + public void testColonColon() throws Exception { + BcelWorld world = new BcelWorld(); + Signature.ClassSignature cSig = new GenericSignatureParser().parseAsClassSignature("Ljava/lang/Object;Ljava/lang/Comparable;"); + UnresolvedType resolved = BcelGenericSignatureToTypeXConverter.classTypeSignature2TypeX( + cSig.superclassSignature, + cSig.formalTypeParameters, + world); + assertEquals("Ljava/lang/Object;",resolved.getSignature()); + UnresolvedType resolvedInt = BcelGenericSignatureToTypeXConverter.classTypeSignature2TypeX( + cSig.superInterfaceSignatures[0], + cSig.formalTypeParameters, + world); + + } + +} diff --git a/weaver5/java5-testsrc/org/aspectj/weaver/patterns/WildTypePatternResolutionTestCase.java b/weaver5/java5-testsrc/org/aspectj/weaver/patterns/WildTypePatternResolutionTestCase.java new file mode 100644 index 000000000..de3fa0222 --- /dev/null +++ b/weaver5/java5-testsrc/org/aspectj/weaver/patterns/WildTypePatternResolutionTestCase.java @@ -0,0 +1,471 @@ +/* ******************************************************************* + * Copyright (c) 2005 Contributors + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Adrian Colyer initial implementation + * ******************************************************************/ +package org.aspectj.weaver.patterns; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import junit.framework.TestCase; + +import org.aspectj.weaver.BoundedReferenceType; +import org.aspectj.weaver.ResolvedType; +import org.aspectj.weaver.TypeFactory; +import org.aspectj.weaver.UnresolvedType; +import org.aspectj.weaver.VersionedDataInputStream; +import org.aspectj.weaver.World; +import org.aspectj.weaver.AjAttribute.WeaverVersionInfo; +import org.aspectj.weaver.bcel.BcelWorld; + +// TODO write test cases for instanceof matching + + public class WildTypePatternResolutionTestCase extends TestCase { + + private World world; + private Bindings bindings; + private SimpleScope scope; + private ResolvedType javaUtilList; + private ResolvedType javaLangString; + private ResolvedType javaUtilListOfString; + private ResolvedType javaUtilListOfDouble; + private ResolvedType javaUtilListOfSomething; + + /** + * Foo where Foo exists and is generic + * Parser creates WildTypePattern namePatterns={Foo} + * resolveBindings resolves Foo to RT(Foo - raw) + * return ExactTypePattern(LFoo;) + */ + public void testSimpleFoo() { + TypePattern rtp = resolveWildTypePattern("List", false); + + assertTrue("resolves to exact type",rtp instanceof ExactTypePattern); + UnresolvedType exactType = rtp.getExactType(); + assertTrue(exactType.isRawType()); + assertEquals("Ljava/util/List;",exactType.getSignature()); + + ResolvedType rt = exactType.resolve(world); + assertEquals("Ljava/util/List;",rt.getSignature()); + assertTrue(rt.isRawType()); + + ExactTypePattern etp = (ExactTypePattern) writeAndRead((ExactTypePattern)rtp); + exactType = etp.getExactType(); + + assertEquals("Ljava/util/List;",exactType.getSignature()); + + rt = exactType.resolve(world); + assertEquals("Ljava/util/List;",rt.getSignature()); + assertTrue(rt.isRawType()); + + assertTrue("matches List",etp.matches(javaUtilList, TypePattern.STATIC).alwaysTrue()); + assertTrue("matches generic List",etp.matches(javaUtilList.getGenericType(),TypePattern.STATIC).alwaysTrue()); + assertTrue("matches parameterized list",etp.matches(javaUtilListOfString,TypePattern.STATIC).alwaysTrue()); + assertTrue("does not match String",etp.matches(javaLangString,TypePattern.STATIC).alwaysFalse()); + } + + /** + * Foo where Foo exists and String meets the bounds + * Parser creates WildTypePattern namePatterns = {Foo}, typeParameters=WTP{String} + * resolveBindings resolves typeParameters to ExactTypePattern(String) + * resolves Foo to RT(Foo) + * returns ExactTypePattern(PFoo; - parameterized) + */ + public void testParameterized() { + TypePattern rtp = resolveWildTypePattern("List", false); + + assertTrue("resolves to exact type",rtp instanceof ExactTypePattern); + UnresolvedType exactType = rtp.getExactType(); + assertTrue(exactType.isParameterizedType()); + assertEquals("Pjava/util/List;",exactType.getSignature()); + + ResolvedType rt = exactType.resolve(world); + assertEquals("Pjava/util/List;",rt.getSignature()); + assertTrue(rt.isParameterizedType()); + + ExactTypePattern etp = (ExactTypePattern) writeAndRead((ExactTypePattern)rtp); + exactType = etp.getExactType(); + + assertEquals("Pjava/util/List;",rt.getSignature()); + assertTrue(rt.isParameterizedType()); + + rt = exactType.resolve(world); + assertEquals("Pjava/util/List;",rt.getSignature()); + assertTrue(rt.isParameterizedType()); + + assertFalse("does not match List",etp.matches(javaUtilList, TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match generic List",etp.matches(javaUtilList.getGenericType(),TypePattern.STATIC).alwaysTrue()); + assertTrue("matches parameterized list",etp.matches(javaUtilListOfString,TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match parameterized list of double",etp.matches(javaUtilListOfDouble,TypePattern.STATIC).alwaysTrue()); + assertTrue("does not match String",etp.matches(javaLangString,TypePattern.STATIC).alwaysFalse()); + + } + + /** + * Foo where Foo exists and takes one bound + * Parser creates WildTypePattern namePatterns = {Foo}, typeParameters=WTP{Str*} + * resolveBindings resolves typeParameters to WTP{Str*} + * resolves Foo to RT(Foo) + * returns WildTypePattern(name = Foo, typeParameters = WTP{Str*} isGeneric=false) + */ + public void testParameterizedWildCard() { + TypePattern rtp = resolveWildTypePattern("List", false); + + assertTrue("resolves to WildTypePattern",rtp instanceof WildTypePattern); + assertTrue("one type parameter", rtp.typeParameters.size() == 1); + assertTrue("missing",ResolvedType.isMissing(rtp.getExactType())); + + WildTypePattern wtp = (WildTypePattern) writeAndRead(rtp); + assertTrue("one type parameter", wtp.typeParameters.size() == 1); + assertTrue("missing",ResolvedType.isMissing(wtp.getExactType())); + assertEquals("Str*",wtp.getTypeParameters().getTypePatterns()[0].toString()); + + assertFalse("does not match List",wtp.matches(javaUtilList, TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match generic List",wtp.matches(javaUtilList.getGenericType(),TypePattern.STATIC).alwaysTrue()); + assertTrue("matches parameterized list",wtp.matches(javaUtilListOfString,TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match parameterized list of double",wtp.matches(javaUtilListOfDouble,TypePattern.STATIC).alwaysTrue()); + assertTrue("does not match String",wtp.matches(javaLangString,TypePattern.STATIC).alwaysFalse()); + } + + /** + * Fo* + * Parser creates WildTypePattern namePatterns = {Fo*}, typeParameters=WTP{String} + * resolveBindings resolves typeParameters to ETP{String} + * returns WildTypePattern(name = Fo*, typeParameters = ETP{String} isGeneric=false) + */ + public void testWildcardParameterized() { + TypePattern rtp = resolveWildTypePattern("Li*", false); + + assertTrue("resolves to WildTypePattern",rtp instanceof WildTypePattern); + assertTrue("one type parameter", rtp.typeParameters.size() == 1); + assertEquals("Ljava/lang/String;",rtp.typeParameters.getTypePatterns()[0].getExactType().getSignature()); + + WildTypePattern wtp = (WildTypePattern) writeAndRead(rtp); + assertTrue("one type parameter", wtp.typeParameters.size() == 1); + assertEquals("Ljava/lang/String;",wtp.typeParameters.getTypePatterns()[0].getExactType().getSignature()); + + assertFalse("does not match List",wtp.matches(javaUtilList, TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match generic List",wtp.matches(javaUtilList.getGenericType(),TypePattern.STATIC).alwaysTrue()); + assertTrue("matches parameterized list",wtp.matches(javaUtilListOfString,TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match parameterized list of double",wtp.matches(javaUtilListOfDouble,TypePattern.STATIC).alwaysTrue()); + assertTrue("does not match String",wtp.matches(javaLangString,TypePattern.STATIC).alwaysFalse()); + } + + /** + * Foo + */ + public void testSomething() { + TypePattern rtp = resolveWildTypePattern("List", false); + + assertTrue("resolves to exact type",rtp instanceof ExactTypePattern); + UnresolvedType exactType = rtp.getExactType(); + assertTrue(exactType.isParameterizedType()); + assertEquals("Pjava/util/List;",exactType.getSignature()); + + ExactTypePattern etp = (ExactTypePattern) writeAndRead(rtp); + exactType = etp.getExactType(); + assertTrue(exactType.isParameterizedType()); + assertEquals("Pjava/util/List;",exactType.getSignature()); + + assertFalse("does not match List",etp.matches(javaUtilList, TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match generic List",etp.matches(javaUtilList.getGenericType(),TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match parameterized list",etp.matches(javaUtilListOfString,TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match parameterized list of double",etp.matches(javaUtilListOfDouble,TypePattern.STATIC).alwaysTrue()); + assertTrue("does not match String",etp.matches(javaLangString,TypePattern.STATIC).alwaysFalse()); + + assertTrue("matches list of something",etp.matches(javaUtilListOfSomething,TypePattern.STATIC).alwaysTrue()); + } + + /** + * Foo + */ + public void testSomethingExtends() { + TypePattern rtp = resolveWildTypePattern("List", false); + + assertTrue("resolves to exact type",rtp instanceof ExactTypePattern); + UnresolvedType exactType = rtp.getExactType(); + assertTrue(exactType.isParameterizedType()); + assertEquals("Pjava/util/List<+Ljava/lang/Number;>;",exactType.getSignature()); + assertTrue("got a bounded reference type",exactType.getTypeParameters()[0] instanceof BoundedReferenceType); + + ExactTypePattern etp = (ExactTypePattern) writeAndRead(rtp); + exactType = etp.getExactType(); + exactType = exactType.resolve(world); + assertTrue(exactType.isParameterizedType()); + assertEquals("Pjava/util/List<+Ljava/lang/Number;>;",exactType.getSignature()); + assertTrue("got a bounded reference type",exactType.getTypeParameters()[0] instanceof BoundedReferenceType); + + assertFalse("does not match List",etp.matches(javaUtilList, TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match generic List",etp.matches(javaUtilList.getGenericType(),TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match parameterized list",etp.matches(javaUtilListOfString,TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match parameterized list of double",etp.matches(javaUtilListOfDouble,TypePattern.STATIC).alwaysTrue()); + assertTrue("does not match String",etp.matches(javaLangString,TypePattern.STATIC).alwaysFalse()); + assertFalse("does not match list of something",etp.matches(javaUtilListOfSomething,TypePattern.STATIC).alwaysTrue()); + + ResolvedType listOfNumber = + TypeFactory.createParameterizedType( + javaUtilList, + new UnresolvedType[] {UnresolvedType.forName("java.lang.Number").resolve(world)}, + world); + + ResolvedType listOfDouble = + TypeFactory.createParameterizedType( + javaUtilList, + new UnresolvedType[] {UnresolvedType.forName("java.lang.Double").resolve(world)}, + world); + + assertFalse("does not match list of number",etp.matches(listOfNumber,TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match list of double",etp.matches(listOfDouble,TypePattern.STATIC).alwaysTrue()); + + ResolvedType extendsNumber = TypeFactory.createTypeFromSignature("+Ljava/lang/Number;").resolve(world); + ResolvedType listOfExtendsNumber = + TypeFactory.createParameterizedType( + javaUtilList, + new UnresolvedType[] {extendsNumber}, + world); + + assertTrue("matches list of ? extends number",etp.matches(listOfExtendsNumber,TypePattern.STATIC).alwaysTrue()); + + } + + /** + * Foo + */ + public void testSomethingExtendsPattern() { + TypePattern rtp = resolveWildTypePattern("List", false); + + assertTrue("resolves to wild type pattern",rtp instanceof WildTypePattern); + assertEquals("one type parameter",1,rtp.getTypeParameters().size()); + TypePattern tp = rtp.getTypeParameters().getTypePatterns()[0]; + assertTrue("parameter is wild",tp instanceof WildTypePattern); + WildTypePattern tpwtp = (WildTypePattern) tp; + assertEquals("?",tpwtp.getNamePatterns()[0].maybeGetSimpleName()); + assertEquals("java.lang.Number+",tpwtp.upperBound.toString()); + + + WildTypePattern wtp = (WildTypePattern) writeAndRead(rtp); + assertEquals("one type parameter",1,wtp.getTypeParameters().size()); + tp = rtp.getTypeParameters().getTypePatterns()[0]; + assertTrue("parameter is wild",tp instanceof WildTypePattern); + tpwtp = (WildTypePattern) tp; + assertEquals("?",tpwtp.getNamePatterns()[0].maybeGetSimpleName()); + assertEquals("java.lang.Number+",tpwtp.upperBound.toString()); + + assertFalse("does not match List",wtp.matches(javaUtilList, TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match generic List",wtp.matches(javaUtilList.getGenericType(),TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match parameterized list",wtp.matches(javaUtilListOfString,TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match parameterized list of double",wtp.matches(javaUtilListOfDouble,TypePattern.STATIC).alwaysTrue()); + assertTrue("does not match String",wtp.matches(javaLangString,TypePattern.STATIC).alwaysFalse()); + assertFalse("does not match list of something",wtp.matches(javaUtilListOfSomething,TypePattern.STATIC).alwaysTrue()); + + ResolvedType listOfNumber = + TypeFactory.createParameterizedType( + javaUtilList, + new UnresolvedType[] {UnresolvedType.forName("java.lang.Number").resolve(world)}, + world); + + ResolvedType listOfDouble = + TypeFactory.createParameterizedType( + javaUtilList, + new UnresolvedType[] {UnresolvedType.forName("java.lang.Double").resolve(world)}, + world); + + assertFalse("does not match list of number",wtp.matches(listOfNumber,TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match list of double",wtp.matches(listOfDouble,TypePattern.STATIC).alwaysTrue()); + + ResolvedType extendsNumber = TypeFactory.createTypeFromSignature("+Ljava/lang/Number;").resolve(world); + ResolvedType listOfExtendsNumber = + TypeFactory.createParameterizedType( + javaUtilList, + new UnresolvedType[] {extendsNumber}, + world); + + assertTrue("matches list of ? extends number",wtp.matches(listOfExtendsNumber,TypePattern.STATIC).alwaysTrue()); + + ResolvedType extendsDouble = TypeFactory.createTypeFromSignature("+Ljava/lang/Double;").resolve(world); + ResolvedType listOfExtendsDouble = + TypeFactory.createParameterizedType( + javaUtilList, + new UnresolvedType[] {extendsDouble}, + world); + + assertTrue("matches list of ? extends double",wtp.matches(listOfExtendsDouble,TypePattern.STATIC).alwaysTrue()); + + } + + + /** + * Foo + */ + public void testSomethingExtendsPatternv2() { + TypePattern rtp = resolveWildTypePattern("List", false); + + assertTrue("resolves to wild type pattern",rtp instanceof WildTypePattern); + assertEquals("one type parameter",1,rtp.getTypeParameters().size()); + TypePattern tp = rtp.getTypeParameters().getTypePatterns()[0]; + assertTrue("parameter is wild",tp instanceof WildTypePattern); + WildTypePattern tpwtp = (WildTypePattern) tp; + assertEquals("?",tpwtp.getNamePatterns()[0].maybeGetSimpleName()); + assertEquals("Num*",tpwtp.upperBound.toString()); + + + WildTypePattern wtp = (WildTypePattern) writeAndRead(rtp); + assertEquals("one type parameter",1,wtp.getTypeParameters().size()); + tp = rtp.getTypeParameters().getTypePatterns()[0]; + assertTrue("parameter is wild",tp instanceof WildTypePattern); + tpwtp = (WildTypePattern) tp; + assertEquals("?",tpwtp.getNamePatterns()[0].maybeGetSimpleName()); + assertEquals("Num*",tpwtp.upperBound.toString()); + + assertFalse("does not match List",wtp.matches(javaUtilList, TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match generic List",wtp.matches(javaUtilList.getGenericType(),TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match parameterized list",wtp.matches(javaUtilListOfString,TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match parameterized list of double",wtp.matches(javaUtilListOfDouble,TypePattern.STATIC).alwaysTrue()); + assertTrue("does not match String",wtp.matches(javaLangString,TypePattern.STATIC).alwaysFalse()); + assertFalse("does not match list of something",wtp.matches(javaUtilListOfSomething,TypePattern.STATIC).alwaysTrue()); + + ResolvedType listOfNumber = + TypeFactory.createParameterizedType( + javaUtilList, + new UnresolvedType[] {UnresolvedType.forName("java.lang.Number").resolve(world)}, + world); + + ResolvedType listOfDouble = + TypeFactory.createParameterizedType( + javaUtilList, + new UnresolvedType[] {UnresolvedType.forName("java.lang.Double").resolve(world)}, + world); + + assertFalse("does not match list of number",wtp.matches(listOfNumber,TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match list of double",wtp.matches(listOfDouble,TypePattern.STATIC).alwaysTrue()); + + ResolvedType extendsNumber = TypeFactory.createTypeFromSignature("+Ljava/lang/Number;").resolve(world); + ResolvedType listOfExtendsNumber = + TypeFactory.createParameterizedType( + javaUtilList, + new UnresolvedType[] {extendsNumber}, + world); + + assertTrue("matches list of ? extends number",wtp.matches(listOfExtendsNumber,TypePattern.STATIC).alwaysTrue()); + + ResolvedType extendsDouble = TypeFactory.createTypeFromSignature("+Ljava/lang/Double;").resolve(world); + ResolvedType listOfExtendsDouble = + TypeFactory.createParameterizedType( + javaUtilList, + new UnresolvedType[] {extendsDouble}, + world); + + assertFalse("does not match list of ? extends double",wtp.matches(listOfExtendsDouble,TypePattern.STATIC).alwaysTrue()); + } + + /** + * Foo + * + */ + public void testSomethingSuper() { + TypePattern rtp = resolveWildTypePattern("List", false); + + assertTrue("resolves to exact type",rtp instanceof ExactTypePattern); + UnresolvedType exactType = rtp.getExactType(); + assertTrue(exactType.isParameterizedType()); + assertEquals("Pjava/util/List<-Ljava/lang/Double;>;",exactType.getSignature()); + assertTrue("got a bounded reference type",exactType.getTypeParameters()[0] instanceof BoundedReferenceType); + + ExactTypePattern etp = (ExactTypePattern) writeAndRead(rtp); + exactType = etp.getExactType(); + exactType = exactType.resolve(world); + assertTrue(exactType.isParameterizedType()); + assertEquals("Pjava/util/List<-Ljava/lang/Double;>;",exactType.getSignature()); + assertTrue("got a bounded reference type",exactType.getTypeParameters()[0] instanceof BoundedReferenceType); + + assertFalse("does not match List",etp.matches(javaUtilList, TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match generic List",etp.matches(javaUtilList.getGenericType(),TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match parameterized list",etp.matches(javaUtilListOfString,TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match parameterized list of double",etp.matches(javaUtilListOfDouble,TypePattern.STATIC).alwaysTrue()); + assertTrue("does not match String",etp.matches(javaLangString,TypePattern.STATIC).alwaysFalse()); + assertFalse("does not match list of something",etp.matches(javaUtilListOfSomething,TypePattern.STATIC).alwaysTrue()); + + ResolvedType listOfNumber = + TypeFactory.createParameterizedType( + javaUtilList, + new UnresolvedType[] {UnresolvedType.forName("java.lang.Number").resolve(world)}, + world); + + ResolvedType listOfDouble = + TypeFactory.createParameterizedType( + javaUtilList, + new UnresolvedType[] {UnresolvedType.forName("java.lang.Double").resolve(world)}, + world); + + assertFalse("does not match list of number",etp.matches(listOfNumber,TypePattern.STATIC).alwaysTrue()); + assertFalse("does not match list of double",etp.matches(listOfDouble,TypePattern.STATIC).alwaysTrue()); + + ResolvedType superDouble = TypeFactory.createTypeFromSignature("-Ljava/lang/Double;").resolve(world); + ResolvedType listOfSuperDouble = + TypeFactory.createParameterizedType( + javaUtilList, + new UnresolvedType[] {superDouble}, + world); + + assertTrue("matches list of ? super double",etp.matches(listOfSuperDouble,TypePattern.STATIC).alwaysTrue()); + } + + private TypePattern resolveWildTypePattern(String source, boolean requireExact) { + WildTypePattern wtp = makeWildTypePattern(source); + return wtp.resolveBindings(scope, bindings, false, requireExact); + } + + private WildTypePattern makeWildTypePattern(String source) { + PatternParser parser = new PatternParser(source); + return (WildTypePattern) parser.parseTypePattern(); + } + + private TypePattern writeAndRead(TypePattern etp) { + try { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(baos); + etp.write(dos); + dos.flush(); + dos.close(); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + VersionedDataInputStream in = new VersionedDataInputStream(bais); + in.setVersion(new WeaverVersionInfo()); + TypePattern ret = TypePattern.read(in, null); + return ret; + } catch (IOException ioEx) { + fail(ioEx + " thrown during serialization"); + } + return null; + } + + protected void setUp() throws Exception { + super.setUp(); + this.world = new BcelWorld(); + this.world.setBehaveInJava5Way(true); + this.bindings = new Bindings(0); + this.scope = new SimpleScope(world , new FormalBinding[] {}); + this.scope.setImportedPrefixes(new String[] {"java.io.","java.util.","java.lang."}); + this.javaLangString = UnresolvedType.forName("java.lang.String").resolve(world); + this.javaUtilList = UnresolvedType.forName("java.util.List").resolve(world); + this.javaUtilListOfString = TypeFactory.createParameterizedType(javaUtilList, new UnresolvedType[] {javaLangString}, world); + this.javaUtilListOfDouble = + TypeFactory.createParameterizedType( + javaUtilList, + new UnresolvedType[] {UnresolvedType.forName("java.lang.Double").resolve(world)}, + world); + this.javaUtilListOfSomething = + TypeFactory.createParameterizedType( + javaUtilList, + new UnresolvedType[] {UnresolvedType.SOMETHING.resolve(world)}, + world); + } +} diff --git a/weaver5/java5-testsrc/org/aspectj/weaver/reflect/ReflectionBasedReferenceTypeDelegateTest.java b/weaver5/java5-testsrc/org/aspectj/weaver/reflect/ReflectionBasedReferenceTypeDelegateTest.java new file mode 100644 index 000000000..f9bef9cc9 --- /dev/null +++ b/weaver5/java5-testsrc/org/aspectj/weaver/reflect/ReflectionBasedReferenceTypeDelegateTest.java @@ -0,0 +1,306 @@ +/* ******************************************************************* + * Copyright (c) 2005 Contributors. + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution and is available at + * http://eclipse.org/legal/epl-v10.html + * + * Contributors: + * Adrian Colyer Initial implementation + * ******************************************************************/ +package org.aspectj.weaver.reflect; + + +import java.util.HashSet; +import java.util.Set; + +import junit.framework.TestCase; + +import org.aspectj.bridge.IMessageHandler; +import org.aspectj.weaver.ReferenceType; +import org.aspectj.weaver.ResolvedMember; +import org.aspectj.weaver.ResolvedType; +import org.aspectj.weaver.UnresolvedType; +import org.aspectj.weaver.bcel.BcelWorld; + +public abstract class ReflectionBasedReferenceTypeDelegateTest extends TestCase { + + protected ReflectionWorld world; + private ResolvedType objectType; + private ResolvedType classType; + + public void testIsAspect() { + assertFalse(objectType.isAspect()); + } + + public void testIsAnnotationStyleAspect() { + assertFalse(objectType.isAnnotationStyleAspect()); + } + + public void testIsInterface() { + assertFalse(objectType.isInterface()); + assertTrue(world.resolve("java.io.Serializable").isInterface()); + } + + public void testIsEnum() { + assertFalse(objectType.isEnum()); + } + + public void testIsAnnotation() { + assertFalse(objectType.isAnnotation()); + } + + public void testIsAnnotationWithRuntimeRetention() { + assertFalse(objectType.isAnnotationWithRuntimeRetention()); + } + + public void testIsClass() { + assertTrue(objectType.isClass()); + assertFalse(world.resolve("java.io.Serializable").isClass()); + } + + public void testIsGeneric() { + assertFalse(objectType.isGenericType()); + } + + public void testIsExposedToWeaver() { + assertFalse(objectType.isExposedToWeaver()); + } + + public void testHasAnnotation() { + assertFalse(objectType.hasAnnotation(UnresolvedType.forName("Foo"))); + } + + public void testGetAnnotations() { + assertEquals("no entries",0,objectType.getAnnotations().length); + } + + public void testGetAnnotationTypes() { + assertEquals("no entries",0,objectType.getAnnotationTypes().length); + } + + public void testGetTypeVariables() { + assertEquals("no entries",0,objectType.getTypeVariables().length); + } + + public void testGetPerClause() { + assertNull(objectType.getPerClause()); + } + + public void testGetModifiers() { + assertEquals(Object.class.getModifiers(),objectType.getModifiers()); + } + + public void testGetSuperclass() { + assertTrue("Superclass of object should be null, but it is: "+objectType.getSuperclass(),objectType.getSuperclass()==null); + assertEquals(objectType,world.resolve("java.lang.Class").getSuperclass()); + ResolvedType d = world.resolve("reflect.tests.D"); + assertEquals(world.resolve("reflect.tests.C"),d.getSuperclass()); + } + + + protected int findMethod(String name, ResolvedMember[] methods) { + for (int i=0; i -1); + + assertEquals(world.resolve("java.lang.String"),methods[idx].getReturnType()); + assertEquals(1, methods[idx].getParameterTypes().length); + assertEquals(objectType,methods[idx].getParameterTypes()[0]); + assertEquals(1,methods[idx].getExceptions().length); + assertEquals(world.resolve("java.lang.Exception"),methods[idx].getExceptions()[0]); + int baridx = findMethod("bar", methods); + int initidx = findMethod("", methods); + assertTrue(baridx > -1); + assertTrue(initidx > -1); + assertTrue(baridx != initidx && baridx != idx && idx <= 2 && initidx <= 2 && baridx <= 2); + + ResolvedType d = world.resolve("reflect.tests.D"); + methods = d.getDeclaredMethods(); + assertEquals(2,methods.length); + + classType = world.resolve("java.lang.Class"); + methods = classType.getDeclaredMethods(); + assertEquals(Class.class.getDeclaredMethods().length + Class.class.getDeclaredConstructors().length, methods.length); + } + + public void testGetDeclaredFields() { + ResolvedMember[] fields = objectType.getDeclaredFields(); + assertEquals(0,fields.length); + + ResolvedType c = world.resolve("reflect.tests.C"); + fields = c.getDeclaredFields(); + + assertEquals(2,fields.length); + assertEquals("f",fields[0].getName()); + assertEquals("s",fields[1].getName()); + assertEquals(ResolvedType.INT,fields[0].getReturnType()); + assertEquals(world.resolve("java.lang.String"),fields[1].getReturnType()); + } + + public void testGetDeclaredInterfaces() { + ResolvedType[] interfaces = objectType.getDeclaredInterfaces(); + assertEquals(0,interfaces.length); + + ResolvedType d = world.resolve("reflect.tests.D"); + interfaces = d.getDeclaredInterfaces(); + assertEquals(1,interfaces.length); + assertEquals(world.resolve("java.io.Serializable"),interfaces[0]); + } + + public void testGetDeclaredPointcuts() { + ResolvedMember[] pointcuts = objectType.getDeclaredPointcuts(); + assertEquals(0,pointcuts.length); + } + + + + public void testSerializableSuperclass() { + ResolvedType serializableType = world.resolve("java.io.Serializable"); + ResolvedType superType = serializableType.getSuperclass(); + assertTrue("Superclass of serializable should be Object but was "+superType,superType.equals(UnresolvedType.OBJECT)); + + BcelWorld bcelworld = new BcelWorld(); + bcelworld.setBehaveInJava5Way(true); + ResolvedType bcelSupertype = bcelworld.resolve(UnresolvedType.SERIALIZABLE).getSuperclass(); + assertTrue("Should be null but is "+bcelSupertype,bcelSupertype.equals(UnresolvedType.OBJECT)); + } + + public void testSubinterfaceSuperclass() { + ResolvedType ifaceType = world.resolve("java.security.Key"); + ResolvedType superType = ifaceType.getSuperclass(); + assertTrue("Superclass should be Object but was "+superType,superType.equals(UnresolvedType.OBJECT)); + + BcelWorld bcelworld = new BcelWorld(); + bcelworld.setBehaveInJava5Way(true); + ResolvedType bcelSupertype = bcelworld.resolve("java.security.Key").getSuperclass(); + assertTrue("Should be null but is "+bcelSupertype,bcelSupertype.equals(UnresolvedType.OBJECT)); + } + + public void testVoidSuperclass() { + ResolvedType voidType = world.resolve(Void.TYPE); + ResolvedType superType = voidType.getSuperclass(); + assertNull(superType); + + BcelWorld bcelworld = new BcelWorld(); + bcelworld.setBehaveInJava5Way(true); + ResolvedType bcelSupertype = bcelworld.resolve("void").getSuperclass(); + assertTrue("Should be null but is "+bcelSupertype,bcelSupertype==null); + } + + public void testIntSuperclass() { + ResolvedType voidType = world.resolve(Integer.TYPE); + ResolvedType superType = voidType.getSuperclass(); + assertNull(superType); + + BcelWorld bcelworld = new BcelWorld(); + bcelworld.setBehaveInJava5Way(true); + ResolvedType bcelSupertype = bcelworld.resolve("int").getSuperclass(); + assertTrue("Should be null but is "+bcelSupertype,bcelSupertype==null); + } + + public void testGenericInterfaceSuperclass_BcelWorldResolution() { + BcelWorld bcelworld = new BcelWorld(); + bcelworld.setBehaveInJava5Way(true); + + UnresolvedType javaUtilMap = UnresolvedType.forName("java.util.Map"); + + ReferenceType rawType = (ReferenceType) bcelworld.resolve(javaUtilMap); + assertTrue("Should be the raw type ?!? "+rawType.getTypekind(),rawType.isRawType()); + + ReferenceType genericType = (ReferenceType)rawType.getGenericType(); + assertTrue("Should be the generic type ?!? "+genericType.getTypekind(),genericType.isGenericType()); + + ResolvedType rt = rawType.getSuperclass(); + assertTrue("Superclass for Map raw type should be Object but was "+rt,rt.equals(UnresolvedType.OBJECT)); + + ResolvedType rt2 = genericType.getSuperclass(); + assertTrue("Superclass for Map generic type should be Object but was "+rt2,rt2.equals(UnresolvedType.OBJECT)); + } + + // FIXME asc maybe. The reflection list of methods returned doesn't include (the static initializer) ... is that really a problem. + public void testCompareSubclassDelegates() { + + boolean barfIfClinitMissing = false; + world.setBehaveInJava5Way(true); + + BcelWorld bcelWorld = new BcelWorld(getClass().getClassLoader(),IMessageHandler.THROW,null); + bcelWorld.setBehaveInJava5Way(true); + UnresolvedType javaUtilHashMap = UnresolvedType.forName("java.util.HashMap"); + ReferenceType rawType =(ReferenceType)bcelWorld.resolve(javaUtilHashMap ); + + ReferenceType rawReflectType =(ReferenceType)world.resolve(javaUtilHashMap ); + ResolvedMember[] rms1 = rawType.getDelegate().getDeclaredMethods(); + ResolvedMember[] rms2 = rawReflectType.getDelegate().getDeclaredMethods(); + StringBuffer errors = new StringBuffer(); + Set one = new HashSet(); + for (int i = 0; i < rms1.length; i++) { + one.add(rms1[i].toString()); + } + Set two = new HashSet(); + for (int i = 0; i < rms2.length; i++) { + two.add(rms2[i].toString()); + } + for (int i = 0;i")) continue; + errors.append("Couldn't find "+rms1[i].toString()+" in the reflection set\n"); + } + } + assertTrue("Errors:"+errors.toString(),errors.length()==0); + + // the good old ibm vm seems to offer clinit through its reflection support (see pr145322) + if (rms1.length==rms2.length) return; + if (barfIfClinitMissing) { + // the numbers must be exact + assertEquals(rms1.length,rms2.length); + } else { + // the numbers can be out by one in favour of bcel + if (rms1.length!=(rms2.length+1)) { + for (int i = 0; i < rms1.length; i++) { + System.err.println("bcel"+i+" is "+rms1[i]); + } + for (int i = 0; i < rms2.length; i++) { + System.err.println("refl"+i+" is "+rms2[i]); + } + } + assertTrue("Should be one extra (clinit) in BCEL case, but bcel="+rms1.length+" reflect="+rms2.length,rms1.length==rms2.length+1); + } + } + + // todo: array of int + + protected void setUp() throws Exception { + world = new ReflectionWorld(getClass().getClassLoader()); + objectType = world.resolve("java.lang.Object"); + } +} diff --git a/weaver5/java5-testsrc/org/aspectj/weaver/reflect/ReflectionWorldTest.java b/weaver5/java5-testsrc/org/aspectj/weaver/reflect/ReflectionWorldTest.java new file mode 100644 index 000000000..39c5ccb5c --- /dev/null +++ b/weaver5/java5-testsrc/org/aspectj/weaver/reflect/ReflectionWorldTest.java @@ -0,0 +1,41 @@ +/* ******************************************************************* + * Copyright (c) 2005 Contributors. + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution and is available at + * http://eclipse.org/legal/epl-v10.html + * + * Contributors: + * Adrian Colyer Initial implementation + * ******************************************************************/ +package org.aspectj.weaver.reflect; + +import junit.framework.TestCase; + +import org.aspectj.weaver.ResolvedType; +import org.aspectj.weaver.World; + +public class ReflectionWorldTest extends TestCase { + + public void testDelegateCreation() { + World world = new ReflectionWorld(getClass().getClassLoader()); + ResolvedType rt = world.resolve("java.lang.Object"); + assertNotNull(rt); + assertEquals("Ljava/lang/Object;",rt.getSignature()); + } + + public void testArrayTypes() { + IReflectionWorld world = new ReflectionWorld(getClass().getClassLoader()); + String[] strArray = new String[1]; + ResolvedType rt = world.resolve(strArray.getClass()); + assertTrue(rt.isArray()); + } + + public void testPrimitiveTypes() { + IReflectionWorld world = new ReflectionWorld(getClass().getClassLoader()); + assertEquals("int",ResolvedType.INT,world.resolve(int.class)); + assertEquals("void",ResolvedType.VOID,world.resolve(void.class)); + } + +} diff --git a/weaver5/testsrc/org/aspectj/weaver/Weaver5ModuleTests.java b/weaver5/testsrc/org/aspectj/weaver/Weaver5ModuleTests.java index a72e34356..96bc6d007 100644 --- a/weaver5/testsrc/org/aspectj/weaver/Weaver5ModuleTests.java +++ b/weaver5/testsrc/org/aspectj/weaver/Weaver5ModuleTests.java @@ -16,18 +16,15 @@ import junit.framework.TestCase; import junit.framework.TestSuite; import org.aspectj.testing.util.TestUtil; -import org.aspectj.weaver.tools.PointcutExpressionTest; /** */ public class Weaver5ModuleTests extends TestCase { public static Test suite() { - TestSuite suite = new TestSuite(Weaver5ModuleTests.class.getName()); + TestSuite suite = new TestSuite(Weaver5ModuleTests.class.getName()); if (TestUtil.is15VMOrGreater()) { - TestUtil.loadTestsReflectively(suite, "org.aspectj.weaver.tools.Java15PointcutExpressionTest", false); - TestUtil.loadTestsReflectively(suite, "org.aspectj.weaver.AllTracing5Tests", false); - suite.addTestSuite(PointcutExpressionTest.class); + TestUtil.loadTestsReflectively(suite, "org.aspectj.weaver.AllWeaver5Tests", false); } else { suite.addTest(TestUtil.testNamed("all tests require 1.5")); } diff --git a/weaver5/testsrc/org/aspectj/weaver/tools/PointcutExpressionTest.java b/weaver5/testsrc/org/aspectj/weaver/tools/PointcutExpressionTest.java deleted file mode 100644 index ac1897a61..000000000 --- a/weaver5/testsrc/org/aspectj/weaver/tools/PointcutExpressionTest.java +++ /dev/null @@ -1,570 +0,0 @@ -/* ******************************************************************* - * Copyright (c) 2005 Contributors. - * All rights reserved. - * This program and the accompanying materials are made available - * under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution and is available at - * http://eclipse.org/legal/epl-v10.html - * - * Contributors: - * Andrew Clement Initial implementation - * ******************************************************************/ - -package org.aspectj.weaver.tools; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.Method; - -import org.aspectj.util.LangUtil; - -import junit.framework.TestCase; - -public class PointcutExpressionTest extends TestCase { - - PointcutParser p; - Constructor asCons; - Constructor bsCons; - Constructor bsStringCons; - Constructor clientCons; - Method a; - Method aa; - Method aaa; - Field x; - Field y; - Method b; - Method bsaa; - Field n; - Method foo; - Method bar; - - public void testMatchesMethodCall() { - PointcutExpression ex = p.parsePointcutExpression("call(* *..A.a*(..))"); - assertTrue("Should match call to A.a()",ex.matchesMethodCall(a,a).alwaysMatches()); - assertTrue("Should match call to A.aaa()",ex.matchesMethodCall(aaa,a).alwaysMatches()); - assertTrue("Should match call to B.aa()",ex.matchesMethodCall(bsaa,a).alwaysMatches()); - assertTrue("Should not match call to B.b()",ex.matchesMethodCall(b,a).neverMatches()); - ex = p.parsePointcutExpression("call(* *..A.a*(int))"); - assertTrue("Should match call to A.aa()",ex.matchesMethodCall(aa,a).alwaysMatches()); - assertTrue("Should not match call to A.a()",ex.matchesMethodCall(a,a).neverMatches()); - ex = p.parsePointcutExpression("call(void aaa(..)) && this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)"); - assertTrue("Should match call to A.aaa() from Client",ex.matchesMethodCall(aaa,foo).alwaysMatches()); - ex = p.parsePointcutExpression("call(void aaa(..)) && this(org.aspectj.weaver.tools.PointcutExpressionTest.B)"); - assertTrue("Should match call to A.aaa() from B",ex.matchesMethodCall(aaa,b).alwaysMatches()); - assertTrue("May match call to A.aaa() from A",ex.matchesMethodCall(aaa,a).maybeMatches()); - assertFalse("May match call to A.aaa() from A",ex.matchesMethodCall(aaa,a).alwaysMatches()); - ex = p.parsePointcutExpression("execution(* *.*(..))"); - assertTrue("Should not match call to A.aa",ex.matchesMethodCall(aa,a).neverMatches()); - // this - ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)"); - assertTrue("Should match Client",ex.matchesMethodCall(a,foo).alwaysMatches()); - assertTrue("Should not match A",ex.matchesMethodCall(a,a).neverMatches()); - ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)"); - assertTrue("Should maybe match B",ex.matchesMethodCall(bsaa,a).maybeMatches()); - assertFalse("Should maybe match B",ex.matchesMethodCall(bsaa,a).alwaysMatches()); - // target - ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.Client)"); - assertTrue("Should not match Client",ex.matchesMethodCall(a,a).neverMatches()); - ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)"); - assertTrue("Should match A",ex.matchesMethodCall(a,a).alwaysMatches()); - ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)"); - assertTrue("Should maybe match A",ex.matchesMethodCall(aa,a).maybeMatches()); - assertFalse("Should maybe match A",ex.matchesMethodCall(aa,a).alwaysMatches()); - // test args - ex = p.parsePointcutExpression("args(..,int)"); - assertTrue("Should match A.aa",ex.matchesMethodCall(aa,a).alwaysMatches()); - assertTrue("Should match A.aaa",ex.matchesMethodCall(aaa,a).alwaysMatches()); - assertTrue("Should not match A.a",ex.matchesMethodCall(a,a).neverMatches()); - // within - ex = p.parsePointcutExpression("within(*..A)"); - assertTrue("Matches in class A",ex.matchesMethodCall(a,a).alwaysMatches()); - assertTrue("Does not match in class B",ex.matchesMethodCall(a,b).neverMatches()); - assertTrue("Matches in class A",ex.matchesMethodCall(a,A.class).alwaysMatches()); - assertTrue("Does not match in class B",ex.matchesMethodCall(a,B.class).neverMatches()); - // withincode - ex = p.parsePointcutExpression("withincode(* a*(..))"); - assertTrue("Should match",ex.matchesMethodCall(b,bsaa).alwaysMatches()); - assertTrue("Should not match",ex.matchesMethodCall(b,b).neverMatches()); - } - - public void testMatchesMethodExecution() { - PointcutExpression ex = p.parsePointcutExpression("execution(* *..A.aa(..))"); - assertTrue("Should match execution of A.aa",ex.matchesMethodExecution(aa).alwaysMatches()); - assertTrue("Should match execution of B.aa",ex.matchesMethodExecution(bsaa).alwaysMatches()); - assertTrue("Should not match execution of A.a",ex.matchesMethodExecution(a).neverMatches()); - ex = p.parsePointcutExpression("call(* *..A.a*(int))"); - assertTrue("Should not match execution of A.a",ex.matchesMethodExecution(a).neverMatches()); - - // test this - ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)"); - assertTrue("Should match A",ex.matchesMethodExecution(a).alwaysMatches()); - ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)"); - assertTrue("Maybe matches B",ex.matchesMethodExecution(a).maybeMatches()); - assertFalse("Maybe matches B",ex.matchesMethodExecution(a).alwaysMatches()); - - // test target - ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)"); - assertTrue("Should match A",ex.matchesMethodExecution(a).alwaysMatches()); - ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)"); - assertTrue("Maybe matches B",ex.matchesMethodExecution(a).maybeMatches()); - assertFalse("Maybe matches B",ex.matchesMethodExecution(a).alwaysMatches()); - - // test args - ex = p.parsePointcutExpression("args(..,int)"); - assertTrue("Should match A.aa",ex.matchesMethodExecution(aa).alwaysMatches()); - assertTrue("Should match A.aaa",ex.matchesMethodExecution(aaa).alwaysMatches()); - assertTrue("Should not match A.a",ex.matchesMethodExecution(a).neverMatches()); - - // within - ex = p.parsePointcutExpression("within(*..A)"); - assertTrue("Matches in class A",ex.matchesMethodExecution(a).alwaysMatches()); - assertTrue("Does not match in class B",ex.matchesMethodExecution(bsaa).neverMatches()); - - // withincode - ex = p.parsePointcutExpression("withincode(* a*(..))"); - assertTrue("Should not match",ex.matchesMethodExecution(a).neverMatches()); - } - - public void testMatchesConstructorCall() { - PointcutExpression ex = p.parsePointcutExpression("call(new(String))"); - assertTrue("Should match A(String)", ex.matchesConstructorCall(asCons,b).alwaysMatches()); - assertTrue("Should match B(String)", ex.matchesConstructorCall(bsStringCons,b).alwaysMatches()); - assertTrue("Should not match B()",ex.matchesConstructorCall(bsCons,foo).neverMatches()); - ex = p.parsePointcutExpression("call(*..A.new(String))"); - assertTrue("Should match A(String)", ex.matchesConstructorCall(asCons,b).alwaysMatches()); - assertTrue("Should not match B(String)",ex.matchesConstructorCall(bsStringCons,foo).neverMatches()); - // this - ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)"); - assertTrue("Should match Client",ex.matchesConstructorCall(asCons,foo).alwaysMatches()); - assertTrue("Should not match A",ex.matchesConstructorCall(asCons,a).neverMatches()); - ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)"); - assertTrue("Should maybe match B",ex.matchesConstructorCall(asCons,a).maybeMatches()); - assertFalse("Should maybe match B",ex.matchesConstructorCall(asCons,a).alwaysMatches()); - // target - ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.Client)"); - assertTrue("Should not match Client",ex.matchesConstructorCall(asCons,foo).neverMatches()); - ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)"); - assertTrue("Should not match A (no target)",ex.matchesConstructorCall(asCons,a).neverMatches()); - // args - ex = p.parsePointcutExpression("args(String)"); - assertTrue("Should match A(String)", ex.matchesConstructorCall(asCons,b).alwaysMatches()); - assertTrue("Should match B(String)", ex.matchesConstructorCall(bsStringCons,foo).alwaysMatches()); - assertTrue("Should not match B()", ex.matchesConstructorCall(bsCons,foo).neverMatches()); - // within - ex = p.parsePointcutExpression("within(*..A)"); - assertTrue("Matches in class A",ex.matchesConstructorCall(asCons,a).alwaysMatches()); - assertTrue("Does not match in class B",ex.matchesConstructorCall(asCons,b).neverMatches()); - // withincode - ex = p.parsePointcutExpression("withincode(* a*(..))"); - assertTrue("Should match",ex.matchesConstructorCall(bsCons,aa).alwaysMatches()); - assertTrue("Should not match",ex.matchesConstructorCall(bsCons,b).neverMatches()); - } - - public void testMatchesConstructorExecution() { - PointcutExpression ex = p.parsePointcutExpression("execution(new(String))"); - assertTrue("Should match A(String)", ex.matchesConstructorExecution(asCons).alwaysMatches()); - assertTrue("Should match B(String)", ex.matchesConstructorExecution(bsStringCons).alwaysMatches()); - assertTrue("Should not match B()", ex.matchesConstructorExecution(bsCons).neverMatches()); - ex = p.parsePointcutExpression("execution(*..A.new(String))"); - assertTrue("Should match A(String)",ex.matchesConstructorExecution(asCons).alwaysMatches()); - assertTrue("Should not match B(String)",ex.matchesConstructorExecution(bsStringCons).neverMatches()); - - // test this - ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)"); - assertTrue("Should match A",ex.matchesConstructorExecution(asCons).alwaysMatches()); - ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)"); - assertTrue("Maybe matches B",ex.matchesConstructorExecution(asCons).maybeMatches()); - assertFalse("Maybe matches B",ex.matchesConstructorExecution(asCons).alwaysMatches()); - assertTrue("Should match B",ex.matchesConstructorExecution(bsCons).alwaysMatches()); - assertTrue("Does not match client",ex.matchesConstructorExecution(clientCons).neverMatches()); - - // test target - ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)"); - assertTrue("Should match A",ex.matchesConstructorExecution(asCons).alwaysMatches()); - ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)"); - assertTrue("Maybe matches B",ex.matchesConstructorExecution(asCons).maybeMatches()); - assertFalse("Maybe matches B",ex.matchesConstructorExecution(asCons).alwaysMatches()); - assertTrue("Should match B",ex.matchesConstructorExecution(bsCons).alwaysMatches()); - assertTrue("Does not match client",ex.matchesConstructorExecution(clientCons).neverMatches()); - - // within - ex = p.parsePointcutExpression("within(*..A)"); - assertTrue("Matches in class A",ex.matchesConstructorExecution(asCons).alwaysMatches()); - assertTrue("Does not match in class B",ex.matchesConstructorExecution(bsCons).neverMatches()); - - // withincode - ex = p.parsePointcutExpression("withincode(* a*(..))"); - assertTrue("Does not match",ex.matchesConstructorExecution(bsCons).neverMatches()); - - // args - ex = p.parsePointcutExpression("args(String)"); - assertTrue("Should match A(String)",ex.matchesConstructorExecution(asCons).alwaysMatches()); - assertTrue("Should match B(String)", ex.matchesConstructorExecution(bsStringCons).alwaysMatches()); - assertTrue("Should not match B()", ex.matchesConstructorExecution(bsCons).neverMatches()); - } - - public void testMatchesAdviceExecution() { - PointcutExpression ex = p.parsePointcutExpression("adviceexecution()"); - assertTrue("Should match (advice) A.a",ex.matchesAdviceExecution(a).alwaysMatches()); - // test this - ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)"); - assertTrue("Should match Client",ex.matchesAdviceExecution(foo).alwaysMatches()); - ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)"); - assertTrue("Maybe matches B",ex.matchesAdviceExecution(a).maybeMatches()); - assertFalse("Maybe matches B",ex.matchesAdviceExecution(a).alwaysMatches()); - assertTrue("Does not match client",ex.matchesAdviceExecution(foo).neverMatches()); - - // test target - ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.Client)"); - assertTrue("Should match Client",ex.matchesAdviceExecution(foo).alwaysMatches()); - ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)"); - assertTrue("Maybe matches B",ex.matchesAdviceExecution(a).maybeMatches()); - assertFalse("Maybe matches B",ex.matchesAdviceExecution(a).alwaysMatches()); - assertTrue("Does not match client",ex.matchesAdviceExecution(foo).neverMatches()); - - // test within - ex = p.parsePointcutExpression("within(*..A)"); - assertTrue("Matches in class A",ex.matchesAdviceExecution(a).alwaysMatches()); - assertTrue("Does not match in class B",ex.matchesAdviceExecution(b).neverMatches()); - - // withincode - ex = p.parsePointcutExpression("withincode(* a*(..))"); - assertTrue("Does not match",ex.matchesAdviceExecution(a).neverMatches()); - - // test args - ex = p.parsePointcutExpression("args(..,int)"); - assertTrue("Should match A.aa",ex.matchesAdviceExecution(aa).alwaysMatches()); - assertTrue("Should match A.aaa",ex.matchesAdviceExecution(aaa).alwaysMatches()); - assertTrue("Should not match A.a",ex.matchesAdviceExecution(a).neverMatches()); - } - - public void testMatchesHandler() { - PointcutExpression ex = p.parsePointcutExpression("handler(Exception)"); - assertTrue("Should match catch(Exception)",ex.matchesHandler(Exception.class,Client.class).alwaysMatches()); - assertTrue("Should not match catch(Throwable)",ex.matchesHandler(Throwable.class,Client.class).neverMatches()); - // test this - ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)"); - assertTrue("Should match Client",ex.matchesHandler(Exception.class,foo).alwaysMatches()); - ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)"); - assertTrue("Maybe matches B",ex.matchesHandler(Exception.class,a).maybeMatches()); - assertFalse("Maybe matches B",ex.matchesHandler(Exception.class,a).alwaysMatches()); - assertTrue("Does not match client",ex.matchesHandler(Exception.class,foo).neverMatches()); - // target - no target for exception handlers - ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.Client)"); - assertTrue("Should match Client",ex.matchesHandler(Exception.class,foo).neverMatches()); - // args - ex = p.parsePointcutExpression("args(Exception)"); - assertTrue("Should match Exception",ex.matchesHandler(Exception.class,foo).alwaysMatches()); - assertTrue("Should match RuntimeException",ex.matchesHandler(RuntimeException.class,foo).alwaysMatches()); - assertTrue("Should not match String",ex.matchesHandler(String.class,foo).neverMatches()); - assertTrue("Maybe matches Throwable",ex.matchesHandler(Throwable.class,foo).maybeMatches()); - assertFalse("Maybe matches Throwable",ex.matchesHandler(Throwable.class,foo).alwaysMatches()); - // within - ex = p.parsePointcutExpression("within(*..Client)"); - assertTrue("Matches in class Client",ex.matchesHandler(Exception.class,foo).alwaysMatches()); - assertTrue("Does not match in class B",ex.matchesHandler(Exception.class,b).neverMatches()); - // withincode - ex = p.parsePointcutExpression("withincode(* a*(..))"); - assertTrue("Matches within aa",ex.matchesHandler(Exception.class,aa).alwaysMatches()); - assertTrue("Does not match within b",ex.matchesHandler(Exception.class,b).neverMatches()); - } - - public void testMatchesInitialization() { - PointcutExpression ex = p.parsePointcutExpression("initialization(new(String))"); - assertTrue("Should match A(String)",ex.matchesInitialization(asCons).alwaysMatches()); - assertTrue("Should match B(String)", ex.matchesInitialization(bsStringCons).alwaysMatches()); - assertTrue("Should not match B()",ex.matchesInitialization(bsCons).neverMatches()); - ex = p.parsePointcutExpression("initialization(*..A.new(String))"); - assertTrue("Should match A(String)", ex.matchesInitialization(asCons).alwaysMatches()); - assertTrue("Should not match B(String)", ex.matchesInitialization(bsStringCons).neverMatches()); - // test this - ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)"); - assertTrue("Should match A",ex.matchesInitialization(asCons).alwaysMatches()); - ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)"); - assertTrue("Maybe matches B",ex.matchesInitialization(asCons).maybeMatches()); - assertFalse("Maybe matches B",ex.matchesInitialization(asCons).alwaysMatches()); - - // test target - ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)"); - assertTrue("Should match A",ex.matchesInitialization(asCons).alwaysMatches()); - ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)"); - assertTrue("Maybe matches B",ex.matchesInitialization(asCons).maybeMatches()); - assertFalse("Maybe matches B",ex.matchesInitialization(asCons).alwaysMatches()); - // within - ex = p.parsePointcutExpression("within(*..A)"); - assertTrue("Matches in class A",ex.matchesInitialization(asCons).alwaysMatches()); - assertTrue("Does not match in class B",ex.matchesInitialization(bsCons).neverMatches()); - // withincode - ex = p.parsePointcutExpression("withincode(* a*(..))"); - assertTrue("Does not match",ex.matchesInitialization(bsCons).neverMatches()); - // args - ex = p.parsePointcutExpression("args(String)"); - assertTrue("Should match A(String)", ex.matchesInitialization(asCons).alwaysMatches()); - assertTrue("Should match B(String)", ex.matchesInitialization(bsStringCons).alwaysMatches()); - assertTrue("Should not match B()",ex.matchesInitialization(bsCons).neverMatches()); - } - - public void testMatchesPreInitialization() { - PointcutExpression ex = p.parsePointcutExpression("preinitialization(new(String))"); - assertTrue("Should match A(String)",ex.matchesPreInitialization(asCons).alwaysMatches()); - assertTrue("Should match B(String)", ex.matchesPreInitialization(bsStringCons).alwaysMatches()); - assertTrue("Should not match B()",ex.matchesPreInitialization(bsCons).neverMatches()); - ex = p.parsePointcutExpression("preinitialization(*..A.new(String))"); - assertTrue("Should match A(String)", ex.matchesPreInitialization(asCons).alwaysMatches()); - assertTrue("Should not match B(String)", ex.matchesPreInitialization(bsStringCons).neverMatches()); - // test this - ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)"); - assertTrue("No match, no this at preinit",ex.matchesPreInitialization(asCons).neverMatches()); - - // test target - ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)"); - assertTrue("No match, no target at preinit",ex.matchesPreInitialization(asCons).neverMatches()); - - // within - ex = p.parsePointcutExpression("within(*..A)"); - assertTrue("Matches in class A",ex.matchesPreInitialization(asCons).alwaysMatches()); - assertTrue("Does not match in class B",ex.matchesPreInitialization(bsCons).neverMatches()); - // withincode - ex = p.parsePointcutExpression("withincode(* a*(..))"); - assertTrue("Does not match",ex.matchesPreInitialization(bsCons).neverMatches()); - // args - ex = p.parsePointcutExpression("args(String)"); - assertTrue("Should match A(String)", ex.matchesPreInitialization(asCons).alwaysMatches()); - assertTrue("Should match B(String)", ex.matchesPreInitialization(bsStringCons).alwaysMatches()); - assertTrue("Should not match B()",ex.matchesPreInitialization(bsCons).neverMatches()); - } - - public void testMatchesStaticInitialization() { - // staticinit - PointcutExpression ex = p.parsePointcutExpression("staticinitialization(*..A+)"); - assertTrue("Matches A",ex.matchesStaticInitialization(A.class).alwaysMatches()); - assertTrue("Matches B",ex.matchesStaticInitialization(B.class).alwaysMatches()); - assertTrue("Doesn't match Client",ex.matchesStaticInitialization(Client.class).neverMatches()); - // this - ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)"); - assertTrue("No this",ex.matchesStaticInitialization(A.class).neverMatches()); - // target - ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)"); - assertTrue("No target",ex.matchesStaticInitialization(A.class).neverMatches()); - - // args - ex = p.parsePointcutExpression("args()"); - assertTrue("No args",ex.matchesStaticInitialization(A.class).alwaysMatches()); - ex = p.parsePointcutExpression("args(String)"); - assertTrue("No args",ex.matchesStaticInitialization(A.class).neverMatches()); - - // within - ex = p.parsePointcutExpression("within(*..A)"); - assertTrue("Matches in class A",ex.matchesStaticInitialization(A.class).alwaysMatches()); - assertTrue("Does not match in class B",ex.matchesStaticInitialization(B.class).neverMatches()); - - // withincode - ex = p.parsePointcutExpression("withincode(* a*(..))"); - assertTrue("Does not match",ex.matchesStaticInitialization(A.class).neverMatches()); - } - - public void testMatchesFieldSet() { - PointcutExpression ex = p.parsePointcutExpression("set(* *..A+.*)"); - assertTrue("matches x",ex.matchesFieldSet(x,a).alwaysMatches()); - assertTrue("matches y",ex.matchesFieldSet(y,foo).alwaysMatches()); - assertTrue("does not match n",ex.matchesFieldSet(n,foo).neverMatches()); - // this - ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)"); - assertTrue("matches Client",ex.matchesFieldSet(x,foo).alwaysMatches()); - assertTrue("does not match A",ex.matchesFieldSet(n,a).neverMatches()); - ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)"); - assertTrue("maybe matches A",ex.matchesFieldSet(x,a).maybeMatches()); - assertFalse("maybe matches A",ex.matchesFieldSet(x,a).alwaysMatches()); - // target - ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)"); - assertTrue("matches B",ex.matchesFieldSet(y,foo).alwaysMatches()); - assertTrue("maybe matches A",ex.matchesFieldSet(x,foo).maybeMatches()); - assertFalse("maybe matches A",ex.matchesFieldSet(x,foo).alwaysMatches()); - // args - ex = p.parsePointcutExpression("args(int)"); - assertTrue("matches x",ex.matchesFieldSet(x,a).alwaysMatches()); - assertTrue("matches y",ex.matchesFieldSet(y,a).alwaysMatches()); - assertTrue("does not match n",ex.matchesFieldSet(n,a).neverMatches()); - // within - ex = p.parsePointcutExpression("within(*..A)"); - assertTrue("Matches in class A",ex.matchesFieldSet(x,a).alwaysMatches()); - assertTrue("Does not match in class B",ex.matchesFieldSet(x,b).neverMatches()); - // withincode - ex = p.parsePointcutExpression("withincode(* a*(..))"); - assertTrue("Should match",ex.matchesFieldSet(x,aa).alwaysMatches()); - assertTrue("Should not match",ex.matchesFieldSet(x,b).neverMatches()); - } - - public void testMatchesFieldGet() { - PointcutExpression ex = p.parsePointcutExpression("get(* *..A+.*)"); - assertTrue("matches x",ex.matchesFieldGet(x,a).alwaysMatches()); - assertTrue("matches y",ex.matchesFieldGet(y,foo).alwaysMatches()); - assertTrue("does not match n",ex.matchesFieldGet(n,foo).neverMatches()); - // this - ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)"); - assertTrue("matches Client",ex.matchesFieldGet(x,foo).alwaysMatches()); - assertTrue("does not match A",ex.matchesFieldGet(n,a).neverMatches()); - ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)"); - assertTrue("maybe matches A",ex.matchesFieldGet(x,a).maybeMatches()); - assertFalse("maybe matches A",ex.matchesFieldGet(x,a).alwaysMatches()); - // target - ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)"); - assertTrue("matches B",ex.matchesFieldGet(y,foo).alwaysMatches()); - assertTrue("maybe matches A",ex.matchesFieldGet(x,foo).maybeMatches()); - assertFalse("maybe matches A",ex.matchesFieldGet(x,foo).alwaysMatches()); - // args - no args at get join point - ex = p.parsePointcutExpression("args(int)"); - assertTrue("matches x",ex.matchesFieldGet(x,a).neverMatches()); - // within - ex = p.parsePointcutExpression("within(*..A)"); - assertTrue("Matches in class A",ex.matchesFieldGet(x,a).alwaysMatches()); - assertTrue("Does not match in class B",ex.matchesFieldGet(x,b).neverMatches()); - // withincode - ex = p.parsePointcutExpression("withincode(* a*(..))"); - assertTrue("Should match",ex.matchesFieldGet(x,aa).alwaysMatches()); - assertTrue("Should not match",ex.matchesFieldGet(x,b).neverMatches()); - } - - public void testArgsMatching() { - // too few args - PointcutExpression ex = p.parsePointcutExpression("args(*,*,*,*)"); - assertTrue("Too few args",ex.matchesMethodExecution(foo).neverMatches()); - assertTrue("Matching #args",ex.matchesMethodExecution(bar).alwaysMatches()); - // one too few + ellipsis - ex = p.parsePointcutExpression("args(*,*,*,..)"); - assertTrue("Matches with ellipsis",ex.matchesMethodExecution(foo).alwaysMatches()); - // exact number + ellipsis - assertTrue("Matches with ellipsis",ex.matchesMethodExecution(bar).alwaysMatches()); - assertTrue("Does not match with ellipsis",ex.matchesMethodExecution(a).neverMatches()); - // too many + ellipsis - ex = p.parsePointcutExpression("args(*,..,*)"); - assertTrue("Matches with ellipsis",ex.matchesMethodExecution(bar).alwaysMatches()); - assertTrue("Does not match with ellipsis",ex.matchesMethodExecution(a).neverMatches()); - assertTrue("Matches with ellipsis",ex.matchesMethodExecution(aaa).alwaysMatches()); - // exact match - ex = p.parsePointcutExpression("args(String,int,Number)"); - assertTrue("Matches exactly",ex.matchesMethodExecution(foo).alwaysMatches()); - // maybe match - ex = p.parsePointcutExpression("args(String,int,Double)"); - assertTrue("Matches maybe",ex.matchesMethodExecution(foo).maybeMatches()); - assertFalse("Matches maybe",ex.matchesMethodExecution(foo).alwaysMatches()); - // never match - ex = p.parsePointcutExpression("args(String,Integer,Number)"); - if (LangUtil.is15VMOrGreater()) { - assertTrue("matches",ex.matchesMethodExecution(foo).alwaysMatches()); - } else { - assertTrue("Does not match",ex.matchesMethodExecution(foo).neverMatches()); - } - } - -// public void testMatchesDynamically() { -// // everything other than this,target,args should just return true -// PointcutExpression ex = p.parsePointcutExpression("call(* *.*(..)) && execution(* *.*(..)) &&" + -// "get(* *) && set(* *) && initialization(new(..)) && preinitialization(new(..)) &&" + -// "staticinitialization(X) && adviceexecution() && within(Y) && withincode(* *.*(..)))"); -// assertTrue("Matches dynamically",ex.matchesDynamically(a,b,new Object[0])); -// // this -// ex = p.parsePointcutExpression("this(String)"); -// assertTrue("String matches",ex.matchesDynamically("",this,new Object[0])); -// assertFalse("Object doesn't match",ex.matchesDynamically(new Object(),this,new Object[0])); -// ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)"); -// assertTrue("A matches",ex.matchesDynamically(new A(""),this,new Object[0])); -// assertTrue("B matches",ex.matchesDynamically(new B(""),this,new Object[0])); -// // target -// ex = p.parsePointcutExpression("target(String)"); -// assertTrue("String matches",ex.matchesDynamically(this,"",new Object[0])); -// assertFalse("Object doesn't match",ex.matchesDynamically(this,new Object(),new Object[0])); -// ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)"); -// assertTrue("A matches",ex.matchesDynamically(this,new A(""),new Object[0])); -// assertTrue("B matches",ex.matchesDynamically(this,new B(""),new Object[0])); -// // args -// ex = p.parsePointcutExpression("args(*,*,*,*)"); -// assertFalse("Too few args",ex.matchesDynamically(null,null,new Object[]{a,b})); -// assertTrue("Matching #args",ex.matchesDynamically(null,null,new Object[]{a,b,aa,aaa})); -// // one too few + ellipsis -// ex = p.parsePointcutExpression("args(*,*,*,..)"); -// assertTrue("Matches with ellipsis",ex.matchesDynamically(null,null,new Object[]{a,b,aa,aaa})); -// // exact number + ellipsis -// assertTrue("Matches with ellipsis",ex.matchesDynamically(null,null,new Object[]{a,b,aa})); -// assertFalse("Does not match with ellipsis",ex.matchesDynamically(null,null,new Object[]{a,b})); -// // too many + ellipsis -// ex = p.parsePointcutExpression("args(*,..,*)"); -// assertTrue("Matches with ellipsis",ex.matchesDynamically(null,null,new Object[]{a,b,aa,aaa})); -// assertFalse("Does not match with ellipsis",ex.matchesDynamically(null,null,new Object[]{a})); -// assertTrue("Matches with ellipsis",ex.matchesDynamically(null,null,new Object[]{a,b})); -// // exact match -// ex = p.parsePointcutExpression("args(String,int,Number)"); -// assertTrue("Matches exactly",ex.matchesDynamically(null,null,new Object[]{"",new Integer(5),new Double(5.0)})); -// ex = p.parsePointcutExpression("args(String,Integer,Number)"); -// assertTrue("Matches exactly",ex.matchesDynamically(null,null,new Object[]{"",new Integer(5),new Double(5.0)})); -// // never match -// ex = p.parsePointcutExpression("args(String,Integer,Number)"); -// assertFalse("Does not match",ex.matchesDynamically(null,null,new Object[]{a,b,aa})); -//} - - public void testGetPointcutExpression() { - PointcutExpression ex = p.parsePointcutExpression("staticinitialization(*..A+)"); - assertEquals("staticinitialization(*..A+)",ex.getPointcutExpression()); - } - - public void testCouldMatchJoinPointsInType() { - PointcutExpression ex = p.parsePointcutExpression("execution(* org.aspectj.weaver.tools.PointcutExpressionTest.B.*(..))"); - assertTrue("Could maybe match String (as best we know at this point)",ex.couldMatchJoinPointsInType(String.class)); - assertTrue("Will always match B",ex.couldMatchJoinPointsInType(B.class)); - ex = p.parsePointcutExpression("within(org.aspectj.weaver.tools.PointcutExpressionTest.B)"); - assertFalse("Will never match String",ex.couldMatchJoinPointsInType(String.class)); - assertTrue("Will always match B",ex.couldMatchJoinPointsInType(B.class)); - } - - public void testMayNeedDynamicTest() { - PointcutExpression ex = p.parsePointcutExpression("execution(* org.aspectj.weaver.tools.PointcutExpressionTest.B.*(..))"); - assertFalse("No dynamic test needed",ex.mayNeedDynamicTest()); - ex = p.parsePointcutExpression("execution(* org.aspectj.weaver.tools.PointcutExpressionTest.B.*(..)) && args(org.aspectj.weaver.tools.PointcutExpressionTest.X)"); - assertTrue("Dynamic test needed",ex.mayNeedDynamicTest()); - } - - protected void setUp() throws Exception { - super.setUp(); - p = PointcutParser.getPointcutParserSupportingAllPrimitivesAndUsingSpecifiedClassloaderForResolution(this.getClass().getClassLoader()); - asCons = A.class.getConstructor(new Class[]{String.class}); - bsCons = B.class.getConstructor(new Class[0]); - bsStringCons = B.class.getConstructor(new Class[]{String.class}); - a = A.class.getMethod("a",new Class[0]); - aa = A.class.getMethod("aa",new Class[]{int.class}); - aaa = A.class.getMethod("aaa",new Class[]{String.class,int.class}); - x = A.class.getDeclaredField("x"); - y = B.class.getDeclaredField("y"); - b = B.class.getMethod("b",new Class[0]); - bsaa = B.class.getMethod("aa",new Class[]{int.class}); - clientCons = Client.class.getConstructor(new Class[0]); - n = Client.class.getDeclaredField("n"); - foo = Client.class.getDeclaredMethod("foo",new Class[]{String.class,int.class,Number.class}); - bar = Client.class.getDeclaredMethod("bar",new Class[]{String.class,int.class,Integer.class,Number.class}); - } - - static class A { - public A(String s) {} - public void a() {} - public void aa(int i) {} - public void aaa(String s, int i) {} - int x; - } - - static class B extends A { - public B() {super("");} - public B(String s) {super(s);} - public String b() { return null; } - public void aa(int i) {} - int y; - } - - static class Client { - public Client() {} - Number n; - public void foo(String s, int i, Number n) {} - public void bar(String s, int i, Integer i2, Number n) {} - } - - static class X {} - -}