use of com.googlecode.gentyref.TypeToken in project evosuite by EvoSuite.
the class TestGenericClass method testIterableAndListBoundaries.
@SuppressWarnings("rawtypes")
@Test
public void testIterableAndListBoundaries() {
Map<TypeVariable<?>, Type> typeMap = new HashMap<TypeVariable<?>, Type>();
final GenericClass iterableIntegerClass = new GenericClass(new TypeToken<java.lang.Iterable<Integer>>() {
}.getType());
TypeVariable<?> var = new TypeVariable() {
public AnnotatedType[] getAnnotatedBounds() {
return null;
}
@Override
public Type[] getBounds() {
return new Type[] { iterableIntegerClass.getType() };
}
@Override
public GenericDeclaration getGenericDeclaration() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getName() {
return "Test";
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Dummy Variable";
}
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
// TODO Auto-generated method stub
return null;
}
public Annotation[] getAnnotations() {
// TODO Auto-generated method stub
return null;
}
public Annotation[] getDeclaredAnnotations() {
// TODO Auto-generated method stub
return null;
}
};
typeMap.put(var, iterableIntegerClass.getType());
GenericClass arrayListClass = new GenericClass(new TypeToken<java.util.ArrayList<String>>() {
}.getType());
Assert.assertFalse(arrayListClass.satisfiesBoundaries(var, typeMap));
arrayListClass = new GenericClass(new TypeToken<java.util.ArrayList<Integer>>() {
}.getType());
Assert.assertTrue(arrayListClass.satisfiesBoundaries(var, typeMap));
}
use of com.googlecode.gentyref.TypeToken in project evosuite by EvoSuite.
the class TestGenericClass method testWildcardWithSuperIntegerBoundaryInstantiation.
@Test
public void testWildcardWithSuperIntegerBoundaryInstantiation() throws ConstructionFailedException {
GenericClass integerWildcardListClass = new GenericClass(new TypeToken<java.util.List<? super Integer>>() {
}.getType());
GenericClass integerListClass = new GenericClass(new TypeToken<java.util.List<Integer>>() {
}.getType());
GenericClass numberListClass = new GenericClass(new TypeToken<java.util.List<Number>>() {
}.getType());
GenericClass objectListClass = new GenericClass(new TypeToken<java.util.List<Object>>() {
}.getType());
Assert.assertTrue(integerWildcardListClass.isAssignableFrom(integerListClass));
Assert.assertTrue(integerWildcardListClass.isAssignableFrom(numberListClass));
Assert.assertTrue(integerWildcardListClass.isAssignableFrom(objectListClass));
GenericClass integerWildcardListInstantiation = integerWildcardListClass.getGenericInstantiation();
Assert.assertTrue(integerWildcardListClass.isAssignableFrom(integerWildcardListInstantiation));
}
use of com.googlecode.gentyref.TypeToken in project evosuite by EvoSuite.
the class TestGenericAccessibleObject method testClassLoaderChange.
@Test
public void testClassLoaderChange() throws NoSuchMethodException, SecurityException, ConstructionFailedException {
Class<?> targetClass = com.examples.with.different.packagename.generic.GenericClassTwoParameters.class;
Method creatorMethod = targetClass.getMethod("create", new Class<?>[] {});
Method targetMethod = targetClass.getMethod("get", new Class<?>[] { Object.class });
Method inspectorMethod = targetClass.getMethod("testMe", new Class<?>[] {});
Constructor<?> intConst = Integer.class.getConstructor(new Class<?>[] { int.class });
GenericClass listOfInteger = new GenericClass(new TypeToken<com.examples.with.different.packagename.generic.GenericClassTwoParameters<Integer, Integer>>() {
}.getType());
GenericMethod genericCreatorMethod = new GenericMethod(creatorMethod, targetClass).getGenericInstantiationFromReturnValue(listOfInteger);
System.out.println(genericCreatorMethod.getGeneratedClass().toString());
GenericMethod genericMethod = new GenericMethod(targetMethod, targetClass).copyWithNewOwner(genericCreatorMethod.getGeneratedClass());
System.out.println(genericMethod.getGeneratedClass().toString());
DefaultTestCase test = new DefaultTestCase();
MethodStatement ms1 = new MethodStatement(test, genericCreatorMethod, (VariableReference) null, new ArrayList<VariableReference>());
test.addStatement(ms1);
IntPrimitiveStatement ps1 = (IntPrimitiveStatement) PrimitiveStatement.getPrimitiveStatement(test, int.class);
test.addStatement(ps1);
GenericConstructor intConstructor = new GenericConstructor(intConst, Integer.class);
List<VariableReference> constParam = new ArrayList<VariableReference>();
constParam.add(ps1.getReturnValue());
ConstructorStatement cs1 = new ConstructorStatement(test, intConstructor, constParam);
// test.addStatement(cs1);
List<VariableReference> callParam = new ArrayList<VariableReference>();
callParam.add(ps1.getReturnValue());
MethodStatement ms2 = new MethodStatement(test, genericMethod, ms1.getReturnValue(), callParam);
test.addStatement(ms2);
Inspector inspector = new Inspector(targetClass, inspectorMethod);
Assertion assertion = new InspectorAssertion(inspector, ms2, ms1.getReturnValue(), 0);
ms2.addAssertion(assertion);
String code = test.toCode();
ClassLoader loader = new InstrumentingClassLoader();
Properties.TARGET_CLASS = targetClass.getCanonicalName();
Properties.CRITERION = new Criterion[1];
Properties.CRITERION[0] = Criterion.MUTATION;
DefaultTestCase testCopy = test.clone();
testCopy.changeClassLoader(loader);
String code2 = testCopy.toCode();
Assert.assertEquals(code, code2);
Assert.assertEquals(code, test.toCode());
testCopy.removeAssertion(assertion);
Assert.assertEquals(code, test.toCode());
// test.removeAssertion(assertion);
test.removeAssertions();
System.out.println(test.toCode());
}
use of com.googlecode.gentyref.TypeToken in project evosuite by EvoSuite.
the class TestGenericAccessibleObject method testGuavaExample3.
@Test
public void testGuavaExample3() throws SecurityException, NoSuchMethodException, ConstructionFailedException {
Class<?> targetClass = com.examples.with.different.packagename.generic.GuavaExample3.class;
GenericClass genericInstantiation = new GenericClass(new TypeToken<com.examples.with.different.packagename.generic.GuavaExample3<String, String, Object>>() {
}.getType());
Method targetMethod = targetClass.getMethod("create", new Class<?>[] { com.examples.with.different.packagename.generic.GuavaExample3.class });
GenericMethod genericMethod = new GenericMethod(targetMethod, targetClass);
System.out.println(genericMethod.getGeneratedClass().toString());
Assert.assertTrue(genericMethod.getGeneratedClass().hasWildcardOrTypeVariables());
System.out.println("------------------");
GenericMethod instantiatedMethod = genericMethod.getGenericInstantiationFromReturnValue(genericInstantiation);
System.out.println(instantiatedMethod.getGeneratedClass().toString());
Assert.assertFalse(instantiatedMethod.getGeneratedClass().hasWildcardOrTypeVariables());
Assert.assertEquals(genericInstantiation, instantiatedMethod.getGeneratedClass());
}
use of com.googlecode.gentyref.TypeToken in project evosuite by EvoSuite.
the class TestGenericAccessibleObject method testGenericMethodFromReturnValueTypeVariable2.
@Test
public void testGenericMethodFromReturnValueTypeVariable2() throws SecurityException, NoSuchMethodException, ConstructionFailedException {
Class<?> targetClass = com.examples.with.different.packagename.generic.GuavaExample4.class;
Method targetMethod = targetClass.getMethod("create", new Class<?>[] {});
GenericMethod genericMethod = new GenericMethod(targetMethod, targetClass);
GenericClass iterableIntegerClass = new GenericClass(new TypeToken<com.examples.with.different.packagename.generic.GuavaExample4<java.lang.Iterable<Integer>>>() {
}.getType());
GenericMethod instantiatedMethod = genericMethod.getGenericInstantiationFromReturnValue(iterableIntegerClass);
System.out.println(instantiatedMethod.getGeneratedClass().toString());
Assert.assertEquals(instantiatedMethod.getGeneratedClass().getRawClass(), GuavaExample4.class);
}
Aggregations