Search in sources :

Example 56 with SpoonException

use of spoon.SpoonException 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 57 with SpoonException

use of spoon.SpoonException in project spoon by INRIA.

the class ProcessingTest method testProcessorWithNoArgumentsInConstructor.

@Test
public void testProcessorWithNoArgumentsInConstructor() throws Exception {
    /* throw correctly an exception when trying to use a processor with constructor with args */
    Launcher l = new Launcher();
    l.getEnvironment().setLevel(Level.ERROR.toString());
    l.buildModel();
    try {
        new JDTBasedSpoonCompiler(l.getFactory()).instantiateAndProcess(Collections.singletonList("spoon.test.processing.ProcessingTest$WrongProcessor"));
        fail();
    } catch (SpoonException e) {
        assertTrue(e.getMessage().startsWith("Unable to instantiate processor"));
        assertTrue(e.getMessage().endsWith("Your processor should have a constructor with no arguments"));
        // we are able to retrieve the exception parent
        assertTrue(e.getCause() instanceof java.lang.InstantiationException);
    }
}
Also used : SpoonException(spoon.SpoonException) Launcher(spoon.Launcher) JDTBasedSpoonCompiler(spoon.support.compiler.jdt.JDTBasedSpoonCompiler) Test(org.junit.Test)

Aggregations

SpoonException (spoon.SpoonException)57 Test (org.junit.Test)15 Launcher (spoon.Launcher)12 CtMethod (spoon.reflect.declaration.CtMethod)9 CtType (spoon.reflect.declaration.CtType)9 CtElement (spoon.reflect.declaration.CtElement)8 Factory (spoon.reflect.factory.Factory)8 File (java.io.File)7 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 CtField (spoon.reflect.declaration.CtField)6 URL (java.net.URL)4 CtTypeReference (spoon.reflect.reference.CtTypeReference)4 Collection (java.util.Collection)3 CompilationUnit (spoon.reflect.cu.CompilationUnit)3 CtExecutable (spoon.reflect.declaration.CtExecutable)3 CtParameter (spoon.reflect.declaration.CtParameter)3 CtScanner (spoon.reflect.visitor.CtScanner)3 Filter (spoon.reflect.visitor.Filter)3 FileNotFoundException (java.io.FileNotFoundException)2