Search in sources :

Example 6 with CtUsedService

use of spoon.reflect.declaration.CtUsedService in project spoon by INRIA.

the class TestModule method testCompleteModuleInfoContentNoClasspath.

@Test
public void testCompleteModuleInfoContentNoClasspath() {
    // contract: all information of the module-info should be available through the model
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/spoon/test/module/simple_module/module-info.java");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.getEnvironment().setComplianceLevel(9);
    launcher.buildModel();
    assertEquals(2, launcher.getModel().getAllModules().size());
    CtModule unnamedModule = launcher.getFactory().Module().getOrCreate(CtModule.TOP_LEVEL_MODULE_NAME);
    assertSame(unnamedModule, launcher.getModel().getUnnamedModule());
    CtModule moduleGreetings = launcher.getFactory().Module().getOrCreate("simple_module");
    assertEquals("simple_module", moduleGreetings.getSimpleName());
    assertEquals(7, moduleGreetings.getModuleDirectives().size());
    List<CtModuleRequirement> requiredModules = moduleGreetings.getRequiredModules();
    assertEquals(1, requiredModules.size());
    CtModuleRequirement moduleRequirement = requiredModules.get(0);
    assertEquals("java.logging", moduleRequirement.getModuleReference().getSimpleName());
    assertTrue(moduleRequirement.getRequiresModifiers().contains(CtModuleRequirement.RequiresModifier.TRANSITIVE));
    List<CtPackageExport> moduleExports = moduleGreetings.getExportedPackages();
    assertEquals(1, moduleExports.size());
    assertEquals("com.greetings.pkg", moduleExports.get(0).getPackageReference().getQualifiedName());
    assertEquals(2, moduleExports.get(0).getTargetExport().size());
    for (CtModuleReference target : moduleExports.get(0).getTargetExport()) {
        if (!target.getSimpleName().equals("com.other.module") && !target.getSimpleName().equals("com.second.module")) {
            fail();
        }
    }
    List<CtPackageExport> moduleOpened = moduleGreetings.getOpenedPackages();
    assertEquals(2, moduleOpened.size());
    CtPackageExport openedFirst = moduleOpened.get(0);
    CtPackageExport openedSecond = moduleOpened.get(1);
    assertEquals("com.greetings.otherpkg", openedFirst.getPackageReference().getSimpleName());
    assertTrue(openedFirst.getTargetExport().isEmpty());
    assertEquals("com.greetings.openpkg", openedSecond.getPackageReference().getSimpleName());
    assertEquals(1, openedSecond.getTargetExport().size());
    assertEquals("com.third.module", openedSecond.getTargetExport().iterator().next().getSimpleName());
    List<CtUsedService> consumedService = moduleGreetings.getUsedServices();
    assertEquals(1, consumedService.size());
    assertEquals("com.greetings.pkg.ConsumedService", consumedService.get(0).getServiceType().getQualifiedName());
    List<CtProvidedService> providedServices = moduleGreetings.getProvidedServices();
    assertEquals(2, providedServices.size());
    CtProvidedService providedService1 = providedServices.get(0);
    CtProvidedService providedService2 = providedServices.get(1);
    assertEquals("com.greetings.pkg.ConsumedService", providedService1.getServiceType().getQualifiedName());
    assertEquals(2, providedService1.getImplementationTypes().size());
    assertEquals("com.greetings.pkg.ProvidedClass1", providedService1.getImplementationTypes().get(0).getQualifiedName());
    assertEquals("com.greetings.otherpkg.ProvidedClass2", providedService1.getImplementationTypes().get(1).getQualifiedName());
    assertEquals("java.logging.Service", providedService2.getServiceType().getQualifiedName());
    assertEquals(1, providedService2.getImplementationTypes().size());
    assertEquals("com.greetings.logging.Logger", providedService2.getImplementationTypes().get(0).getQualifiedName());
}
Also used : CtPackageExport(spoon.reflect.declaration.CtPackageExport) CtProvidedService(spoon.reflect.declaration.CtProvidedService) CtModuleReference(spoon.reflect.reference.CtModuleReference) Launcher(spoon.Launcher) CtModuleRequirement(spoon.reflect.declaration.CtModuleRequirement) CtModule(spoon.reflect.declaration.CtModule) CtUsedService(spoon.reflect.declaration.CtUsedService) Test(org.junit.Test)

