Search in sources :

Example 96 with CtMethod

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;
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtPackageReference(spoon.reflect.reference.CtPackageReference) CtMethod(spoon.reflect.declaration.CtMethod)

Example 97 with CtMethod

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;
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtMethod(spoon.reflect.declaration.CtMethod)

Example 98 with CtMethod

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;
}
Also used : CtMethod(spoon.reflect.declaration.CtMethod)

Example 99 with CtMethod

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();
    }
}
Also used : Diff(gumtree.spoon.diff.Diff) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) Logger(org.slf4j.Logger) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) Main(fr.inria.stamp.Main) AstComparator(gumtree.spoon.AstComparator) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) IOException(java.io.IOException) Factory(spoon.reflect.factory.Factory) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) Collections(edu.emory.mathcs.backport.java.util.Collections) File(java.io.File) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) List(java.util.List) CtType(spoon.reflect.declaration.CtType) AmplificationChecker(fr.inria.diversify.utils.AmplificationChecker) BufferedReader(java.io.BufferedReader) CtMethod(spoon.reflect.declaration.CtMethod) AstComparator(gumtree.spoon.AstComparator) Diff(gumtree.spoon.diff.Diff) File(java.io.File) IOException(java.io.IOException)

Example 100 with CtMethod

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());
}
Also used : AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) StatementAdd(fr.inria.diversify.dspot.amplifier.StatementAdd) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) Collectors(java.util.stream.Collectors) List(java.util.List) Utils(fr.inria.Utils) CtClass(spoon.reflect.declaration.CtClass) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) AbstractTest(fr.inria.AbstractTest) CtMethod(spoon.reflect.declaration.CtMethod) Factory(spoon.reflect.factory.Factory) StatementAdd(fr.inria.diversify.dspot.amplifier.StatementAdd) CtMethod(spoon.reflect.declaration.CtMethod) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Aggregations

CtMethod (spoon.reflect.declaration.CtMethod)240 Test (org.junit.Test)163 Factory (spoon.reflect.factory.Factory)77 Launcher (spoon.Launcher)73 CtClass (spoon.reflect.declaration.CtClass)47 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)47 CtType (spoon.reflect.declaration.CtType)45 AbstractTest (fr.inria.AbstractTest)36 ArrayList (java.util.ArrayList)35 List (java.util.List)33 CtTypeReference (spoon.reflect.reference.CtTypeReference)31 CtInvocation (spoon.reflect.code.CtInvocation)26 CtStatement (spoon.reflect.code.CtStatement)26 AmplificationHelper (fr.inria.diversify.utils.AmplificationHelper)19 Collectors (java.util.stream.Collectors)19 CtLiteral (spoon.reflect.code.CtLiteral)18 CtElement (spoon.reflect.declaration.CtElement)18 CtIf (spoon.reflect.code.CtIf)16 CtAnnotation (spoon.reflect.declaration.CtAnnotation)16 CtField (spoon.reflect.declaration.CtField)16