Search in sources :

Example 1 with AbstractTostada

use of spoon.test.filters.testclasses.AbstractTostada in project spoon by INRIA.

the class FilterTest method testSubInheritanceHierarchyResolver.

@Test
public void testSubInheritanceHierarchyResolver() throws Exception {
    // contract; SubInheritanceHierarchyResolver supports finding subtypes in an incremental manner
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.buildModel();
    SubInheritanceHierarchyResolver resolver = new SubInheritanceHierarchyResolver(launcher.getModel().getRootPackage());
    // contract: by default, nothing is sent to the consumer
    resolver.forEachSubTypeInPackage(new CtConsumer<CtType<?>>() {

        @Override
        public void accept(CtType<?> ctType) {
            fail();
        }
    });
    // we add a type
    resolver.addSuperType(launcher.getFactory().Type().createReference(AbstractTostada.class));
    class Counter {

        int counter = 0;
    }
    Counter c = new Counter();
    resolver.forEachSubTypeInPackage(new CtConsumer<CtType<?>>() {

        @Override
        public void accept(CtType<?> ctType) {
            c.counter++;
        }
    });
    // there are 5 subtypes of AbstractTostada
    assertEquals(5, c.counter);
    // we add a type already visited
    resolver.addSuperType(launcher.getFactory().Type().createReference(Tostada.class));
    // nothing is sent to the consumer
    resolver.forEachSubTypeInPackage(new CtConsumer<CtType<?>>() {

        @Override
        public void accept(CtType<?> ctType) {
            fail();
        }
    });
    // we add a new type
    resolver.addSuperType(launcher.getFactory().Type().createReference(ITostada.class));
    Counter c2 = new Counter();
    resolver.forEachSubTypeInPackage(new CtConsumer<CtType<?>>() {

        @Override
        public void accept(CtType<?> ctType) {
            c2.counter++;
            assertEquals("spoon.test.filters.testclasses.Tacos", ctType.getQualifiedName());
        }
    });
    // only one subtype remains unvisited
    assertEquals(1, c2.counter);
}
Also used : AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) Tostada(spoon.test.filters.testclasses.Tostada) ITostada(spoon.test.filters.testclasses.ITostada) AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) SubTostada(spoon.test.filters.testclasses.SubTostada) CtType(spoon.reflect.declaration.CtType) SubInheritanceHierarchyResolver(spoon.support.visitor.SubInheritanceHierarchyResolver) Launcher(spoon.Launcher) ITostada(spoon.test.filters.testclasses.ITostada) Test(org.junit.Test)

Example 2 with AbstractTostada

use of spoon.test.filters.testclasses.AbstractTostada in project spoon by INRIA.

the class FilterTest method testOverridingMethodFromSubClassOfInterface.

@Test
public void testOverridingMethodFromSubClassOfInterface() 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<AbstractTostada> anAbstractTostada = launcher.getFactory().Class().get(AbstractTostada.class);
    List<CtMethod<?>> overridingMethods = Query.getElements(launcher.getFactory(), new OverridingMethodFilter(anAbstractTostada.getMethodsByName("make").get(0)));
    assertEquals(2, overridingMethods.size());
    assertEquals("spoon.test.filters.testclasses.AbstractTostada$1", overridingMethods.get(0).getParent(CtClass.class).getQualifiedName());
    assertEquals(Tostada.class, overridingMethods.get(1).getParent(CtClass.class).getActualClass());
    final CtClass<Tostada> aTostada = launcher.getFactory().Class().get(Tostada.class);
    overridingMethods = Query.getElements(launcher.getFactory(), new OverridingMethodFilter(aTostada.getMethodsByName("make").get(0)));
    assertEquals(1, overridingMethods.size());
    assertEquals("spoon.test.filters.testclasses.AbstractTostada$1", overridingMethods.get(0).getParent(CtClass.class).getQualifiedName());
}
Also used : AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) Tostada(spoon.test.filters.testclasses.Tostada) ITostada(spoon.test.filters.testclasses.ITostada) AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) SubTostada(spoon.test.filters.testclasses.SubTostada) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) OverridingMethodFilter(spoon.reflect.visitor.filter.OverridingMethodFilter) Test(org.junit.Test)

Example 3 with AbstractTostada

use of spoon.test.filters.testclasses.AbstractTostada in project spoon by INRIA.

the class FilterTest method testgetTopDefinitions.

