Search in sources :

Example 41 with CtMethod

use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.

the class StatementAddTest method testStatementAddOnUnderTest.

@Test
public void testStatementAddOnUnderTest() throws Exception {
    Factory factory = Utils.getFactory();
    CtClass<Object> ctClass = factory.Class().get("fr.inria.mutation.ClassUnderTestTest");
    AmplificationHelper.setSeedRandom(23L);
    StatementAdd amplificator = new StatementAdd();
    amplificator.reset(ctClass);
    CtMethod originalMethod = Utils.findMethod(ctClass, "testLit");
    List<CtMethod> amplifiedMethods = amplificator.apply(originalMethod);
    System.out.println(amplifiedMethods);
    assertEquals(2, amplifiedMethods.size());
    List<String> expectedCalledMethod = Arrays.asList("plusOne", "minusOne");
    assertTrue(amplifiedMethods.stream().allMatch(amplifiedMethod -> amplifiedMethod.filterChildren(new TypeFilter<CtInvocation<?>>(CtInvocation.class) {

        @Override
        public boolean matches(CtInvocation<?> element) {
            return expectedCalledMethod.contains(element.getExecutable().getSimpleName());
        }
    }).first() != null));
}
Also used : TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Arrays(java.util.Arrays) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) CtInvocation(spoon.reflect.code.CtInvocation) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) List(java.util.List) Utils(fr.inria.Utils) CtClass(spoon.reflect.declaration.CtClass) Assert.assertEquals(org.junit.Assert.assertEquals) AbstractTest(fr.inria.AbstractTest) CtMethod(spoon.reflect.declaration.CtMethod) Factory(spoon.reflect.factory.Factory) CtInvocation(spoon.reflect.code.CtInvocation) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Example 42 with CtMethod

use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.

the class StringLiteralAmplifierTest method testAmplify.

@Test
public void testAmplify() throws Exception {
    final String nameMethod = "methodString";
    CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
    AmplificationHelper.setSeedRandom(42L);
    Amplifier amplificator = new StringLiteralAmplifier();
    amplificator.reset(literalMutationClass);
    CtMethod method = literalMutationClass.getMethod(nameMethod);
    List<CtMethod> mutantMethods = amplificator.apply(method);
    System.out.println(mutantMethods);
    assertEquals(29, mutantMethods.size());
}
Also used : CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Example 43 with CtMethod

use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.

the class StringLiteralAmplifierTest method testDoesNotAmplifyChar.

@Test
public void testDoesNotAmplifyChar() throws Exception {
    final String nameMethod = "methodCharacter";
    CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
    AmplificationHelper.setSeedRandom(42L);
    Amplifier mutator = new StringLiteralAmplifier();
    mutator.reset(literalMutationClass);
    CtMethod method = literalMutationClass.getMethod(nameMethod);
    List<CtMethod> mutantMethods = mutator.apply(method);
    assertTrue(mutantMethods.isEmpty());
}
Also used : CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Example 44 with CtMethod

use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.

the class TestMethodCallAdderTest method testMethodCallAddAll.

@Test
public void testMethodCallAddAll() throws Exception {
    /*
            Test that we reuse method call in a test for each used method in the test.
                3 method are called in the original test, we produce 3 test methods.
         */
    CtClass<Object> testClass = Utils.getFactory().Class().get("fr.inria.mutation.ClassUnderTestTest");
    TestMethodCallAdder methodCallAdder = new TestMethodCallAdder();
    methodCallAdder.reset(testClass);
    final CtMethod<?> originalMethod = testClass.getMethods().stream().filter(m -> "testAddCall".equals(m.getSimpleName())).findFirst().get();
    List<CtMethod> amplifiedMethods = methodCallAdder.apply(originalMethod);
    assertEquals(2, amplifiedMethods.size());
    for (int i = 0; i < amplifiedMethods.size(); i++) {
        CtMethod amplifiedMethod = amplifiedMethods.get(i);
        assertEquals(originalMethod.getBody().getStatements().size() + 1, amplifiedMethod.getBody().getStatements().size());
        // +1 to skip the construction statement.
        CtStatement expectedStatement = originalMethod.getBody().getStatements().get(i + 1);
        assertEquals(expectedStatement.toString(), amplifiedMethod.getBody().getStatements().get(i + 1).toString());
        assertEquals(expectedStatement.toString(), amplifiedMethod.getBody().getStatements().get(i + 2).toString());
    }
}
Also used : CtStatement(spoon.reflect.code.CtStatement) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Example 45 with CtMethod

use of spoon.reflect.declaration.CtMethod in project spoon by INRIA.

the class AnnotationFactory method annotate.

/**
 * Creates/updates an element's annotation value.
 *
 * @param element
 * 		the program element to annotate
 * @param annotationType
 * 		the annotation type
 * @param annotationElementName
 * 		the annotation element name
 * @param value
 * 		the value of the annotation element
 * @return the created/updated annotation
 */
public <A extends Annotation> CtAnnotation<A> annotate(CtElement element, CtTypeReference<A> annotationType, String annotationElementName, Object value) {
    final CtAnnotation<A> annotation = annotate(element, annotationType);
    boolean isArray;
    // try with CT reflection
    CtAnnotationType<A> ctAnnotationType = ((CtAnnotationType<A>) annotation.getAnnotationType().getDeclaration());
    boolean newValue = annotation.getValue(annotationElementName) == null;
    if (ctAnnotationType != null) {
        CtMethod<?> e = ctAnnotationType.getMethod(annotationElementName);
        isArray = (e.getType() instanceof CtArrayTypeReference);
    } else {
        Method m;
        try {
            m = annotation.getAnnotationType().getActualClass().getMethod(annotationElementName, new Class[0]);
        } catch (Exception ex) {
            annotation.addValue(annotationElementName, value);
            return annotation;
        }
        isArray = m.getReturnType().isArray();
    }
    if (isArray || newValue) {
        annotation.addValue(annotationElementName, value);
    } else {
        throw new SpoonException("cannot assign an array to a non-array annotation element");
    }
    return annotation;
}
Also used : SpoonException(spoon.SpoonException) Method(java.lang.reflect.Method) CtMethod(spoon.reflect.declaration.CtMethod) CtArrayTypeReference(spoon.reflect.reference.CtArrayTypeReference) SpoonException(spoon.SpoonException)

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