Search in sources :

Example 1 with GlobalAnnotation

use of spoon.test.annotation.testclasses.GlobalAnnotation in project spoon by INRIA.

the class PackageTest method testAddAnnotationToPackage.

@Test
public void testAddAnnotationToPackage() throws Exception {
    // contract: Created package-info should used imports in auto-import
    final Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/pkg/testclasses/Foo.java");
    spoon.getEnvironment().setAutoImports(true);
    File outputDir = new File("./target/spoon-packageinfo");
    spoon.getEnvironment().setSourceOutputDirectory(outputDir);
    spoon.buildModel();
    CtAnnotationType annotationType = (CtAnnotationType) spoon.getFactory().Annotation().get(GlobalAnnotation.class);
    CtAnnotation annotation = spoon.getFactory().Core().createAnnotation();
    annotation.setAnnotationType(annotationType.getReference());
    CtPackage ctPackage = spoon.getFactory().Package().get("spoon.test.pkg.testclasses");
    ctPackage.addAnnotation(annotation);
    JavaOutputProcessor outputProcessor = spoon.createOutputWriter();
    outputProcessor.process(ctPackage);
    File packageInfo = new File(outputDir, "spoon/test/pkg/testclasses/package-info.java");
    assertTrue(packageInfo.exists());
    canBeBuilt(packageInfo, 8);
    List<String> lines = Files.readAllLines(packageInfo.toPath());
    assertFalse(lines.isEmpty());
    for (String s : lines) {
        if (s.trim().startsWith("import")) {
            assertEquals("import spoon.test.annotation.testclasses.GlobalAnnotation;", s.trim());
        }
        if (s.trim().startsWith("@")) {
            assertEquals("@GlobalAnnotation", s.trim());
        }
    }
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) GlobalAnnotation(spoon.test.annotation.testclasses.GlobalAnnotation) Launcher(spoon.Launcher) CtAnnotationType(spoon.reflect.declaration.CtAnnotationType) CtPackage(spoon.reflect.declaration.CtPackage) File(java.io.File) JavaOutputProcessor(spoon.support.JavaOutputProcessor) Test(org.junit.Test)

Aggregations

File (java.io.File)1 Test (org.junit.Test)1 Launcher (spoon.Launcher)1 CtAnnotation (spoon.reflect.declaration.CtAnnotation)1 CtAnnotationType (spoon.reflect.declaration.CtAnnotationType)1 CtPackage (spoon.reflect.declaration.CtPackage)1 JavaOutputProcessor (spoon.support.JavaOutputProcessor)1 GlobalAnnotation (spoon.test.annotation.testclasses.GlobalAnnotation)1