@Test
public void testgetTopDefinitions() throws Exception {
    // contract: getTopDefinitions returns the correct number of definitions
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.run();
    // start with ToStatda
    final CtClass<Tostada> aTostada = launcher.getFactory().Class().get(Tostada.class);
    List<CtMethod<?>> methods;
    methods = orderByName(aTostada.getMethodsByName("make").get(0).getTopDefinitions());
    assertEquals(2, methods.size());
    assertEquals("AbstractTostada", methods.get(0).getDeclaringType().getSimpleName());
    assertEquals("ITostada", methods.get(1).getDeclaringType().getSimpleName());
    methods = orderByName(aTostada.getMethodsByName("prepare").get(0).getTopDefinitions());
    assertEquals(1, methods.size());
    assertEquals("AbstractTostada", methods.get(0).getDeclaringType().getSimpleName());
    methods = orderByName(aTostada.getMethodsByName("toString").get(0).getTopDefinitions());
    assertEquals(1, methods.size());
    assertEquals("Object", methods.get(0).getDeclaringType().getSimpleName());
    methods = orderByName(aTostada.getMethodsByName("honey").get(0).getTopDefinitions());
    assertEquals(2, methods.size());
    assertEquals("AbstractTostada", methods.get(0).getDeclaringType().getSimpleName());
    assertEquals("Honey", methods.get(1).getDeclaringType().getSimpleName());
    methods = orderByName(aTostada.getMethodsByName("foo").get(0).getTopDefinitions());
    assertEquals(0, methods.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) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 4 with AbstractTostada

use of spoon.test.filters.testclasses.AbstractTostada in project spoon by INRIA.

the class FilterTest method testOverriddenMethodFromSubClassOfInterface.

@Test
public void testOverriddenMethodFromSubClassOfInterface() throws Exception {
    // contract: When we ask all overridden methods from an overriding method, we must returns all methods
    // above and not below.
    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<AbstractTostada> anAbstractTostada = launcher.getFactory().Class().get(AbstractTostada.class);
    final List<CtMethod<?>> overriddenMethods = Query.getElements(launcher.getFactory(), new OverriddenMethodFilter(anAbstractTostada.getMethodsByName("make").get(0)));
    assertEquals(1, overriddenMethods.size());
    assertEquals(ITostada.class, overriddenMethods.get(0).getParent(CtInterface.class).getActualClass());
    final CtClass<Tostada> aTostada = launcher.getFactory().Class().get(Tostada.class);
    OverriddenMethodFilter filter = new OverriddenMethodFilter(aTostada.getMethodsByName("make").get(0));
    final List<CtMethod<?>> overriddenMethodsFromSub = Query.getElements(launcher.getFactory(), filter);
    assertEquals(2, overriddenMethodsFromSub.size());
    assertEquals(AbstractTostada.class, overriddenMethodsFromSub.get(0).getParent(CtType.class).getActualClass());
    assertEquals(ITostada.class, overriddenMethodsFromSub.get(1).getParent(CtType.class).getActualClass());
}
Also used : OverriddenMethodFilter(spoon.reflect.visitor.filter.OverriddenMethodFilter) AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) Tostada(spoon.test.filters.testclasses.Tostada) ITostada(spoon.test.filters.testclasses.ITostada) AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) SubTostada(spoon.test.filters.testclasses.SubTostada) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 5 with AbstractTostada

use of spoon.test.filters.testclasses.AbstractTostada in project spoon by INRIA.

the class FilterTest method testOverriddenMethodFromAbstractClass.

@Test
public void testOverriddenMethodFromAbstractClass() throws Exception {
    // contract: When we declare an abstract method on an abstract class, we must return an empty list
    // when we ask all overriden methods from this 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<AbstractTostada> aClass = launcher.getFactory().Class().get(AbstractTostada.class);
    assertEquals(0, Query.getElements(launcher.getFactory(), new OverriddenMethodFilter(aClass.getMethodsByName("prepare").get(0))).size());
    assertEquals(0, aClass.getMethodsByName("prepare").get(0).map(new OverriddenMethodQuery()).list().size());
}
Also used : OverriddenMethodFilter(spoon.reflect.visitor.filter.OverriddenMethodFilter) AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) OverriddenMethodQuery(spoon.reflect.visitor.filter.OverriddenMethodQuery) Launcher(spoon.Launcher) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)6 Launcher (spoon.Launcher)6 AbstractTostada (spoon.test.filters.testclasses.AbstractTostada)6 CtMethod (spoon.reflect.declaration.CtMethod)4 ITostada (spoon.test.filters.testclasses.ITostada)4 SubTostada (spoon.test.filters.testclasses.SubTostada)4 Tostada (spoon.test.filters.testclasses.Tostada)4 OverriddenMethodFilter (spoon.reflect.visitor.filter.OverriddenMethodFilter)2 OverridingMethodFilter (spoon.reflect.visitor.filter.OverridingMethodFilter)2 TreeSet (java.util.TreeSet)1 CtType (spoon.reflect.declaration.CtType)1 OverriddenMethodQuery (spoon.reflect.visitor.filter.OverriddenMethodQuery)1 DeepRepresentationComparator (spoon.support.comparator.DeepRepresentationComparator)1 SubInheritanceHierarchyResolver (spoon.support.visitor.SubInheritanceHierarchyResolver)1