use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.
the class TestCodeVisitorTest method testGenerics_staticMethodWithExtends.
@Test
public void testGenerics_staticMethodWithExtends() throws NoSuchMethodException, ConstructionFailedException {
// first construct a test case for the Generic method
TestCase tc = new DefaultTestCase();
TestFactory.getInstance().addConstructor(tc, new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0);
Method m = TestCodeVisitorTest.class.getDeclaredMethod("foo", Servlet.class);
GenericMethod gm = new GenericMethod(m, TestCodeVisitorTest.class);
TestFactory.getInstance().addMethod(tc, gm, 1, 0);
// Check if generic types were correctly analyzed/inferred
Type[] types = gm.getParameterTypes();
// only 1 input
assertEquals(1, types.length);
Type type = types[0];
Assert.assertNotNull(type);
WildcardTypeImpl wt = (WildcardTypeImpl) type;
assertEquals(0, wt.getLowerBounds().length);
assertEquals(1, wt.getUpperBounds().length);
Class<?> upper = (Class<?>) wt.getUpperBounds()[0];
assertEquals(Object.class, upper);
// Finally, visit the test
TestCodeVisitor visitor = new TestCodeVisitor();
// should not throw exception
tc.accept(visitor);
}
use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.
the class FunctionalMockStatementTest method testAll_twice.
@Test
public void testAll_twice() 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_twice", 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(14, types.size());
}
use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.
the class FunctionalMockStatementTest method testBase.
@Test
public void testBase() throws Exception {
TestCase tc = new DefaultTestCase();
final int MOCKED_VALUE = 42;
VariableReference mockedInput = tc.addStatement(new IntPrimitiveStatement(tc, MOCKED_VALUE));
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("base", Foo.class), FunctionalMockStatementTest.class), null, Arrays.asList(mock)));
// if not executed, should be no way to tell if needs new inputs
Assert.assertFalse(mockStmt.doesNeedToUpdateInputs());
Assert.assertEquals(0, mockStmt.getNumParameters());
// execute first time with default mock
Scope scope = execute(tc);
Integer val = (Integer) scope.getObject(result);
// default mock value should be 0
Assert.assertEquals(0, val.intValue());
// after execution, there should be one variable to provide
Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
List<Type> types = mockStmt.updateMockedMethods();
Assert.assertEquals(1, types.size());
Assert.assertEquals(int.class, types.get(0));
// add int variable to list of mock expected returns
mockStmt.addMissingInputs(Arrays.asList(mockedInput));
Assert.assertEquals(1, mockStmt.getNumParameters());
Assert.assertTrue(mockStmt.getParameterReferences().get(0).same(mockedInput));
// re-execute with initialized mock
scope = new Scope();
for (Statement st : tc) {
st.execute(scope, System.out);
}
val = (Integer) scope.getObject(result);
Assert.assertEquals(MOCKED_VALUE, val.intValue());
}
use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.
the class TestTestSuiteMinimizer method minimizeSuiteFullCoverageWithTwoFitnessFunctionsMinimizeTestsEnabled.
@Test
public void minimizeSuiteFullCoverageWithTwoFitnessFunctionsMinimizeTestsEnabled() throws ClassNotFoundException, NoSuchFieldException, SecurityException, ConstructionFailedException, NoSuchMethodException {
Properties.TARGET_CLASS = FlagExample1.class.getCanonicalName();
Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
GenericClass clazz = new GenericClass(sut);
DefaultTestCase test = new DefaultTestCase();
GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
TestFactory testFactory = TestFactory.getInstance();
testFactory.addConstructor(test, gc, 0, 0);
List<VariableReference> parameters = new ArrayList<VariableReference>();
for (int i = 0; i < 10; i++) {
IntPrimitiveStatement ips = new IntPrimitiveStatement(test, 28234 + i);
VariableReference vr = test.addStatement(ips, i + 1);
}
ConstructorStatement ct = new ConstructorStatement(test, gc, parameters);
Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class });
GenericMethod method = new GenericMethod(m, sut);
testFactory.addMethod(test, method, 11, 0);
parameters = new ArrayList<VariableReference>();
for (int i = 12; i < 15; i++) {
IntPrimitiveStatement ips = new IntPrimitiveStatement(test, i);
VariableReference vr = test.addStatement(ips, i);
}
ct = new ConstructorStatement(test, gc, parameters);
testFactory.addMethod(test, method, 15, 0);
assertEquals(16, test.size());
TestSuiteChromosome tsc = new TestSuiteChromosome();
tsc.addTest(test);
TestSuiteFitnessFunction branch = new BranchCoverageSuiteFitness();
double previous_branch_fitness = branch.getFitness(tsc);
tsc.setFitness(branch, previous_branch_fitness);
assertEquals(previous_branch_fitness, 0.0, 0.0);
TestSuiteFitnessFunction defuse = new DefUseCoverageSuiteFitness();
double previous_defuse_fitness = defuse.getFitness(tsc);
tsc.setFitness(defuse, previous_defuse_fitness);
assertEquals(previous_defuse_fitness, 0.0, 0.0);
List<TestFitnessFactory<? extends TestFitnessFunction>> factories = new ArrayList<TestFitnessFactory<? extends TestFitnessFunction>>();
factories.add(new BranchCoverageFactory());
factories.add(new DefUseCoverageFactory());
TestSuiteMinimizer minimizer = new TestSuiteMinimizer(factories);
minimizer.minimize(tsc, true);
assertEquals(2, tsc.getTests().size());
assertEquals(3, tsc.getTests().get(0).size());
assertEquals(3, tsc.getTests().get(1).size());
// assertTrue(tsc.getTests().get(0).toCode().equals("FlagExample1 flagExample1_0 = new FlagExample1();\nint int0 = 28234;\nflagExample1_0.testMe(int0);\n"));
// assertTrue(tsc.getTests().get(1).toCode().equals("FlagExample1 flagExample1_0 = new FlagExample1();\nint int0 = 28241;\nflagExample1_0.testMe(int0);\n"));
double branch_fitness = branch.getFitness(tsc);
assertEquals(previous_branch_fitness, branch_fitness, 0.0);
double defuse_fitness = defuse.getFitness(tsc);
assertEquals(previous_defuse_fitness, defuse_fitness, 0.0);
}
use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.
the class TestTestSuiteMinimizer method minimizeSuiteHalfCoverage.
@Test
public void minimizeSuiteHalfCoverage() throws ClassNotFoundException, NoSuchFieldException, SecurityException, ConstructionFailedException, NoSuchMethodException {
Properties.TARGET_CLASS = FlagExample1.class.getCanonicalName();
Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
GenericClass clazz = new GenericClass(sut);
DefaultTestCase test = new DefaultTestCase();
GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
TestFactory testFactory = TestFactory.getInstance();
testFactory.addConstructor(test, gc, 0, 0);
List<VariableReference> parameters = new ArrayList<VariableReference>();
for (int i = 0; i < 10; i++) {
IntPrimitiveStatement ips = new IntPrimitiveStatement(test, 28234 + i);
VariableReference vr = test.addStatement(ips, i + 1);
}
ConstructorStatement ct = new ConstructorStatement(test, gc, parameters);
Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class });
GenericMethod method = new GenericMethod(m, sut);
testFactory.addMethod(test, method, 11, 0);
assertEquals(12, test.size());
TestSuiteChromosome tsc = new TestSuiteChromosome();
tsc.addTest(test);
TestSuiteFitnessFunction ff = new BranchCoverageSuiteFitness();
double previous_fitness = ff.getFitness(tsc);
tsc.setFitness(ff, previous_fitness);
assertEquals(previous_fitness, 2.0, 0.0);
TestSuiteMinimizer minimizer = new TestSuiteMinimizer(new BranchCoverageFactory());
minimizer.minimize(tsc, false);
assertEquals(1, tsc.getTests().size());
assertEquals(3, tsc.getTests().get(0).size());
// assertTrue(tsc.getTests().get(0).toCode().equals("FlagExample1 flagExample1_0 = new FlagExample1();\nint int0 = 28234;\nflagExample1_0.testMe(int0);\n"));
double fitness = ff.getFitness(tsc);
assertEquals(previous_fitness, fitness, 0.0);
}
Aggregations