use of org.evosuite.testcase.variable.VariableReference 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.testcase.variable.VariableReference in project evosuite by EvoSuite.
the class TestDSETestSuiteCookie method buildTestCase0.
/**
* Creates the test case:
*
* <code>
* int int0 = 10;
* int int1 = 10;
* int int2 = 10;
* Foo.bar(int0,int1,int2);
* </code>
*
* @return
* @throws NoSuchMethodException
* @throws SecurityException
* @throws ClassNotFoundException
*/
private static DefaultTestCase buildTestCase0() throws NoSuchMethodException, SecurityException, ClassNotFoundException {
TestCaseBuilder builder = new TestCaseBuilder();
VariableReference int0 = builder.appendIntPrimitive(10);
VariableReference int1 = builder.appendIntPrimitive(10);
VariableReference int2 = builder.appendIntPrimitive(10);
Class<?> fooClass = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
Method barMethod = fooClass.getMethod("bar", int.class, int.class, int.class);
builder.appendMethod(null, barMethod, int0, int1, int2);
return builder.getDefaultTestCase();
}
use of org.evosuite.testcase.variable.VariableReference 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.testcase.variable.VariableReference in project evosuite by EvoSuite.
the class FunctionalMockStatementTest method testPackageLevel_differentPackage_instrumentation_public.
/*
* This test fails when Mockito is instrumented (it would pass if org.mockito. is excluded from instrumentation.
* However, in the packaged version, Mockito is shaded and thus isn't actually instrumented. I don't have a good
* solution to fix this test, hence I've marked it as ignored. (Gordon, 9.2.2018)
*/
@Test
public void testPackageLevel_differentPackage_instrumentation_public() throws Exception {
TestCase tc = new DefaultTestCase();
RuntimeInstrumentation.setAvoidInstrumentingShadedClasses(true);
ClassPathHandler.getInstance().changeTargetCPtoTheSameAsEvoSuite();
InstrumentingClassLoader loader = new InstrumentingClassLoader();
Class<?> example = loader.loadClass("com.examples.with.different.packagename.fm.ExamplePublicLevel");
VariableReference ref = new VariableReferenceImpl(tc, example);
FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, example);
tc.addStatement(mockStmt);
execute(tc);
}
use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.
the class FunctionalMockStatementTest method testAll_once.
@Test
public void testAll_once() throws Exception {
TestCase tc = new DefaultTestCase();
VariableReference ref = new VariableReferenceImpl(tc, Foo.class);
FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, Foo.class);
VariableReference mock = tc.addStatement(mockStmt);
VariableReference result = tc.addStatement(new MethodStatement(tc, new GenericMethod(this.getClass().getDeclaredMethod("all_once", Foo.class), FunctionalMockStatementTest.class), null, Arrays.asList(mock)));
Assert.assertFalse(mockStmt.doesNeedToUpdateInputs());
Assert.assertEquals(0, mockStmt.getNumParameters());
// execute first time with default mock
Scope scope = execute(tc);
Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
List<Type> types = mockStmt.updateMockedMethods();
Assert.assertEquals(7, types.size());
}
Aggregations