Search in sources :

Example 36 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class FilterTest method testOverridingMethodFromSubClassOfAbstractClass.

@Test
public void testOverridingMethodFromSubClassOfAbstractClass() throws Exception {
    // contract: When we ask all overriding methods from an overriding method, we must returns all methods
    // below and not above (including the declaration).
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.run();
    final CtClass<Tostada> aTostada = launcher.getFactory().Class().get(Tostada.class);
    TreeSet<CtMethod<?>> ts = new TreeSet<CtMethod<?>>(new DeepRepresentationComparator());
    List<CtMethod<?>> elements = Query.getElements(launcher.getFactory(), new OverridingMethodFilter(aTostada.getMethodsByName("prepare").get(0)));
    ts.addAll(elements);
    final List<CtMethod<?>> overridingMethods = Arrays.asList(ts.toArray(new CtMethod[0]));
    assertEquals(3, overridingMethods.size());
    assertEquals("spoon.test.filters.testclasses.AbstractTostada$1", overridingMethods.get(2).getParent(CtClass.class).getQualifiedName());
    assertEquals(SubTostada.class, overridingMethods.get(1).getParent(CtClass.class).getActualClass());
    assertEquals("spoon.test.filters.testclasses.Tostada$1", overridingMethods.get(0).getParent(CtClass.class).getQualifiedName());
    final CtClass<SubTostada> aSubTostada = launcher.getFactory().Class().get(SubTostada.class);
    assertEquals(0, Query.getElements(launcher.getFactory(), new OverridingMethodFilter(aSubTostada.getMethodsByName("prepare").get(0))).size());
}
Also used : Tostada(spoon.test.filters.testclasses.Tostada) ITostada(spoon.test.filters.testclasses.ITostada) AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) SubTostada(spoon.test.filters.testclasses.SubTostada) DeepRepresentationComparator(spoon.support.comparator.DeepRepresentationComparator) TreeSet(java.util.TreeSet) Launcher(spoon.Launcher) SubTostada(spoon.test.filters.testclasses.SubTostada) CtMethod(spoon.reflect.declaration.CtMethod) OverridingMethodFilter(spoon.reflect.visitor.filter.OverridingMethodFilter) Test(org.junit.Test)

Example 37 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class FilterTest method testReuseOfBaseQuery.

