use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.
the class TestCodeVisitorTest method testInnerClassAbstractEnum.
/*
* There are some weird enum constructs in Closure, so we need to check that enum names
* don't contain the name of the anonymous class they might represent
*/
@Test
public void testInnerClassAbstractEnum() throws NoSuchMethodException, ConstructionFailedException {
// first construct a test case for the Generic method
TestCase tc = new DefaultTestCase();
VariableReference userObject = TestFactory.getInstance().addConstructor(tc, new GenericConstructor(AbstractEnumUser.class.getDeclaredConstructor(), AbstractEnumUser.class), 0, 0);
EnumPrimitiveStatement primitiveStatement = new EnumPrimitiveStatement(tc, AbstractEnumInInnerClass.AnEnum.class);
primitiveStatement.setValue(AbstractEnumInInnerClass.AnEnum.FOO);
VariableReference enumObject = tc.addStatement(primitiveStatement);
Method m = AbstractEnumUser.class.getDeclaredMethod("foo", AbstractEnumInInnerClass.AnEnum.class);
GenericMethod gm = new GenericMethod(m, AbstractEnumUser.class);
MethodStatement ms = new MethodStatement(tc, gm, userObject, Arrays.asList(enumObject));
tc.addStatement(ms);
// Finally, visit the test
TestCodeVisitor visitor = new TestCodeVisitor();
// should not throw exception
tc.accept(visitor);
String code = visitor.getCode();
System.out.println(code);
assertFalse(code.contains("= AbstractEnumInInnerClass.AnEnum.1.FOO"));
assertTrue(code.contains("= AbstractEnumInInnerClass.AnEnum.FOO"));
}
use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.
the class TestCodeVisitorTest method testInnerClassEnum.
@Test
public void testInnerClassEnum() throws Throwable {
// first construct a test case for the Generic method
TestCase tc = new DefaultTestCase();
VariableReference userObject = TestFactory.getInstance().addConstructor(tc, new GenericConstructor(EnumUser.class.getDeclaredConstructor(), EnumUser.class), 0, 0);
EnumPrimitiveStatement primitiveStatement = new EnumPrimitiveStatement(tc, EnumInInnerClass.AnEnum.class);
primitiveStatement.setValue(EnumInInnerClass.AnEnum.FOO);
VariableReference enumObject = tc.addStatement(primitiveStatement);
Method m = EnumUser.class.getDeclaredMethod("foo", EnumInInnerClass.AnEnum.class);
GenericMethod gm = new GenericMethod(m, EnumUser.class);
MethodStatement ms = new MethodStatement(tc, gm, userObject, Arrays.asList(enumObject));
tc.addStatement(ms);
// Finally, visit the test
TestCodeVisitor visitor = new TestCodeVisitor();
// should not throw exception
tc.accept(visitor);
String code = visitor.getCode();
assertTrue(code.contains("= EnumInInnerClass.AnEnum.FOO"));
}
use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.
the class TestGenericAccessibleObject method testLinkedList.
@Test
public void testLinkedList() throws SecurityException, NoSuchMethodException, ConstructionFailedException {
Class<?> targetClass = java.util.LinkedList.class;
Method targetMethod = targetClass.getMethod("get", new Class<?>[] { int.class });
GenericMethod genericMethod = new GenericMethod(targetMethod, targetClass);
System.out.println(genericMethod.getGeneratedClass().toString());
Assert.assertTrue(genericMethod.getGeneratedClass().hasWildcardOrTypeVariables());
GenericMethod instantiatedMethod = genericMethod.getGenericInstantiation();
System.out.println(instantiatedMethod.getGeneratedClass().toString());
Assert.assertFalse(instantiatedMethod.getGeneratedClass().hasWildcardOrTypeVariables());
}
use of org.evosuite.utils.generic.GenericMethod 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 org.evosuite.utils.generic.GenericMethod 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());
}
Aggregations