Search in sources :

Example 1 with CtConsumableFunction

use of spoon.reflect.visitor.chain.CtConsumableFunction in project spoon by INRIA.

the class FilterTest method testEarlyTerminatingQuery.

@Test
public void testEarlyTerminatingQuery() throws Exception {
    // contract: a method first evaluates query until first element is found and then terminates the query
    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 {

        boolean wasTerminated = false;

        void failIfTerminated(String place) {
            assertTrue("The " + place + " is called after query was terminated.", wasTerminated == false);
        }
    }
    Context context = new Context();
    CtMethod firstMethod = launcher.getFactory().Package().getRootPackage().filterChildren(e -> {
        context.failIfTerminated("Filter#match of filterChildren");
        return true;
    }).map((CtElement e) -> {
        context.failIfTerminated("Array returning CtFunction#apply of map");
        // send result twice to check that second item is skipped
        return new CtElement[] { e, e };
    }).map((CtElement e) -> {
        context.failIfTerminated("List returning CtFunction#apply of map");
        // send result twice to check that second item is skipped
        return Arrays.asList(new CtElement[] { e, e });
    }).map((CtElement e, CtConsumer<Object> out) -> {
        context.failIfTerminated("CtConsumableFunction#apply of map");
        if (e instanceof CtMethod) {
            // this call should pass and cause termination of the query
            out.accept(e);
            context.wasTerminated = true;
        // let it call out.accept(e); once more to check that next step is not called after query is terminated
        }
        out.accept(e);
    }).map(e -> {
        context.failIfTerminated("CtFunction#apply of map after CtConsumableFunction");
        return e;
    }).first(CtMethod.class);
    assertTrue(firstMethod != null);
    assertTrue(context.wasTerminated);
}
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) CtConsumer(spoon.reflect.visitor.chain.CtConsumer) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 2 with CtConsumableFunction

use of spoon.reflect.visitor.chain.CtConsumableFunction in project spoon by INRIA.

the class FilterTest method testClassCastExceptionOnForEach.

