use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.
the class EnvironmentDataSystemTest method testOnSpecificTest.
@Test
public void testOnSpecificTest() throws ClassNotFoundException, ConstructionFailedException, NoSuchMethodException, SecurityException {
Properties.TARGET_CLASS = DseBar.class.getCanonicalName();
Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
Class<?> fooClass = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(DseFoo.class.getCanonicalName());
GenericClass clazz = new GenericClass(sut);
DefaultTestCase test = new DefaultTestCase();
// String string0 = "baz5";
VariableReference stringVar = test.addStatement(new StringPrimitiveStatement(test, "baz5"));
// DseFoo dseFoo0 = new DseFoo();
GenericConstructor fooConstructor = new GenericConstructor(fooClass.getConstructors()[0], fooClass);
ConstructorStatement fooConstructorStatement = new ConstructorStatement(test, fooConstructor, Arrays.asList(new VariableReference[] {}));
VariableReference fooVar = test.addStatement(fooConstructorStatement);
// String fileName = new String("/home/galeotti/README.txt")
String path = "/home/galeotti/README.txt";
EvoSuiteFile evosuiteFile = new EvoSuiteFile(path);
FileNamePrimitiveStatement fileNameStmt = new FileNamePrimitiveStatement(test, evosuiteFile);
test.addStatement(fileNameStmt);
Method fooIncMethod = fooClass.getMethod("inc", new Class<?>[] {});
GenericMethod incMethod = new GenericMethod(fooIncMethod, fooClass);
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
// DseBar dseBar0 = new DseBar(string0);
GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
ConstructorStatement constructorStatement = new ConstructorStatement(test, gc, Arrays.asList(new VariableReference[] { stringVar }));
VariableReference callee = test.addStatement(constructorStatement);
// dseBar0.coverMe(dseFoo0);
Method m = clazz.getRawClass().getMethod("coverMe", new Class<?>[] { fooClass });
GenericMethod method = new GenericMethod(m, sut);
MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] { fooVar }));
test.addStatement(ms);
System.out.println(test);
TestSuiteChromosome suite = new TestSuiteChromosome();
BranchCoverageSuiteFitness fitness = new BranchCoverageSuiteFitness();
BranchCoverageMap.getInstance().searchStarted(null);
assertEquals(4.0, fitness.getFitness(suite), 0.1F);
suite.addTest(test);
assertEquals(1.0, fitness.getFitness(suite), 0.1F);
System.out.println("Test suite: " + suite);
Properties.CONCOLIC_TIMEOUT = Integer.MAX_VALUE;
TestSuiteLocalSearch localSearch = TestSuiteLocalSearch.selectTestSuiteLocalSearch();
LocalSearchObjective<TestSuiteChromosome> localObjective = new DefaultLocalSearchObjective<TestSuiteChromosome>();
localObjective.addFitnessFunction(fitness);
localSearch.doSearch(suite, localObjective);
System.out.println("Fitness: " + fitness.getFitness(suite));
System.out.println("Test suite: " + suite);
assertEquals("Local search failed to cover class", 0.0, fitness.getFitness(suite), 0.1F);
BranchCoverageMap.getInstance().searchFinished(null);
}
use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.
the class HashCodeReturnsNormallyContract method addAssertionAndComments.
@Override
public void addAssertionAndComments(Statement statement, List<VariableReference> variables, Throwable exception) {
TestCase test = statement.getTestCase();
int position = statement.getPosition();
VariableReference a = variables.get(0);
try {
Method hashCodeMethod = a.getGenericClass().getRawClass().getMethod("hashCode", new Class<?>[] {});
GenericMethod method = new GenericMethod(hashCodeMethod, a.getGenericClass());
Statement st1 = new MethodStatement(test, method, a, Arrays.asList(new VariableReference[] {}));
test.addStatement(st1, position + 1);
st1.addComment("Throws exception: " + exception.getMessage());
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.
the class ContractChecker method loadJUnitTheories.
private void loadJUnitTheories() {
if (Properties.JUNIT_THEORIES.isEmpty())
return;
for (String theoryName : Properties.JUNIT_THEORIES.split(":")) {
try {
Class<?> theory = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(theoryName);
Constructor<?> constructor = theory.getConstructor();
if (!Modifier.isPublic(constructor.getModifiers())) {
logger.info("Theory class does not have public default constructor");
continue;
}
for (Method method : theory.getDeclaredMethods()) {
if (method.isAnnotationPresent(Theory.class)) {
logger.info("Found theory method: " + method.getName());
if (method.getParameterTypes().length != 1) {
logger.info("Wrong number of arguments!");
continue;
}
try {
GenericMethod gm = new GenericMethod(method, theory);
JUnitTheoryContract contract = new JUnitTheoryContract(gm);
contracts.add(contract);
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (ClassNotFoundException e) {
logger.warn("Could not load theory " + theoryName + ": " + e);
} catch (NoSuchMethodException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SecurityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.
the class EqualsHashcodeContract method addAssertionAndComments.
@Override
public void addAssertionAndComments(Statement statement, List<VariableReference> variables, Throwable exception) {
TestCase test = statement.getTestCase();
VariableReference a = variables.get(0);
VariableReference b = variables.get(1);
try {
Method equalsMethod = a.getGenericClass().getRawClass().getMethod("equals", new Class<?>[] { Object.class });
Method hashCodeMethod = a.getGenericClass().getRawClass().getMethod("hashCode", new Class<?>[] {});
GenericMethod genericEqualsMethod = new GenericMethod(equalsMethod, a.getGenericClass());
GenericMethod genericHashCodeMethod = new GenericMethod(hashCodeMethod, a.getGenericClass());
// Create x = a.equals(b)
Statement st1 = new MethodStatement(test, genericEqualsMethod, a, Arrays.asList(new VariableReference[] { b }));
VariableReference x = test.addStatement(st1, statement.getPosition() + 1);
// Create y = a.hashCode();
Statement st2 = new MethodStatement(test, genericHashCodeMethod, a, Arrays.asList(new VariableReference[] {}));
VariableReference y = test.addStatement(st2, statement.getPosition() + 2);
// Create z = b.hashCode();
Statement st3 = new MethodStatement(test, genericHashCodeMethod, b, Arrays.asList(new VariableReference[] {}));
VariableReference z = test.addStatement(st3, statement.getPosition() + 3);
// Create w = z == z
VariableReference w = new VariableReferenceImpl(test, boolean.class);
PrimitiveExpression exp = new PrimitiveExpression(test, w, y, Operator.EQUALS, z);
w = test.addStatement(exp, statement.getPosition() + 4);
Statement newStatement = test.getStatement(w.getStPosition());
// Create assertEquals(x, w)
EqualsAssertion assertion = new EqualsAssertion();
assertion.setStatement(newStatement);
assertion.setSource(x);
assertion.setDest(w);
assertion.setValue(true);
newStatement.addAssertion(assertion);
newStatement.addComment("Violates contract equals - hashcode");
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.
the class ToStringReturnsNormallyContract method addAssertionAndComments.
@Override
public void addAssertionAndComments(Statement statement, List<VariableReference> variables, Throwable exception) {
TestCase test = statement.getTestCase();
int position = statement.getPosition();
VariableReference a = variables.get(0);
try {
Method hashCodeMethod = a.getGenericClass().getRawClass().getMethod("toString", new Class<?>[] {});
GenericMethod method = new GenericMethod(hashCodeMethod, a.getGenericClass());
Statement st1 = new MethodStatement(test, method, a, Arrays.asList(new VariableReference[] {}));
test.addStatement(st1, position + 1);
st1.addComment("Throws exception: " + exception.getMessage());
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Aggregations