@Test
public void testReuseOfBaseQuery() throws Exception {
    // contract: an empty  query can be used on several inputs
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.run();
    CtClass<?> cls = launcher.getFactory().Class().get(Tacos.class);
    CtClass<?> cls2 = launcher.getFactory().Class().get(Tostada.class);
    // here is the query
    CtQuery q = launcher.getFactory().Query().createQuery().map((CtClass c) -> c.getSimpleName());
    // using it on a first input
    assertEquals("Tacos", q.setInput(cls).list().get(0));
    // using it on a second input
    assertEquals("Tostada", q.setInput(cls2).list().get(0));
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtQuery(spoon.reflect.visitor.chain.CtQuery) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 38 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class FilterTest method testNameFilterWithGenericType.

@Test
public void testNameFilterWithGenericType() {
    // contract: NamedElementFilter of T should only return T elements
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/imports/testclasses/internal4/Constants.java");
    spoon.buildModel();
    CtType type = spoon.getFactory().Type().get(Constants.class);
    List<CtMethod> ctMethods = type.getElements(new NamedElementFilter<>(CtMethod.class, "CONSTANT"));
    assertTrue(ctMethods.isEmpty());
    List<CtField> ctFields = type.getElements(new NamedElementFilter<>(CtField.class, "CONSTANT"));
    assertEquals(1, ctFields.size());
    assertTrue(ctFields.get(0) instanceof CtField);
}
Also used : CtType(spoon.reflect.declaration.CtType) CtField(spoon.reflect.declaration.CtField) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 39 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class FilterTest method testInvalidQueryStepFailurePolicyIgnore.

@Test
public void testInvalidQueryStepFailurePolicyIgnore() throws Exception {
    // contract: with QueryFailurePolicy.IGNORE, no exception is thrown
    // and only valid elements are kept for the next step
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.run();
    class Context {

        int count = 0;
    }
    Context context = new Context();
    launcher.getFactory().Package().getRootPackage().filterChildren((CtElement c) -> {
        return true;
    }).name("step1").map((CtMethod<?> m) -> m).name("invalidStep2").map((o) -> o).name("step3").failurePolicy(QueryFailurePolicy.IGNORE).forEach((CtElement c) -> {
        assertTrue(c instanceof CtMethod);
        context.count++;
    });
    assertTrue(context.count > 0);
}
Also used : Arrays(java.util.Arrays) Launcher(spoon.Launcher) CtSwitch(spoon.reflect.code.CtSwitch) CtInterface(spoon.reflect.declaration.CtInterface) CtConsumableFunction(spoon.reflect.visitor.chain.CtConsumableFunction) Tostada(spoon.test.filters.testclasses.Tostada) CtNamedElement(spoon.reflect.declaration.CtNamedElement) CtQueryImpl(spoon.reflect.visitor.chain.CtQueryImpl) CtType(spoon.reflect.declaration.CtType) QueryFailurePolicy(spoon.reflect.visitor.chain.QueryFailurePolicy) CtElement(spoon.reflect.declaration.CtElement) ReturnOrThrowFilter(spoon.reflect.visitor.filter.ReturnOrThrowFilter) FieldAccessFilterTacos(spoon.test.filters.testclasses.FieldAccessFilterTacos) OverridingMethodFilter(spoon.reflect.visitor.filter.OverridingMethodFilter) DeepRepresentationComparator(spoon.support.comparator.DeepRepresentationComparator) Assert.fail(org.junit.Assert.fail) CtMethodImpl(spoon.support.reflect.declaration.CtMethodImpl) CtExecutable(spoon.reflect.declaration.CtExecutable) Query(spoon.reflect.visitor.Query) CompositeFilter(spoon.reflect.visitor.filter.CompositeFilter) CtQuery(spoon.reflect.visitor.chain.CtQuery) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtScannerFunction(spoon.reflect.visitor.filter.CtScannerFunction) ModelUtils.build(spoon.testing.utils.ModelUtils.build) CtField(spoon.reflect.declaration.CtField) Predicate(java.util.function.Predicate) Collection(java.util.Collection) QualifiedNameComparator(spoon.support.comparator.QualifiedNameComparator) Antojito(spoon.test.filters.testclasses.Antojito) ModelUtils(spoon.testing.utils.ModelUtils) Collectors(java.util.stream.Collectors) CtModule(spoon.reflect.declaration.CtModule) Constants(spoon.test.imports.testclasses.internal4.Constants) CtFieldAccess(spoon.reflect.code.CtFieldAccess) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) LineFilter(spoon.reflect.visitor.filter.LineFilter) List(java.util.List) AnnotationFilter(spoon.reflect.visitor.filter.AnnotationFilter) Assert.assertFalse(org.junit.Assert.assertFalse) CtMethod(spoon.reflect.declaration.CtMethod) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtIf(spoon.reflect.code.CtIf) ITostada(spoon.test.filters.testclasses.ITostada) SubInheritanceHierarchyResolver(spoon.support.visitor.SubInheritanceHierarchyResolver) CtPackage(spoon.reflect.declaration.CtPackage) TreeSet(java.util.TreeSet) CtStatement(spoon.reflect.code.CtStatement) AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) SubTostada(spoon.test.filters.testclasses.SubTostada) CtExpression(spoon.reflect.code.CtExpression) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) CtNewClass(spoon.reflect.code.CtNewClass) Filter(spoon.reflect.visitor.Filter) FieldAccessFilter(spoon.reflect.visitor.filter.FieldAccessFilter) ScanningMode(spoon.reflect.visitor.chain.ScanningMode) InvocationFilter(spoon.reflect.visitor.filter.InvocationFilter) Before(org.junit.Before) CtLoop(spoon.reflect.code.CtLoop) CtScannerListener(spoon.reflect.visitor.chain.CtScannerListener) Iterator(java.util.Iterator) CtInvocation(spoon.reflect.code.CtInvocation) Assert.assertNotNull(org.junit.Assert.assertNotNull) OverriddenMethodFilter(spoon.reflect.visitor.filter.OverriddenMethodFilter) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) CtVariable(spoon.reflect.declaration.CtVariable) OverriddenMethodQuery(spoon.reflect.visitor.filter.OverriddenMethodQuery) Tacos(spoon.test.filters.testclasses.Tacos) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) FilteringOperator(spoon.reflect.visitor.filter.FilteringOperator) CtTypeReference(spoon.reflect.reference.CtTypeReference) Assert.assertNull(org.junit.Assert.assertNull) ParentFunction(spoon.reflect.visitor.filter.ParentFunction) CtCFlowBreak(spoon.reflect.code.CtCFlowBreak) CtConsumer(spoon.reflect.visitor.chain.CtConsumer) CtClass(spoon.reflect.declaration.CtClass) ModifierKind(spoon.reflect.declaration.ModifierKind) CtFieldReference(spoon.reflect.reference.CtFieldReference) RegexFilter(spoon.reflect.visitor.filter.RegexFilter) Comparator(java.util.Comparator) Assert.assertEquals(org.junit.Assert.assertEquals) CtFunction(spoon.reflect.visitor.chain.CtFunction) CtElement(spoon.reflect.declaration.CtElement) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 40 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class FilterTest method testEmptyQuery.

