Search in sources :

Example 1 with ProcessorPropertiesImpl

use of spoon.processing.ProcessorPropertiesImpl 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 ProcessorPropertiesImpl

use of spoon.processing.ProcessorPropertiesImpl 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 3 with ProcessorPropertiesImpl

use of spoon.processing.ProcessorPropertiesImpl 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)3 Launcher (spoon.Launcher)3 AbstractManualProcessor (spoon.processing.AbstractManualProcessor)3 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