use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.
the class AmplificationHelper method cloneTestMethod.
/**
* Clones a test method.
*
* Performs necessary integration with JUnit and adds timeout.
*
* @param method Method to be cloned
* @param suffix Suffix for the cloned method's name
* @return The cloned method
*/
private static CtMethod cloneTestMethod(CtMethod method, String suffix) {
CtMethod cloned_method = cloneMethod(method, suffix);
CtAnnotation testAnnotation = cloned_method.getAnnotations().stream().filter(annotation -> annotation.toString().contains("Test")).findFirst().orElse(null);
if (testAnnotation != null) {
cloned_method.removeAnnotation(testAnnotation);
}
testAnnotation = method.getFactory().Core().createAnnotation();
CtTypeReference<Object> ref = method.getFactory().Core().createTypeReference();
ref.setSimpleName("Test");
CtPackageReference refPackage = method.getFactory().Core().createPackageReference();
refPackage.setSimpleName("org.junit");
ref.setPackage(refPackage);
testAnnotation.setAnnotationType(ref);
Map<String, Object> elementValue = new HashMap<>();
elementValue.put("timeout", timeOutInMs);
testAnnotation.setElementValues(elementValue);
cloned_method.addAnnotation(testAnnotation);
cloned_method.addThrownType(method.getFactory().Type().createReference(Exception.class));
return cloned_method;
}
use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.
the class AmplificationHelper method cloneMethod.
/**
* Clones a method.
*
* @param method Method to be cloned
* @param suffix Suffix for the cloned method's name
* @return The cloned method
*/
private static CtMethod cloneMethod(CtMethod method, String suffix) {
CtMethod cloned_method = method.clone();
// rename the clone
cloned_method.setSimpleName(method.getSimpleName() + (suffix.isEmpty() ? "" : suffix + cloneNumber));
cloneNumber++;
CtAnnotation toRemove = cloned_method.getAnnotations().stream().filter(annotation -> annotation.toString().contains("Override")).findFirst().orElse(null);
if (toRemove != null) {
cloned_method.removeAnnotation(toRemove);
}
return cloned_method;
}
use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.
the class Counter method getInputOfSinceOrigin.
public static Integer getInputOfSinceOrigin(CtMethod method) {
CtMethod currentMethod = method;
CtMethod parent;
int countAssertion = 0;
while ((parent = AmplificationHelper.getAmpTestParent(currentMethod)) != null) {
countAssertion += getInputOf(currentMethod);
currentMethod = parent;
}
countAssertion += getInputOf(currentMethod);
return countAssertion;
}
use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.
the class SelectorOnDiff method getModifiedMethod.
@SuppressWarnings("unchecked")
private static Set<CtMethod> getModifiedMethod(String pathFile1, String pathFile2) {
try {
final File file1 = new File(pathFile1);
final File file2 = new File(pathFile2);
if (!file1.exists() || !file2.exists()) {
return Collections.emptySet();
}
Diff result = (new AstComparator()).compare(file1, file2);
return result.getRootOperations().stream().map(operation -> operation.getSrcNode().getParent(CtMethod.class)).collect(Collectors.toSet());
} catch (Exception ignored) {
// if something bad happen, we do not care, we go for next file
return Collections.emptySet();
}
}
use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.
the class AssertGeneratorHelperTest method testNoInstrumentationOnGeneratedObject.
/**
* this test aims at verifying that dspot does not generate assertion for generated object.
* To do this, it will check that the instrumentation does not add observation points on those objects.
* If no observation point is added, any assertion would be generated.
*/
@Test
public void testNoInstrumentationOnGeneratedObject() throws Exception {
final String packageName = "fr.inria.statementaddarray";
InputProgram inputProgram = Utils.getInputProgram();
final Factory factory = inputProgram.getFactory();
inputProgram.setFactory(factory);
AmplificationHelper.setSeedRandom(32L);
StatementAdd amplifier = new StatementAdd(packageName);
amplifier.reset(factory.Class().get(packageName + ".ClassTargetAmplify"));
CtMethod<?> ctMethod = Utils.findMethod(factory.Class().get(packageName + ".TestClassTargetAmplify"), "test");
List<CtMethod> amplifiedMethods = amplifier.apply(ctMethod);
final List<CtMethod<?>> instrumentedAmplifiedTests = amplifiedMethods.stream().map(method -> AssertGeneratorHelper.createTestWithLog(method, "fr.inria.statementaddarray")).collect(Collectors.toList());
assertEquals(5, amplifiedMethods.size());
final String expectedInstrumentedBodyAfterAmplification_test_sd6_withlog = "{" + AmplificationHelper.LINE_SEPARATOR + " fr.inria.statementaddarray.ClassTargetAmplify clazz = new fr.inria.statementaddarray.ClassTargetAmplify();" + AmplificationHelper.LINE_SEPARATOR + " fr.inria.diversify.compare.ObjectLog.log(clazz, \"clazz\", \"test_sd6__1\");" + AmplificationHelper.LINE_SEPARATOR + " // StatementAdd: generate variable from return value" + AmplificationHelper.LINE_SEPARATOR + " fr.inria.statementaddarray.ClassParameterAmplify __DSPOT_invoc_3 = clazz.methodWithReturn();" + AmplificationHelper.LINE_SEPARATOR + " // StatementAdd: add invocation of a method" + AmplificationHelper.LINE_SEPARATOR + " __DSPOT_invoc_3.method1();" + AmplificationHelper.LINE_SEPARATOR + " fr.inria.diversify.compare.ObjectLog.log(clazz, \"clazz\", \"test_sd6__1___end\");" + AmplificationHelper.LINE_SEPARATOR + "}";
assertEquals(expectedInstrumentedBodyAfterAmplification_test_sd6_withlog, instrumentedAmplifiedTests.stream().filter(ctMethod1 -> "test_sd6_withlog".equals(ctMethod1.getSimpleName())).findFirst().get().getBody().toString());
}
Aggregations