@Test
public void testEmptyQuery() throws Exception {
    // contract: unbound or empty query
    final Launcher launcher = new Launcher();
    // contract: empty query returns no element
    assertEquals(0, launcher.getFactory().createQuery().list().size());
    assertEquals(0, launcher.getFactory().createQuery((Object) null).list().size());
    // contract: empty query returns no element
    launcher.getFactory().createQuery().forEach(x -> fail());
    launcher.getFactory().createQuery((Object) null).forEach(x -> fail());
    // contract: empty query calls no mapping
    assertEquals(0, launcher.getFactory().createQuery().map(x -> {
        fail();
        return true;
    }).list().size());
    assertEquals(0, launcher.getFactory().createQuery((Object) null).map(x -> {
        fail();
        return true;
    }).list().size());
    // contract: empty query calls no filterChildren
    assertEquals(0, launcher.getFactory().createQuery().filterChildren(x -> {
        fail();
        return true;
    }).list().size());
    assertEquals(0, launcher.getFactory().createQuery((Object) null).filterChildren(x -> {
        fail();
        return true;
    }).list().size());
}
Also used : Arrays(java.util.Arrays) Launcher(spoon.Launcher) CtSwitch(spoon.reflect.code.CtSwitch) CtInterface(spoon.reflect.declaration.CtInterface) CtConsumableFunction(spoon.reflect.visitor.chain.CtConsumableFunction) Tostada(spoon.test.filters.testclasses.Tostada) CtNamedElement(spoon.reflect.declaration.CtNamedElement) CtQueryImpl(spoon.reflect.visitor.chain.CtQueryImpl) CtType(spoon.reflect.declaration.CtType) QueryFailurePolicy(spoon.reflect.visitor.chain.QueryFailurePolicy) CtElement(spoon.reflect.declaration.CtElement) ReturnOrThrowFilter(spoon.reflect.visitor.filter.ReturnOrThrowFilter) FieldAccessFilterTacos(spoon.test.filters.testclasses.FieldAccessFilterTacos) OverridingMethodFilter(spoon.reflect.visitor.filter.OverridingMethodFilter) DeepRepresentationComparator(spoon.support.comparator.DeepRepresentationComparator) Assert.fail(org.junit.Assert.fail) CtMethodImpl(spoon.support.reflect.declaration.CtMethodImpl) CtExecutable(spoon.reflect.declaration.CtExecutable) Query(spoon.reflect.visitor.Query) CompositeFilter(spoon.reflect.visitor.filter.CompositeFilter) CtQuery(spoon.reflect.visitor.chain.CtQuery) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtScannerFunction(spoon.reflect.visitor.filter.CtScannerFunction) ModelUtils.build(spoon.testing.utils.ModelUtils.build) CtField(spoon.reflect.declaration.CtField) Predicate(java.util.function.Predicate) Collection(java.util.Collection) QualifiedNameComparator(spoon.support.comparator.QualifiedNameComparator) Antojito(spoon.test.filters.testclasses.Antojito) ModelUtils(spoon.testing.utils.ModelUtils) Collectors(java.util.stream.Collectors) CtModule(spoon.reflect.declaration.CtModule) Constants(spoon.test.imports.testclasses.internal4.Constants) CtFieldAccess(spoon.reflect.code.CtFieldAccess) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) LineFilter(spoon.reflect.visitor.filter.LineFilter) List(java.util.List) AnnotationFilter(spoon.reflect.visitor.filter.AnnotationFilter) Assert.assertFalse(org.junit.Assert.assertFalse) CtMethod(spoon.reflect.declaration.CtMethod) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtIf(spoon.reflect.code.CtIf) ITostada(spoon.test.filters.testclasses.ITostada) SubInheritanceHierarchyResolver(spoon.support.visitor.SubInheritanceHierarchyResolver) CtPackage(spoon.reflect.declaration.CtPackage) TreeSet(java.util.TreeSet) CtStatement(spoon.reflect.code.CtStatement) AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) SubTostada(spoon.test.filters.testclasses.SubTostada) CtExpression(spoon.reflect.code.CtExpression) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) CtNewClass(spoon.reflect.code.CtNewClass) Filter(spoon.reflect.visitor.Filter) FieldAccessFilter(spoon.reflect.visitor.filter.FieldAccessFilter) ScanningMode(spoon.reflect.visitor.chain.ScanningMode) InvocationFilter(spoon.reflect.visitor.filter.InvocationFilter) Before(org.junit.Before) CtLoop(spoon.reflect.code.CtLoop) CtScannerListener(spoon.reflect.visitor.chain.CtScannerListener) Iterator(java.util.Iterator) CtInvocation(spoon.reflect.code.CtInvocation) Assert.assertNotNull(org.junit.Assert.assertNotNull) OverriddenMethodFilter(spoon.reflect.visitor.filter.OverriddenMethodFilter) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) CtVariable(spoon.reflect.declaration.CtVariable) OverriddenMethodQuery(spoon.reflect.visitor.filter.OverriddenMethodQuery) Tacos(spoon.test.filters.testclasses.Tacos) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) FilteringOperator(spoon.reflect.visitor.filter.FilteringOperator) CtTypeReference(spoon.reflect.reference.CtTypeReference) Assert.assertNull(org.junit.Assert.assertNull) ParentFunction(spoon.reflect.visitor.filter.ParentFunction) CtCFlowBreak(spoon.reflect.code.CtCFlowBreak) CtConsumer(spoon.reflect.visitor.chain.CtConsumer) CtClass(spoon.reflect.declaration.CtClass) ModifierKind(spoon.reflect.declaration.ModifierKind) CtFieldReference(spoon.reflect.reference.CtFieldReference) RegexFilter(spoon.reflect.visitor.filter.RegexFilter) Comparator(java.util.Comparator) Assert.assertEquals(org.junit.Assert.assertEquals) CtFunction(spoon.reflect.visitor.chain.CtFunction) Launcher(spoon.Launcher) Test(org.junit.Test)

Aggregations

Launcher (spoon.Launcher)524 Test (org.junit.Test)486 Factory (spoon.reflect.factory.Factory)163 CtClass (spoon.reflect.declaration.CtClass)111 CtMethod (spoon.reflect.declaration.CtMethod)79 File (java.io.File)75 CtType (spoon.reflect.declaration.CtType)66 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)60 CtTypeReference (spoon.reflect.reference.CtTypeReference)48 SpoonModelBuilder (spoon.SpoonModelBuilder)44 ArrayList (java.util.ArrayList)43 CtAnnotation (spoon.reflect.declaration.CtAnnotation)38 CtInvocation (spoon.reflect.code.CtInvocation)32 CtElement (spoon.reflect.declaration.CtElement)27 CtPackage (spoon.reflect.declaration.CtPackage)27 FileSystemFile (spoon.support.compiler.FileSystemFile)26 CtStatement (spoon.reflect.code.CtStatement)25 CtField (spoon.reflect.declaration.CtField)24 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)24 SpoonAPI (spoon.SpoonAPI)22