Search in sources :

Example 1 with AbstractManualProcessor

use of spoon.processing.AbstractManualProcessor in project spoon by INRIA.

the class ProcessingTest method testInitProperties.

@Test
public void testInitProperties() throws Exception {
    class AProcessor extends AbstractManualProcessor {

        @Property
        String aString;

        @Property
        int anInt;

        @Property
        Object anObject;

        @Property
        int[] arrayInt;

        @Property
        List<String> listString;

        @Property
        boolean[] arrayBoolean;

        @Property
        Map<String, Double> mapStringDouble;

        @Override
        public void process() {
        }
    }
    ;
    AProcessor p = new AProcessor();
    Launcher launcher = new Launcher();
    p.setFactory(launcher.getFactory());
    ProcessorProperties props = new ProcessorPropertiesImpl();
    props.set("aString", "foo");
    props.set("anInt", 5);
    Object o = new Object();
    props.set("anObject", o);
    int[] arrayInt = new int[] { 1, 2, 3 };
    props.set("arrayInt", arrayInt);
    props.set("listString", Arrays.asList(new String[] { "42" }));
    boolean[] arrayBoolean = new boolean[] { true };
    props.set("arrayBoolean", arrayBoolean);
    HashMap<String, Double> mapTest = new HashMap<>();
    mapTest.put("foobar", 42.42);
    props.set("mapStringDouble", mapTest);
    ProcessorUtils.initProperties(p, props);
    assertEquals("foo", p.aString);
    assertEquals(5, p.anInt);
    assertSame(o, p.anObject);
    assertSame(arrayInt, p.arrayInt);
    assertEquals(Arrays.asList(new String[] { "42" }), p.listString);
    assertSame(arrayBoolean, p.arrayBoolean);
    assertSame(mapTest, p.mapStringDouble);
}
Also used : HashMap(java.util.HashMap) AbstractManualProcessor(spoon.processing.AbstractManualProcessor) ProcessorProperties(spoon.processing.ProcessorProperties) ProcessorPropertiesImpl(spoon.processing.ProcessorPropertiesImpl) Launcher(spoon.Launcher) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with AbstractManualProcessor

use of spoon.processing.AbstractManualProcessor in project spoon by INRIA.

the class SpoonArchitectureEnforcerTest method testFactorySubFactory.

@Test
public void testFactorySubFactory() throws Exception {
    // contract:: all subfactory methods must also be in the main factory
    // this is very important for usability and discoverability
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/main/java/spoon/reflect/factory");
    class SanityCheck {

        int val = 0;
    }
    ;
    SanityCheck sanityCheck = new SanityCheck();
    launcher.addProcessor(new AbstractManualProcessor() {

        @Override
        public void process() {
            CtType factoryImpl = getFactory().Interface().get(Factory.class);
            CtPackage factoryPackage = getFactory().Package().getOrCreate("spoon.reflect.factory");
            CtInterface itf = getFactory().Interface().create("MegaFactoryItf");
            CtClass impl = getFactory().Class().create("MegaFactory");
            for (CtType<?> t : factoryPackage.getTypes()) {
                // 
                if (t.getSimpleName().startsWith("Mega"))
                    continue;
                for (CtMethod<?> m : t.getMethods()) {
                    // we check only public methods
                    if (m.hasModifier(ModifierKind.PUBLIC) == false)
                        continue;
                    // we only consider factory methods
                    if (!m.getSimpleName().startsWith("create"))
                        continue;
                    // too generic, what should we create??
                    if (m.getSimpleName().equals("create")) {
                        String simpleNameType = m.getType().getSimpleName().replace("Ct", "");
                        CtMethod method = m.clone();
                        method.setSimpleName("create" + simpleNameType);
                        assertTrue(method.getSignature() + " (from " + t.getQualifiedName() + ") is not present in the main factory", factoryImpl.hasMethod(method));
                        continue;
                    }
                    // too generic, is it a fieldref? an execref? etc
                    if (m.getSimpleName().equals("createReference"))
                        continue;
                    if (m.getModifiers().contains(ModifierKind.ABSTRACT))
                        continue;
                    sanityCheck.val++;
                    // the core assertion
                    assertTrue(m.getSignature() + " is not present in the main factory", factoryImpl.hasMethod(m));
                }
            }
        }
    });
    launcher.run();
    assertTrue(sanityCheck.val > 100);
}
Also used : CtInterface(spoon.reflect.declaration.CtInterface) CtClass(spoon.reflect.declaration.CtClass) AbstractManualProcessor(spoon.processing.AbstractManualProcessor) CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtPackage(spoon.reflect.declaration.CtPackage) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 3 with AbstractManualProcessor

