Search in sources :

Example 6 with TypeToken

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));
}
Also used : HashMap(java.util.HashMap) Annotation(java.lang.annotation.Annotation) WildcardType(java.lang.reflect.WildcardType) AnnotatedType(java.lang.reflect.AnnotatedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) AnnotatedType(java.lang.reflect.AnnotatedType) TypeVariable(java.lang.reflect.TypeVariable) GenericClass(org.evosuite.utils.generic.GenericClass) TypeToken(com.googlecode.gentyref.TypeToken) GenericClass(org.evosuite.utils.generic.GenericClass) Test(org.junit.Test)

Example 7 with TypeToken

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));
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass) TypeToken(com.googlecode.gentyref.TypeToken) Test(org.junit.Test)

Example 8 with TypeToken

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());
}
Also used : InspectorAssertion(org.evosuite.assertion.InspectorAssertion) ArrayList(java.util.ArrayList) GenericMethod(org.evosuite.utils.generic.GenericMethod) GenericClass(org.evosuite.utils.generic.GenericClass) Inspector(org.evosuite.assertion.Inspector) InstrumentingClassLoader(org.evosuite.instrumentation.InstrumentingClassLoader) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) Assertion(org.evosuite.assertion.Assertion) InspectorAssertion(org.evosuite.assertion.InspectorAssertion) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) InstrumentingClassLoader(org.evosuite.instrumentation.InstrumentingClassLoader) TypeToken(com.googlecode.gentyref.TypeToken) Test(org.junit.Test)

Example 9 with TypeToken

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());
}
Also used : GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) GenericClass(org.evosuite.utils.generic.GenericClass) TypeToken(com.googlecode.gentyref.TypeToken) Test(org.junit.Test)

Example 10 with TypeToken

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);
}
Also used : GuavaExample4(com.examples.with.different.packagename.generic.GuavaExample4) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) GenericClass(org.evosuite.utils.generic.GenericClass) TypeToken(com.googlecode.gentyref.TypeToken) Test(org.junit.Test)

Aggregations

TypeToken (com.googlecode.gentyref.TypeToken)20 GenericClass (org.evosuite.utils.generic.GenericClass)20 Test (org.junit.Test)20 Method (java.lang.reflect.Method)6 GenericMethod (org.evosuite.utils.generic.GenericMethod)6 ParameterizedType (java.lang.reflect.ParameterizedType)3 AnnotatedType (java.lang.reflect.AnnotatedType)2 Type (java.lang.reflect.Type)2 WildcardType (java.lang.reflect.WildcardType)2 LinkedList (java.util.LinkedList)2 GuavaExample4 (com.examples.with.different.packagename.generic.GuavaExample4)1 Annotation (java.lang.annotation.Annotation)1 TypeVariable (java.lang.reflect.TypeVariable)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Assertion (org.evosuite.assertion.Assertion)1 Inspector (org.evosuite.assertion.Inspector)1 InspectorAssertion (org.evosuite.assertion.InspectorAssertion)1 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)1