diff options
author | mwebster <mwebster> | 2006-08-07 16:41:05 +0000 |
---|---|---|
committer | mwebster <mwebster> | 2006-08-07 16:41:05 +0000 |
commit | 6dca4cc41d0af83c9baeb1aa1734b48cec11b1b0 (patch) | |
tree | 10d9e0127bcee36905c427d91d50e0de6af28a97 /weaver5/java5-testsrc/org/aspectj | |
parent | 51ace9a1a3bbcb24fd2a0f08ff6fdbec27f47738 (diff) | |
download | aspectj-6dca4cc41d0af83c9baeb1aa1734b48cec11b1b0.tar.gz aspectj-6dca4cc41d0af83c9baeb1aa1734b48cec11b1b0.zip |
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)
Diffstat (limited to 'weaver5/java5-testsrc/org/aspectj')
14 files changed, 1531 insertions, 2 deletions
diff --git a/weaver5/java5-testsrc/org/aspectj/weaver/AllTracing5Tests.java b/weaver5/java5-testsrc/org/aspectj/weaver/AllTracing5Tests.java index bc6e594b7..bf68cb56c 100644 --- a/weaver5/java5-testsrc/org/aspectj/weaver/AllTracing5Tests.java +++ b/weaver5/java5-testsrc/org/aspectj/weaver/AllTracing5Tests.java @@ -16,7 +16,7 @@ import junit.framework.TestSuite; public class AllTracing5Tests { public static Test suite() { - TestSuite suite = new TestSuite("Test for org.aspectj.weaver"); + TestSuite suite = new TestSuite(AllTracing5Tests.class.getName()); //$JUnit-BEGIN$ suite.addTestSuite(Jdk14TraceFactoryTest.class); suite.addTestSuite(Jdk14TraceTest.class); diff --git a/weaver5/java5-testsrc/org/aspectj/weaver/AllWeaver5Tests.java b/weaver5/java5-testsrc/org/aspectj/weaver/AllWeaver5Tests.java new file mode 100644 index 000000000..b46d84011 --- /dev/null +++ b/weaver5/java5-testsrc/org/aspectj/weaver/AllWeaver5Tests.java @@ -0,0 +1,36 @@ +/* ******************************************************************* + * Copyright (c) 2005-2006 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 + * Matthew Webster Move from default package + * ******************************************************************/ +package org.aspectj.weaver; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.aspectj.weaver.bcel.AsmDelegateTests5; +import org.aspectj.weaver.reflect.ReflectionWorldTest; +import org.aspectj.weaver.tools.PointcutExpressionTest; + +public class AllWeaver5Tests { + + public static Test suite() { + TestSuite suite = new TestSuite(AllWeaver5Tests.class.getName()); + //$JUnit-BEGIN$ + suite.addTest(AllTracing5Tests.suite()); + suite.addTest(AsmDelegateTests5.suite()); + suite.addTest(BcweaverModuleTests15.suite()); + suite.addTestSuite(PointcutExpressionTest.class); + suite.addTestSuite(ReflectionWorldTest.class); + //$JUnit-END$ + return suite; + } + +} diff --git a/weaver5/java5-testsrc/org/aspectj/weaver/BcweaverModuleTests15.java b/weaver5/java5-testsrc/org/aspectj/weaver/BcweaverModuleTests15.java new file mode 100644 index 000000000..74636407e --- /dev/null +++ b/weaver5/java5-testsrc/org/aspectj/weaver/BcweaverModuleTests15.java @@ -0,0 +1,37 @@ +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.weaver.bcel.BcelGenericSignatureToTypeXTestCase; +import org.aspectj.weaver.patterns.WildTypePatternResolutionTestCase; +import org.aspectj.weaver.tools.Java15PointcutExpressionTest; + +public class BcweaverModuleTests15 extends TestCase { + public static Test suite() { + TestSuite suite = new TestSuite(BcweaverModuleTests15.class.getName()); + suite.addTestSuite(BcelGenericSignatureToTypeXTestCase.class); + suite.addTestSuite(BoundedReferenceTypeTestCase.class); + suite.addTest(Java15PointcutExpressionTest.suite()); + suite.addTestSuite(MemberTestCase15.class); + suite.addTestSuite(ReferenceTypeTestCase.class); + suite.addTest(TestJava5ReflectionBasedReferenceTypeDelegate.suite()); + suite.addTestSuite(TypeVariableTestCase.class); + suite.addTestSuite(TypeVariableReferenceTypeTestCase.class); + suite.addTestSuite(WildTypePatternResolutionTestCase.class); + return suite; + } + + public BcweaverModuleTests15(String name) { super(name); } +} diff --git a/weaver5/java5-testsrc/org/aspectj/weaver/BoundedReferenceTypeTestCase.java b/weaver5/java5-testsrc/org/aspectj/weaver/BoundedReferenceTypeTestCase.java new file mode 100644 index 000000000..b46a5a6a6 --- /dev/null +++ b/weaver5/java5-testsrc/org/aspectj/weaver/BoundedReferenceTypeTestCase.java @@ -0,0 +1,105 @@ +/* ******************************************************************* + * 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; + +import org.aspectj.weaver.bcel.BcelWorld; + +import junit.framework.TestCase; + +public class BoundedReferenceTypeTestCase extends TestCase { + + ReferenceType javaLangClass; + ReferenceType javaLangObject; + BoundedReferenceType extendsClass; + BoundedReferenceType superClass; + BoundedReferenceType extendsWithExtras; + + public void testSignature() { + String extendsSig = extendsClass.getSignature(); + assertEquals("+Ljava/lang/Class;",extendsSig); + assertEquals("-Ljava/lang/Class;",superClass.getSignature()); + } + + public void testExtendsBounds() { + assertFalse("has no lower bound",extendsClass.hasLowerBound()); + assertNull("no lower bound",extendsClass.getLowerBound()); + assertEquals(javaLangClass,extendsClass.getUpperBound()); + assertEquals("no interface bounds",0,extendsClass.getInterfaceBounds().length); + } + + public void testSuperBounds() { + assertTrue("has lower bound",superClass.hasLowerBound()); + assertEquals(javaLangClass,superClass.getLowerBound()); + assertEquals("Ljava/lang/Object;",superClass.getUpperBound().getSignature()); + assertEquals("no interface bounds",0,superClass.getInterfaceBounds().length); + } + + public void testIsExtends() { + assertTrue(extendsClass.isExtends); + assertFalse(superClass.isExtends); + } + + public void testIsSuper() { + assertTrue(superClass.isSuper); + assertFalse(extendsClass.isSuper); + } + + public void testGetDeclaredInterfacesNoAdditions() { + ResolvedType[] rt1 = extendsClass.getDeclaredInterfaces(); + ResolvedType[] rt2 = javaLangClass.getDeclaredInterfaces(); + assertEquals("same length",rt1.length,rt2.length); + for (int i = 0; i < rt2.length; i++) { + assertEquals("same methods",rt1[i],rt2[i]); + } + } + + public void testGetDeclaredInterfacesWithInterfaceBounds() { + ResolvedType[] rt1 = extendsWithExtras.getDeclaredInterfaces(); + ResolvedType[] rt2 = javaLangClass.getDeclaredInterfaces(); + assertEquals("one extra interface",rt1.length,rt2.length + 1); + for (int i = 0; i < rt2.length; i++) { + assertEquals("same methods",rt1[i],rt2[i]); + } + assertEquals("Ljava/util/List;",rt1[rt1.length-1].getSignature()); + } + + // all other methods in signature are delegated to upper bound... + // representative test + public void testGetDeclaredMethodsExtends() { + ResolvedMember[] rm1 = extendsClass.getDeclaredMethods(); + ResolvedMember[] rm2 = javaLangClass.getDeclaredMethods(); + assertEquals("same length",rm1.length,rm2.length); + for (int i = 0; i < rm2.length; i++) { + assertEquals("same methods",rm1[i],rm2[i]); + } + } + + public void testGetDeclaredMethodsSuper() { + ResolvedMember[] rm1 = superClass.getDeclaredMethods(); + ResolvedMember[] rm2 = javaLangObject.getDeclaredMethods(); + assertEquals("same length",rm1.length,rm2.length); + for (int i = 0; i < rm2.length; i++) { + assertEquals("same methods",rm1[i],rm2[i]); + } + } + + protected void setUp() throws Exception { + super.setUp(); + BcelWorld world = new BcelWorld(); + javaLangClass = (ReferenceType) world.resolve(UnresolvedType.forName("java/lang/Class")); + javaLangObject = (ReferenceType) world.resolve(UnresolvedType.OBJECT); + extendsClass = new BoundedReferenceType(javaLangClass,true,world); + superClass = new BoundedReferenceType(javaLangClass,false,world); + extendsWithExtras = new BoundedReferenceType(javaLangClass,true,world, + new ReferenceType[] {(ReferenceType)world.resolve(UnresolvedType.forName("java/util/List"))}); + } +} diff --git a/weaver5/java5-testsrc/org/aspectj/weaver/MemberTestCase15.java b/weaver5/java5-testsrc/org/aspectj/weaver/MemberTestCase15.java new file mode 100644 index 000000000..8014289e7 --- /dev/null +++ b/weaver5/java5-testsrc/org/aspectj/weaver/MemberTestCase15.java @@ -0,0 +1,81 @@ +/* ******************************************************************* + * 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; + +import org.aspectj.weaver.bcel.BcelWorld; + +import junit.framework.TestCase; + +/** + * @author colyer + * + */ +public class MemberTestCase15 extends TestCase { + + public void testCanBeParameterizedRegularMethod() { + BcelWorld world = new BcelWorld(); + ResolvedType javaLangClass = world.resolve(UnresolvedType.forName("java/lang/Class")); + ResolvedMember[] methods = javaLangClass.getDeclaredMethods(); + ResolvedMember getAnnotations = null; + for (int i = 0; i < methods.length; i++) { + if (methods[i].getName().equals("getAnnotations")) { + getAnnotations = methods[i]; + break; + } + } + if (getAnnotations != null) { // so can run on non-Java 5 +// System.out.println("got it"); + assertFalse(getAnnotations.canBeParameterized()); + } + } + + public void testCanBeParameterizedGenericMethod() { + BcelWorld world = new BcelWorld(); + world.setBehaveInJava5Way(true); + ResolvedType javaLangClass = world.resolve(UnresolvedType.forName("java.lang.Class")); + javaLangClass = javaLangClass.getGenericType(); + if (javaLangClass == null) return; // for < 1.5 + ResolvedMember[] methods = javaLangClass.getDeclaredMethods(); + ResolvedMember asSubclass = null; + for (int i = 0; i < methods.length; i++) { + if (methods[i].getName().equals("asSubclass")) { + asSubclass = methods[i]; + break; + } + } + if (asSubclass != null) { // so can run on non-Java 5 +// System.out.println("got it"); + assertTrue(asSubclass.canBeParameterized()); + } + } + + public void testCanBeParameterizedMethodInGenericType() { + BcelWorld world = new BcelWorld(); + world.setBehaveInJava5Way(true); + ResolvedType javaUtilList = world.resolve(UnresolvedType.forName("java.util.List")); + javaUtilList = javaUtilList.getGenericType(); + if (javaUtilList == null) return; // for < 1.5 + ResolvedMember[] methods = javaUtilList.getDeclaredMethods(); + ResolvedMember add = null; + for (int i = 0; i < methods.length; i++) { + if (methods[i].getName().equals("add")) { + add = methods[i]; + break; + } + } + if (add != null) { // so can run on non-Java 5 +// System.out.println("got it"); + assertTrue(add.canBeParameterized()); + } + } + +} diff --git a/weaver5/java5-testsrc/org/aspectj/weaver/ReferenceTypeTestCase.java b/weaver5/java5-testsrc/org/aspectj/weaver/ReferenceTypeTestCase.java new file mode 100644 index 000000000..0d84c135a --- /dev/null +++ b/weaver5/java5-testsrc/org/aspectj/weaver/ReferenceTypeTestCase.java @@ -0,0 +1,58 @@ +/* ******************************************************************* + * 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; + +import org.aspectj.weaver.bcel.BcelWorld; + +import junit.framework.TestCase; + +// test cases for Adrian's additions to ReferenceType +// XXX - couldn't find any unit test cases for the rest of the ReferenceType class +public class ReferenceTypeTestCase extends TestCase { + + public void testIsRawTrue() { + BcelWorld world = new BcelWorld(); + world.setBehaveInJava5Way(true); + UnresolvedType javaLangClass = UnresolvedType.forName("java.lang.Class"); + ResolvedType rtx = world.resolve(javaLangClass); + assertTrue("Resolves to reference type",(rtx instanceof ReferenceType)); + ReferenceType rt = (ReferenceType) rtx; + assertTrue("java.lang.Class is raw",rt.isRawType()); + } + + public void testIsRawFalse() { + BcelWorld world = new BcelWorld(); + world.setBehaveInJava5Way(true); + UnresolvedType javaLangObject = UnresolvedType.forName("java.lang.Object"); + ResolvedType rtx = world.resolve(javaLangObject); + assertTrue("Resolves to reference type",(rtx instanceof ReferenceType)); + ReferenceType rt = (ReferenceType) rtx; + assertFalse("java.lang.Object is not raw",rt.isRawType()); + } + + public void testIsGenericTrue() { + BcelWorld world = new BcelWorld(); + world.setBehaveInJava5Way(true); + UnresolvedType javaLangClass = UnresolvedType.forName("java.lang.Class"); + ResolvedType rtx = world.resolve(javaLangClass); + assertTrue("java.lang.Class has underpinning generic type",rtx.getGenericType().isGenericType()); + } + + public void testIsGenericFalse() { + BcelWorld world = new BcelWorld(); + world.setBehaveInJava5Way(true); + UnresolvedType javaLangObject = UnresolvedType.forName("java.lang.Object"); + ResolvedType rtx = world.resolve(javaLangObject); + assertFalse(rtx.isGenericType()); + } + +} diff --git a/weaver5/java5-testsrc/org/aspectj/weaver/TestJava5ReflectionBasedReferenceTypeDelegate.java b/weaver5/java5-testsrc/org/aspectj/weaver/TestJava5ReflectionBasedReferenceTypeDelegate.java index decb97bea..94f13adf4 100644 --- a/weaver5/java5-testsrc/org/aspectj/weaver/TestJava5ReflectionBasedReferenceTypeDelegate.java +++ b/weaver5/java5-testsrc/org/aspectj/weaver/TestJava5ReflectionBasedReferenceTypeDelegate.java @@ -12,11 +12,13 @@ package org.aspectj.weaver; + import junit.framework.Test; import junit.framework.TestSuite; import org.aspectj.weaver.reflect.ReflectionBasedReferenceTypeDelegateTest; + public class TestJava5ReflectionBasedReferenceTypeDelegate extends ReflectionBasedReferenceTypeDelegateTest { public static Test suite() { @@ -73,4 +75,4 @@ public class TestJava5ReflectionBasedReferenceTypeDelegate extends ReflectionBas assertTrue("Superclass for Map generic type should be Object but was "+rt2,rt2.equals(UnresolvedType.OBJECT)); } -} +}
\ No newline at end of file diff --git a/weaver5/java5-testsrc/org/aspectj/weaver/TypeVariableReferenceTypeTestCase.java b/weaver5/java5-testsrc/org/aspectj/weaver/TypeVariableReferenceTypeTestCase.java new file mode 100644 index 000000000..15edbae4b --- /dev/null +++ b/weaver5/java5-testsrc/org/aspectj/weaver/TypeVariableReferenceTypeTestCase.java @@ -0,0 +1,49 @@ +/* ******************************************************************* + * 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; + +import org.aspectj.weaver.bcel.BcelWorld; + +import junit.framework.TestCase; + +/** + * @author colyer + * + */ +public class TypeVariableReferenceTypeTestCase extends TestCase { + + ReferenceType javaLangClass; + ReferenceType javaLangObject; + BoundedReferenceType extendsClass; + BoundedReferenceType superClass; + BoundedReferenceType extendsWithExtras; + BcelWorld world; + + public void testConstructionByNameAndVariable() { + TypeVariable tv = new TypeVariable("T",javaLangClass); + TypeVariableReferenceType tvrt = new TypeVariableReferenceType(tv,world); + assertEquals("T",tvrt.getTypeVariable().getName()); + assertEquals(javaLangClass,tvrt.getUpperBound()); + } + + protected void setUp() throws Exception { + super.setUp(); + world = new BcelWorld(); + javaLangClass = (ReferenceType) world.resolve(UnresolvedType.forName("java/lang/Class")); + javaLangObject = (ReferenceType) world.resolve(UnresolvedType.OBJECT); + extendsClass = new BoundedReferenceType(javaLangClass,true,world); + superClass = new BoundedReferenceType(javaLangClass,false,world); + extendsWithExtras = new BoundedReferenceType(javaLangClass,true,world, + new ReferenceType[] {(ReferenceType)world.resolve(UnresolvedType.forName("java/util/List"))}); + } + +} diff --git a/weaver5/java5-testsrc/org/aspectj/weaver/TypeVariableTestCase.java b/weaver5/java5-testsrc/org/aspectj/weaver/TypeVariableTestCase.java new file mode 100644 index 000000000..ec2547acd --- /dev/null +++ b/weaver5/java5-testsrc/org/aspectj/weaver/TypeVariableTestCase.java @@ -0,0 +1,125 @@ +/* ******************************************************************* + * 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; + +import org.aspectj.weaver.bcel.BcelWorld; + +import junit.framework.TestCase; + +public class TypeVariableTestCase extends TestCase { + + private UnresolvedType javaLangNumber; + private UnresolvedType javaLangDouble; + private UnresolvedType javaUtilList; + private UnresolvedType javaIoSerializable; + private World world; + + public void testDefaultBounds() { + TypeVariable tv = new TypeVariable("T"); + assertEquals("Object",UnresolvedType.OBJECT,tv.getUpperBound()); + assertEquals("no additional bounds",0,tv.getAdditionalInterfaceBounds().length); + assertNull("no lower bound",tv.getLowerBound()); + } + + public void testName() { + TypeVariable tv = new TypeVariable("T"); + assertEquals("T",tv.getName()); + } + + public void testUpperBound() { + TypeVariable tv = new TypeVariable("N",javaLangNumber); + assertEquals("java.lang.Number",javaLangNumber,tv.getUpperBound()); + } + + public void testAdditionalUpperBounds() { + TypeVariable tv = new TypeVariable("E",UnresolvedType.OBJECT,new UnresolvedType[] {javaUtilList}); + assertEquals("1 additional bound",1,tv.getAdditionalInterfaceBounds().length); + assertEquals("java.util.List",javaUtilList,tv.getAdditionalInterfaceBounds()[0]); + } + + public void testLowerBound() { + TypeVariable tv = new TypeVariable("X",UnresolvedType.OBJECT,new UnresolvedType[0],javaLangDouble); + assertEquals("java.lang.Double",javaLangDouble,tv.getLowerBound()); + } + + public void testResolution() { + TypeVariable tv = new TypeVariable( + "T", + javaLangNumber, + new UnresolvedType[] {javaUtilList}, + javaLangDouble + ); + tv.resolve(world); + assertEquals("resolved number",javaLangNumber.resolve(world),tv.getUpperBound()); + assertEquals("resolved list",javaUtilList.resolve(world), + tv.getAdditionalInterfaceBounds()[0]); + assertEquals("resolved double",javaLangDouble.resolve(world),tv.getLowerBound()); + } + + public void testBindWithoutResolve() { + TypeVariable tv = new TypeVariable("X"); + try { + tv.canBeBoundTo(null); + fail ("Should throw illegal state exception"); + } catch (IllegalStateException ex) {} + } + + public void testCanBindToUpperMatch() { + TypeVariable tv = new TypeVariable("X",javaLangNumber); + tv.resolve(world); + assertTrue(tv.canBeBoundTo(javaLangDouble.resolve(world))); + } + + public void testCanBindToUpperFail() { + TypeVariable tv = new TypeVariable("X",javaLangNumber); + tv.resolve(world); + assertFalse(tv.canBeBoundTo(UnresolvedType.OBJECT.resolve(world))); + } + + public void testCanBindToInterfaceMatch() { + TypeVariable tv = new TypeVariable("T",javaLangNumber,new UnresolvedType[] {javaIoSerializable}); + tv.resolve(world); + assertTrue(tv.canBeBoundTo(javaLangDouble.resolve(world))); + } + + public void testCanBindToInterfaceFail() { + TypeVariable tv = new TypeVariable("T",javaLangNumber,new UnresolvedType[] {javaUtilList}); + tv.resolve(world); + assertFalse(tv.canBeBoundTo(javaLangDouble.resolve(world))); + } + + public void testCanBindToLowerMatch() { + TypeVariable tv = new TypeVariable("T",javaLangNumber,new UnresolvedType[0],javaLangDouble); + tv.resolve(world); + assertTrue(tv.canBeBoundTo(javaLangNumber.resolve(world))); + } + + public void testCanBindToLowerFail() { + TypeVariable tv = new TypeVariable("T",javaLangNumber,new UnresolvedType[0],javaLangNumber); + tv.resolve(world); + assertFalse(tv.canBeBoundTo(javaLangDouble.resolve(world))); + } + + protected void setUp() throws Exception { + super.setUp(); + javaLangNumber = UnresolvedType.forSignature("Ljava/lang/Number;"); + javaLangDouble = UnresolvedType.forSignature("Ljava/lang/Double;"); + javaIoSerializable = UnresolvedType.forSignature("Ljava/io/Serializable;"); + javaUtilList = UnresolvedType.forSignature("Ljava/util/List;"); + world = new BcelWorld(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + +} diff --git a/weaver5/java5-testsrc/org/aspectj/weaver/bcel/AsmDelegateTests5.java b/weaver5/java5-testsrc/org/aspectj/weaver/bcel/AsmDelegateTests5.java new file mode 100644 index 000000000..f88b2c336 --- /dev/null +++ b/weaver5/java5-testsrc/org/aspectj/weaver/bcel/AsmDelegateTests5.java @@ -0,0 +1,147 @@ +/* ******************************************************************* + * Copyright (c) 2006 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: + * Andy Clement initial implementation + * Matthew Webster move Java 5 tests + * ******************************************************************/ + + +package org.aspectj.weaver.bcel; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.aspectj.weaver.AbstractWorldTestCase; +import org.aspectj.weaver.BcweaverTests; +import org.aspectj.weaver.ResolvedMember; +import org.aspectj.weaver.ResolvedType; +import org.aspectj.weaver.TypeVariable; +import org.aspectj.weaver.UnresolvedType; +import org.aspectj.weaver.World; + + +/** + * This is a test case for the nameType parts of worlds. + */ +public class AsmDelegateTests5 extends AbstractWorldTestCase { + + public static Test suite() { + TestSuite suite = new TestSuite(AsmDelegateTests5.class.getName()); + suite.addTestSuite(AsmDelegateTests5.class); + return suite; + } + + private final BcelWorld world = new BcelWorld(BcweaverTests.TESTDATA_PATH+"/forAsmDelegateTesting/stuff.jar"); + + public AsmDelegateTests5(String name) { + super(name); + } + + protected World getWorld() { + return world; + } + + // --- testcode + + /** + * 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(); + + 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 <U> Class<? extends U> asSubclass(Class<U> 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)<tvs.length) s.append(","); + } + s.append("]"); + return s.toString(); + } + + public void testCompareGenericFields() { + 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(); + + ResolvedMember[] bcelFields = bcelJavaLangClass.getDeclaredFields(); + ResolvedMember[] asmFields = asmJavaLangClass.getDeclaredFields(); + + for (int i = 0; i < bcelFields.length; i++) { + UnresolvedType bcelFieldType = bcelFields[i].getGenericReturnType(); + UnresolvedType asmFieldType = asmFields[i].getGenericReturnType(); + if (!bcelFields[i].getGenericReturnType().toDebugString().equals(asmFields[i].getGenericReturnType().toDebugString())) { + fail("These fields look different when viewed through ASM or BCEL\nBCEL='"+bcelFieldType.toDebugString()+ + "'\n ASM='"+asmFieldType.toDebugString()+"'"); + } + } + } + +} diff --git a/weaver5/java5-testsrc/org/aspectj/weaver/bcel/BcelGenericSignatureToTypeXTestCase.java b/weaver5/java5-testsrc/org/aspectj/weaver/bcel/BcelGenericSignatureToTypeXTestCase.java new file mode 100644 index 000000000..28cf82f5a --- /dev/null +++ b/weaver5/java5-testsrc/org/aspectj/weaver/bcel/BcelGenericSignatureToTypeXTestCase.java @@ -0,0 +1,71 @@ +/* ******************************************************************* + * 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.bcel; + +import junit.framework.TestCase; + +import org.aspectj.apache.bcel.Repository; +import org.aspectj.apache.bcel.classfile.GenericSignatureParser; +import org.aspectj.apache.bcel.classfile.JavaClass; +import org.aspectj.apache.bcel.classfile.Signature; +import org.aspectj.weaver.UnresolvedType; + +/** + * @author colyer + * + */ +public class BcelGenericSignatureToTypeXTestCase extends TestCase { + + public void testEnumFromHell() throws Exception { + BcelWorld world = new BcelWorld(); + JavaClass javaLangEnum = Repository.lookupClass("java/lang/Enum"); + Signature.ClassSignature cSig = javaLangEnum.getGenericClassTypeSignature(); + UnresolvedType superclass = + BcelGenericSignatureToTypeXConverter.classTypeSignature2TypeX( + cSig.superclassSignature, + cSig.formalTypeParameters, + world + ); + assertEquals("Ljava/lang/Object;",superclass.getSignature()); + assertEquals("2 superinterfaces",2,cSig.superInterfaceSignatures.length); + UnresolvedType comparable = + BcelGenericSignatureToTypeXConverter.classTypeSignature2TypeX( + cSig.superInterfaceSignatures[0], + cSig.formalTypeParameters, + world + ); + assertEquals("Pjava/lang/Comparable<TE;>;",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("<T::Ljava/io/Serializable;>Ljava/lang/Object;Ljava/lang/Comparable<TT;>;"); + 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<String> 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<String>; - parameterized) + */ + public void testParameterized() { + TypePattern rtp = resolveWildTypePattern("List<String>", false); + + assertTrue("resolves to exact type",rtp instanceof ExactTypePattern); + UnresolvedType exactType = rtp.getExactType(); + assertTrue(exactType.isParameterizedType()); + assertEquals("Pjava/util/List<Ljava/lang/String;>;",exactType.getSignature()); + + ResolvedType rt = exactType.resolve(world); + assertEquals("Pjava/util/List<Ljava/lang/String;>;",rt.getSignature()); + assertTrue(rt.isParameterizedType()); + + ExactTypePattern etp = (ExactTypePattern) writeAndRead((ExactTypePattern)rtp); + exactType = etp.getExactType(); + + assertEquals("Pjava/util/List<Ljava/lang/String;>;",rt.getSignature()); + assertTrue(rt.isParameterizedType()); + + rt = exactType.resolve(world); + assertEquals("Pjava/util/List<Ljava/lang/String;>;",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<Str*> 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<Str*>", 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*<String> + * 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*<String>", 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<? extends Number> + */ + public void testSomethingExtends() { + TypePattern rtp = resolveWildTypePattern("List<? extends Number>", 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<? extends Number+> + */ + public void testSomethingExtendsPattern() { + TypePattern rtp = resolveWildTypePattern("List<? extends Number+>", 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<? extends Num*> + */ + public void testSomethingExtendsPatternv2() { + TypePattern rtp = resolveWildTypePattern("List<? extends Num*>", 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<? super Number> + * + */ + public void testSomethingSuper() { + TypePattern rtp = resolveWildTypePattern("List<? super Double>", 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<methods.length; i++) { + if (name.equals(methods[i].getName())) { + return i; + } + } + return -1; + } + + protected int findMethod(String name, int numArgs, ResolvedMember[] methods) { + for (int i=0; i<methods.length; i++) { + if (name.equals(methods[i].getName()) && (methods[i].getParameterTypes().length == numArgs)) { + return i; + } + } + return -1; + } + + public void testGetDeclaredMethods() { + ResolvedMember[] methods = objectType.getDeclaredMethods(); + assertEquals(Object.class.getDeclaredMethods().length + Object.class.getDeclaredConstructors().length, methods.length); + + ResolvedType c = world.resolve("reflect.tests.C"); + methods = c.getDeclaredMethods(); + assertEquals(3,methods.length); + int idx = findMethod("foo", methods); + assertTrue(idx > -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("<init>", 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 <clinit> (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<rms2.length;i++) { + if (!one.contains(rms2[i].toString())) { + errors.append("Couldn't find "+rms2[i].toString()+" in the bcel set\n"); + } + } + for (int i = 0;i<rms1.length;i++) { + if (!two.contains(rms1[i].toString())) { + if (!barfIfClinitMissing && rms1[i].getName().equals("<clinit>")) 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)); + } + +} |