Search in sources :

Example 41 with Launcher

use of spoon.Launcher 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 42 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class FilterTest method testQueryBuilderWithFilterChain.

@Test
public void testQueryBuilderWithFilterChain() throws Exception {
    // contract: query methods can be lazy evaluated in a foreach
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.run();
    class Context {

        CtMethod<?> method;

        int count = 0;
    }
    Context context = new Context();
    // chaining queries
    CtQuery q = launcher.getFactory().Package().getRootPackage().filterChildren(new TypeFilter<CtMethod<?>>(CtMethod.class)).map((CtMethod<?> method) -> {
        context.method = method;
        return method;
    }).map(new OverriddenMethodQuery());
    // actual evaluation
    q.forEach((CtMethod<?> method) -> {
        assertTrue(context.method.getReference().isOverriding(method.getReference()));
        assertTrue(context.method.isOverriding(method));
        context.count++;
    });
    // sanity check
    assertTrue(context.count > 0);
}
Also used : OverriddenMethodQuery(spoon.reflect.visitor.filter.OverriddenMethodQuery) CtQuery(spoon.reflect.visitor.chain.CtQuery) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 43 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class FilterTest method testInvocationFilterWithExecutableInLibrary.

@Test
public void testInvocationFilterWithExecutableInLibrary() throws Exception {
    // contract: When we have an invocation of an executable declared in a library,
    // we can filter it and get the executable of the invocation.
    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<Tacos> aTacos = launcher.getFactory().Class().get(Tacos.class);
    final CtInvocation<?> invSize = aTacos.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class) {

        @Override
        public boolean matches(CtInvocation<?> element) {
            if (element.getExecutable() == null) {
                return false;
            }
            return "size".equals(element.getExecutable().getSimpleName()) && super.matches(element);
        }
    }).get(0);
    final List<CtInvocation<?>> invocations = aTacos.getElements(new InvocationFilter(invSize.getExecutable()));
    assertEquals(1, invocations.size());
    final CtInvocation<?> expectedInv = invocations.get(0);
    assertNotNull(expectedInv);
    final CtExecutableReference<?> expectedExecutable = expectedInv.getExecutable();
    assertNotNull(expectedExecutable);
    assertEquals("size", expectedExecutable.getSimpleName());
    assertNull(expectedExecutable.getDeclaration());
    CtExecutable<?> exec = expectedExecutable.getExecutableDeclaration();
    assertEquals("size", exec.getSimpleName());
    assertEquals("ArrayList", ((CtClass) exec.getParent()).getSimpleName());
    final CtExecutable<?> declaration = expectedExecutable.getExecutableDeclaration();
    assertNotNull(declaration);
    assertEquals("size", declaration.getSimpleName());
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) InvocationFilter(spoon.reflect.visitor.filter.InvocationFilter) FieldAccessFilterTacos(spoon.test.filters.testclasses.FieldAccessFilterTacos) Tacos(spoon.test.filters.testclasses.Tacos) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 44 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class FilterTest method testFilterQueryStep.

@Test
public void testFilterQueryStep() throws Exception {
    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();
    // Contract: the filter(Filter) can be used to detect if input of query step should pass to next query step.
    List<CtElement> realList = launcher.getFactory().Package().getRootPackage().filterChildren(e -> {
        return true;
    }).select(new TypeFilter<>(CtClass.class)).list();
    List<CtElement> expectedList = launcher.getFactory().Package().getRootPackage().filterChildren(new TypeFilter<>(CtClass.class)).list();
    assertArrayEquals(expectedList.toArray(), realList.toArray());
    assertTrue(expectedList.size() > 0);
}
Also used : CtElement(spoon.reflect.declaration.CtElement) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 45 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class FilterTest method testFunctionQueryStep.

@Test
public void testFunctionQueryStep() throws Exception {
    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();
    CtQuery query = launcher.getFactory().Package().getRootPackage().filterChildren((CtClass<?> c) -> {
        return true;
    }).name("filter CtClass only").map((CtClass<?> c) -> c.getSuperInterfaces()).name("super interfaces").map((CtTypeReference<?> iface) -> iface.getTypeDeclaration()).map((CtType<?> iface) -> iface.getAllMethods()).name("allMethods if interface").map((CtMethod<?> method) -> method.getSimpleName().equals("make")).map((CtMethod<?> m) -> m.getType()).map((CtTypeReference<?> t) -> t.getTypeDeclaration());
    ((CtQueryImpl) query).logging(true);
    query.forEach((CtInterface<?> c) -> {
        assertEquals("ITostada", c.getSimpleName());
        context.count++;
    });
    assertTrue(context.count > 0);
}
Also used : CtInterface(spoon.reflect.declaration.CtInterface) CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtQuery(spoon.reflect.visitor.chain.CtQuery) Launcher(spoon.Launcher) CtQueryImpl(spoon.reflect.visitor.chain.CtQueryImpl) CtMethod(spoon.reflect.declaration.CtMethod) 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