@Test
public void testClassCastExceptionOnForEach() throws Exception {
    // contract: bound query, without any mapping
    // This test could fail with a version of JDK <= 8.0.40.
    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();
        // contract: if the query produces elements which cannot be cast to forEach consumer, then they are ignored
        launcher.getFactory().Package().getRootPackage().filterChildren(null).forEach((CtType t) -> {
            context.count++;
        });
        assertTrue(context.count > 0);
    }
    {
        Context context = new Context();
        // contract: if the for each implementation made by lambda throws CCE then it is reported
        try {
            launcher.getFactory().Package().getRootPackage().filterChildren(null).forEach((CtType t) -> {
                context.count++;
                throw new ClassCastException("TEST");
            });
            fail("It must fail, because body of forEach should be called and thrown CCE");
        } catch (ClassCastException e) {
            assertTrue(context.count > 0);
            assertEquals("TEST", e.getMessage());
        }
    }
    {
        Context context = new Context();
        // contract: if the for each implementation made by local class throws CCE then it is reported
        try {
            launcher.getFactory().Package().getRootPackage().filterChildren(null).forEach(new CtConsumer<CtType>() {

                @Override
                public void accept(CtType t) {
                    context.count++;
                    throw new ClassCastException("TEST");
                }
            });
            fail("It must fail, because body of forEach should be called and thrown CCE");
        } catch (ClassCastException e) {
            assertTrue(context.count > 0);
            assertEquals("TEST", e.getMessage());
        }
    }
    {
        Context context = new Context();
        // contract: if the select implementation made by local class throws CCE then it is reported
        try {
            launcher.getFactory().Package().getRootPackage().filterChildren(null).select(new Filter<CtType>() {

                @Override
                public boolean matches(CtType element) {
                    context.count++;
                    throw new ClassCastException("TEST");
                }
            }).list();
            fail("It must fail, because body of select thrown CCE");
        } catch (ClassCastException e) {
            assertTrue(context.count > 0);
            assertEquals("TEST", e.getMessage());
        }
    }
    {
        Context context = new Context();
        // contract: if the select implementation made by lambda throws CCE then it is reported
        try {
            launcher.getFactory().Package().getRootPackage().filterChildren(null).select((CtType element) -> {
                context.count++;
                throw new ClassCastException("TEST");
            }).list();
            fail("It must fail, because body of select thrown CCE");
        } catch (ClassCastException e) {
            assertTrue(context.count > 0);
            assertEquals("TEST", e.getMessage());
        }
    }
    {
        Context context = new Context();
        // contract: if the map(CtFunction) implementation made by local class throws CCE then it is reported
        try {
            launcher.getFactory().Package().getRootPackage().filterChildren(null).map(new CtFunction<CtType, Object>() {

                @Override
                public Object apply(CtType input) {
                    context.count++;
                    throw new ClassCastException("TEST");
                }
            }).failurePolicy(QueryFailurePolicy.IGNORE).list();
            fail("It must fail, because body of map thrown CCE");
        } catch (ClassCastException e) {
            assertTrue(context.count > 0);
            assertEquals("TEST", e.getMessage());
        }
    }
    {
        Context context = new Context();
        // contract: if the map(CtFunction) implementation made by lambda throws CCE then it is reported
        try {
            launcher.getFactory().Package().getRootPackage().filterChildren(null).map((CtType input) -> {
                context.count++;
                throw new ClassCastException("TEST");
            }).failurePolicy(QueryFailurePolicy.IGNORE).list();
            fail("It must fail, because body of map thrown CCE");
        } catch (ClassCastException e) {
            assertTrue(context.count > 0);
            assertEquals("TEST", e.getMessage());
        }
    }
    {
        Context context = new Context();
        // contract: if the map(CtConsumableFunction) implementation made by local class throws CCE then it is reported
        try {
            launcher.getFactory().Package().getRootPackage().filterChildren(null).map(new CtConsumableFunction<CtType>() {

                @Override
                public void apply(CtType input, CtConsumer<Object> outputConsumer) {
                    context.count++;
                    throw new ClassCastException("TEST");
                }
            }).failurePolicy(QueryFailurePolicy.IGNORE).list();
            fail("It must fail, because body of map thrown CCE");
        } catch (ClassCastException e) {
            assertTrue(context.count > 0);
            assertEquals("TEST", e.getMessage());
        }
    }
    {
        Context context = new Context();
        // contract: if the map(CtConsumableFunction) implementation made by lambda throws CCE then it is reported
        try {
            launcher.getFactory().Package().getRootPackage().filterChildren(null).map((CtType input, CtConsumer<Object> outputConsumer) -> {
                context.count++;
                throw new ClassCastException("TEST");
            }).failurePolicy(QueryFailurePolicy.IGNORE).list();
            fail("It must fail, because body of map thrown CCE");
        } catch (ClassCastException e) {
            assertTrue(context.count > 0);
            assertEquals("TEST", e.getMessage());
        }
    }
}
Also used : CtType(spoon.reflect.declaration.CtType) CtConsumableFunction(spoon.reflect.visitor.chain.CtConsumableFunction) Launcher(spoon.Launcher) CtConsumer(spoon.reflect.visitor.chain.CtConsumer) CtFunction(spoon.reflect.visitor.chain.CtFunction) Test(org.junit.Test)

Example 3 with CtConsumableFunction

use of spoon.reflect.visitor.chain.CtConsumableFunction in project spoon by INRIA.

the class FilterTest method testElementMapConsumableFunction.

// now testing map(CtConsumableFunction)
@Test
public void testElementMapConsumableFunction() throws Exception {
    // contract: a method map(CtConsumableFunction) is provided
    // a simple consumer.accept() is equivalent to a single return in a CtFunction
    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);
    // long version
    class aFunction implements CtConsumableFunction<CtClass> {

        @Override
        public void apply(CtClass c, CtConsumer out) {
            // equivalent to a single return
            out.accept(c.getParent());
        }
    }
    assertEquals(cls.getParent(), cls.map(new aFunction()).list().get(0));
    // now the same with Java8 one-liner
    assertEquals(cls.getParent(), cls.map((CtClass<?> c, CtConsumer<Object> out) -> out.accept(c.getParent())).list().get(0));
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtConsumableFunction(spoon.reflect.visitor.chain.CtConsumableFunction) Launcher(spoon.Launcher) CtConsumer(spoon.reflect.visitor.chain.CtConsumer) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 Launcher (spoon.Launcher)3 CtConsumableFunction (spoon.reflect.visitor.chain.CtConsumableFunction)3 CtConsumer (spoon.reflect.visitor.chain.CtConsumer)3 CtClass (spoon.reflect.declaration.CtClass)2 CtType (spoon.reflect.declaration.CtType)2 CtFunction (spoon.reflect.visitor.chain.CtFunction)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 Iterator (java.util.Iterator)1 List (java.util.List)1 TreeSet (java.util.TreeSet)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 Assert.assertArrayEquals (org.junit.Assert.assertArrayEquals)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertFalse (org.junit.Assert.assertFalse)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1