From 1c588042ce875a495131e3fb5f7bcaf12f07c1a8 Mon Sep 17 00:00:00 2001 From: acolyer Date: Fri, 8 Jul 2005 10:26:53 +0000 Subject: [PATCH] test cases for generics updates made by amc --- .../weaver/BoundedReferenceTypeTestCase.java | 105 ++++++++++++++++++ .../weaver/GenericsWildCardTypeXTestCase.java | 37 ++++++ .../org/aspectj/weaver/MemberTestCase.java | 56 +++++++++- .../aspectj/weaver/ReferenceTypeTestCase.java | 40 +++++++ .../TypeVariableReferenceTypeTestCase.java | 57 ++++++++++ .../org/aspectj/weaver/TypeXTestCase.java | 9 ++ 6 files changed, 303 insertions(+), 1 deletion(-) create mode 100644 weaver/testsrc/org/aspectj/weaver/BoundedReferenceTypeTestCase.java create mode 100644 weaver/testsrc/org/aspectj/weaver/GenericsWildCardTypeXTestCase.java create mode 100644 weaver/testsrc/org/aspectj/weaver/ReferenceTypeTestCase.java create mode 100644 weaver/testsrc/org/aspectj/weaver/TypeVariableReferenceTypeTestCase.java diff --git a/weaver/testsrc/org/aspectj/weaver/BoundedReferenceTypeTestCase.java b/weaver/testsrc/org/aspectj/weaver/BoundedReferenceTypeTestCase.java new file mode 100644 index 000000000..f8df609da --- /dev/null +++ b/weaver/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() { + ResolvedTypeX[] rt1 = extendsClass.getDeclaredInterfaces(); + ResolvedTypeX[] 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() { + ResolvedTypeX[] rt1 = extendsWithExtras.getDeclaredInterfaces(); + ResolvedTypeX[] 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(TypeX.forName("java/lang/Class")); + javaLangObject = (ReferenceType) world.resolve(TypeX.OBJECT); + extendsClass = new BoundedReferenceType(javaLangClass,true,world); + superClass = new BoundedReferenceType(javaLangClass,false,world); + extendsWithExtras = new BoundedReferenceType(javaLangClass,true,world, + new ReferenceType[] {(ReferenceType)world.resolve(TypeX.forName("java/util/List"))}); + } +} diff --git a/weaver/testsrc/org/aspectj/weaver/GenericsWildCardTypeXTestCase.java b/weaver/testsrc/org/aspectj/weaver/GenericsWildCardTypeXTestCase.java new file mode 100644 index 000000000..b3d127b1b --- /dev/null +++ b/weaver/testsrc/org/aspectj/weaver/GenericsWildCardTypeXTestCase.java @@ -0,0 +1,37 @@ +/* ******************************************************************* + * 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 GenericsWildCardTypeXTestCase extends TestCase { + + public void testIdentity() { + TypeX anything = GenericsWildcardTypeX.GENERIC_WILDCARD; + assertEquals("?",anything.getSignature()); + } + + public void testResolving() { + BoundedReferenceType brt = (BoundedReferenceType) + GenericsWildcardTypeX.GENERIC_WILDCARD.resolve(new BcelWorld()); + assertEquals("?",brt.getSignature()); + assertTrue(brt.isExtends()); + assertEquals("Ljava/lang/Object;",brt.getUpperBound().getSignature()); + } + +} diff --git a/weaver/testsrc/org/aspectj/weaver/MemberTestCase.java b/weaver/testsrc/org/aspectj/weaver/MemberTestCase.java index 28a2b89fe..117cf6b22 100644 --- a/weaver/testsrc/org/aspectj/weaver/MemberTestCase.java +++ b/weaver/testsrc/org/aspectj/weaver/MemberTestCase.java @@ -1,5 +1,6 @@ /* ******************************************************************* * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC). + * 2005 contributors * All rights reserved. * This program and the accompanying materials are made available * under the terms of the Common Public License v1.0 @@ -7,7 +8,8 @@ * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: - * PARC initial implementation + * PARC initial implementation + * Adrian Colyer, canBeParameterized tests * ******************************************************************/ @@ -16,6 +18,7 @@ package org.aspectj.weaver; import junit.framework.TestCase; import org.aspectj.testing.util.TestUtil; +import org.aspectj.weaver.bcel.BcelWorld; /** * This is a test case for all the portions of Member that don't require a world. @@ -142,6 +145,57 @@ public class MemberTestCase extends TestCase { isStaticTest(m, true); } + public void testCanBeParameterizedRegularMethod() { + BcelWorld world = new BcelWorld(); + ResolvedTypeX javaLangClass = world.resolve(TypeX.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(); + ResolvedTypeX javaLangClass = world.resolve(TypeX.forName("java/lang/Class")); + 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(); + ResolvedTypeX javaUtilList = world.resolve(TypeX.forName("java/util/List")); + 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()); + } + } + private void isStaticTest(Member m, boolean b) { assertEquals(m + " is static", b, m.isStatic()); } diff --git a/weaver/testsrc/org/aspectj/weaver/ReferenceTypeTestCase.java b/weaver/testsrc/org/aspectj/weaver/ReferenceTypeTestCase.java new file mode 100644 index 000000000..43ce65d8e --- /dev/null +++ b/weaver/testsrc/org/aspectj/weaver/ReferenceTypeTestCase.java @@ -0,0 +1,40 @@ +/* ******************************************************************* + * 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 testIsGenericTrue() { + BcelWorld world = new BcelWorld(); + TypeX javaLangClass = TypeX.forName("java/lang/Class"); + ResolvedTypeX rtx = world.resolve(javaLangClass); + assertTrue("Resolves to reference type",(rtx instanceof ReferenceType)); + ReferenceType rt = (ReferenceType) rtx; + assertTrue("java.lang.Class is generic",rt.isGeneric()); + } + + public void testIsGenericFalse() { + BcelWorld world = new BcelWorld(); + TypeX javaLangObject = TypeX.forName("java/lang/Object"); + ResolvedTypeX rtx = world.resolve(javaLangObject); + assertTrue("Resolves to reference type",(rtx instanceof ReferenceType)); + ReferenceType rt = (ReferenceType) rtx; + assertFalse("java.lang.Object is not generic",rt.isGeneric()); + } + +} diff --git a/weaver/testsrc/org/aspectj/weaver/TypeVariableReferenceTypeTestCase.java b/weaver/testsrc/org/aspectj/weaver/TypeVariableReferenceTypeTestCase.java new file mode 100644 index 000000000..de75f49be --- /dev/null +++ b/weaver/testsrc/org/aspectj/weaver/TypeVariableReferenceTypeTestCase.java @@ -0,0 +1,57 @@ +/* ******************************************************************* + * 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 testConstructionByNameAndBound() { + TypeVariableReferenceType tvrt = new TypeVariableReferenceType("T",javaLangClass,true,world); + assertEquals("T",tvrt.getTypeVariableName()); + assertTrue(tvrt.isExtends); + assertEquals(javaLangClass,tvrt.getUpperBound()); + } + + public void testConstructionByNameAndVariable() { + TypeVariable tv = new TypeVariable("T",javaLangClass); + TypeVariableReferenceType tvrt = new TypeVariableReferenceType(tv,world); + assertEquals("T",tvrt.getTypeVariableName()); + assertTrue(tvrt.isExtends); + assertEquals(javaLangClass,tvrt.getUpperBound()); + } + + protected void setUp() throws Exception { + super.setUp(); + world = new BcelWorld(); + javaLangClass = (ReferenceType) world.resolve(TypeX.forName("java/lang/Class")); + javaLangObject = (ReferenceType) world.resolve(TypeX.OBJECT); + extendsClass = new BoundedReferenceType(javaLangClass,true,world); + superClass = new BoundedReferenceType(javaLangClass,false,world); + extendsWithExtras = new BoundedReferenceType(javaLangClass,true,world, + new ReferenceType[] {(ReferenceType)world.resolve(TypeX.forName("java/util/List"))}); + } + +} diff --git a/weaver/testsrc/org/aspectj/weaver/TypeXTestCase.java b/weaver/testsrc/org/aspectj/weaver/TypeXTestCase.java index 500baacd8..ca6b27329 100644 --- a/weaver/testsrc/org/aspectj/weaver/TypeXTestCase.java +++ b/weaver/testsrc/org/aspectj/weaver/TypeXTestCase.java @@ -104,6 +104,15 @@ public class TypeXTestCase extends TestCase { // System.err.println(tx.dump()); } + public void testTypeXForParameterizedTypes() { + TypeX stringType = TypeX.forName("java/lang/String"); + TypeX listOfStringType = TypeX.forParameterizedTypes("java/util/List", new TypeX[] {stringType}); + assertEquals("1 type param",1,listOfStringType.typeParameters.length); + assertEquals(stringType,listOfStringType.typeParameters[0]); + assertTrue(listOfStringType.isParameterized()); + assertFalse(listOfStringType.isGeneric()); + } + private void checkTX(TypeX tx,boolean shouldBeParameterized,int numberOfTypeParameters) { assertTrue("Expected parameterization flag to be "+shouldBeParameterized,tx.isParameterized()==shouldBeParameterized); if (numberOfTypeParameters==0) { -- 2.39.5