use of spoon.processing.AbstractManualProcessor in project spoon by INRIA.

the class ProcessingTest method testInitPropertiesWithWrongType.

@Test
public void testInitPropertiesWithWrongType() throws Exception {
    class AProcessor extends AbstractManualProcessor {

        @Property
        String aString;

        @Property
        int anInt;

        @Property
        Object anObject;

        @Override
        public void process() {
        }
    }
    ;
    AProcessor p = new AProcessor();
    Launcher launcher = new Launcher();
    p.setFactory(launcher.getFactory());
    ProcessorProperties props = new ProcessorPropertiesImpl();
    props.set("aString", "foo");
    props.set("anObject", "foo");
    props.set("anInt", "foo");
    try {
        ProcessorUtils.initProperties(p, props);
        fail();
    } catch (SpoonException e) {
        assertTrue(e.getMessage().contains("anInt"));
    }
    assertEquals("foo", p.aString);
    assertEquals(0, p.anInt);
    assertNull(p.anObject);
}
Also used : AbstractManualProcessor(spoon.processing.AbstractManualProcessor) ProcessorProperties(spoon.processing.ProcessorProperties) ProcessorPropertiesImpl(spoon.processing.ProcessorPropertiesImpl) SpoonException(spoon.SpoonException) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 4 with AbstractManualProcessor

use of spoon.processing.AbstractManualProcessor in project spoon by INRIA.

the class ProcessingTest method testInitPropertiesWithStringType.

@Test
public void testInitPropertiesWithStringType() throws Exception {
    class AProcessor extends AbstractManualProcessor {

        @Property
        String aString;

        @Property
        int anInt;

        @Property
        Object anObject;

        @Property
        int[] arrayInt;

        @Property
        List<String> listString;

        @Property
        boolean[] arrayBoolean;

        @Property
        Map<String, Double> mapStringDouble;

        @Override
        public void process() {
        }
    }
    ;
    AProcessor p = new AProcessor();
    Launcher launcher = new Launcher();
    p.setFactory(launcher.getFactory());
    ProcessorProperties props = new ProcessorPropertiesImpl();
    props.set("aString", "foo");
    props.set("anInt", "42");
    props.set("anObject", "{}");
    props.set("arrayInt", "[42,43]");
    props.set("listString", "[\"foo\", \"bar\"]");
    props.set("arrayBoolean", "[true]");
    props.set("mapStringDouble", "{\"foo\": 10.21, \"bar\": 14.42}");
    ProcessorUtils.initProperties(p, props);
    assertEquals("foo", p.aString);
    assertEquals(42, p.anInt);
    assertNotNull(p.anObject);
    assertArrayEquals(new int[] { 42, 43 }, p.arrayInt);
    assertEquals(Arrays.asList(new String[] { "foo", "bar" }), p.listString);
    assertArrayEquals(new boolean[] { true }, p.arrayBoolean);
    Map<String, Double> mamap = new HashMap<>();
    mamap.put("foo", 10.21);
    mamap.put("bar", 14.42);
    assertEquals(mamap, p.mapStringDouble);
}
Also used : AbstractManualProcessor(spoon.processing.AbstractManualProcessor) ProcessorProperties(spoon.processing.ProcessorProperties) ProcessorPropertiesImpl(spoon.processing.ProcessorPropertiesImpl) HashMap(java.util.HashMap) Launcher(spoon.Launcher) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 Launcher (spoon.Launcher)4 AbstractManualProcessor (spoon.processing.AbstractManualProcessor)4 ProcessorProperties (spoon.processing.ProcessorProperties)3 ProcessorPropertiesImpl (spoon.processing.ProcessorPropertiesImpl)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 SpoonException (spoon.SpoonException)1 CtClass (spoon.reflect.declaration.CtClass)1 CtInterface (spoon.reflect.declaration.CtInterface)1 CtMethod (spoon.reflect.declaration.CtMethod)1 CtPackage (spoon.reflect.declaration.CtPackage)1 CtType (spoon.reflect.declaration.CtType)1 Factory (spoon.reflect.factory.Factory)1