Search in sources :

Example 66 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter in project dspot by STAMP-project.

the class StatementAddTest method testOnClassWithJavaObjects.

@Test
public void testOnClassWithJavaObjects() throws Exception {
    /*
            Test that the StatementAdd amplifier is able to generate, and manage Collection and Map from java.util
         */
    final String packageName = "fr.inria.statementadd";
    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 + ".ClassTarget"));
    CtMethod<?> ctMethod = Utils.findMethod(factory.Class().get(packageName + ".TestClassTarget"), "test");
    List<CtMethod> amplifiedMethods = amplifier.apply(ctMethod);
    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) 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) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Example 67 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter in project dspot by STAMP-project.

the class StatementAddTest method testStatementAddOnArrayObjects.

@Test
public void testStatementAddOnArrayObjects() 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);
    System.out.println(amplifiedMethods);
    assertEquals(5, amplifiedMethods.size());
    List<String> expectedCalledMethod = Arrays.asList("methodWithArrayParatemeter", "methodWithArrayParatemeterFromDomain", "methodWithDomainParameter", "methodWithReturn", "method1");
    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) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Example 68 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter 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 69 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter in project dspot by STAMP-project.

the class AmplificationHelper method createAmplifiedTest.

public static CtType createAmplifiedTest(List<CtMethod<?>> ampTest, CtType<?> classTest, Minimizer minimizer) {
    CtType amplifiedTest = classTest.clone();
    final String amplifiedName = classTest.getSimpleName().startsWith("Test") ? classTest.getSimpleName() + "Ampl" : "Ampl" + classTest.getSimpleName();
    amplifiedTest.setSimpleName(amplifiedName);
    classTest.getMethods().stream().filter(AmplificationChecker::isTest).forEach(amplifiedTest::removeMethod);
    if (minimize) {
        ampTest.stream().map(minimizer::minimize).forEach(amplifiedTest::addMethod);
    } else {
        ampTest.forEach(amplifiedTest::addMethod);
    }
    final CtTypeReference classTestReference = classTest.getReference();
    amplifiedTest.getElements(new TypeFilter<CtTypeReference>(CtTypeReference.class) {

        @Override
        public boolean matches(CtTypeReference element) {
            return element.equals(classTestReference) && super.matches(element);
        }
    }).forEach(ctTypeReference -> ctTypeReference.setSimpleName(amplifiedName));
    classTest.getPackage().addType(amplifiedTest);
    return amplifiedTest;
}
Also used : CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) TypeFilter(spoon.reflect.visitor.filter.TypeFilter)

Example 70 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter in project dspot by STAMP-project.

the class AmplificationHelper method computeClassProvider.

@Deprecated
public static Set<CtType> computeClassProvider(CtType testClass) {
    List<CtType> types = Query.getElements(testClass.getParent(CtPackage.class), new TypeFilter(CtType.class));
    types = types.stream().filter(Objects::nonNull).filter(type -> type.getPackage() != null).filter(type -> type.getPackage().getQualifiedName().equals(testClass.getPackage().getQualifiedName())).collect(Collectors.toList());
    if (testClass.getParent(CtType.class) != null) {
        types.add(testClass.getParent(CtType.class));
    }
    types.addAll(types.stream().flatMap(type -> getImport(type).stream()).collect(Collectors.toSet()));
    return new HashSet<>(types);
}
Also used : TypeFilter(spoon.reflect.visitor.filter.TypeFilter) IntStream(java.util.stream.IntStream) CtPackageReference(spoon.reflect.reference.CtPackageReference) java.util(java.util) Logger(org.slf4j.Logger) ImportScanner(spoon.reflect.visitor.ImportScanner) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) CtInvocation(spoon.reflect.code.CtInvocation) DSpotCompiler(fr.inria.diversify.utils.compilation.DSpotCompiler) CtComment(spoon.reflect.code.CtComment) LoggerFactory(org.slf4j.LoggerFactory) DecimalFormat(java.text.DecimalFormat) ImportScannerImpl(spoon.reflect.visitor.ImportScannerImpl) Description(org.junit.runner.Description) CtPackage(spoon.reflect.declaration.CtPackage) Collectors(java.util.stream.Collectors) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtType(spoon.reflect.declaration.CtType) TestListener(fr.inria.stamp.test.listener.TestListener) Query(spoon.reflect.visitor.Query) Minimizer(fr.inria.stamp.minimization.Minimizer) CtImport(spoon.reflect.declaration.CtImport) CtMethod(spoon.reflect.declaration.CtMethod) CtType(spoon.reflect.declaration.CtType) CtPackage(spoon.reflect.declaration.CtPackage) TypeFilter(spoon.reflect.visitor.filter.TypeFilter)

Aggregations

TypeFilter (spoon.reflect.visitor.filter.TypeFilter)132 Test (org.junit.Test)119 Launcher (spoon.Launcher)54 Factory (spoon.reflect.factory.Factory)52 CtMethod (spoon.reflect.declaration.CtMethod)43 CtInvocation (spoon.reflect.code.CtInvocation)27 CtClass (spoon.reflect.declaration.CtClass)24 CtLiteral (spoon.reflect.code.CtLiteral)18 ReferenceTypeFilter (spoon.reflect.visitor.filter.ReferenceTypeFilter)18 AbstractTest (fr.inria.AbstractTest)17 List (java.util.List)17 CtTypeReference (spoon.reflect.reference.CtTypeReference)17 ArrayList (java.util.ArrayList)14 CtBlock (spoon.reflect.code.CtBlock)10 CtConstructorCall (spoon.reflect.code.CtConstructorCall)10 CtIf (spoon.reflect.code.CtIf)10 CtStatement (spoon.reflect.code.CtStatement)10 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)10 File (java.io.File)9 CtElement (spoon.reflect.declaration.CtElement)8