use of org.evosuite.utils.generic.GenericConstructor in project evosuite by EvoSuite.
the class FactoryTestSystemTest method testFieldForWrongPosition.
@Test(expected = ConstructionFailedException.class)
public void testFieldForWrongPosition() throws ConstructionFailedException, NoSuchMethodException, SecurityException, ClassNotFoundException, NoSuchFieldException {
TestFactory testFactory = TestFactory.getInstance();
Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(FactoryExample.class.getCanonicalName());
GenericConstructor constructor = new GenericConstructor(sut.getConstructor(), sut);
GenericField field = new GenericField(sut.getField("setMe"), sut);
DefaultTestCase test = new DefaultTestCase();
Properties.PRIMITIVE_REUSE_PROBABILITY = 0.0;
Properties.OBJECT_REUSE_PROBABILITY = 0.0;
VariableReference var1 = testFactory.addConstructor(test, constructor, 0, 0);
testFactory.reset();
testFactory.addFieldFor(test, var1, field, 0);
}
use of org.evosuite.utils.generic.GenericConstructor in project evosuite by EvoSuite.
the class InputCoverageFitnessFunctionSystemTest method testInputCoverageClassWithField.
@Test
public void testInputCoverageClassWithField() throws NoSuchFieldException, NoSuchMethodException {
Class<?> sut = ClassWithField.class;
DefaultTestCase tc = new DefaultTestCase();
// ClassWithField classWithField0 = new ClassWithField();
GenericConstructor constructor = new GenericConstructor(sut.getConstructors()[0], sut);
ConstructorStatement constructorStatement = new ConstructorStatement(tc, constructor, Arrays.asList(new VariableReference[] {}));
VariableReference obj = tc.addStatement(constructorStatement);
// classWithField0.testFoo(classWithField0.BOOLEAN_FIELD);
FieldReference field = new FieldReference(tc, new GenericField(sut.getDeclaredField("BOOLEAN_FIELD"), sut), obj);
Method m = sut.getMethod("testFoo", new Class<?>[] { Boolean.TYPE });
GenericMethod gm = new GenericMethod(m, sut);
tc.addStatement(new MethodStatement(tc, gm, obj, Arrays.asList(new VariableReference[] { field })));
// classWithField0.BOOLEAN_FIELD = false;
VariableReference boolRef = tc.addStatement(new BooleanPrimitiveStatement(tc, false));
tc.addStatement(new AssignmentStatement(tc, field, boolRef));
tc.addStatement(new MethodStatement(tc, gm, obj, Arrays.asList(new VariableReference[] { field })));
Properties.TARGET_CLASS = sut.getCanonicalName();
Properties.JUNIT_TESTS = true;
TestSuiteChromosome testSuite = new TestSuiteChromosome();
testSuite.addTest(tc);
FitnessFunction ffunction = FitnessFunctions.getFitnessFunction(Properties.Criterion.INPUT);
assertEquals("Should be 0.0", 0.0, ffunction.getFitness(testSuite), 0.0);
assertEquals("Should be 1.0", 1.0, testSuite.getCoverage(ffunction), 0.0);
}
use of org.evosuite.utils.generic.GenericConstructor in project evosuite by EvoSuite.
the class DeleteMutationSystemTest method getTwoIntTest.
private TestCase getTwoIntTest(int x, int y, int x1, int y1) throws NoSuchMethodException, SecurityException, ConstructionFailedException, ClassNotFoundException {
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();
VariableReference callee = testFactory.addConstructor(test, gc, 0, 0);
test.addStatement(new IntPrimitiveStatement(test, x));
test.addStatement(new IntPrimitiveStatement(test, y));
VariableReference wrongIntVar1 = test.addStatement(new IntPrimitiveStatement(test, x1));
VariableReference wrongIntVar2 = test.addStatement(new IntPrimitiveStatement(test, y1));
Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class, int.class });
GenericMethod method = new GenericMethod(m, sut);
MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] { wrongIntVar1, wrongIntVar2 }));
test.addStatement(ms);
return test;
}
use of org.evosuite.utils.generic.GenericConstructor in project evosuite by EvoSuite.
the class PoolSystemTest method testPoolWithException.
@Test
public void testPoolWithException() throws IOException, NoSuchMethodException, SecurityException {
File f = File.createTempFile("EvoSuiteTestPool", null, FileUtils.getTempDirectory());
String filename = f.getAbsolutePath();
f.delete();
System.out.println(filename);
EvoSuite evosuite = new EvoSuite();
String targetClass = DependencyClassWithException.class.getCanonicalName();
Properties.TARGET_CLASS = targetClass;
Properties.getTargetClassAndDontInitialise();
TestCase test = new DefaultTestCase();
VariableReference instance = test.addStatement(new ConstructorStatement(test, new GenericConstructor(DependencyClassWithException.class.getConstructors()[0], DependencyClassWithException.class), new ArrayList<VariableReference>()));
VariableReference int42 = test.addStatement(new IntPrimitiveStatement(test, 42));
GenericMethod foo = new GenericMethod(DependencyClassWithException.class.getMethod("foo", int.class), DependencyClassWithException.class);
test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
String[] command = new String[] { "-generateSuite", "-class", targetClass };
TestSuiteChromosome best = new TestSuiteChromosome();
best.addTest(test);
ObjectPool pool = new ObjectPool();
pool.addSequence(new GenericClass(DependencyClassWithException.class), test);
pool.writePool(filename);
System.out.println("EvolvedTestSuite:\n" + best);
resetStaticVariables();
setDefaultPropertiesForTestCases();
targetClass = ClassDependingOnExceptionClass.class.getCanonicalName();
Properties.TARGET_CLASS = targetClass;
Properties.P_OBJECT_POOL = 0.8;
Properties.OBJECT_POOLS = filename;
ObjectPoolManager.getInstance().initialisePool();
// Properties.SEARCH_BUDGET = 50000;
command = new String[] { "-generateSuite", "-class", targetClass };
Object result = evosuite.parseCommandLine(command);
GeneticAlgorithm<?> ga = getGAFromResult(result);
best = (TestSuiteChromosome) ga.getBestIndividual();
System.out.println("EvolvedTestSuite:\n" + best);
Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001);
f = new File(filename);
f.delete();
}
use of org.evosuite.utils.generic.GenericConstructor in project evosuite by EvoSuite.
the class TestConstantInliner method testArrayIndexInlining.
@Test
public void testArrayIndexInlining() throws NoSuchMethodException, SecurityException {
DefaultTestCase test = new DefaultTestCase();
ConstructorStatement cs = new ConstructorStatement(test, new GenericConstructor(Object.class.getConstructor(), Object.class), new ArrayList<VariableReference>());
VariableReference objectVar = test.addStatement(cs);
ArrayStatement as = new ArrayStatement(test, Object[].class, 3);
test.addStatement(as);
ArrayReference arrayVar = as.getArrayReference();
ArrayIndex ai0 = new ArrayIndex(test, arrayVar, 0);
ArrayIndex ai1 = new ArrayIndex(test, arrayVar, 1);
ArrayIndex ai2 = new ArrayIndex(test, arrayVar, 2);
test.addStatement(new AssignmentStatement(test, ai0, objectVar));
test.addStatement(new AssignmentStatement(test, ai1, objectVar));
test.addStatement(new AssignmentStatement(test, ai2, objectVar));
ConstructorStatement sutCS = new ConstructorStatement(test, new GenericConstructor(ObjectParameter.class.getConstructor(), ObjectParameter.class), new ArrayList<VariableReference>());
VariableReference sut = test.addStatement(sutCS);
List<VariableReference> parameters = new ArrayList<VariableReference>();
parameters.add(ai0);
test.addStatement(new MethodStatement(test, new GenericMethod(ObjectParameter.class.getMethods()[0], ObjectParameter.class), sut, parameters));
parameters = new ArrayList<VariableReference>();
parameters.add(ai1);
test.addStatement(new MethodStatement(test, new GenericMethod(ObjectParameter.class.getMethods()[0], ObjectParameter.class), sut, parameters));
parameters = new ArrayList<VariableReference>();
parameters.add(ai2);
test.addStatement(new MethodStatement(test, new GenericMethod(ObjectParameter.class.getMethods()[0], ObjectParameter.class), sut, parameters));
System.out.println(test.toCode());
ConstantInliner inliner = new ConstantInliner();
inliner.inline(test);
String code = test.toCode();
assertFalse(code.contains("objectParameter0.testMe(objectArray0"));
}
Aggregations