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);
}
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);
}
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);
}
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);
}
Aggregations