Example 7 with CtUsedService

use of spoon.reflect.declaration.CtUsedService in project spoon by INRIA.

the class TestModule method testModuleInfoWithComments.

@Test
public void testModuleInfoWithComments() {
    // contract: documentation on module-info elements should be managed
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.getEnvironment().setComplianceLevel(9);
    launcher.getEnvironment().setCommentEnabled(true);
    launcher.addInputResource(MODULE_RESOURCES_PATH + "/module_with_comments/module-info.java");
    launcher.buildModel();
    assertEquals(2, launcher.getModel().getAllModules().size());
    CtModule module = launcher.getFactory().Module().getModule("module_with_comments");
    assertNotNull(module);
    assertTrue(module.isOpenModule());
    List<CtComment> comments = module.getComments();
    assertEquals(1, comments.size());
    CtComment comment = comments.get(0);
    assertEquals("This is the main module of the application", comment.getContent());
    assertEquals(CtComment.CommentType.JAVADOC, comment.getCommentType());
    assertEquals(3, module.getModuleDirectives().size());
    CtModuleRequirement moduleRequirement = module.getRequiredModules().get(0);
    comments = moduleRequirement.getComments();
    assertEquals(1, comments.size());
    comment = comments.get(0);
    assertEquals("this is needed for logging stuff", comment.getContent());
    assertEquals(CtComment.CommentType.INLINE, comment.getCommentType());
    CtProvidedService providedService = module.getProvidedServices().get(0);
    comments = providedService.getComments();
    assertEquals(1, comments.size());
    comment = comments.get(0);
    assertEquals("A specific implementation", comment.getContent());
    assertEquals(CtComment.CommentType.JAVADOC, comment.getCommentType());
    CtUsedService usedService = module.getUsedServices().get(0);
    comments = usedService.getComments();
    assertEquals(1, comments.size());
    comment = comments.get(0);
    assertEquals("A simple implementation", comment.getContent());
    assertEquals(CtComment.CommentType.BLOCK, comment.getCommentType());
}
Also used : CtComment(spoon.reflect.code.CtComment) CtProvidedService(spoon.reflect.declaration.CtProvidedService) Launcher(spoon.Launcher) CtModuleRequirement(spoon.reflect.declaration.CtModuleRequirement) CtModule(spoon.reflect.declaration.CtModule) CtUsedService(spoon.reflect.declaration.CtUsedService) Test(org.junit.Test)

Aggregations

CtUsedService (spoon.reflect.declaration.CtUsedService)7 CtModule (spoon.reflect.declaration.CtModule)4 CtModuleRequirement (spoon.reflect.declaration.CtModuleRequirement)4 CtProvidedService (spoon.reflect.declaration.CtProvidedService)4 Test (org.junit.Test)3 Launcher (spoon.Launcher)3 CtPackageExport (spoon.reflect.declaration.CtPackageExport)2 ArrayList (java.util.ArrayList)1 ExportsStatement (org.eclipse.jdt.internal.compiler.ast.ExportsStatement)1 OpensStatement (org.eclipse.jdt.internal.compiler.ast.OpensStatement)1 ProvidesStatement (org.eclipse.jdt.internal.compiler.ast.ProvidesStatement)1 RequiresStatement (org.eclipse.jdt.internal.compiler.ast.RequiresStatement)1 UsesStatement (org.eclipse.jdt.internal.compiler.ast.UsesStatement)1 CtComment (spoon.reflect.code.CtComment)1 CtModuleDirective (spoon.reflect.declaration.CtModuleDirective)1 CtModuleReference (spoon.reflect.reference.CtModuleReference)1 CtTypeReference (spoon.reflect.reference.CtTypeReference)1 CtUsedServiceImpl (spoon.support.reflect.declaration.CtUsedServiceImpl)1