Search in sources :

Example 31 with SpoonModelBuilder

use of spoon.SpoonModelBuilder in project spoon by INRIA.

the class EqualTest method testEqualsEmptyException.

@Test
public void testEqualsEmptyException() throws Exception {
    Factory factory = new Launcher().createFactory();
    String realParam1 = "\"\"";
    String content = "" + "class X {" + "public Object foo() {" + " Integer.getInteger(" + realParam1 + ");" + " return \"\";" + "}};";
    SpoonModelBuilder builder = new JDTSnippetCompiler(factory, content);
    try {
        builder.build();
    } catch (Exception e) {
        e.printStackTrace();
        fail("Unable create model");
    }
    CtClass<?> clazz1 = (CtClass<?>) factory.Type().getAll().get(0);
    CtMethod<?> method = (CtMethod<?>) clazz1.getMethods().toArray()[0];
    CtInvocation<?> invo = (CtInvocation<?>) method.getBody().getStatement(0);
    CtLiteral<?> argument1 = (CtLiteral<?>) invo.getArguments().get(0);
    assertEquals(realParam1, argument1.toString());
    CtReturn<?> returnStatement = (CtReturn<?>) method.getBody().getStatement(1);
    CtLiteral<?> returnExp = (CtLiteral<?>) returnStatement.getReturnedExpression();
    assertEquals(realParam1, returnExp.toString());
    try {
        assertEquals(argument1, returnExp);
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Factory(spoon.reflect.factory.Factory) CtClass(spoon.reflect.declaration.CtClass) CtInvocation(spoon.reflect.code.CtInvocation) CtLiteral(spoon.reflect.code.CtLiteral) CtReturn(spoon.reflect.code.CtReturn) JDTSnippetCompiler(spoon.support.compiler.jdt.JDTSnippetCompiler) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 32 with SpoonModelBuilder

use of spoon.SpoonModelBuilder in project spoon by INRIA.

the class CompilationTest method testCompilationInEmptyDir.

@Test
public void testCompilationInEmptyDir() throws Exception {
    // Contract: Spoon can be launched in an empty folder as a working directory
    // See: https://github.com/INRIA/spoon/pull/1208 and https://github.com/INRIA/spoon/issues/1246
    // This test does not fail (it's not enough to change user.dir we should launch process inside that dir) but it explains the problem
    String userDir = System.getProperty("user.dir");
    File testFile = new File("src/test/resources/compilation/compilation-tests/IBar.java");
    String absoluteTestPath = testFile.getAbsolutePath();
    Path tempDirPath = Files.createTempDirectory("test_compilation");
    System.setProperty("user.dir", tempDirPath.toFile().getAbsolutePath());
    SpoonModelBuilder compiler = new Launcher().createCompiler();
    compiler.addInputSource(new File(absoluteTestPath));
    compiler.setBinaryOutputDirectory(tempDirPath.toFile());
    compiler.compile(SpoonModelBuilder.InputType.FILES);
    System.setProperty("user.dir", userDir);
    assertThat(tempDirPath.toFile().listFiles().length, not(0));
}
Also used : Path(java.nio.file.Path) SpoonModelBuilder(spoon.SpoonModelBuilder) Launcher(spoon.Launcher) File(java.io.File) Test(org.junit.Test)

Example 33 with SpoonModelBuilder

use of spoon.SpoonModelBuilder in project spoon by INRIA.

the class ModelUtils method buildNoClasspath.

/**
 * Utility method for testing: creates the noclasspath model of the given `classesToBuild` from src/test/java and returns the factory
 */
public static Factory buildNoClasspath(Class<?>... classesToBuild) throws Exception {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    SpoonModelBuilder comp = launcher.createCompiler();
    for (Class<?> classToBuild : classesToBuild) {
        comp.addInputSources(SpoonResourceHelper.resources("./src/test/java/" + classToBuild.getName().replace('.', '/') + ".java"));
    }
    comp.build();
    return comp.getFactory();
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Launcher(spoon.Launcher)

Example 34 with SpoonModelBuilder

use of spoon.SpoonModelBuilder in project spoon by INRIA.

the class ModelUtils method build.

/**
 * Builds the Spoon mode of the `filesToBuild` given as parameter
 */
public static Factory build(File... filesToBuild) {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    SpoonModelBuilder comp = launcher.createCompiler();
    for (File fileToBuild : filesToBuild) {
        try {
            comp.addInputSource(SpoonResourceHelper.createResource(fileToBuild));
        } catch (FileNotFoundException e) {
            throw new RuntimeException("File not found", e);
        }
    }
    comp.build();
    return comp.getFactory();
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) FileNotFoundException(java.io.FileNotFoundException) Launcher(spoon.Launcher) File(java.io.File)

Example 35 with SpoonModelBuilder

use of spoon.SpoonModelBuilder in project spoon by INRIA.

the class ModelUtils method build.

/**
 * Utility method for testing: creates the model of `packageName` from src/test/java and returns the CtType corresponding to `className`
 */
public static <T extends CtType<?>> T build(String packageName, String className) throws Exception {
    SpoonModelBuilder comp = new Launcher().createCompiler();
    comp.addInputSources(SpoonResourceHelper.resources("./src/test/java/" + packageName.replace('.', '/') + "/" + className + ".java"));
    comp.build();
    return comp.getFactory().Package().get(packageName).getType(className);
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Launcher(spoon.Launcher)

Aggregations

SpoonModelBuilder (spoon.SpoonModelBuilder)45 Launcher (spoon.Launcher)43 Test (org.junit.Test)35 Factory (spoon.reflect.factory.Factory)29 File (java.io.File)17 CtClass (spoon.reflect.declaration.CtClass)16 CtMethod (spoon.reflect.declaration.CtMethod)6 SpoonResource (spoon.compiler.SpoonResource)5 CtInvocation (spoon.reflect.code.CtInvocation)5 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)4 DefaultCoreFactory (spoon.support.DefaultCoreFactory)4 JDTSnippetCompiler (spoon.support.compiler.jdt.JDTSnippetCompiler)4 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)4 Before (org.junit.Before)3 FileNotFoundException (java.io.FileNotFoundException)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 CtParameterRemoveRefactoring (spoon.refactoring.CtParameterRemoveRefactoring)2 CtNewClass (spoon.reflect.code.CtNewClass)2 CtExecutable (spoon.reflect.declaration.CtExecutable)2 FactoryImpl (spoon.reflect.factory.FactoryImpl)2