Search in sources :

Example 6 with MethodAdderOnExistingObjectsAmplifier

use of eu.stamp_project.dspot.amplifier.amplifiers.MethodAdderOnExistingObjectsAmplifier in project dspot by STAMP-project.

the class MethodAdderOnExistingObjectsAmplifierTest method testInLoop.

@Test
public void testInLoop() throws Exception {
    /*
            Test that MethodAdderOnExistingObjectsAmplifier amplifier is able to add statement inside a loop if this loop has not brackets
         */
    final String packageName = "fr.inria.statementadd";
    final Factory factory = launcher.getFactory();
    RandomHelper.setSeedRandom(32L);
    MethodAdderOnExistingObjectsAmplifier amplifier = new MethodAdderOnExistingObjectsAmplifier();
    amplifier.reset(factory.Class().get(packageName + ".ClassTarget"));
    CtMethod<?> ctMethod = factory.Class().get(packageName + ".TestClassTarget").getMethodsByName("testWithLoop").get(0);
    // the original body of the loop has one statement
    assertEquals(1, ((CtBlock<?>) ctMethod.getElements(new TypeFilter<>(CtForEach.class)).get(0).getBody()).getStatements().size());
    CtMethod<?> amplifiedMethod = amplifier.amplify(ctMethod, 0).collect(Collectors.toList()).get(0);
    // elements has been added by the amplification: a method call and a local variable (needed to call the method)
    assertEquals(3, ((CtBlock<?>) amplifiedMethod.getElements(new TypeFilter<>(CtForEach.class)).get(0).getBody()).getStatements().size());
}
Also used : MethodAdderOnExistingObjectsAmplifier(eu.stamp_project.dspot.amplifier.amplifiers.MethodAdderOnExistingObjectsAmplifier) CtBlock(spoon.reflect.code.CtBlock) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 7 with MethodAdderOnExistingObjectsAmplifier

use of eu.stamp_project.dspot.amplifier.amplifiers.MethodAdderOnExistingObjectsAmplifier in project dspot by STAMP-project.

the class MethodAdderOnExistingObjectsAmplifierTest method testOnClassWithJavaObjects.

@Test
public void testOnClassWithJavaObjects() throws Exception {
    /*
            Test that the MethodAdderOnExistingObjectsAmplifier amplifier is able to generate, and manage Collection and Map from java.util
         */
    final String packageName = "fr.inria.statementadd";
    final Factory factory = launcher.getFactory();
    RandomHelper.setSeedRandom(32L);
    MethodAdderOnExistingObjectsAmplifier amplifier = new MethodAdderOnExistingObjectsAmplifier();
    amplifier.reset(factory.Class().get(packageName + ".ClassTarget"));
    CtMethod<?> ctMethod = factory.Class().get(packageName + ".TestClassTarget").getMethodsByName("test").get(0);
    List<CtMethod> amplifiedMethods = amplifier.amplify(ctMethod, 0).collect(Collectors.toList());
    assertEquals(7, amplifiedMethods.size());
    List<String> expectedCalledMethod = Arrays.asList("getList", "getSizeOf", "getSizeOfTypedCollection", "getSizeOfTypedMap");
    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) MethodAdderOnExistingObjectsAmplifier(eu.stamp_project.dspot.amplifier.amplifiers.MethodAdderOnExistingObjectsAmplifier) Arrays(java.util.Arrays) RandomHelper(eu.stamp_project.dspot.amplifier.amplifiers.utils.RandomHelper) AbstractTestOnSample(eu.stamp_project.dspot.AbstractTestOnSample) CtForEach(spoon.reflect.code.CtForEach) CtInvocation(spoon.reflect.code.CtInvocation) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) Collectors(java.util.stream.Collectors) List(java.util.List) CtClass(spoon.reflect.declaration.CtClass) CtBlock(spoon.reflect.code.CtBlock) Assert.assertEquals(org.junit.Assert.assertEquals) CtMethod(spoon.reflect.declaration.CtMethod) MethodAdderOnExistingObjectsAmplifier(eu.stamp_project.dspot.amplifier.amplifiers.MethodAdderOnExistingObjectsAmplifier) CtInvocation(spoon.reflect.code.CtInvocation) Factory(spoon.reflect.factory.Factory) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

MethodAdderOnExistingObjectsAmplifier (eu.stamp_project.dspot.amplifier.amplifiers.MethodAdderOnExistingObjectsAmplifier)7 Test (org.junit.Test)7 CtMethod (spoon.reflect.declaration.CtMethod)6 Factory (spoon.reflect.factory.Factory)6 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)6 AbstractTestOnSample (eu.stamp_project.dspot.AbstractTestOnSample)5 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 Assert.assertEquals (org.junit.Assert.assertEquals)5 CtBlock (spoon.reflect.code.CtBlock)5 CtInvocation (spoon.reflect.code.CtInvocation)5 CtClass (spoon.reflect.declaration.CtClass)5 RandomHelper (eu.stamp_project.dspot.amplifier.amplifiers.utils.RandomHelper)4 Arrays (java.util.Arrays)4 Assert.assertTrue (org.junit.Assert.assertTrue)4 CtForEach (spoon.reflect.code.CtForEach)4 SimpleInputAmplDistributor (eu.stamp_project.dspot.amplifier.SimpleInputAmplDistributor)1 IterationDecoratorAmplifier (eu.stamp_project.dspot.amplifier.amplifiers.IterationDecoratorAmplifier)1 NumberLiteralAmplifier (eu.stamp_project.dspot.amplifier.amplifiers.NumberLiteralAmplifier)1 ReturnValueAmplifier (eu.stamp_project.dspot.amplifier.amplifiers.ReturnValueAmplifier)1