Search in sources :

Example 1 with Launcher

use of spoon.Launcher in project Ex2Amplifier by STAMP-project.

the class AbstractTest method initLauncher.

private Launcher initLauncher(String pathToSources, String pathToTestSources, String dependencies) {
    Launcher launcher = new Launcher();
    launcher.getEnvironment().setAutoImports(true);
    launcher.getEnvironment().setNoClasspath(false);
    launcher.getEnvironment().setCommentEnabled(true);
    String[] sourcesArray = (pathToSources + AmplificationHelper.PATH_SEPARATOR + pathToTestSources).split(AmplificationHelper.PATH_SEPARATOR);
    Arrays.stream(sourcesArray).forEach(launcher::addInputResource);
    if (!dependencies.isEmpty()) {
        String[] dependenciesArray = dependencies.split(AmplificationHelper.PATH_SEPARATOR);
        launcher.getModelBuilder().setSourceClasspath(dependenciesArray);
    }
    launcher.buildModel();
    return launcher;
}
Also used : Launcher(spoon.Launcher)

Example 2 with Launcher

use of spoon.Launcher in project Ex2Amplifier by STAMP-project.

the class ArgumentsExtractorTest method testOnTavern.

@Test
public void testOnTavern() throws Exception {
    /*
            Test the ArgumentsExtractor on the tavern project
         */
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setAutoImports(true);
    launcher.getEnvironment().setNoClasspath(false);
    launcher.addInputResource("src/test/resources/tavern/src/test/java/fr/inria/stamp/MainTest.java");
    launcher.addInputResource("src/test/resources/tavern/src/main/java/fr/inria/stamp/tavern/Seller.java");
    launcher.addInputResource("src/test/resources/tavern/src/main/java/fr/inria/stamp/tavern/Player.java");
    launcher.addInputResource("src/test/resources/tavern/src/main/java/fr/inria/stamp/tavern/Item.java");
    launcher.buildModel();
    final CtClass<Object> testClass = launcher.getFactory().Class().get("fr.inria.stamp.MainTest");
    final CtMethod<?> test = testClass.getMethodsByName("test").get(0);
    final CtMethod<?> extractedMethod = ArgumentsExtractor.performExtraction(test);
    System.out.println(extractedMethod);
    // same as the MainGenerator, the resulting method from ArgumentsExtractor should be compilable
    testClass.addMethod(extractedMethod);
    launcher.getModelBuilder().setBinaryOutputDirectory(new File("target/trash/"));
    launcher.getModelBuilder().compile(SpoonModelBuilder.InputType.CTTYPES);
    assertEquals(expectMethodTavern, extractedMethod.toString());
    assertNotEquals(test.toString(), extractedMethod.toString());
}
Also used : Launcher(spoon.Launcher) File(java.io.File) Test(org.junit.Test)

Example 3 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class ArraysTest method testCtNewArrayInnerCtNewArray.

@Test
public void testCtNewArrayInnerCtNewArray() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("src/test/java/spoon/test/arrays/testclasses/Foo.java");
    launcher.setSourceOutputDirectory("target/foo");
    launcher.buildModel();
    launcher.prettyprint();
    try {
        launcher.getModelBuilder().compile();
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : Launcher(spoon.Launcher) Test(org.junit.Test)

Example 4 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class CommentTest method testCommentsInResourcesWithWindowsEOL.

@Test
public void testCommentsInResourcesWithWindowsEOL() throws IOException {
    // contract: the WindowsEOL.java contains MS Windows \r\n as EOL
    try (InputStream is = new FileInputStream(new File("./src/test/java/spoon/test/comment/testclasses/WindowsEOL.java"))) {
        int b;
        boolean lastWasCR = false;
        while ((b = is.read()) != -1) {
            if (lastWasCR) {
                // next must be LF
                assertTrue(b == '\n');
                lastWasCR = false;
            }
            if (b == '\r') {
                lastWasCR = true;
            }
        }
    }
    final Launcher launcher = new Launcher();
    launcher.run(new String[] { "-i", "./src/test/java/spoon/test/comment/testclasses/WindowsEOL.java", "-o", "./target/spooned/", "-c" });
    Factory f = launcher.getFactory();
    CtClass<?> type = (CtClass<?>) f.Type().get(WindowsEOL.class);
    CtJavaDoc classJavaDoc = (CtJavaDoc) type.getComments().get(0);
    // contract: test that java doc is printed correctly
    String str = classJavaDoc.toString();
    StringTokenizer st = new StringTokenizer(str, System.getProperty("line.separator"));
    boolean first = true;
    while (st.hasMoreTokens()) {
        String line = st.nextToken();
        if (first) {
            // first
            first = false;
            assertTrue(line.length() == 3);
            assertEquals("/**", line);
        } else {
            if (st.hasMoreTokens()) {
                // in the middle
                assertTrue(line.length() >= 2);
                assertEquals(" *", line.substring(0, 2));
            } else {
                // last
                assertTrue(line.length() == 3);
                assertEquals(" */", line.substring(0, 3));
            }
        }
    }
    // This test passes on MS Windows too - why spoon uses `\n` on MS Windows too?
    assertEquals("This file contains MS Windows EOL.\n" + "It is here to test whether comments are printed well\n" + "in this case", classJavaDoc.getContent());
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) FileInputStream(java.io.FileInputStream) CtClass(spoon.reflect.declaration.CtClass) StringTokenizer(java.util.StringTokenizer) CtJavaDoc(spoon.reflect.code.CtJavaDoc) Launcher(spoon.Launcher) WindowsEOL(spoon.test.comment.testclasses.WindowsEOL) File(java.io.File) Test(org.junit.Test)

Example 5 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class CtTypeTest method testHasMethodInDefaultMethod.

@Test
public void testHasMethodInDefaultMethod() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/ctType/testclasses/X.java");
    launcher.getEnvironment().setComplianceLevel(8);
    launcher.run();
    final CtClass<?> x = launcher.getFactory().Class().get("spoon.test.ctType.testclasses.W");
    final CtInterface<?> z = launcher.getFactory().Interface().get("spoon.test.ctType.testclasses.Z");
    final CtMethod<?> superMethod = z.getMethods().iterator().next();
    assertTrue(x.hasMethod(superMethod));
}
Also used : Launcher(spoon.Launcher) Test(org.junit.Test)

Aggregations

Launcher (spoon.Launcher)524 Test (org.junit.Test)486 Factory (spoon.reflect.factory.Factory)163 CtClass (spoon.reflect.declaration.CtClass)111 CtMethod (spoon.reflect.declaration.CtMethod)79 File (java.io.File)75 CtType (spoon.reflect.declaration.CtType)66 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)60 CtTypeReference (spoon.reflect.reference.CtTypeReference)48 SpoonModelBuilder (spoon.SpoonModelBuilder)44 ArrayList (java.util.ArrayList)43 CtAnnotation (spoon.reflect.declaration.CtAnnotation)38 CtInvocation (spoon.reflect.code.CtInvocation)32 CtElement (spoon.reflect.declaration.CtElement)27 CtPackage (spoon.reflect.declaration.CtPackage)27 FileSystemFile (spoon.support.compiler.FileSystemFile)26 CtStatement (spoon.reflect.code.CtStatement)25 CtField (spoon.reflect.declaration.CtField)24 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)24 SpoonAPI (spoon.SpoonAPI)22